Last active
April 7, 2020 11:10
-
-
Save Dragorn421/85241b204d79bd61d5e4b0cdcd5ed994 to your computer and use it in GitHub Desktop.
Blender script for changing the internal order of objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# WARNING: this may make blender forget some stuff, | |
# it at least forgets the hide/shown status of every object | |
# this is useful when export order is an issue | |
# MODIFY THIS | |
# move element at index 3 by -2, so it ends up at index 1 | |
# move element at index 7 by 3, so it ends up at index 10 | |
moves = [[3,-2],[7,3]] | |
# OR order objects by name | |
# get base list with [o.name for o in bpy.context.scene.objects.values()] | |
# objects not in this list will be left unchanged, objects here will be put after | |
# uncomment to use instead of moves (remove # on next line) | |
#orderedNames = ['Cube', 'Icosphere'] | |
import bpy | |
# context | |
scene = bpy.context.scene | |
if orderedNames is None: | |
objects = [o for o in scene.objects] | |
# compute the moves | |
for move in moves: | |
index = move[0] | |
moveBy = move[1] | |
e = objects[index] | |
del objects[index] | |
objects.insert(index+moveBy, e) | |
else: | |
objects = [scene.objects[n] for n in orderedNames] | |
# for each name, put the corresponding object at the end of the object list | |
# could be greatly improved if blender had a simple way to get an object's parent collection? | |
for object in objects: | |
# find the object's parent collection (breadth-first search) | |
toVisit = [scene.collection] | |
objCollection = None | |
while len(toVisit) > 0: | |
c = toVisit[0] | |
del toVisit[0] | |
toVisit += c.children | |
if object in c.objects.values(): | |
objCollection = c | |
break | |
# move object to end of list | |
if objCollection is None: | |
print('/!\ Couldnt find parent collection of ' + object.name) # hopefully unreachable | |
else: | |
objCollection.objects.unlink(object) | |
objCollection.objects.link(object) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment