Finding children of object

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
draco
Posts: 0
Joined: Mon Apr 24, 2006 9:16 pm

Finding children of object

Post by draco »

Hello
Is there a way to obtain object children in python script ?
I'm searching for some functionality similar to blender select->grouped->children command. It shouldn't select objects but just return them of course :)

Ben
Posts: 9
Joined: Wed Oct 23, 2002 8:32 pm

Post by Ben »

Code: Select all

def getChildren(Myobj):
     for obj in Blender.Objects.Get():
          pare = obj.getParent()
          if pare.name == My_obj.name:
                return obj    
obj is the children of Myobj

draco
Posts: 0
Joined: Mon Apr 24, 2006 9:16 pm

Post by draco »

I thought that there should be some direct way to obtain object children, but you're right, it will do what i want.
Thanks :)

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv »

A blender Object does not keep a list of its children. (what a harsh world when parents do not know their children!) But luckily, children do know their parents.

Unless I am even more blind than usual, Ben's code snippet only returns the first child object. You could replace the body of his func with something like this to get a more complete list:

return [ob for ob in Object.Get() if ob.parent == myObj ]

an-toni
Posts: 0
Joined: Wed Mar 17, 2004 5:20 pm

Post by an-toni »

the version i did at orange seems to be: (from http://www.blender.org/~antont/obscene.py )

Code: Select all

def getChildren(ob, scene): #does not scale. fix api.
	def isParent(ob, c):
		p = c.getParent()
		if p is not None: 
			if p.name == ob.name:
				return True
		else:
			False
	return [c for c in scene.getChildren()
			if isParent(ob, c)]
works, dunno if is optimal. and dont know if the comment (fix api) makes sense, as like Stivs said the info is not there.. (and list comprehensions are looped in C so i dont know if a c-written version would be sig. faster. i did not have probs with the speed of this)

~Toni

Post Reply