diff --git a/random_material_assigners/random_material_assigner_per_face_new_features.py b/random_material_assigners/random_material_assigner_per_face_new_features.py index bfd6085..9d02a89 100644 --- a/random_material_assigners/random_material_assigner_per_face_new_features.py +++ b/random_material_assigners/random_material_assigner_per_face_new_features.py @@ -25,14 +25,14 @@ # Start of project : 2013-01-25 by Tamir Lousky # Last modified : 2013-03-20 # -# Acknowledgements +# Acknowledgements # ================ # # Blender: Patrick Boelens (helpful tuts!), CoDEmanX (useful bmesh info @ BA forums!), -# ni-ko-o-kin ( useful vertex groups info @ BA forums), +# ni-ko-o-kin ( useful vertex groups info @ BA forums), # supergra ( great tip on how to select loose parts @ BA forums ). -bl_info = { +bl_info = { "name" : "Random Face Material Assigner", "author" : "Tamir Lousky", "version" : (2, 0, 0), @@ -56,12 +56,12 @@ class random_mat_panel(bpy.types.Panel): def draw( self, context): # Draw panel UI elements # layout = self.layout # Reference to panel layout object - + props = context.scene.face_assigner # Create reference material assigner property group col1 = layout.column() # Create a column col1.label(text="Randomly assign materials to each face on the object") - + box = layout.box() # Draw a box col2 = box.column(align=True) # Create a column col2.prop( props, "rand_seed" ) # Create randomization seed property on column @@ -81,7 +81,7 @@ def get_verts_and_groups( self ): ob = bpy.context.object groups = {} - + for group in ob.vertex_groups: groups[str(group.index)] = [] @@ -99,7 +99,7 @@ def randomize( self, context ): object's mesh, from its list of materials (filtered by the mat_prefix) """ random.seed(self.rand_seed) # Set the randomization seed - + all_materials = bpy.context.object.data.materials filtered_materials = [] @@ -109,7 +109,7 @@ def randomize( self, context ): filtered_materials.append(material) # And filter them in else: filtered_materials = all_materials # If there's no prefix, use all materials - + no_of_materials = len(filtered_materials) # Count all/filtered materials on object bpy.ops.object.mode_set(mode = 'EDIT') # Go to edit mode to create bmesh @@ -119,22 +119,22 @@ def randomize( self, context ): ## Distribute materials based on vertex groups if self.assign_method == 'Vertex Group': - + vgroups = self.get_verts_and_groups() # Get vgroups if vgroups and len( vgroups.keys() ) > 0: # make sure that there are actually vgroups on this mesh - + for vgroup in list( vgroups.keys() ): # get random material index - rand_mat_index = random.randint(0, no_of_materials - 1) + rand_mat_index = random.randint(0, no_of_materials - 1) # Go to vertex selection mode bpy.ops.mesh.select_mode( - use_extend=False, - use_expand=False, + use_extend=False, + use_expand=False, type='VERT') bpy.ops.mesh.select_all(action='DESELECT') # Deselect all verts - + # Select all the vertices in the vertex group for vert in vgroups[vgroup]: bm.verts[vert].select_set(True) @@ -142,7 +142,7 @@ def randomize( self, context ): # Go to face selection mode bm.select_mode = {'FACE'} bm.select_flush(True) - + # iterate over all selected faces and assign vgroup material for face in bm.faces: if face.select: @@ -157,32 +157,33 @@ def randomize( self, context ): ## Distribute materials by loose parts elif self.assign_method == 'Loose Parts': + bm.verts.ensure_lookup_table() vert_indices = [ vert.index for vert in bm.verts ] # Reference all vertex indices for vert in vert_indices: bpy.ops.mesh.select_all(action='DESELECT') # Deselect all verts - + bm.verts[vert].select = True - + # Select all verts linked to this one (on the same island or "loose part") bpy.ops.mesh.select_linked( limit=False ) - + # Go to face selection mode bm.select_mode = {'FACE'} bm.select_flush(True) - rand_mat_index = random.randint(0, no_of_materials - 1) - + rand_mat_index = random.randint(0, no_of_materials - 1) + # iterate over all selected (linked) faces and assign material for face in bm.faces: if face.select: face.material_index = rand_mat_index # Assign random material to face - + # remove selected vertices from list for vert in bm.verts: if vert.select: removed = vert_indices.pop( vert_indices.index(vert.index) ) - + ob.data.update() # Update the mesh from the bmesh data bpy.ops.object.mode_set(mode = 'OBJECT') # Return to object mode @@ -193,31 +194,31 @@ def randomize( self, context ): description = "Randomization seed", options = {'ANIMATABLE'}, update = randomize - ) + ) mat_prefix = bpy.props.StringProperty( # Prefix to filter materials by - name = "mat_prefix", + name = "mat_prefix", description = "Material name filter", default = "", update = randomize ) items = [ - ('Face', 'Face', ''), - ('Vertex Group', 'Vertex Group', ''), + ('Face', 'Face', ''), + ('Vertex Group', 'Vertex Group', ''), ('Loose Parts', 'Loose Parts', '') ] assign_method = bpy.props.EnumProperty( # Material distribution method name = "Material distribution method", - items = items, + items = items, default = 'Face' ) - + def register(): bpy.utils.register_module(__name__) bpy.types.Scene.face_assigner = bpy.props.PointerProperty(type=rand_mat_assigner) - + def unregister(): bpy.utils.unregister_module(__name__) bpy.types.Scene.face_assigner = bpy.props.PointerProperty(type=rand_mat_assigner) \ No newline at end of file