1

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

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51
georch
  • 121
  • 2

1 Answers1

1

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)

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51