2

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?

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51
zeronofreya
  • 359
  • 1
  • 8
  • 2
    it should be selected_editable_objects, not selected_objects see https://blender.stackexchange.com/questions/248274/a-comprehensive-list-of-operator-overrides – Gorgious Apr 15 '23 at 17:12

1 Answers1

1

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

Harry McKenzie
  • 10,995
  • 8
  • 23
  • 51
  • 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): – X Y Apr 15 '23 at 16:10
  • ah forgot to test it without. well even better if it works without. please edit and remove hehe – Harry McKenzie Apr 15 '23 at 16:12
  • @XY found out from Gorgious its faster to use with but it should be selected_editable_objects https://blender.stackexchange.com/a/275070/142292 – Harry McKenzie Apr 16 '23 at 07:55