0
$\begingroup$

I can't get bpy.ops.make_links_data to work with context overrides. When I select objects manually, it works fine, but the following doesn't:

bpy.ops.object.make_links_data({
    "active_object": source, 
    "object": source, 
    "selected_objects": [source, target]}, 
type='MODIFIERS')

I would expect the objects given "selected_objects" to have the same modifiers as the object in "active_object" or the one in "object". This does not seem to happen. (I can't see anything happening at all.)

When I manually link the modifiers (which works), the line

bpy.ops.object.make_links_data(type='MODIFIERS')

appears in the scripting panel.

(This is in Blender 2.8.)

$\endgroup$
3
  • $\begingroup$ Could you provide more details what you are trying to do and what result is expected? $\endgroup$ Commented May 27, 2019 at 12:13
  • $\begingroup$ I'm sorry. I've added some. $\endgroup$
    – georch
    Commented May 28, 2019 at 18:14
  • $\begingroup$ I would recommend to use this approach https://blender.stackexchange.com/a/4883/50172 instead of using of operator. Also I have tested the proposed method on my side and it works $\endgroup$ Commented May 30, 2019 at 12:20

1 Answer 1

0
$\begingroup$

You have to override the context like this:

import bpy

area_type = 'VIEW_3D'
areas  = [area for area in bpy.context.window.screen.areas if area.type == area_type]

override = {
    'window': bpy.context.window,
    'screen': bpy.context.window.screen,
    'area': areas[0],
    'region': [region for region in areas[0].regions if region.type == 'WINDOW'][0],
}

bpy.ops.object.make_links_data(override, type='MODIFIERS')

But it is best to avoid the bpy.ops which is dependent on context, and directly manipulate the data. You can easily create the same function which is independent of context like so:

import bpy

def link_modifiers(active_object, selected_objects, clear_existing_modifiers=True):
    if len(selected_objects) <= 1:
        return

    for o in bpy.context.selected_objects:
        if o is active_object:
            continue

        if clear_existing_modifiers:
            o.modifiers.clear()

        for m in active_object.modifiers:
            o.modifiers.new(m.name, m.type)

link_modifiers(bpy.context.view_layer.objects.active, bpy.context.selected_objects, True)
$\endgroup$

You must log in to answer this question.

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