i wrote a script that will “select by material”… in object mode it’ll select all the objecxts that use the material you choose, in edit mode it’ll select all the faces that use the material in the active object…
and yes, you do have to iterate through all objects in the scene…
#!BPY
"""
Name: 'Find Items with Material'
Blender: 234
Group: 'Object'
Tooltip: 'In object mode: selects all objects with the chosen material/n in edit mode selects faces.'
"""
import bpy
from Blender import *
from Blender import Scene, Mesh, Window, sys
import BPyMessages
#late version!
def getAllMaterials(is_editmode):
matlist =[]
if is_editmode:
scn = bpy.data.scenes.active
ob_act = scn.objects.active
me = ob_act.getData(mesh=1)
mats= me.materials
for m in mats:
matlist.append(m.name)
return matlist
else:
for mat in bpy.data.materials:
matlist.append(mat.name)
return matlist
def ui(is_editmode):
#create a menu for the user to pick materials from
#get the existing materials
matlist = getAllMaterials(is_editmode)
pupmessage = "Select Objects With Material:%t"
if is_editmode:
pupmessage = "Select Faces With Material:%t"
Puplist = pupmessage
for mat in matlist:
Puplist+= "|"+ mat
#Ok, make the menu
chan = Draw.PupMenu(Puplist,30)
if chan != -1: #something was chosen process it
try:
material = matlist[chan-1]
print"chosen Material is", material
return material
except:
return none
def findMaterial(matToFind):
is_editmode = Window.EditMode()
scn = bpy.data.scenes.active
if is_editmode:
Window.EditMode(0)
#only current object
ob_act = scn.objects.active
me = ob_act.getData(mesh=1)
mats = me.materials
if mats:
i=0
matindex = 0
for m in mats:
if m.name == matToFind:
matindex = i
break
i += 1
for f in me.faces:
f.sel = False
if f.mat == matindex:
f.sel = True
else:
f.sel = False
else:
print "no mats on mesh"
me.update()
Window.EditMode(is_editmode)
else:
#check each scene starting with current:
scn = bpy.data.scenes.active
print "----------------"
for ob in scn.objects:
ob.sel = False
try:
if not ob.lib and ob.type == 'Mesh': # object isn't from a library and is a mes
me = ob.getData(mesh=1)
mats = me.materials
if len(mats) >= 1:
for m in mats:
if m.name == matToFind:
ob.sel = True
print("found %s in object " %matToFind, ob.name)
break
else:
ob.sel = False
except:
print "error with" ,ob.name
continue
Window.WaitCursor(0)
#Draw.PupMenu('Material "%s" Not found.' % matToFind)
def main():
is_editmode = Window.EditMode()
matToFind = ui(is_editmode)
if matToFind == None:
return
findMaterial(matToFind)
if __name__ == '__main__':
main()