merge with 2.5 at r18679
[blender.git] / release / scripts / vertexpaint_from_material.py
1 #!BPY
2 """
3 Name: 'Copy from Material...'
4 Blender: 242
5 Group: 'VertexPaint'
6 Tooltip: 'Writes material diffuse color as vertex colors.'
7 """
8
9 __author__ = "Campbell Barton"
10 __url__ = ("www.blender.org", "blenderartists.org")
11 __version__ = "1.0"
12
13 __bpydoc__ = """\
14 This script copies material colors to vertex colors.
15 Optionaly you can operate on all faces and many objects as well as multiplying with the current color.
16 """
17
18
19 from Blender import *
20
21 def matcol(mat):
22         '''
23         Returns the material color as a tuple 3 from 0 to 255
24         '''
25         if mat:
26                 return \
27                 int(mat.R*255),\
28                 int(mat.G*255),\
29                 int(mat.B*255)
30         else:
31                 return None
32
33 def mat2vcol(PREF_SEL_FACES_ONLY, PREF_ACTOB_ONLY, PREF_MULTIPLY_COLOR):
34         scn= Scene.GetCurrent()
35         if PREF_ACTOB_ONLY:
36                 obs= [scn.getActiveObject()]
37         else:
38                 obs= Object.GetSelected()
39                 ob= scn.getActiveObject()
40                 if ob not in obs:
41                         obs.append(ob)
42         
43         
44         for ob in obs:
45                 if ob.type != 'Mesh':
46                         continue
47                 
48                 me= ob.getData(mesh=1)
49                 
50                 try:
51                         me.vertexColors=True
52                 except: # no faces
53                         continue
54                 
55                 matcols= [matcol(mat) for mat in me.materials]
56                 len_matcols= len(matcols)
57                 
58                 for f in me.faces:
59                         if not PREF_SEL_FACES_ONLY or f.sel:
60                                 f_mat= f.mat
61                                 if f_mat < len_matcols:
62                                         mat= matcols[f.mat]
63                                         if mat:
64                                                 if PREF_MULTIPLY_COLOR:
65                                                         for c in f.col:
66                                                                 c.r= int(((c.r/255.0) * (mat[0]/255.0)) * 255.0)
67                                                                 c.g= int(((c.g/255.0) * (mat[1]/255.0)) * 255.0)
68                                                                 c.b= int(((c.b/255.0) * (mat[2]/255.0)) * 255.0)
69                                                 else:
70                                                         for c in f.col:
71                                                                 c.r=mat[0]
72                                                                 c.g=mat[1]
73                                                                 c.b=mat[2]
74                 me.update()
75
76 def main():
77         # Create the variables.
78         
79         PREF_SEL_FACES_ONLY= Draw.Create(1)
80         PREF_ACTOB_ONLY= Draw.Create(1)
81         PREF_MULTIPLY_COLOR = Draw.Create(0)
82         
83         pup_block = [\
84         ('Sel Faces Only', PREF_SEL_FACES_ONLY, 'Only apply to selected faces.'),\
85         ('Active Only', PREF_ACTOB_ONLY, 'Operate on all selected objects.'),\
86         ('Multiply Existing', PREF_MULTIPLY_COLOR, 'Multiplies material color with existing.'),\
87         ]
88         
89         if not Draw.PupBlock('VCols from Material', pup_block):
90                 return
91         
92         PREF_SEL_FACES_ONLY= PREF_SEL_FACES_ONLY.val
93         PREF_ACTOB_ONLY= PREF_ACTOB_ONLY.val
94         PREF_MULTIPLY_COLOR= PREF_MULTIPLY_COLOR.val
95
96         mat2vcol(PREF_SEL_FACES_ONLY, PREF_ACTOB_ONLY, PREF_MULTIPLY_COLOR)
97
98 if __name__=='__main__':
99         main()