Notifications
Clear all

[Closed] Going through an hierarchy

I have a master-node which has a master-controller asigned, and I need to go through it’s children to add slave-controllers and to be added to a nodeTab. I have hierarchized as Sphere01 -> Box01 -> Box02-> … -> BoxN ( -> symbol means has child. So, Box01 is child of Sphere01 and so on).

The questions is, How many children has Spehre01? 1 or N ? I need to be N, but if it returns that has 1 child, how I go through this hierarchy to get N children?

3 Replies

Recursively, of course
Assuming you are asking about MAXScript and not the SDK, an example would be

(
theHierarchy = #() –init. an array to collect children
fn getChildren theParent = ( –pass a parent as argument
for o in theParent.children do ( –for each child of the current parent
append theHierarchy o –append to the array
getChildren o –then call recursively to get its children
)
)
getChildren $ –select parent, call recursive function
print theHierarchy –print the collected children
theHierarchy.count –this is the total # of children including all sub-branches
)

Is a SDK plugin, not an script. Sorry!

There we go!.

int GetNumChildren(INode *node)
{
	int numChildren = 0;

	for (int i = 0; i < node->NumberOfChildren(); i++, numChildren++)
		numChildren += GetNumChildren(node->GetChildNode(i));

	return numChildren;
}

This function will return the number of children the given node has.