2
$\begingroup$

I'm trying to create some tool for blender that requires to store and show some boolean value for each object.

enter image description here

I have create simple Panel in Object context with one bool property sadly it's not stored between objects but globally.

My code looks like this:

    import bpy

from bpy.props import (BoolProperty,
                       PointerProperty,
                       )

from bpy.types import (Panel,
                       Operator,
                       PropertyGroup,
                       )



class CustomProperties(PropertyGroup):

    custom_bool: BoolProperty(
        name="Custom Bool",
        description="Bool",
        default = False
        )

class OBJECT_PT_CustomPanel(Panel):
    bl_label = "Custom Panel"
    bl_idname = "OBJECT_PT_custom_panel"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"
    
    def draw(self, context):
        layout = self.layout
        scene = context.scene
        custom_tool = scene.custom_tool
        
        layout.prop(custom_tool, "custom_bool")

classes = (
    CustomProperties,
    OBJECT_PT_CustomPanel
)

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

    bpy.types.Scene.custom_tool = PointerProperty(type=CustomProperties)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
    del bpy.types.Scene.custom_tool


if __name__ == "__main__":
    register()

I have tried to add custom property for object and update operator accordingly to active object but it's gives my an error:

Writing to ID classes in this context is not allowed: Cube.001, Object datablock, error setting Object.custom_tool

Here is code of that test:

import bpy

from bpy.props import (BoolProperty,
                       PointerProperty,
                       )

from bpy.types import (Panel,
                       Operator,
                       PropertyGroup,
                       )



class CustomProperties(PropertyGroup):

    custom_bool: BoolProperty(
        name="Custom Bool",
        description="Bool",
        default = False
        )

class OBJECT_PT_CustomPanel(Panel):
    bl_label = "Custom Panel"
    bl_idname = "OBJECT_PT_custom_panel"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"
    
    def draw(self, context):
        layout = self.layout
        scene = context.scene
        active = context.active_object
        custom_tool = scene.custom_tool
        
        if "custom_tool" in active:
            custom_toolcustom_bool = active["custom_tool"]
        else:
            active["custom_tool"] = False
            
        layout.prop(custom_tool, "custom_bool")

classes = (
    CustomProperties,
    OBJECT_PT_CustomPanel
)

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

    bpy.types.Scene.custom_tool = PointerProperty(type=CustomProperties)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
    del bpy.types.Scene.custom_tool


if __name__ == "__main__":
    register()

How to store some value in the object and display it in object panel?

Thanks!

$\endgroup$

1 Answer 1

3
$\begingroup$

Change bpy.types.Scene to bpy.types.Object in register and unregister. where you use “scene.” Use “active_object.” instead. or whatever actual object you want.

Adding a property to a type means everything you make of that type has that property and you can use it like a built in property.

$\endgroup$
0

You must log in to answer this question.

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