Hi, had a quick look and here’s some notes
def count_materials():
var = bpy.data.materials
x = str(var)
mat_count = x[16:-22]
max = int(mat_count)
return max
max = len(bpy.data.materials)
var = obj.material_slots.data.active_material
var = str(var)
Material = var[23:-3]
mat = obj.active_material
Material = mat.name
When you set up a property with bpy.types.Scene.xxxx = bpy.props.XxxxProperty you can access it via scene.xxxx rather than scene[‘xxxx’]. This way it will return the default value. Otherwise you need to initialise the property or it throws a bug.
Lastly a menu construct instead
import bpy
from bpy.props import StringProperty
def get_objs_with_mat(context, mat_name):
ob_list = []
for ob in context.scene.objects:
mats = [s.name for s in ob.material_slots]
if mat_name in mats:
ob_list.append(ob.name)
print("%d objects with material:%s" % (len(ob_list),mat_name))
print(ob_list)
class SimpleOperator(bpy.types.Operator):
"""Print Material name to Console"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"
mat = StringProperty(default="")
@classmethod
def poll(cls, context):
return bool(len(bpy.data.materials))
def execute(self, context):
print(self.mat)
context.screen.search = self.mat
get_objs_with_mat(context, self.mat)
return {'FINISHED'}
class SimpleCustomMenu(bpy.types.Menu):
bl_label = "Materials"
bl_idname = "OBJECT_MT_simple_custom_menu"
def draw(self, context):
layout = self.layout
for mat in bpy.data.materials:
op = layout.operator("object.simple_operator", text=mat.name)
op.mat = mat.name
def register():
bpy.types.Screen.search = StringProperty(default="")
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.register_module(__name__)
if __name__ == "__main__":
register()
# The menu can also be called from scripts
bpy.ops.wm.call_menu(name=SimpleCustomMenu.bl_idname)
The layout
row.menu("OBJECT_MT_simple_custom_menu", text=context.screen.search)
Another useful way to make an updated list is with a standard py property
import bpy
def get_objs_with_mat(self):
ob_list = []
for ob in self.scene.objects:
mats = [s.name for s in ob.material_slots]
if self.screen.search in mats:
ob_list.append(ob.name)
print("%d objects with material:%s" % (len(ob_list),self.screen.search))
return ob_list
bpy.types.Context.ob_matlist = property(get_objs_with_mat)
Here is the output from the console
>>> C.screen.search = D.materials['Material'].name
>>> C.screen.search
'Material'
>>> C.ob_matlist
5 objects with material:Material
['Cube.004', 'Cube.003', 'Cube.002', 'Cube.001', 'Cube']
PS… just noticed I used screen.search rather than scene.search… no reason… tired.