利用者:Kwk/GSoC2010/FasterWayToAddMaps

提供: wiki
< 利用者:Kwk
2010年8月16日 (月) 21:57時点におけるwiki>Kwkによる版 (Footer)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

About

This page is about a task in my GSoC 2010 project.

Situation

Currently there are quite a lot of steps needed to add bump, specular and diffuse maps to the currently active material.

Purpose

The purpose of this task was to simplify the setup the above steps. Of course, the parameters of each map need to be adjusted by the artist for tweaking, but the setup is basically always the same for all the maps:

  1. Select a material
  2. Add a texture
    1. Name the texture
  3. Set the texture to use an image
  4. Create an image to be used in the texture
    1. Name the image
  5. Adjust the influence settings for the texutre regarding the type of map (bump, specular, diffuse, ...)

End-User documentation

To add bump, specular and diffuse maps to the next slot of the current active material, you can either chose the hard way of setting everything up yourself or you can do it the following way:

  1. Start Blender.
  2. With the default cube selected, go to the UV/Image Editor.

Shows how to go to UV/Image Editor.

  1. Show the view properties by hitting either N or clicking on "View" and then "Properties".

How to enable view properties.

  1. Add as many bump, specular, or diffuse maps as you like

The new buttons to add maps with default setup for later tweaking.

This Video [1] shows both, the old and the new shorter way to add maps.

Please remember,
no standard functionality is changed with those three buttons.


Technical details

For this feature, only Python coding was needed. Since the patch is pretty short I include it here. It should be self-explanatory

Index: release/scripts/ui/space_image.py
===================================================================
--- release/scripts/ui/space_image.py	(Revision 31363)
+++ release/scripts/ui/space_image.py	(Arbeitskopie)
@@ -663,6 +663,31 @@
         row.operator("brush.curve_preset", icon="LINCURVE", text="").shape = 'LINE'
         row.operator("brush.curve_preset", icon="NOCURVE", text="").shape = 'MAX'
 
+class IMAGE_PT_paint_add_map(bpy.types.Panel):
+    bl_space_type = 'IMAGE_EDITOR'
+    bl_region_type = 'UI'
+    bl_label = "Add Maps"
+    bl_context = "texture"
+    bl_default_closed = False
+
+    @classmethod
+    def poll(self, context):
+        # TBD
+        return True       
+
+    def draw(self, context):
+        layout = self.layout
+
+        row = layout.row()
+
+        split = layout.split()
+
+        row = split.row(align=True)
+        row.label(text="Add:")
+        row.operator("image.material_add_map", text="Diffuse").channel_type = 'DIFFUSE' # , icon='FILE_IMAGE'
+        row.operator("image.material_add_map", text="Bump").channel_type = 'BUMP'
+        row.operator("image.material_add_map", text="Specular").channel_type = 'SPECULAR'
+
 def register():
     pass
 
Index: release/scripts/op/image.py
===================================================================
--- release/scripts/op/image.py	(Revision 31363)
+++ release/scripts/op/image.py	(Arbeitskopie)
@@ -188,7 +188,72 @@
 
         return {'FINISHED'}
 
+def check_free_texture_slots(mat):
+    """Returns True if there are texture slots free in mat; otherwise False is returned"""
+    if mat != None:
+        for slot in mat.texture_slots:
+            if slot == None:
+                return True
+    return False
 
+class MaterialAddMap(bpy.types.Operator):
+    '''Add new map (e.g. diffuse, bump, specular) to the active material's next free texture slot.'''
+    bl_idname = "image.material_add_map"
+    bl_label = "Add Map"
+    bl_options = {'REGISTER', 'UNDO'}
+
+    channel_type = bpy.props.EnumProperty(items=(
+            ('DIFFUSE', "Diffuse Map", "A 32 Bit RGB(A) color map"),
+            ('SPECULAR', "Specular Map", "A 8 Bit greyscale map"),
+            ('BUMP', "Bump Map", "A 8 Bit greyscale map")
+            ),                
+            name="Channel Type",
+            description="This defines how the new image will be used in the texture.",
+            default='DIFFUSE')
+
+    @classmethod
+    def poll(self, context):
+        ''' Tests if a texture slot is free '''
+        mat = bpy.context.active_object.active_material
+        return check_free_texture_slots(mat)   
+    
+    def execute(self, context):
+        ctype = self.properties.channel_type
+        mat = bpy.context.active_object.active_material
+        tex = None
+        
+        # Tests if a texture slot is free
+        if check_free_texture_slots(mat):
+            tex = bpy.data.textures.new(ctype)
+        
+        if tex != None:            
+            # Set texture type to be an bpy.types.ImageTexture object
+            tex.type = 'IMAGE'
+            tex = tex.recast_type()
+            
+            # Decide which parts shall be influenced by the image texture
+            if ctype == 'DIFFUSE':
+                mat.add_texture(tex, texture_coordinates='UV', map_to='COLOR')
+            elif ctype == 'BUMP':
+                mat.add_texture(tex, texture_coordinates='UV', map_to='NORMAL')
+            elif ctype == 'SPECULAR':
+                mat.add_texture(tex, texture_coordinates='UV', map_to='SPECULARITY')
+
+            mat.texture_slots[tex.name]
+               
+            # Create a new image (named after 
+            img = bpy.data.images.new(ctype)
+    
+            # Set image file format
+            # If I remember correctly, this is default.
+            img.file_format = 'TARGA'
+            
+            # Assign image to texture            
+            tex.image = img
+            
+
+        return {'FINISHED'}
+
 def register():
     pass