0
$\begingroup$

I am working on an addon and the UI works fine except for when I delete an object in the collection the button goes gray and stops working. When I undo it goes back to normal, but when I press it I get this error Python: Traceback (most recent call last): Text", line 87, in execute Text", line 71, in main Text", line 15, in create_point_light1 ReferenceError: StructRNA of type Collection has been removed

I think it is a problem with the collections

code here:

import bpy

collection = bpy.context.collection

def create_point_light1(name):
    light_data = bpy.data.lights.new(name=name, type='POINT')
    light_data.color = (1,1,1)
    light_data.energy = 100
    light_data.shadow_soft_size = 1.75

    light_object = bpy.data.objects.new(name=name, object_data=light_data)
    light_object.location = (1.35, -2.5, 2.56)
    
    collection.objects.link(light_object)

def create_point_light2(name):
    
    light_data = bpy.data.lights.new(name=name, type='POINT')
    light_data.color = (1,1,1)
    light_data.energy = 250
    light_data.shadow_soft_size = 1.75

    light_object = bpy.data.objects.new(name=name, object_data=light_data)
    light_object.location = (-1.175, -5.5, 4.64)
    
    collection.objects.link(light_object)
    
def create_point_light3(name):
    
    light_data = bpy.data.lights.new(name=name, type='POINT')
    light_data.color = (1,1,1)
    light_data.energy = 500
    light_data.shadow_soft_size = 2.15

    light_object = bpy.data.objects.new(name=name, object_data=light_data)
    light_object.location = (-6.7, -0.1, 2.2)
    
    collection.objects.link(light_object)
    
def create_point_light4(name):
    
    light_data = bpy.data.lights.new(name=name, type='POINT')
    light_data.color = (1, 0.612277, 0.441099)
    light_data.energy = 1000
    light_data.shadow_soft_size = 3.17
    
    light_object = bpy.data.objects.new(name=name, object_data=light_data)
    light_object.location = (0, -0.6, 3.2)
    
    collection.objects.link(light_object)
    
def create_spot_light(name):
    
    light_data = bpy.data.lights.new(name=name, type='SPOT')
    light_data.color = (1, 0.868938, 0.686679)
    light_data.energy = 10000
    light_data.spot_size = 180


    light_object = bpy.data.objects.new(name=name, object_data=light_data)
    light_object.location = (0, 0, 14)
    
    collection.objects.link(light_object)





def main(context):
    create_point_light1("Point")
    create_point_light2("Point")
    create_point_light3("Point")
    create_point_light4("Point")
    create_spot_light("Spot")

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        main(context)
        return {'FINISHED'}


def menu_func(self, context):
    self.layout.operator(SimpleOperator.bl_idname, text=SimpleOperator.bl_label)








class LayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout

        scene = context.scene

       


        # Big render button
        layout.label(text="Big Button:")
        row = layout.row()
        row.scale_y = 3.0
        row.operator("object.simple_operator")



def register():
    bpy.utils.register_class(SimpleOperator)
    bpy.types.VIEW3D_MT_object.append(menu_func)
    bpy.utils.register_class(LayoutDemoPanel)


def unregister():
    bpy.utils.unregister_class(SimpleOperator)
    bpy.types.VIEW3D_MT_object.remove(menu_func)
    bpy.utils.unregister_class(LayoutDemoPanel)


if __name__ == "__main__":
    register()
$\endgroup$

1 Answer 1

0
$\begingroup$

when I delete an object in the collection the button goes gray and stops working

That's because after you delete the active object, poll becomes false

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

When I undo it goes back to normal, but when I press it I get this error

This is because it's still using the old data you stored in a global variable. You should avoid storing Blender data in globals like that. Instead, access the current collection every time you want to use it ie

    collection.objects.link(light_object)  # bad, uses stale collection
    bpy.context.collection.objects.link(light_object)  # good, up to date
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .