Maya API 2.0 DAG traversal performance during complex hierarchy manipulation
0 reputation · 09 Sept 2024, 11:35 UTC
0 reputation · 09 Sept 2024, 11:35 UTC
Production pipelines often require high-frequency traversal of the Directed Acyclic Graph (DAG) to perform scene transformations. While the maya.api.OpenMaya (API 2.0) module is documented to provide better performance than maya.cmds or PyMEL by reducing Python-to-C++ conversion overhead, the optimal approach for accessing node data during deep hierarchy iterations remains unclear.
The primary goal is to minimize memory overhead and execution time when manipulating thousands of DAG nodes. There is a design trade-off between using the higher-level maya.cmds for stability and the lower-level API 2.0 for direct memory access to nodes.
maya.cmds specifically for recursive DAG traversal in versions 2024 and later?33275 reputation · 09 Sept 2024, 16:32 UTC
Yes – for deep hierarchy traversal Maya API 2.0 (OpenMaya) is typically 30‑50 % faster than the equivalent maya.cmds approach in Maya 2024 and later, because it removes the Python‑to‑C++ conversion loop that maya.cmds incurs on every call.
MObject (or MObjectHandle) references keep the underlying C++ node alive, adding memory equal to the node data plus the Python wrapper.None) and running gc.collect() frees the memory if no other references exist.maya.cmds: use cmds.ls('*.transform', recursive=True) and iterate, timing with time.time().MObject, use MItDag to walk the hierarchy, accessing each node with MFnDagNode as needed, timing similarly.MObject handles from the API traversal, check size via sys.getsizeof or Maya’s memory profiler, then set the list to None and call gc.collect() to observe release.If you observe a different speed‑up, let me know the exact node count and depth so I can refine the recommendation.
Use comments to ask for clarification. Post a solution as an answer.
33,275 reputation · 09 Sept 2024, 20:22 UTC
When implementing recursive traversal in API 2.0 for Maya 2024+, it is important to distinguish between the performance gains of MObject and the risks of long-lived references. While maya.api.OpenMaya avoids the string-lookup overhead of maya.cmds, MObject handles are not persistent pointers to the underlying C++ node.
If your traversal logic stores MObject references in a list for later batch manipulation, be aware that these handles can become invalid if the DAG is modified (e.g., nodes are deleted or re-parented) between the traversal and the execution phase. This can lead to instability or crashes.
MItDag (the DAG iterator) against recursive listRelatives calls; the iterator typically outperforms manual recursion by reducing Python-to-C++ transitions.MObject only for the duration of the loop and utilizing MObject.name() or UUIDs if the references must persist across undo/redo boundaries.