Get full hierarchy of objects path (from object to the top parent)

Hi,

Imagine that Cylinder’s parent is a Plane and Plane’s parent is a Cube (in the picture). So please, could you tell me if there is any command (in the script) that does the following:

(aka) bpy.data.objects[‘Cylinder’].getFullHierarchy() which would return something like:
Cube/Plane/Cylinder

If there is no such command, what would could do the same functionality?

Thanks!


Something like:
results = []
node = bpy.data.objects[‘Cube’]
while node:
results.append(node)
node = node.parent

results.reverse()
print( “/”.join([node.name for node in results]) )

untested as I am at work :slight_smile:

Thank you very much!

I tried this and I get an error at node.name: AttributeError: ‘NoneType’ object has no attribute ‘name’

I also tried just to output node and bpy.data.objects[‘Cube’] just as a print statement in a console and it shows this:
<bpy_struct, Object(“Cube”)>
<bpy_struct, Object(“Plane”)>

(so how it is supposed to be)

So this is very strange why the node.name doesn’t work

I also tried bpy.data.objects[‘Cube’].name and it outputs Cube (just fine)
On the other hand node.name gives me that error. (and they seem to be of the same type, so idk what is wrong)

Do you have any clue?

Just change it to
print( “/”.join([node.name for node in results if node]) )

or in the while check the node is not None before appending it to the list

More pythonic is results[::-1]

import bpy

ob = bpy.context.object

tree = [ob]
while ob.parent:
    ob = ob.parent
    tree.append(ob)
    
print("/".join(map(lambda x: x.name, tree[::-1])))

OMG it works! Thank you metalix!!! That’s awesome!
I am a beginner so sorry if it was a dump question lol