Updating modifiers using script - context issue

Hi,

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)

Thanks!

Hello, could you paste the script as formatted text in your post ? I’m a bit reluctant to download a blend file which I know will execute a (broken) custom python script. Cheers :slight_smile:

Hello,

import bpy


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 …).

Thank you

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?

Do names have to be different?
If you could give those layers unique names (like, say, “trans_color”), maybe you’d be able to transfer by ‘NAME’.

That temp_override turned out to be almost unusable.
For example while it sets the new context for bpy.ops.object.delete(), it doesn’t do anything with bpy.ops.object.convert().

You can try this:

with bpy.context.temp_override(object = obj, selected_objects = [obj]):
    bpy.ops.object.convert(target='MESH')

It doesn’t change the context for convert() at all.
But if you change the operator to delete() it will correctly delete obj.

temp_override_limited.blend (787.5 KB)

1 Like

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.