View difference between Paste ID: fygivah2 and TAntxkmR
SHOW: | | - or go back to the newest paste.
1
# ##### BEGIN GPL LICENSE BLOCK #####
2
#
3
#  This program is free software; you can redistribute it and/or
4
#  modify it under the terms of the GNU General Public License
5
#  as published by the Free Software Foundation; either version 2
6
#  of the License, or (at your option) any later version.
7
#
8
#  This program is distributed in the hope that it will be useful,
9
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
#  GNU General Public License for more details.
12
#
13
#  You should have received a copy of the GNU General Public License
14
#  along with this program; if not, write to the Free Software Foundation,
15
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
#
17
# ##### END GPL LICENSE BLOCK #####
18
19
#
20
#
21
#  Author            : Tamir Lousky (tlousky@gmail.com)
22
#
23
#  Homepage(Wiki)    : http://biological3d.wordpress.com/
24
#
25
#  Start of project              : 2013-01-25 by Clemens Barth
26
#  Last modified                 : 2013-01-30
27
#
28
#  Acknowledgements 
29
#  ================
30
#
31
#  Blender: Patrick Boelens (helpful tuts!), CoDEmanX (useful bmesh info on BA forums!)
32
33
bl_info = {    
34-
    "name"       : "Random Face Material Assigner",
34+
    "name"        : "Random Face Material Assigner",
35-
    "author"     : "Tamir Lousky",
35+
    "author"      : "Tamir Lousky",
36-
    "version"    : (1, 0, 0),
36+
    "version"     : (1, 0, 0),
37-
    "blender"    : (2, 65, 0),
37+
    "blender"     : (2, 65, 0),
38-
    "category"   : "Materials",
38+
    "category"    : "Materials",
39-
    "location"   : "3D View >> Tools",
39+
    "location"    : "3D View >> Tools",
40-
    "wiki_url"   : "http://blenderartists.org/forum/showthread.php?279723",
40+
    "wiki_url"    : "http://bioblog3d.wordpress.com/2013/01/30/random-meshface-material-assigner",
41-
    "tracker_url": "http://blenderartists.org/forum/showthread.php?279723",
41+
    "download_url": "http://pastebin.com/download.php?i=TAntxkmR",
42-
    "description": "Assign random materials to mesh object faces."}
42+
    "description" : "Assign random materials to mesh object faces."}
43
44
import bpy
45
import random
46
import bmesh
47
48
class random_mat_panel(bpy.types.Panel):
49
    bl_idname      = "randomFaceMatPanel"
50
    bl_label       = "Random Face Material Assigner"
51
    bl_space_type  = 'VIEW_3D'
52
    bl_region_type = 'TOOLS'
53
    bl_context     = 'objectmode'
54
55
    def draw( self, context):                # Draw panel UI elements #
56
        layout = self.layout                 # Reference to panel layout object
57
        
58
        props = context.scene.face_assigner  # Create reference material assigner property group
59
60
        col1 = layout.column()               # Create a column
61
        col1.label(text="Randomly assign materials to each face on the object")
62
        
63
        box = layout.box()                   # Draw a box
64
        col2 = box.column(align=True)        # Create a column
65
        col2.prop( props, "rand_seed"  )     # Create randomization seed property on column
66
        col2.label(text="use this field to filter materials by name")
67
        col2.prop( props, "mat_prefix" )     # And the material prefix property too
68
69
class rand_mat_assigner(bpy.types.PropertyGroup):
70
   
71
    def randomize( self, context ):
72
        """ function name: randomize
73
            description:   This function assigns a random material to each face on the selected
74
                           object's mesh, from its list of materials (filtered by the mat_prefix)
75
        """
76
        random.seed(self.rand_seed)   # Set the randomization seed
77
    
78
        all_materials = bpy.context.object.data.materials
79
        filtered_materials = []
80
81
        if self.mat_prefix != "":                       # IF the user entered a prefix
82
            for material in all_materials:              # Iterate over all the object's materials
83
                if self.mat_prefix in material.name:    # Look for materials with the prefix
84
                    filtered_materials.append(material) # And filter them in
85
        else:
86
            filtered_materials = all_materials          # If there's no prefix, use all materials
87
        
88
        no_of_materials = len(filtered_materials)       # Count all/filtered materials on object
89
90
        bpy.ops.object.mode_set(mode = 'EDIT')          # Go to edit mode to create bmesh
91
        ob = bpy.context.object
92
93
        bm = bmesh.from_edit_mesh(ob.data)              # Create bmesh object from object mesh
94
95
        for face in bm.faces:    # Iterate over all of the object's faces
96
            face.material_index = random.randint(0, no_of_materials - 1)  # Assign random material to face
97
            
98
        ob.data.update()                            # Update the mesh from the bmesh data
99
        bpy.ops.object.mode_set(mode = 'OBJECT')    # Return to object mode
100
101
        return {'FINISHED'}
102
103
    rand_seed = bpy.props.IntProperty(     # Randomization seed
104
        name="rand_seed",
105
        description="Randomization seed",
106
        update=randomize)  
107
    mat_prefix = bpy.props.StringProperty( # Prefix to filter materials by
108
        name="mat_prefix", 
109
        description="Material name filter",
110
        default="",
111
        update=randomize)
112
113
def register():
114
    bpy.utils.register_module(__name__)
115
    bpy.types.Scene.face_assigner = bpy.props.PointerProperty(type=rand_mat_assigner)
116
    
117
def unregister():
118
    bpy.utils.unregister_module(__name__)