[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [2838] trunk/py/scripts/addons/ space_view3d_materials_utils.py: - make pep8 compliant
Campbell Barton
ideasman42 at gmail.com
Sat Dec 31 10:53:09 CET 2011
Revision: 2838
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=2838
Author: campbellbarton
Date: 2011-12-31 09:52:48 +0000 (Sat, 31 Dec 2011)
Log Message:
-----------
- make pep8 compliant
- fix error when a mesh had a None material
- no need to set the object as avtive when assigning materials
Modified Paths:
--------------
trunk/py/scripts/addons/space_view3d_materials_utils.py
Modified: trunk/py/scripts/addons/space_view3d_materials_utils.py
===================================================================
--- trunk/py/scripts/addons/space_view3d_materials_utils.py 2011-12-31 09:20:59 UTC (rev 2837)
+++ trunk/py/scripts/addons/space_view3d_materials_utils.py 2011-12-31 09:52:48 UTC (rev 2838)
@@ -29,36 +29,44 @@
"blender": (2, 5, 6),
"api": 35324,
"location": "View3D > Q key",
- "description": "Menu of material tools (assign, select by etc) in the 3D View",
+ "description": "Menu of material tools (assign, select..) in the 3D View",
"warning": "",
- "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
- "Scripts/3D interaction/Materials Utils",
- "tracker_url": "https://projects.blender.org/tracker/index.php?"\
- "func=detail&aid=22140",
+ "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"
+ "Scripts/3D interaction/Materials Utils",
+ "tracker_url": "https://projects.blender.org/tracker/index.php?"
+ "func=detail&aid=22140",
"category": "3D View"}
"""
This script has several functions and operators... grouped for convenience
* assign material:
- offers the user a list of ALL the materials in the blend file and an additional "new" entry
- the chosen material will be assigned to all the selected objects in object mode.
-
+ offers the user a list of ALL the materials in the blend file and an
+ additional "new" entry the chosen material will be assigned to all the
+ selected objects in object mode.
+
in edit mode the selected faces get the selected material applied.
- if the user chose "new" the new material can be renamed using the "last operator" section of the toolbox
- After assigning the material "clean material slots" and "material to texface" are auto run to keep things tidy (see description bellow)
+ if the user chose "new" the new material can be renamed using the
+ "last operator" section of the toolbox.
+ After assigning the material "clean material slots" and
+ "material to texface" are auto run to keep things tidy
+ (see description bellow)
* select by material
- in object mode this offers the user a menu of all materials in the blend file
- any objects using the selected material will become selected, any objects without the material will be removed from selection.
-
- in edit mode: the menu offers only the materials attached to the current object. It will select the faces that use the material and deselect those that do not.
+ in object mode this offers the user a menu of all materials in the blend
+ file any objects using the selected material will become selected, any
+ objects without the material will be removed from selection.
+ in edit mode: the menu offers only the materials attached to the current
+ object. It will select the faces that use the material and deselect those
+ that do not.
+
* clean material slots
- for all selected objects any empty material slots or material slots with materials that are not used by the mesh faces will be removed.
+ for all selected objects any empty material slots or material slots with
+ materials that are not used by the mesh faces will be removed.
-* Any un-used materials and slots will be removed
+* Any un-used materials and slots will be removed
"""
@@ -66,92 +74,92 @@
from bpy.props import*
-def replace_material(m1 , m2, all_objects = False):
- #replace material named m1 with material named m2
- #m1 is the name of original material
- #m2 is the name of the material to replace it with
- #'all' will replace throughout the blend file
- try:
- matorg = bpy.data.materials[m1]
- matrep = bpy.data.materials[m2]
-
-
+def replace_material(m1, m2, all_objects=False):
+ # replace material named m1 with material named m2
+ # m1 is the name of original material
+ # m2 is the name of the material to replace it with
+ # 'all' will replace throughout the blend file
+
+ matorg = bpy.data.materials.get(m1)
+ matrep = bpy.data.materials.get(m2)
+
+ if matorg and matrep:
#store active object
scn = bpy.context.scene
ob_active = bpy.context.active_object
-
+
if all_objects:
objs = bpy.data.objects
-
+
else:
objs = bpy.context.selected_editable_objects
-
+
for ob in objs:
if ob.type == 'MESH':
scn.objects.active = ob
- print(ob.name)
- ms = ob.material_slots.values()
-
- for m in ms:
+
+ for m in ob.material_slots.values():
if m.material == matorg:
m.material = matrep
- #don't break the loop as the material can be
+ # don't break the loop as the material can be
# ref'd more than once
-
- #restore active object
- scn.objects.active = ob_active
- except:
+
+ else:
print('no match to replace')
-def select_material_by_name(find_mat):
- #in object mode selects all objects with material find_mat
- #in edit mode selects all faces with material find_mat
-
+
+def select_material_by_name(find_mat_name):
+ #in object mode selects all objects with material find_mat_name
+ #in edit mode selects all faces with material find_mat_name
+
+ find_mat = bpy.data.materials.get(find_mat_name)
+
+ if find_mat is None:
+ return
+
#check for editmode
editmode = False
scn = bpy.context.scene
-
+
#set selection mode to faces
- scn.tool_settings.mesh_select_mode =[False,False,True]
-
+ scn.tool_settings.mesh_select_mode = False, False, True
+
actob = bpy.context.active_object
if actob.mode == 'EDIT':
- editmode =True
+ editmode = True
bpy.ops.object.mode_set()
-
-
+
if not editmode:
- objs = bpy.data.objects
+ objs = bpy.data.objects
for ob in objs:
- typ = ['MESH','CURVE', 'SURFACE', 'FONT', 'META']
- if ob.type in typ:
+ if ob.type in {'MESH', 'CURVE', 'SURFACE', 'FONT', 'META'}:
ms = ob.material_slots.values()
for m in ms:
- if m.material.name == find_mat:
+ if m.material == find_mat:
ob.select = True
- #the active object may not have the mat!
- #set it to one that does!
+ # the active object may not have the mat!
+ # set it to one that does!
scn.objects.active = ob
break
else:
ob.select = False
-
- #deselect non-meshes
+
+ #deselect non-meshes
else:
ob.select = False
-
+
else:
#it's editmode, so select the faces
ob = actob
ms = ob.material_slots.values()
-
+
#same material can be on multiple slots
- slot_indeces =[]
+ slot_indeces = []
i = 0
# found = False # UNUSED
for m in ms:
- if m.material.name == find_mat:
+ if m.material == find_mat:
slot_indeces.append(i)
# found = True # UNUSED
i += 1
@@ -161,29 +169,30 @@
f.select = True
else:
f.select = False
- me.update()
+ me.update()
if editmode:
- bpy.ops.object.mode_set(mode = 'EDIT')
+ bpy.ops.object.mode_set(mode='EDIT')
+
def mat_to_texface():
- #assigns the first image in each material to the faces in the active uvlayer
- #for all selected objects
-
+ # assigns the first image in each material to the faces in the active
+ # uvlayer for all selected objects
+
#check for editmode
editmode = False
-
+
actob = bpy.context.active_object
if actob.mode == 'EDIT':
- editmode =True
+ editmode = True
bpy.ops.object.mode_set()
-
+
for ob in bpy.context.selected_editable_objects:
if ob.type == 'MESH':
#get the materials from slots
ms = ob.material_slots.values()
-
+
#build a list of images, one per material
- images=[]
+ images = []
#get the textures from the mats
for m in ms:
gotimage = False
@@ -195,17 +204,16 @@
if tex.type == 'IMAGE':
img = tex.image
images.append(img)
- gotimage =True
+ gotimage = True
break
-
+
if not gotimage:
print('noimage on', m.name)
images.append(None)
-
- #now we have the images
- #applythem to the uvlayer
-
-
+
+ # now we have the images
+ # applythem to the uvlayer
+
me = ob.data
#got uvs?
if not me.uv_textures:
@@ -213,7 +221,7 @@
scn.objects.active = ob
bpy.ops.mesh.uv_texture_add()
scn.objects.active = actob
-
+
#get active uvlayer
for t in me.uv_textures:
if t.active:
@@ -224,15 +232,13 @@
uvtex[f.index].image = images[f.material_index]
else:
uvtex[f.index].image = None
-
+
me.update()
-
-
+
if editmode:
- bpy.ops.object.mode_set(mode = 'EDIT')
-
-
+ bpy.ops.object.mode_set(mode='EDIT')
+
def assignmatslots(ob, matlist):
#given an object and a list of material names
#removes all material slots form the object
@@ -246,15 +252,14 @@
for s in ob.material_slots:
bpy.ops.object.material_slot_remove()
-
- #re-add them and assign material
+ # re-add them and assign material
i = 0
for m in matlist:
mat = bpy.data.materials[m]
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-extensions-cvs
mailing list