I am using this code which works very well for one object. I select Object_A and it moves to the collection MyCollection. If I select two or more objects, how would I change the code?
bpy.data.objects['Object_A'].select_set(True)
obj = bpy.context.window.scene.objects['Protrusive']
bpy.context.view_layer.objects.active = obj
A = bpy.context.object
A_old_coll = A.users_collection
terrain_coll_name = "MyCollection"
if terrain_coll_name not in bpy.data.collections:
terrain_coll = bpy.data.collections.new(name=terrain_coll_name)
bpy.context.scene.collection.children.link(terrain_coll)
else:
terrain_coll = bpy.data.collections[terrain_coll_name]
terrain_coll.objects.link(A)
for ob in A_old_coll:
ob.objects.unlink(A)

MyCollectionas a sub-collection of the scene collection -> add aprint(coll_target)before the check @MichaelTeiniker Btw: Wouldn't write an asnwer without testing it. More importantly try to understand the principle. – brockmann Apr 20 '20 at 09:10coll_target = context.collection– batFINGER Apr 20 '20 at 15:21
– Andy Feb 21 '24 at 17:02coll_target = bpy.data.collections["CollectionXYZ"]