0
$\begingroup$

I can change object's property using operator or just pasting bpy.data.objects["BF_platform_twelve"].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.

enter image description here

When im just checking value everything works fine except editing objects property

layout.prop(mytool, "my_enum")

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

Error:

Python: Traceback (most recent call last): File "D:\desktop\BF_Default.blend\blenderfight.py", 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: <unknown location>:-1

Full code:

import bpy

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


class Platform_Panel(bpy.types.Panel):
    bl_label = "Платформа"
    bl_idname = "Platform_Panel111"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "BlenderFight"
    
    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, "my_enum")

        if mytool.my_enum == 'OP0':
            bpy.data.objects["BF_platform_twelve"].hide_viewport = True #Writing to ID classes in this context is not allowed. location: <unknown location>:-1
            layout.prop(camera, "lens", text="Угол обзора") #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__ == "__main__":
    register()
$\endgroup$
2
  • $\begingroup$ You cannot alter data in a draw method. $\endgroup$
    – batFINGER
    Jan 19 at 10:06
  • $\begingroup$ Also simply use camera = scene.camera there is no need for camera = bpy.data.objects[bpy.context.scene.camera.name] which will not work if there is no scene camera. $\endgroup$
    – batFINGER
    Jan 19 at 10:12
0
$\begingroup$

Draw Def using only for UI.
I had to add "update" 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["Macbook_Gold"]["00_iSight Camera Indicator"] = getattr(self, origin)

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.