maximum recursion depth exceeded

I want to count all nodes of a model
Im using a simple DFS algorithm


def printnode(node):
	#global ab
	print node.getX(), node.getY(), node.getZ()

def nodecount(node):
	printnode(node)
	if node.getNumChildren() == 0:
		return 1
	else:
		count= 0
		for i in range(0, node.getNumChildren() ):
			count+= nodecount(node)
		return count

For testing I used different models:
*)box.egg worked
*)tiny from IP’s tutorial crashed
*)and many others

Traceback (most recent call last):
  File "nodetester.py", line 48, in ?
    nodecount( model )
  File "nodetester.py", line 34, in nodecount
    count+= nodecount(node)
  File "nodetester.py", line 34, in nodecount
    count+= nodecount(node)
  File "nodetester.py", line 34, in nodecount
    count+= nodecount(node)
  File "nodetester.py", line 34, in nodecount
    count+= nodecount(node)
  File "nodetester.py", line 34, in nodecount
    count+= nodecount(node)
  File "nodetester.py", line 34, in nodecount
    count+= nodecount(node)
  File "nodetester.py", line 34, in nodecount
    count+= nodecount(node)
....
    File "nodetester.py", line 34, in nodecount
    count+= nodecount(node)
  File "nodetester.py", line 28, in nodecount
    printnode(node)
RuntimeError: maximum recursion depth exceeded

Its there a way to avoid it or does this mean i have to use BFS?
Martin

I think there may be an error in the code that was posted. The lines


		for i in range(0, node.getNumChildren() ):
			count+= nodecount(node)
		return count

will recurse on the current node, instead of the child nodes of the current node. So the algorithm would only succeed if there was only one node; otherwise it would fail to terminate.

Try nodecount(node.getChild(i)) instead.

In the general case, both Depth First Search and Breadth First Search algorithms should usually work equally well on Panda3d node trees; success or failure is dependent on the maximum node depth in both algorithms, so if one chokes on exceeding its recursion limit, the other probably will too.

Best of luck!
-Mark

A thanks. Stupid me. :imp:
Thanks it works now
Martin