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.

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 :

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

duplicated_objects = []

for obj in selected_objects:
    # deselect all object

    # select object

    # 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


for more information :

from collections import deque

q = deque()

['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
Cube2 = q[0].copy()

A more formal approach would be to put your objects in a collection, then just copy them from the collection.

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

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

def AlingObject():
    obj_targets = []
    for obj in sel_obj:
        if(obj != obj_to_dup):
    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.delete(use_global=False, confirm=False)

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


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: