Trying to make something like a clothing manager addon for myself. Easy to add, enable and disable objects. I'm not sure if this is the right way, because I can't get which button is pressed. But I don't even know what exactly I should be looking at to solve this.
draw:
def draw(self, context):
layout = self.layout
scene = context.scene
col = layout.column(align=False)
layout.operator( "add.select")
layout.label(text=" Outfit List:")
SCENE_OBJ = bpy.context.scene.objects
for tmp_obj in SCENE_OBJ:
if tmp_obj.type == 'MESH':
if ".mf1t." in tmp_obj.name:
if ".mf1t.active" in tmp_obj.name:
layout.operator("on.off", text=tmp_obj.data.name, text_ctxt='hoho', depress=True)
if ".mf1t.null" in tmp_obj.name:
layout.operator("on.off", text=tmp_obj.data.name, text_ctxt='haha', depress=False)
operator:
class op_add (bpy.types.Operator):
bl_idname = "add.select"
bl_label = "Add to List"
def execute(self, context):
CS_OBJ = bpy.context.selected_objects
for tmp_obj in CS_OBJ:
if tmp_obj.type == 'MESH':
if ".mf1t." not in tmp_obj.name:
tmp_obj.name = tmp_obj.name + ".mf1t.active"
class op_on_off (bpy.types.Operator):
bl_idname = "on.off"
bl_label = "On / Off"
def execute(self, context):
if context.window_manager:
if ".mf1t.active" in tmp_obj.name:
tmp_obj.name = tmp_obj.name.replace("active", "null")
if ".mf1t.null" in tmp_obj.name:
tmp_obj.name = tmp_obj.name.replace("null", "active")
--------------------------------------------------------------------
I'll post my result here. Perhaps this will help someone, but Marty answer is already pretty clear. Result (Thank to Marty Fouts):
bl_info = {
"name" : "On/Off Manager Explain",
"description" : "A demonstration addon (https://blender.stackexchange.com/questions/252081/create-buttons-from-the-objects-with-same-action-for-each-of-them-get-name-pre)",
"author" : "Marty Fouts and duales",
"version" : (1, 0, 0),
"blender" : (2, 80, 0),
"location" : "View3D",
"warning" : "",
"support" : "COMMUNITY",
"doc_url" : "https://blender.stackexchange.com/questions/252081/create-buttons-from-the-objects-with-same-action-for-each-of-them-get-name-pre",
"category" : "3D View"
}
import bpy
from bpy.types import Operator
from bpy.types import Panel
from bpy.props import StringProperty
class op_add_exp(Operator):
bl_idname = "op.add"
bl_label = "Add to Global"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
for tmp_obj in bpy.context.selected_objects:
if tmp_obj.type == 'MESH':
if ".mf1t." not in tmp_obj.name:
tmp_obj.name = tmp_obj.name + ".mf1t.active"
return {'FINISHED'}
class op_on_off_exp(Operator):
""" tooltip goes here """
bl_idname = "op.on_off"
bl_label = "Foo Operator"
bl_options = {"REGISTER", "UNDO"}
foo : StringProperty(name="foo", description="foobar")
@classmethod
def poll(cls, context):
return context.mode == "OBJECT"
def execute(self, context):
for tmp_obj in bpy.context.scene.objects:
if ".mf1t.active" in tmp_obj.name:
if tmp_obj.name == self.foo:
tmp_obj.name = tmp_obj.name.replace("active", "null")
tmp_obj.hide_viewport = True
tmp_obj.hide_render = True
elif ".mf1t.null" in tmp_obj.name:
if tmp_obj.name == self.foo:
tmp_obj.name = tmp_obj.name.replace("null", "active")
tmp_obj.hide_viewport = False
tmp_obj.hide_render = False
return {'FINISHED'}
class panel_manager_exp(Panel):
"""Display test button twice. Invoke it with different arguments"""
bl_label = "On/Off Manager Exp"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Item"
def draw(self, context):
self.layout.operator(op_add_exp.bl_idname, text="Add Selected OBJ")
self.layout.label(text="Added Object List")
col = self.layout.column(align=True)
for tmp_obj in bpy.context.scene.objects:
if tmp_obj.type == 'MESH':
if ".mf1t." in tmp_obj.name:
if ".mf1t.active" in tmp_obj.name:
prop = col.operator(op_on_off_exp.bl_idname, text=tmp_obj.data.name, depress=True).foo=tmp_obj.name
if ".mf1t.null" in tmp_obj.name:
prop = col.operator(op_on_off_exp.bl_idname, text=tmp_obj.data.name, depress=False).foo=tmp_obj.name
classes = [
op_on_off_exp,
op_add_exp,
panel_manager_exp,
]
def register():
for c in classes:
bpy.utils.register_class(c)
def unregister():
for c in classes:
bpy.utils.unregister_class(c)
if __name__ == '__main__':
register()
And in the end you will be able to do something like this. Simple but convenient.