1
$\begingroup$

blender-3.4.1

import bpy

objs = [obj.name for obj in bpy.data.objects if obj.visible_get()]

print(objs)

active_object = bpy.data.objects["Cube"]
selected_objects = [
    active_object, 
    bpy.data.objects["Cube.001"]
    ]  
with bpy.context.temp_override(active_object=active_object, selected_objects=selected_objects):
    bpy.ops.object.join()

output:

['Cube', 'Cube.001']
Warning: Active object is not a selected mesh

What did i do wrong?

$\endgroup$
1

1 Answer 1

0
$\begingroup$

Simply assigning the selected objects to variable selected_objects doesn't mean you can join them. They have to be actually selected for bpy.ops.object.join() to work. Here's the script that will work:

import bpy

objs = [obj.name for obj in bpy.data.objects if obj.visible_get()]

print(objs)

active_object = bpy.data.objects["Cube"]
selected_objects = [
    active_object, 
    bpy.data.objects["Cube.001"]
]

with bpy.context.temp_override(active_object=active_object, selected_objects=selected_objects):
    bpy.ops.object.select_all(action='DESELECT')
    for obj in selected_objects:
        obj.select_set(True)
    bpy.context.view_layer.objects.active = active_object
    bpy.ops.object.join()
$\endgroup$
3
  • $\begingroup$ But why is a temp_override function needed? It also works without : with bpy.context.temp_override(active_object=active_object, selected_objects=selected_objects): $\endgroup$
    – X Y
    Apr 15 at 16:10
  • $\begingroup$ ah forgot to test it without. well even better if it works without. please edit and remove hehe $\endgroup$ Apr 15 at 16:12
  • $\begingroup$ @XY found out from Gorgious its faster to use with but it should be selected_editable_objects blender.stackexchange.com/a/275070/142292 $\endgroup$ Apr 16 at 7:55

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