1
$\begingroup$

I am developing a small script that should store some object custom properties assigned through a panel. Then the script prints these properties when a button (operator) is pressed. Generally, everything works fine. I just have two small issues:

1)When I delete an object, I get an error in the console saying: AttributeError: 'NoneType' object has no attribute 'temp_prop'. It's not a big deal, as everything still works fine, I can correctly assign properties to objects and print them. However, it's just a bit annoying, and it would be great to know why this error pops up every time I delete an object.

2)The other thing is related to the line del bpy.types.Object.temp_prop (commented line at the end of the code). I have seen similar codes using this line, what does it do? My code works fine also without it.

import bpy
from bpy.props import FloatProperty

class MyPropertySettings(bpy.types.PropertyGroup):
    my_property1: bpy.props.FloatProperty(name="prop1")
    my_property2: bpy.props.FloatProperty(name="prop2")

class MyPanel(bpy.types.Panel):
    bl_label = "Details"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_context = "objectmode"

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

        layout.prop(context.object.temp_prop, "my_property1")
        row = layout.row()
        layout.prop(context.object.temp_prop, "my_property2")
        row = layout.row()
        layout.operator("addonname.myop_operator")


class MyOperator(bpy.types.Operator):
    bl_label = "operator print"
    bl_idname = "addonname.myop_operator"

    def execute(self, context):
        print(bpy.data.objects['Cube'].temp_prop.my_property1)
        return {'FINISHED'}
       
       
def register():
    bpy.utils.register_class(MyPropertySettings)
    bpy.utils.register_class(MyPanel)
    bpy.utils.register_class(MyOperator)
    bpy.types.Object.temp_prop = bpy.props.PointerProperty(type=MyPropertySettings)

def unregister():
    bpy.utils.unregister_class(MyPropertySettings)
    bpy.utils.unregister_class(MyPanel)
    bpy.utils.register_class(MyOperator)
    #del bpy.types.Object.temp_prop

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.