Edit recent commit, no need to assign dummy vars
[blender-addons.git] / space_view3d_materials_utils.py
blob7e6fefdf55e3496b2af07c40b331f19f57fb0952
1 # (c) 2010 Michael Williamson (michaelw)
2 # ported from original by Michael Williamson
4 # ##### BEGIN GPL LICENSE BLOCK #####
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # ##### END GPL LICENSE BLOCK #####
22 bl_info = {
23 "name": "Material Utils",
24 "author": "michaelw",
25 "version": (1, 6),
26 "blender": (2, 66, 6),
27 "location": "View3D > ctrl-Q key",
28 "description": "Menu of material tools (assign, select..) in the 3D View",
29 "warning": "",
30 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
31 "Scripts/3D interaction/Materials Utils",
32 "category": "Material",
35 """
36 This script has several functions and operators, grouped for convenience:
38 * assign material:
39 offers the user a list of ALL the materials in the blend file and an
40 additional "new" entry the chosen material will be assigned to all the
41 selected objects in object mode.
43 in edit mode the selected polygons get the selected material applied.
45 if the user chose "new" the new material can be renamed using the
46 "last operator" section of the toolbox.
47 After assigning the material "clean material slots" and
48 "material to texface" are auto run to keep things tidy
49 (see description bellow)
52 * select by material
53 in object mode this offers the user a menu of all materials in the blend
54 file any objects using the selected material will become selected, any
55 objects without the material will be removed from selection.
57 in edit mode: the menu offers only the materials attached to the current
58 object. It will select the polygons that use the material and deselect those
59 that do not.
61 * clean material slots
62 for all selected objects any empty material slots or material slots with
63 materials that are not used by the mesh polygons will be removed.
65 * remove material slots
66 removes all material slots of the active object.
68 * material to texface
69 transfers material assignments to the UV editor. This is useful if you
70 assigned materials in the properties editor, as it will use the already
71 set up materials to assign the UV images per-face. It will use the first
72 enabled image texture it finds.
74 * texface to materials
75 creates texture materials from images assigned in UV editor.
77 * replace materials
78 lets your replace one material by another. Optionally for all objects in
79 the blend, otherwise for selected editable objects only. An additional
80 option allows you to update object selection, to indicate which objects
81 were affected and which not.
83 * set fake user
84 enable/disable fake user for materials. You can chose for which materials
85 it shall be set, materials of active / selected / objects in current scene
86 or used / unused / all materials.
88 """
91 import bpy
92 from bpy.props import StringProperty, BoolProperty, EnumProperty
95 def fake_user_set(fake_user='ON', materials='UNUSED'):
96 if materials == 'ALL':
97 mats = (mat for mat in bpy.data.materials if mat.library is None)
98 elif materials == 'UNUSED':
99 mats = (mat for mat in bpy.data.materials if mat.library is None and mat.users == 0)
100 else:
101 mats = []
102 if materials == 'ACTIVE':
103 objs = [bpy.context.active_object]
104 elif materials == 'SELECTED':
105 objs = bpy.context.selected_objects
106 elif materials == 'SCENE':
107 objs = bpy.context.scene.objects
108 else: # materials == 'USED'
109 objs = bpy.data.objects
110 # Maybe check for users > 0 instead?
112 """ more reable than the following generator:
113 for ob in objs:
114 if hasattr(ob.data, "materials"):
115 for mat in ob.data.materials:
116 if mat.library is None: #and not in mats:
117 mats.append(mat)
119 mats = (mat for ob in objs if hasattr(ob.data, "materials") for mat in ob.data.materials if mat.library is None)
121 for mat in mats:
122 mat.use_fake_user = fake_user == 'ON'
124 for area in bpy.context.screen.areas:
125 if area.type in ('PROPERTIES', 'NODE_EDITOR'):
126 area.tag_redraw()
129 def replace_material(m1, m2, all_objects=False, update_selection=False):
130 # replace material named m1 with material named m2
131 # m1 is the name of original material
132 # m2 is the name of the material to replace it with
133 # 'all' will replace throughout the blend file
135 matorg = bpy.data.materials.get(m1)
136 matrep = bpy.data.materials.get(m2)
138 if matorg != matrep and None not in (matorg, matrep):
139 #store active object
140 scn = bpy.context.scene
142 if all_objects:
143 objs = bpy.data.objects
145 else:
146 objs = bpy.context.selected_editable_objects
148 for ob in objs:
149 if ob.type == 'MESH':
151 match = False
153 for m in ob.material_slots:
154 if m.material == matorg:
155 m.material = matrep
156 # don't break the loop as the material can be
157 # ref'd more than once
159 # Indicate which objects were affected
160 if update_selection:
161 ob.select = True
162 match = True
164 if update_selection and not match:
165 ob.select = False
167 #else:
168 # print('Replace material: nothing to replace')
171 def select_material_by_name(find_mat_name):
172 #in object mode selects all objects with material find_mat_name
173 #in edit mode selects all polygons with material find_mat_name
175 find_mat = bpy.data.materials.get(find_mat_name)
177 if find_mat is None:
178 return
180 #check for editmode
181 editmode = False
183 scn = bpy.context.scene
185 #set selection mode to polygons
186 scn.tool_settings.mesh_select_mode = False, False, True
188 actob = bpy.context.active_object
189 if actob.mode == 'EDIT':
190 editmode = True
191 bpy.ops.object.mode_set()
193 if not editmode:
194 objs = bpy.data.objects
195 for ob in objs:
196 if ob.type in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}:
197 ms = ob.material_slots
198 for m in ms:
199 if m.material == find_mat:
200 ob.select = True
201 # the active object may not have the mat!
202 # set it to one that does!
203 scn.objects.active = ob
204 break
205 else:
206 ob.select = False
208 #deselect non-meshes
209 else:
210 ob.select = False
212 else:
213 #it's editmode, so select the polygons
214 ob = actob
215 ms = ob.material_slots
217 #same material can be on multiple slots
218 slot_indeces = []
219 i = 0
220 # found = False # UNUSED
221 for m in ms:
222 if m.material == find_mat:
223 slot_indeces.append(i)
224 # found = True # UNUSED
225 i += 1
226 me = ob.data
227 for f in me.polygons:
228 if f.material_index in slot_indeces:
229 f.select = True
230 else:
231 f.select = False
232 me.update()
233 if editmode:
234 bpy.ops.object.mode_set(mode='EDIT')
237 def mat_to_texface():
238 # assigns the first image in each material to the polygons in the active
239 # uvlayer for all selected objects
241 #check for editmode
242 editmode = False
244 actob = bpy.context.active_object
245 if actob.mode == 'EDIT':
246 editmode = True
247 bpy.ops.object.mode_set()
249 for ob in bpy.context.selected_editable_objects:
250 if ob.type == 'MESH':
251 #get the materials from slots
252 ms = ob.material_slots
254 #build a list of images, one per material
255 images = []
256 #get the textures from the mats
257 for m in ms:
258 if m.material is None:
259 continue
260 gotimage = False
261 textures = zip(m.material.texture_slots, m.material.use_textures)
262 for t, enabled in textures:
263 if enabled and t is not None:
264 tex = t.texture
265 if tex.type == 'IMAGE':
266 img = tex.image
267 images.append(img)
268 gotimage = True
269 break
271 if not gotimage:
272 print('noimage on', m.name)
273 images.append(None)
275 # now we have the images
276 # applythem to the uvlayer
278 me = ob.data
279 #got uvs?
280 if not me.uv_textures:
281 scn = bpy.context.scene
282 scn.objects.active = ob
283 bpy.ops.mesh.uv_texture_add()
284 scn.objects.active = actob
286 #get active uvlayer
287 for t in me.uv_textures:
288 if t.active:
289 uvtex = t.data
290 for f in me.polygons:
291 #check that material had an image!
292 if images[f.material_index] is not None:
293 uvtex[f.index].image = images[f.material_index]
294 else:
295 uvtex[f.index].image = None
297 me.update()
299 if editmode:
300 bpy.ops.object.mode_set(mode='EDIT')
303 def assignmatslots(ob, matlist):
304 #given an object and a list of material names
305 #removes all material slots form the object
306 #adds new ones for each material in matlist
307 #adds the materials to the slots as well.
309 scn = bpy.context.scene
310 ob_active = bpy.context.active_object
311 scn.objects.active = ob
313 for s in ob.material_slots:
314 bpy.ops.object.material_slot_remove()
316 # re-add them and assign material
317 i = 0
318 for m in matlist:
319 mat = bpy.data.materials[m]
320 ob.data.materials.append(mat)
321 i += 1
323 # restore active object:
324 scn.objects.active = ob_active
327 def cleanmatslots():
328 #check for edit mode
329 editmode = False
330 actob = bpy.context.active_object
331 if actob.mode == 'EDIT':
332 editmode = True
333 bpy.ops.object.mode_set()
335 objs = bpy.context.selected_editable_objects
337 for ob in objs:
338 if ob.type == 'MESH':
339 mats = ob.material_slots.keys()
341 #check the polygons on the mesh to build a list of used materials
342 usedMatIndex = [] # we'll store used materials indices here
343 faceMats = []
344 me = ob.data
345 for f in me.polygons:
346 #get the material index for this face...
347 faceindex = f.material_index
349 #indices will be lost: Store face mat use by name
350 currentfacemat = mats[faceindex]
351 faceMats.append(currentfacemat)
353 # check if index is already listed as used or not
354 found = 0
355 for m in usedMatIndex:
356 if m == faceindex:
357 found = 1
358 #break
360 if found == 0:
361 #add this index to the list
362 usedMatIndex.append(faceindex)
364 #re-assign the used mats to the mesh and leave out the unused
365 ml = []
366 mnames = []
367 for u in usedMatIndex:
368 ml.append(mats[u])
369 #we'll need a list of names to get the face indices...
370 mnames.append(mats[u])
372 assignmatslots(ob, ml)
374 # restore face indices:
375 i = 0
376 for f in me.polygons:
377 matindex = mnames.index(faceMats[i])
378 f.material_index = matindex
379 i += 1
381 if editmode:
382 bpy.ops.object.mode_set(mode='EDIT')
385 def assign_mat(matname="Default"):
386 # get active object so we can restore it later
387 actob = bpy.context.active_object
389 # check if material exists, if it doesn't then create it
390 found = False
391 for m in bpy.data.materials:
392 if m.name == matname:
393 target = m
394 found = True
395 break
396 if not found:
397 target = bpy.data.materials.new(matname)
399 # if objectmode then set all polygons
400 editmode = False
401 allpolygons = True
402 if actob.mode == 'EDIT':
403 editmode = True
404 allpolygons = False
405 bpy.ops.object.mode_set()
407 objs = bpy.context.selected_editable_objects
409 for ob in objs:
410 # set the active object to our object
411 scn = bpy.context.scene
412 scn.objects.active = ob
414 if ob.type in {'CURVE', 'SURFACE', 'FONT', 'META'}:
415 found = False
416 i = 0
417 for m in bpy.data.materials:
418 if m.name == matname:
419 found = True
420 index = i
421 break
422 i += 1
423 if not found:
424 index = i - 1
425 targetlist = [index]
426 assignmatslots(ob, targetlist)
428 elif ob.type == 'MESH':
429 # check material slots for matname material
430 found = False
431 i = 0
432 mats = ob.material_slots
433 for m in mats:
434 if m.name == matname:
435 found = True
436 index = i
437 #make slot active
438 ob.active_material_index = i
439 break
440 i += 1
442 if not found:
443 index = i
444 #the material is not attached to the object
445 ob.data.materials.append(target)
447 #now assign the material:
448 me = ob.data
449 if allpolygons:
450 for f in me.polygons:
451 f.material_index = index
452 elif allpolygons == False:
453 for f in me.polygons:
454 if f.select:
455 f.material_index = index
456 me.update()
458 #restore the active object
459 bpy.context.scene.objects.active = actob
460 if editmode:
461 bpy.ops.object.mode_set(mode='EDIT')
464 def check_texture(img, mat):
465 #finds a texture from an image
466 #makes a texture if needed
467 #adds it to the material if it isn't there already
469 tex = bpy.data.textures.get(img.name)
471 if tex is None:
472 tex = bpy.data.textures.new(name=img.name, type='IMAGE')
474 tex.image = img
476 #see if the material already uses this tex
477 #add it if needed
478 found = False
479 for m in mat.texture_slots:
480 if m and m.texture == tex:
481 found = True
482 break
483 if not found and mat:
484 mtex = mat.texture_slots.add()
485 mtex.texture = tex
486 mtex.texture_coords = 'UV'
487 mtex.use_map_color_diffuse = True
490 def texface_to_mat():
491 # editmode check here!
492 editmode = False
493 ob = bpy.context.object
494 if ob.mode == 'EDIT':
495 editmode = True
496 bpy.ops.object.mode_set()
498 for ob in bpy.context.selected_editable_objects:
500 faceindex = []
501 unique_images = []
503 # get the texface images and store indices
504 if (ob.data.uv_textures):
505 for f in ob.data.uv_textures.active.data:
506 if f.image:
507 img = f.image
508 #build list of unique images
509 if img not in unique_images:
510 unique_images.append(img)
511 faceindex.append(unique_images.index(img))
513 else:
514 img = None
515 faceindex.append(None)
517 # check materials for images exist; create if needed
518 matlist = []
519 for i in unique_images:
520 if i:
521 try:
522 m = bpy.data.materials[i.name]
523 except:
524 m = bpy.data.materials.new(name=i.name)
525 continue
527 finally:
528 matlist.append(m.name)
529 # add textures if needed
530 check_texture(i, m)
532 # set up the object material slots
533 assignmatslots(ob, matlist)
535 #set texface indices to material slot indices..
536 me = ob.data
538 i = 0
539 for f in faceindex:
540 if f is not None:
541 me.polygons[i].material_index = f
542 i += 1
543 if editmode:
544 bpy.ops.object.mode_set(mode='EDIT')
546 def remove_materials():
548 for ob in bpy.data.objects:
549 print (ob.name)
550 try:
551 bpy.ops.object.material_slot_remove()
552 print ("removed material from " + ob.name)
553 except:
554 print (ob.name + " does not have materials.")
555 # -----------------------------------------------------------------------------
556 # operator classes:
558 class VIEW3D_OT_texface_to_material(bpy.types.Operator):
559 """Create texture materials for images assigned in UV editor"""
560 bl_idname = "view3d.texface_to_material"
561 bl_label = "Texface Images to Material/Texture (Material Utils)"
562 bl_options = {'REGISTER', 'UNDO'}
564 @classmethod
565 def poll(cls, context):
566 return context.active_object is not None
568 def execute(self, context):
569 if context.selected_editable_objects:
570 texface_to_mat()
571 return {'FINISHED'}
572 else:
573 self.report({'WARNING'},
574 "No editable selected objects, could not finish")
575 return {'CANCELLED'}
578 class VIEW3D_OT_assign_material(bpy.types.Operator):
579 """Assign a material to the selection"""
580 bl_idname = "view3d.assign_material"
581 bl_label = "Assign Material (Material Utils)"
582 bl_options = {'REGISTER', 'UNDO'}
584 matname = StringProperty(
585 name='Material Name',
586 description='Name of Material to Assign',
587 default="",
588 maxlen=63,
591 @classmethod
592 def poll(cls, context):
593 return context.active_object is not None
595 def execute(self, context):
596 mn = self.matname
597 print(mn)
598 assign_mat(mn)
599 cleanmatslots()
600 mat_to_texface()
601 return {'FINISHED'}
604 class VIEW3D_OT_clean_material_slots(bpy.types.Operator):
605 """Removes any material slots from selected objects """ \
606 """that are not used by the mesh"""
607 bl_idname = "view3d.clean_material_slots"
608 bl_label = "Clean Material Slots (Material Utils)"
609 bl_options = {'REGISTER', 'UNDO'}
611 @classmethod
612 def poll(cls, context):
613 return context.active_object is not None
615 def execute(self, context):
616 cleanmatslots()
617 return {'FINISHED'}
620 class VIEW3D_OT_material_to_texface(bpy.types.Operator):
621 """Transfer material assignments to UV editor"""
622 bl_idname = "view3d.material_to_texface"
623 bl_label = "Material Images to Texface (Material Utils)"
624 bl_options = {'REGISTER', 'UNDO'}
626 @classmethod
627 def poll(cls, context):
628 return context.active_object is not None
630 def execute(self, context):
631 mat_to_texface()
632 return {'FINISHED'}
634 class VIEW3D_OT_material_remove(bpy.types.Operator):
635 """Remove all material slots from active objects"""
636 bl_idname = "view3d.material_remove"
637 bl_label = "Remove All Material Slots (Material Utils)"
638 bl_options = {'REGISTER', 'UNDO'}
640 @classmethod
641 def poll(cls, context):
642 return context.active_object is not None
644 def execute(self, context):
645 remove_materials()
646 return {'FINISHED'}
649 class VIEW3D_OT_select_material_by_name(bpy.types.Operator):
650 """Select geometry with this material assigned to it"""
651 bl_idname = "view3d.select_material_by_name"
652 bl_label = "Select Material By Name (Material Utils)"
653 bl_options = {'REGISTER', 'UNDO'}
654 matname = StringProperty(
655 name='Material Name',
656 description='Name of Material to Select',
657 maxlen=63,
660 @classmethod
661 def poll(cls, context):
662 return context.active_object is not None
664 def execute(self, context):
665 mn = self.matname
666 select_material_by_name(mn)
667 return {'FINISHED'}
670 class VIEW3D_OT_replace_material(bpy.types.Operator):
671 """Replace a material by name"""
672 bl_idname = "view3d.replace_material"
673 bl_label = "Replace Material (Material Utils)"
674 bl_options = {'REGISTER', 'UNDO'}
676 matorg = StringProperty(
677 name="Original",
678 description="Material to replace",
679 maxlen=63,
681 matrep = StringProperty(name="Replacement",
682 description="Replacement material",
683 maxlen=63,
685 all_objects = BoolProperty(
686 name="All objects",
687 description="Replace for all objects in this blend file",
688 default=True,
690 update_selection = BoolProperty(
691 name="Update Selection",
692 description="Select affected objects and deselect unaffected",
693 default=True,
696 # Allow to replace all objects even without a selection / active object
697 #@classmethod
698 #def poll(cls, context):
699 # return context.active_object is not None
701 def draw(self, context):
702 layout = self.layout
703 layout.prop_search(self, "matorg", bpy.data, "materials")
704 layout.prop_search(self, "matrep", bpy.data, "materials")
705 layout.prop(self, "all_objects")
706 layout.prop(self, "update_selection")
708 def invoke(self, context, event):
709 return context.window_manager.invoke_props_dialog(self)
711 def execute(self, context):
712 replace_material(self.matorg, self.matrep, self.all_objects, self.update_selection)
713 return {'FINISHED'}
716 class VIEW3D_OT_fake_user_set(bpy.types.Operator):
717 """Enable/disable fake user for materials"""
718 bl_idname = "view3d.fake_user_set"
719 bl_label = "Set Fake User (Material Utils)"
720 bl_options = {'REGISTER', 'UNDO'}
722 fake_user = EnumProperty(
723 name="Fake User",
724 description="Turn fake user on or off",
725 items=(('ON', "On", "Enable fake user"),('OFF', "Off", "Disable fake user")),
726 default='ON'
729 materials = EnumProperty(
730 name="Materials",
731 description="Which materials of objects to affect",
732 items=(('ACTIVE', "Active object", "Materials of active object only"),
733 ('SELECTED', "Selected objects", "Materials of selected objects"),
734 ('SCENE', "Scene objects", "Materials of objects in current scene"),
735 ('USED', "Used", "All materials used by objects"),
736 ('UNUSED', "Unused", "Currently unused materials"),
737 ('ALL', "All", "All materials in this blend file")),
738 default='UNUSED'
741 def draw(self, context):
742 layout = self.layout
743 layout.prop(self, "fake_user", expand=True)
744 layout.prop(self, "materials")
746 def invoke(self, context, event):
747 return context.window_manager.invoke_props_dialog(self)
749 def execute(self, context):
750 fake_user_set(self.fake_user, self.materials)
751 return {'FINISHED'}
754 # -----------------------------------------------------------------------------
755 # menu classes
757 class VIEW3D_MT_master_material(bpy.types.Menu):
758 bl_label = "Material Utils Menu"
760 def draw(self, context):
761 layout = self.layout
762 layout.operator_context = 'INVOKE_REGION_WIN'
764 layout.menu("VIEW3D_MT_assign_material", icon='ZOOMIN')
765 layout.menu("VIEW3D_MT_select_material", icon='HAND')
766 layout.separator()
767 layout.operator("view3d.clean_material_slots",
768 text="Clean Material Slots",
769 icon='CANCEL')
770 layout.operator("view3d.material_remove",
771 text="Remove Material Slots",
772 icon='CANCEL')
773 layout.operator("view3d.material_to_texface",
774 text="Material to Texface",
775 icon='MATERIAL_DATA')
776 layout.operator("view3d.texface_to_material",
777 text="Texface to Material",
778 icon='MATERIAL_DATA')
780 layout.separator()
781 layout.operator("view3d.replace_material",
782 text='Replace Material',
783 icon='ARROW_LEFTRIGHT')
785 layout.operator("view3d.fake_user_set",
786 text='Set Fake User',
787 icon='UNPINNED')
790 class VIEW3D_MT_assign_material(bpy.types.Menu):
791 bl_label = "Assign Material"
793 def draw(self, context):
794 layout = self.layout
795 layout.operator_context = 'INVOKE_REGION_WIN'
796 for material_name in bpy.data.materials.keys():
797 layout.operator("view3d.assign_material",
798 text=material_name,
799 icon='MATERIAL_DATA').matname = material_name
801 layout.operator("view3d.assign_material",
802 text="Add New",
803 icon='ZOOMIN')
806 class VIEW3D_MT_select_material(bpy.types.Menu):
807 bl_label = "Select by Material"
809 def draw(self, context):
810 layout = self.layout
811 layout.operator_context = 'INVOKE_REGION_WIN'
813 ob = context.object
814 layout.label
815 if ob.mode == 'OBJECT':
816 #show all used materials in entire blend file
817 for material_name, material in bpy.data.materials.items():
818 if material.users > 0:
819 layout.operator("view3d.select_material_by_name",
820 text=material_name,
821 icon='MATERIAL_DATA',
822 ).matname = material_name
824 elif ob.mode == 'EDIT':
825 #show only the materials on this object
826 mats = ob.material_slots.keys()
827 for m in mats:
828 layout.operator("view3d.select_material_by_name",
829 text=m,
830 icon='MATERIAL_DATA').matname = m
833 def register():
834 bpy.utils.register_module(__name__)
836 kc = bpy.context.window_manager.keyconfigs.addon
837 if kc:
838 km = kc.keymaps.new(name="3D View", space_type="VIEW_3D")
839 kmi = km.keymap_items.new('wm.call_menu', 'Q', 'PRESS', ctrl=True)
840 kmi.properties.name = "VIEW3D_MT_master_material"
843 def unregister():
844 bpy.utils.unregister_module(__name__)
846 kc = bpy.context.window_manager.keyconfigs.addon
847 if kc:
848 km = kc.keymaps["3D View"]
849 for kmi in km.keymap_items:
850 if kmi.idname == 'wm.call_menu':
851 if kmi.properties.name == "VIEW3D_MT_master_material":
852 km.keymap_items.remove(kmi)
853 break
855 if __name__ == "__main__":
856 register()