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 #####
23 "name": "Material Utils",
26 "blender": (2, 66, 6),
27 "location": "View3D > Q key",
28 "description": "Menu of material tools (assign, select..) in the 3D View",
30 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
31 "Scripts/3D interaction/Materials Utils",
32 "tracker_url": "https://developer.blender.org/T22140",
33 "category": "Material"}
36 This script has several functions and operators, grouped for convenience:
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)
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
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.
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.
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.
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.
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)
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:
114 if hasattr(ob.data, "materials"):
115 for mat in ob.data.materials:
116 if mat.library is None: #and not in mats:
119 mats
= (mat
for ob
in objs
if hasattr(ob
.data
, "materials") for mat
in ob
.data
.materials
if mat
.library
is None)
122 mat
.use_fake_user
= fake_user
== 'ON'
124 for area
in bpy
.context
.screen
.areas
:
125 if area
.type in ('PROPERTIES', 'NODE_EDITOR'):
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
):
140 scn
= bpy
.context
.scene
143 objs
= bpy
.data
.objects
146 objs
= bpy
.context
.selected_editable_objects
149 if ob
.type == 'MESH':
153 for m
in ob
.material_slots
:
154 if m
.material
== matorg
:
156 # don't break the loop as the material can be
157 # ref'd more than once
159 # Indicate which objects were affected
164 if update_selection
and not match
:
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
)
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':
191 bpy
.ops
.object.mode_set()
194 objs
= bpy
.data
.objects
196 if ob
.type in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}:
197 ms
= ob
.material_slots
199 if m
.material
== find_mat
:
201 # the active object may not have the mat!
202 # set it to one that does!
203 scn
.objects
.active
= ob
213 #it's editmode, so select the polygons
215 ms
= ob
.material_slots
217 #same material can be on multiple slots
220 # found = False # UNUSED
222 if m
.material
== find_mat
:
223 slot_indeces
.append(i
)
224 # found = True # UNUSED
227 for f
in me
.polygons
:
228 if f
.material_index
in slot_indeces
:
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
244 actob
= bpy
.context
.active_object
245 if actob
.mode
== 'EDIT':
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
256 #get the textures from the mats
258 if m
.material
is None:
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:
265 if tex
.type == 'IMAGE':
272 print('noimage on', m
.name
)
275 # now we have the images
276 # applythem to the uvlayer
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
287 for t
in me
.uv_textures
:
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
]
295 uvtex
[f
.index
].image
= None
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
319 mat
= bpy
.data
.materials
[m
]
320 ob
.data
.materials
.append(mat
)
323 # restore active object:
324 scn
.objects
.active
= ob_active
330 actob
= bpy
.context
.active_object
331 if actob
.mode
== 'EDIT':
333 bpy
.ops
.object.mode_set()
335 objs
= bpy
.context
.selected_editable_objects
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
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
355 for m
in usedMatIndex
:
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
367 for u
in usedMatIndex
:
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:
376 for f
in me
.polygons
:
377 matindex
= mnames
.index(faceMats
[i
])
378 f
.material_index
= matindex
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
391 for m
in bpy
.data
.materials
:
392 if m
.name
== matname
:
397 target
= bpy
.data
.materials
.new(matname
)
399 # if objectmode then set all polygons
402 if actob
.mode
== 'EDIT':
405 bpy
.ops
.object.mode_set()
407 objs
= bpy
.context
.selected_editable_objects
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'}:
417 for m
in bpy
.data
.materials
:
418 if m
.name
== matname
:
426 assignmatslots(ob
, targetlist
)
428 elif ob
.type == 'MESH':
429 # check material slots for matname material
432 mats
= ob
.material_slots
434 if m
.name
== matname
:
438 ob
.active_material_index
= i
444 #the material is not attached to the object
445 ob
.data
.materials
.append(target
)
447 #now assign the material:
450 for f
in me
.polygons
:
451 f
.material_index
= index
452 elif allpolygons
== False:
453 for f
in me
.polygons
:
455 f
.material_index
= index
458 #restore the active object
459 bpy
.context
.scene
.objects
.active
= actob
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
)
472 tex
= bpy
.data
.textures
.new(name
=img
.name
, type='IMAGE')
476 #see if the material already uses this tex
479 for m
in mat
.texture_slots
:
480 if m
and m
.texture
== tex
:
483 if not found
and mat
:
484 mtex
= mat
.texture_slots
.add()
486 mtex
.texture_coords
= 'UV'
487 mtex
.use_map_color_diffuse
= True
490 def texface_to_mat():
491 # editmode check here!
493 ob
= bpy
.context
.object
494 if ob
.mode
== 'EDIT':
496 bpy
.ops
.object.mode_set()
498 for ob
in bpy
.context
.selected_editable_objects
:
503 # get the texface images and store indices
504 if (ob
.data
.uv_textures
):
505 for f
in ob
.data
.uv_textures
.active
.data
:
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
))
515 faceindex
.append(None)
517 # check materials for images exist; create if needed
519 for i
in unique_images
:
522 m
= bpy
.data
.materials
[i
.name
]
524 m
= bpy
.data
.materials
.new(name
=i
.name
)
528 matlist
.append(m
.name
)
529 # add textures if needed
532 # set up the object material slots
533 assignmatslots(ob
, matlist
)
535 #set texface indices to material slot indices..
541 me
.polygons
[i
].material_index
= f
544 bpy
.ops
.object.mode_set(mode
='EDIT')
546 def remove_materials():
548 for ob
in bpy
.data
.objects
:
551 bpy
.ops
.object.material_slot_remove()
552 print ("removed material from " + ob
.name
)
554 print (ob
.name
+ " does not have materials.")
555 # -----------------------------------------------------------------------------
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'}
565 def poll(cls
, context
):
566 return context
.active_object
is not None
568 def execute(self
, context
):
569 if context
.selected_editable_objects
:
573 self
.report({'WARNING'},
574 "No editable selected objects, could not finish")
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',
592 def poll(cls
, context
):
593 return context
.active_object
is not None
595 def execute(self
, context
):
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'}
612 def poll(cls
, context
):
613 return context
.active_object
is not None
615 def execute(self
, context
):
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'}
627 def poll(cls
, context
):
628 return context
.active_object
is not None
630 def execute(self
, context
):
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'}
641 def poll(cls
, context
):
642 return context
.active_object
is not None
644 def execute(self
, context
):
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',
661 def poll(cls
, context
):
662 return context
.active_object
is not None
664 def execute(self
, context
):
666 select_material_by_name(mn
)
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(
678 description
="Material to replace",
681 matrep
= StringProperty(name
="Replacement",
682 description
="Replacement material",
685 all_objects
= BoolProperty(
687 description
="Replace for all objects in this blend file",
690 update_selection
= BoolProperty(
691 name
="Update Selection",
692 description
="Select affected objects and deselect unaffected",
696 # Allow to replace all objects even without a selection / active object
698 #def poll(cls, context):
699 # return context.active_object is not None
701 def draw(self
, context
):
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
)
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(
724 description
="Turn fake user on or off",
725 items
=(('ON', "On", "Enable fake user"),('OFF', "Off", "Disable fake user")),
729 materials
= EnumProperty(
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")),
741 def draw(self
, context
):
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
)
754 # -----------------------------------------------------------------------------
757 class VIEW3D_MT_master_material(bpy
.types
.Menu
):
758 bl_label
= "Material Utils Menu"
760 def draw(self
, context
):
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')
767 layout
.operator("view3d.clean_material_slots",
768 text
="Clean Material Slots",
770 layout
.operator("view3d.material_remove",
771 text
="Remove Material Slots",
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')
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',
790 class VIEW3D_MT_assign_material(bpy
.types
.Menu
):
791 bl_label
= "Assign Material"
793 def draw(self
, context
):
795 layout
.operator_context
= 'INVOKE_REGION_WIN'
796 for material_name
in bpy
.data
.materials
.keys():
797 layout
.operator("view3d.assign_material",
799 icon
='MATERIAL_DATA').matname
= material_name
801 layout
.operator("view3d.assign_material",
806 class VIEW3D_MT_select_material(bpy
.types
.Menu
):
807 bl_label
= "Select by Material"
809 def draw(self
, context
):
811 layout
.operator_context
= 'INVOKE_REGION_WIN'
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",
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()
828 layout
.operator("view3d.select_material_by_name",
830 icon
='MATERIAL_DATA').matname
= m
834 bpy
.utils
.register_module(__name__
)
836 kc
= bpy
.context
.window_manager
.keyconfigs
.addon
838 km
= kc
.keymaps
.new(name
="3D View", space_type
="VIEW_3D")
839 kmi
= km
.keymap_items
.new('wm.call_menu', 'Q', 'PRESS')
840 kmi
.properties
.name
= "VIEW3D_MT_master_material"
844 bpy
.utils
.unregister_module(__name__
)
846 kc
= bpy
.context
.window_manager
.keyconfigs
.addon
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
)
855 if __name__
== "__main__":