Cant change objects' property - Writing to ID classes in this context is not allowed. location: :-1 I can change object's property using operator or just pasting bpy.data.objects[&quot;BF_platform_twelve&quot;].hide_viewport = True at the beigining of file or in python console, but i need to change object's property by changing drop down value only.
This works fine, but i need to get rid of button.

When im just checking value everything works fine except editing objects property
layout.prop(mytool, &quot;my_enum&quot;)

if mytool.my_enum == 'OP0':
     bpy.data.objects[&quot;BF_platform_twelve&quot;].hide_viewport = True #Writing to ID classes in this context is not allowed. location: &lt;unknown location&gt;:-1
     layout.prop(camera, &quot;lens&quot;, text=&quot;Угол обзора&quot;) #This works fine

Error:
Python: Traceback (most recent call last): File &quot;D:\desktop\BF_Default.blend\blenderfight.py&quot;, line 51, in draw AttributeError: Writing to ID classes in this context is not allowed: BF_platform_twelve,  Object datablock, error setting Object.hide_viewport  location: &lt;unknown location&gt;:-1 
Full code:
import bpy

class MyProperties(bpy.types.PropertyGroup):
    my_enum : bpy.props.EnumProperty(
        name= &quot;&quot;,
        description= &quot;sample text&quot;,
        items= [('OP0', &quot;Ничего&quot;, &quot;&quot;),
                ('OP1', &quot;Квадрат&quot;, &quot;&quot;),
                ('OP2', &quot;Двенадцатигранник&quot;, &quot;&quot;)
        ]
    )


class Platform_Panel(bpy.types.Panel):
    bl_label = &quot;Платформа&quot;
    bl_idname = &quot;Platform_Panel111&quot;
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = &quot;BlenderFight&quot;
    
    def draw(self, context):
        layout = self.layout
        scene = context.scene
        mytool = scene.my_tool
        camera = bpy.data.cameras[bpy.context.scene.camera.name]
        render=bpy.data.scenes[0].render
        cycles=bpy.context.scene.cycles
        
        layout.prop(mytool, &quot;my_enum&quot;)

        if mytool.my_enum == 'OP0':
            bpy.data.objects[&quot;BF_platform_twelve&quot;].hide_viewport = True #Writing to ID classes in this context is not allowed. location: &lt;unknown location&gt;:-1
            layout.prop(camera, &quot;lens&quot;, text=&quot;Угол обзора&quot;) #This works fine
 
 
classes = [MyProperties, Platform_Panel]
 
 
 
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
        bpy.types.Scene.my_tool = bpy.props.PointerProperty(type= MyProperties)
 
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
        del bpy.types.Scene.my_tool
 
 
 
if __name__ == &quot;__main__&quot;:
    register()
Draw Def using only for UI. 
I had to add &quot;update&quot; in DD menu props that call a function in case value was changed
def common_update(self, context, origin):
    if origin == 'my_enum':
        print('button_one', getattr(self, origin))
        # bpy.data.objects[&quot;Macbook_Gold&quot;][&quot;00_iSight Camera Indicator&quot;] = getattr(self, origin)

class MyProperties(bpy.types.PropertyGroup):
my_enum : bpy.props.EnumProperty(
    update=lambda self, context: common_update(self, context, 'my_enum'), #&lt;------------
    name= &quot;&quot;,
    description= &quot;sample text&quot;,
    items= [('OP0', &quot;Ничего&quot;, &quot;&quot;),
            ('OP1', &quot;Квадрат&quot;, &quot;&quot;),
            ('OP2', &quot;Двенадцатигранник&quot;, &quot;&quot;)
    ]
)