[PYTHON] Script to search all selected object hierarchies in Blender

The listRelatives -ad command in Maya In Maya, it can be done in one line, but in Blender, it seems that it can not be done easily (?), And it seems that you can not do it unless you create a recursive function yourself.

↓ If you execute with one object selected, all the object names in the lower hierarchy will be printed.

import bpy

def return_hierarchy(ob):
	hierarchyList = []
	def recurse(ob):
		hierarchyList.append(ob.name)
		if not len(ob.children) == 0:
			for child in ob.children:
				recurse(child)
		return
	recurse(ob)
	return hierarchyList

selectList = bpy.context.selected_objects
his = return_hierarchy(selectList[0])
for hi in his:
	print(hi)

hierarchyList.append (ob.name) If you change it to hierarchyList.append (ob), you can get the object itself instead of the name.

By the way, here's how to do something like listRelatives -c https://qiita.com/pekochun/items/48433271c9d7e20a0b7c

Personal notes I'm a little unsure, so please let me know if you make a mistake.

Recommended Posts

Script to search all selected object hierarchies in Blender
Just print the selected object in Blender
[Blender] Print all ShapeKey names attached to the object
Script to mysqldump to all MySQL DBs
[Blender] Script to check if the selected one is a mesh
Articles to read when Blender Python script code doesn't work in 2.80
Pin current directory to script directory in Python
Sample script to trap signals in Python
To reference environment variables in Python in Blender
How to move BufferImageStim object in PsychoPy
In Blender, a script that just joins all objects directly under a particular group
How to run some script regularly in Django
Program to search all permutations (for competitive programming)
Difference in object ID due to import in Python