How to define the order of Objects ?

Hi

Liero made that great “Export to SVG” Script.

My question is, how to get the correct order of Objects depending on how they lay on each other ?
At the moment the Script measures only the distance to the camera of each Object which results in a incorrect order.
See here.

Is there any other way to detect the correct order ?
Maybe a more “physical” way ?
I mean basically the Objects are already correct in the Viewport and Blender already should “know” how the Objects lay on and to each other. From this knowledge the Script could take the correct order for the SVG Export… maybe I’m wrong.

What do you mean ?

Kind regards
Alain

why don’t you ask Liero?

I think the order is the way they are created and added to the list of objects !

happy bl

Not sure what you mean by the order the objects lay on each other, one way to sort objects based on the order is to create a list of lists of the objects you want, with the axis in question as the first value (i.e. object.location[0]) of each sub list then to sort the main list with .sort or sorted()

@proxe
Thanks for the hint.
How can I sort “list.sort()” by the first item and then by the second item and then by the third item and so on … ?

Edit:
It seems that it does it automatically that way, right ?

Edit 2:
How can I force .sort to sort only one specific item ?

Something like this?


for i in range(3):
  list.sort(key=lambda x: x[i])

range could be fed with the list length with len(list)

If you want to use the second item for sorting; list.sort(key=lambda x: x[1])

Thanks, it works.

Other question:
How can I go through each vertex of a Meshobject and storing only the distance from the vertex to the Camera Locations which is the closest to the Camera ?

Liero made this nice Export SVG Script. And I’m trying to change this part to my needs.


            # remover objetos con coordenadas no validas
            sel_valid = [o for o in sel if str_xy(o.matrix_world.to_translation())[4]]

            # ordenar los objetos segun distancia al visor -usa origen de objetos-
            if wm.order_obj:
                distancia = [(round((cam_co-o.location).length_squared,5), round(o.location.z,5), o.name) for o in sel_valid]
                distancia.sort(key=lambda x: x[1])
                print (distancia)
                sel_valid = [sce.objects[d[2]] for d in distancia]

For selected mesh objects, nearest vertex per object to camera object.
nearest_first variable is the list of object names, starting the nearest object to the camera.


import bpy


def distance(loc0,loc1):
    return (loc0-loc1).length


cam = bpy.data.objects['Camera'].location
obj = {}
for o in bpy.context.selected_objects:


    mat = o.matrix_world
    min_dist = min([distance(mat*i.co,cam) for i in o.data.vertices])
    obj[o.name] = min_dist    


nearest_first = sorted(obj, key=obj.get)    
print (nearest_first)