Hello,
I have a script that is working mostly but the problem I’m having is how to get the right button index.
Basically the script creates additional buttons in the ui and each new created button will have next to it another one that will delete that specific row, but I cannot find a way to do it since right now the delete operator is looking at the val value number of its row and not to the index.
If you run the script and create few buttons the value one will tell in the console its index but how can a tell to another button to access it? Is the RemoveButtonOperator the one I’m trying to solve.
Here is the code:
import bpy
class SceneItems(bpy.types.PropertyGroup):
value : bpy.props.IntProperty()
class AddButtonOperator(bpy.types.Operator):
bl_idname = "scene.add_button_operator"
bl_label = "Add Button"
def execute(self, context):
id = len(context.scene.newbutton)
new = context.scene.newbutton.add()
new.name = str(id)
new.value = id
return {'FINISHED'}
class ButtonOperator(bpy.types.Operator):
bl_idname = "scene.button_operator"
bl_label = "Button"
id : bpy.props.IntProperty()
def execute(self, context):
print("Pressed button ", self.id)
return {'FINISHED'}
class RemoveButtonOperator(bpy.types.Operator):
bl_idname = "scene.remove_button_operator"
bl_label = "Remove Button"
id : bpy.props.IntProperty()
def execute(self, context):
bpy.context.scene.newbutton.remove(self.id)
print("Pressed button ", self.id)
return {'FINISHED'}
class FancyPanel(bpy.types.Panel):
bl_label = "Fancy Panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
def draw(self, context):
self.layout.operator("scene.add_button_operator")
for item in context.scene.newbutton:
row = self.layout.row(align=True)
row.prop(item, "value")
row.operator("scene.button_operator", text="Button #"+item.name).id = item.value
row.operator("scene.remove_button_operator", text="Delete #"+item.name).id = item.value
classes = (
SceneItems,
AddButtonOperator,
ButtonOperator,
FancyPanel,
RemoveButtonOperator,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.newbutton = bpy.props.CollectionProperty(type = SceneItems)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()
Cheers,
Juan