0
$\begingroup$

Alright so blender can output an objectlist with this:

`for obj in bpy.context.selected_objects:`
 blenderCipher.write('\n'+obj.name+'\n')

The issue is the order that blender sets this in. I want it to be based off of the number the object is named to. For example, Model_01, Model_02, Model_03, Model_04, ect.

Blender would instead order it at random like model_05, model_04, model_01, model_03 ect.

$\endgroup$

1 Answer 1

0
$\begingroup$

bpy.context.selected_objects is a list of objects, it is not guaranteed to be in any specific order, to get the list in order you need to sort it yourself.

Python has the builtin function sorted() to sort lists. As it can't sort objects directly you need to specify a key function that can provide a sortable key. Using the example from pythons Sorting How-To you can use attrgetter() to get the objects name attribute to use for sorting.

import bpy
from operator import attrgetter

for obj in sorted(bpy.context.selected_objects, key=attrgetter('name')):
    print(obj.name)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .