Parent recursion

Hi all

Long time without posting!

After getting back into Blender after a break, I was wondering about the following: in my game I have objects made of multiple parts parented to each other, and I quite often need to find the child of something. I also sometimes need to ‘go back up the chain’ and find the parent of an object- however now I need to find a parent of a parent, like childrenRecursive but in reverse.

Currently, I have just gone directly (just as it works, although painful to look at) e.g.


B= A.parent
C= B.parent
D= C.parent

It works OK, and I can’t think of the chain getting any longer (i.e. to find E, F etc) but is there a fast Pythonic way of getting to a parent of a parent? I’ve had a look in the forums but I can’t find much (unless my search terms are vague).

Finally, my game Python and logic need to be as fast as possible- would what I use now be faster than a function? In fact, is there a reason why Blenders API does not have this function to start with?

Thanks for your time!

Paul

If you need to reference grandparents or more generically ‘ancestors’ of a game object, you can just use:


obj.parent.parent

I doubt that you will get much faster than that unless there are special cases that can be optimized. As far as needing your code to be as fast as possible, you should worry about that once it becomes a problem. Usually when your ‘code is too slow’ it is because you are doing something very inefficient or using a slow algorithm. Writing ‘fast code’ only makes a difference once you are using the correct algorithms and have good memory management and access patterns.

are the items assembled in game?

if not why not?

during assembly store the “root” and the “hierarchy” in each piece,

upon dissection, update,

own[‘Root’]=Root
own[‘Children’]=Children.recursive

etc

this is what I did for wrectified to store a hierarchy before I break the hierarchy, to parent all pieces to a “fake root”
Assemble->get hierarchy->remove parents->get center of gravity->put “fake root” compound center of mass in place and set mass, parent all to fake root, each fake root also had to be a libNew item,

this was the only way I could compound physics to have a correct mass+center of mass


def parents(obj):
    if obj.parent:
        return [obj.parent] + parents(obj.parent)
    return []

Nice! Thanks for the replies!