Updating old sript to 2.93

Hi all, I have an old script but I’m failing on making it work in 2.93. Can anyone point me to the right direction please?

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.collection)
        new = context.scene.collection.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.collection:
           row = self.layout.row(align=True)
           row.prop(item, "value")
           row.operator("scene.button_operator", text="Button #"+item.name).id = item.value


def register():
    bpy.utils.register_class(SceneItems)
    bpy.utils.register_class(AddButtonOperator)
    bpy.utils.register_class(ButtonOperator)
    bpy.utils.register_class(FancyPanel)

    bpy.types.Scene.collection = bpy.props.CollectionProperty(type = SceneItems)

Cheers,
Juan

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:

  1. you never call “register”, so nothing will ever be registered.
  2. you’re not defining properties as attributes.
  3. you are redefining python’s built in id() function, which shouldn’t break anything given the scope but it’s not a great idea either.

You are rigth I apologise for that.

This is the error I get when clicking on the button
image

I tried the register code bellow instead, but I got a new error when running the script
image

classes = (
    SceneItems,
    AddButtonOperator,
    ButtonOperator,
    FancyPanel,
)

def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.Scene.collection = bpy.props.CollectionProperty(type = SceneItems)

def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
        
if __name__ == "__main__":
    register()

scene.collection already exists, choose a different name

1 Like

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:
image

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()

I got it working, I had to change the = for : in this places

value = bpy.props.IntProperty()

for

value : bpy.props.IntProperty()

and then

id = bpy.props.IntProperty()

for

id : bpy.props.IntProperty()

Right, that’s what I was talking about here:

Ah ok sorry, just I’m getting use to the python therminology still :slight_smile: