Sort and duplicate objects in a list

Good evening, I hope everyone has a wonderful Christmas and a happy New Year.

I am new to blender, I come from Maya and I am trying to adapt to the blender API and I would like someone to help me

I have created a list with the selection of objects using: bpy.context.selected_objects, however when I print the list I am surprised that it is ordered according to the name and not according to the order in which the objects were selected.
The other query is how to duplicate an object from that list and store it in a variable.

I hope you can help me, I will thank you forever.
Regards.

bpy.context.selected_objects is kinda weird, but actually the order is by time object created so from old to new

how blender object work some thing like this
– Object [location, scale, rotation, ect…]
– – Data [mesh:(vertex, edge, face), armature:(bone), ect…]

we can share data to another object (Link), so when we edit the geometry on object A the object B is effected

for more information : https://docs.blender.org/api/current/bpy.types.Object.html

there’s many way to duplicate object, here some beginner friendly snippet code

import bpy

# get selected objects
selected_objects = bpy.context.selected_objects.copy()

# deselect all object
bpy.ops.object.select_all(action='DESELECT')

duplicated_objects = []

for obj in selected_objects:
    # deselect all object
    bpy.ops.object.select_all(action='DESELECT')

    # select object
    obj.select_set(True)

    # duplicate object
    bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False, "mode":'TRANSLATION'})

    # get and store duplicated object
    duplicated_object = bpy.context.selected_objects[0]

    # append to duplicated_objects
    duplicated_objects.append(duplicated_object)

print(duplicated_objects)

for more information : https://docs.blender.org/api/current/bpy.ops.html

from collections import deque

q = deque()

dir(q)
['append', 'appendleft', 'clear', 'copy', 'count', 'extend', 'extendleft', 'index', 'insert', 'maxlen', 'pop', 'popleft', 'remove', 'reverse', 'rotate']

A deque can do things a list cannot.

How to duplicate an object from that list and store it in a variable:

bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
Cube = bpy.context.active_object
q.append(Cube)
Cube2 = q[0].copy()

A more formal approach would be to put your objects in a collection, then just copy them from the collection. https://www.youtube.com/watch?v=opZy2OJp8co

Thank you very much for your help, now I understand how the selection list works in blender.
Here I share my script, what it does is replace the selected objects with the last selected object, copying its transformations.
Thanks for your help.

import bpy
# from mathutils import Vector

#Creamos la variable vacía y Seleccinamos los objetos 
dup_objList = [] #lista vacía
sel_obj = bpy.context.selected_objects
obj_to_dup = bpy.context.object


def DuplicateObject():
    
    #Primero deseleccionamos todos los objetos seleccionados
    for i in range(len(sel_obj)-1):
        bpy.ops.object.select_all(action="DESELECT")
        obj_to_dup.select_set(True)

        #duplicamos el objetos seleccionado
        bpy.ops.object.duplicate(linked=False, mode='TRANSLATION')
        obj_dup = bpy.context.selected_objects[0]
        dup_objList.append(obj_dup)
    
    # print(dup_objList)

def AlingObject():
    
    obj_targets = []
    for obj in sel_obj:
        if(obj != obj_to_dup):
            obj_targets.append(obj)
            print(obj.name)
    for i, item in enumerate (obj_targets):
        dup_objList[i].location = item.matrix_world.to_translation()
        dup_objList[i].rotation_euler = item.matrix_world.to_euler('XYZ')
        dup_objList[i].scale = item.matrix_world.to_scale()
        
        #Eliminamos los objetos targets
        bpy.ops.object.select_all(action="DESELECT")
        item.select_set(True)
        bpy.ops.object.delete(use_global=False, confirm=False)

    # print ("Active Obj: ",bpy.context.active_object)

DuplicateObject()
AlingObject()

1 Like

I would recommend to straight using matrix world.

dup_objList[i].matrix_world = item.matrix_world.copy()

and parenting in blender also kinda weird, better using matrix world if you had more better understand with matrix

1 Like

You are very right, I can do it directly. Thanks a lot :slight_smile: :wink: