anyone can explain to me why the script can’t adjust the modifier, unless I make the object active?
It correctly gets the modifier, but it uses incorrect context (from the active object) when trying to change it … which is weird to me.
I tried to use bpy.context.temp_override(), but that doesn’t help either. bpy_modifiers_edit.blend (730.6 KB)
mod = bpy.data.objects["Cube"].modifiers["DataTransfer"]
mod.layers_vcol_loop_select_dst = "Color1"
… when different object than “Cube” is active, it throws error about non-existing color attribute (the currently active object doesn’t have “Color1” layer, but I am trying to change the modifier of Cube …).
I don’t believe this is a context override issue, else Blender would raise an error with the keyword poll in it. For some reason Blender is only detecting the color attributes (bpy.types.Object.data.color_attributes) of your other, actively selected object - and only then if the object’s color attribute is of type: Face Corner > Byte Color.
As seen here, the source for these specific API functions have been undergoing some changes, so it’s possible something got broken or is renamed to something else, here’s the official documentation if you want to take a look at it.
The only solutions I have to offer at this time is to:
Either manually select your desired object via API code.
Save and recreate your modifier from scratch using API code.
Hi, thank you for the answer, explaining why this is happening.
I have a script which copies & applies Data Transfer modifiers from active to (many) selected (using Link), and because the layer mapping isn’t reliable https://developer.blender.org/T100472, I have to fix it after modifiers are assigned.
Unfortunately because of this context issue I need to make each object active to be able to set the layers, which makes the script much slower when having many objects in .blend.
But I still don’t understand why adjusting the modifier inside with bpy.context.temp_override(object = cube) doesn’t help in this case?
Context override overrides layout context, Editor context to be exact. To my understanding, the Modifiers tab, which is in the Properties Editor, has the same context after switching objects though. It’s the same context in this scenario I believe, so context isn’t the issue. It’s object selection.