GameObject functions getChildren() and getChildrenRecursive()

This commit is contained in:
Campbell Barton 2008-07-20 17:18:46 +00:00
parent 433c43932b
commit e95e2fb43e
3 changed files with 53 additions and 0 deletions

@ -886,6 +886,8 @@ PyMethodDef KX_GameObject::Methods[] = {
{"getParent", (PyCFunction)KX_GameObject::sPyGetParent,METH_NOARGS},
{"setParent", (PyCFunction)KX_GameObject::sPySetParent,METH_O},
{"removeParent", (PyCFunction)KX_GameObject::sPyRemoveParent,METH_NOARGS},
{"getChildren", (PyCFunction)KX_GameObject::sPyGetChildren,METH_NOARGS},
{"getChildrenRecursive", (PyCFunction)KX_GameObject::sPyGetChildrenRecursive,METH_NOARGS},
{"getMesh", (PyCFunction)KX_GameObject::sPyGetMesh,METH_VARARGS},
{"getPhysicsId", (PyCFunction)KX_GameObject::sPyGetPhysicsId,METH_NOARGS},
{"getPropertyNames", (PyCFunction)KX_GameObject::sPyGetPropertyNames,METH_NOARGS},
@ -1303,6 +1305,43 @@ PyObject* KX_GameObject::PyRemoveParent(PyObject* self)
Py_RETURN_NONE;
}
static void walk_children(SG_Node* node, PyObject *list, bool recursive)
{
NodeList& children = node->GetSGChildren();
for (NodeList::iterator childit = children.begin();!(childit==children.end());++childit)
{
SG_Node* childnode = (*childit);
KX_GameObject* childobj = (KX_GameObject*)childnode->GetSGClientObject();
if (childobj != NULL) // This is a GameObject
{
// add to the list
PyList_Append(list, (PyObject *)childobj);
}
// if the childobj is NULL then this may be an inverse parent link
// so a non recursive search should still look down this node.
if (recursive || childobj==NULL) {
walk_children(childnode, list, recursive);
}
}
}
PyObject* KX_GameObject::PyGetChildren(PyObject* self)
{
PyObject * list = PyList_New(0);
walk_children(m_pSGNode, list, 0);
return list;
}
PyObject* KX_GameObject::PyGetChildrenRecursive(PyObject* self)
{
PyObject * list = PyList_New(0);
walk_children(m_pSGNode, list, 1);
return list;
}
PyObject* KX_GameObject::PyGetMesh(PyObject* self,
PyObject* args,
PyObject* kwds)

@ -746,6 +746,8 @@ public:
KX_PYMETHOD_NOARGS(KX_GameObject,GetParent);
KX_PYMETHOD_O(KX_GameObject,SetParent);
KX_PYMETHOD_NOARGS(KX_GameObject,RemoveParent);
KX_PYMETHOD_NOARGS(KX_GameObject,GetChildren);
KX_PYMETHOD_NOARGS(KX_GameObject,GetChildrenRecursive);
KX_PYMETHOD(KX_GameObject,GetMesh);
KX_PYMETHOD_NOARGS(KX_GameObject,GetPhysicsId);
KX_PYMETHOD_NOARGS(KX_GameObject,GetPropertyNames);

@ -214,6 +214,18 @@ class KX_GameObject:
"""
Removes this objects parent.
"""
def getChildren():
"""
Return a list of immediate children of this object.
@rtype: list
@return: a list of all this objects children.
"""
def getChildrenRecursive():
"""
Return a list of children of this object, including all their childrens children.
@rtype: list
@return: a list of all this objects children recursivly.
"""
def getMesh(mesh):
"""
Gets the mesh object for this object.