Maya API: Fastest way to iterate DAG nodes

Hey all,

Having troubles iterating over a maya scene quickly to get all lights.

So we have a maya 2011 scene with a largeish number of objects (5000+).
Say we have 3 or 4 point lights in that scene that we want to get in our API.


MItDag iter( MItDag::kBreadthFirst, MFn::kPointLight);

for ( ; !iter.isDone(); iter.next() )
{
	MDagPath lightPath;
	iter.getPath(lightPath);
	potentialLights.append(lightPath);
}

Now, the iteration loop above is fast, as it only iterates over 4 objects.

However, the creation of the MItDag object (the first line of code above) is very very slow, as (im guessing) it interally iterates over each DAG object and checks if its a point light, adding it to the list of objects to iterate over if it is.

If i change the search type to kDepthFirst, things are much faster, but still not as fast as I’d like (its still taking about 5-6 seconds to execute that first line of code).

Some questions:

  • Why is the creation of this iterator so slow in large scenes?
  • Why does changing kBreadthFirst to kDepthFirst speed it up so much? Aren’t they both (internally) checking every single DAG node vs kPointLight?
  • How come the mel command
maya.cmds.ls(type="light")

executes instantly, whereas these API calls are much slower?

Most important question :slight_smile:

  • What is the fastest way to get a list of lights from the API then?

A really easy way to do some tests is to compile the scanDagSyntax plugin from the maya devkit (for some reason, it only runs in Release mode though for me), then call it with the following:

maya.cmds.scanDagSyntax(depthFirst=True, lights=True);

Hmm, I did a quick test using Python API calls from script editor with about 33k dag objects in the scene and MItDag was pretty much instant in filtering for the lights.

Try MItDependencyNodes and see if it’s any faster.

Thanks ShadowM8.

Was that depth or breadthfirst search? How many groups/levels deep were your created objects?

I’ll give MItDependencyNodes a go, but since DAG nodes are a subset of DG nodes, i’d be surprised if it sped up :slight_smile:

Most were in branches 10 levels deep and I nested a few branches 100+ deep for testing and placed my light as the last child too of one of them.

MDepthFist is indeed a faster option. Hard to tell why without knowing how their recursion is working but I would guess that maybe BreadsFirst hits some objects multiple times.

Cool, thanks for your help M8