what is failing? are you getting an error? what does it say? you’re more likely to get help here if you give us all of the information you have rather than dropping a script and have us guess what the problem might be. That said, here are some guesses:
you never call “register”, so nothing will ever be registered.
you’re not defining properties as attributes.
you are redefining python’s built inid() function, which shouldn’t break anything given the scope but it’s not a great idea either.
Awesome, thanks I now it is working partially, the first time I click on the button a new button is added but when I click again another error gets printed in the console:
Here is the updated 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 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
classes = (
SceneItems,
AddButtonOperator,
ButtonOperator,
FancyPanel,
)
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()