Search code examples
listnodesmaya

Maya: Get name of all nodes created by script


I was wondering if there’s a way to get the name of any new node every time a new node is created. I’d like to compile a list of all the nodes created by my script so they can be cleanly deleted all at once if needed.

*Edit - A better solution would be if I can get to the name of all the new nodes in the scene after the script is done executing. This way if my script renames a new node while executing, the correct name will be stored.


Solution

  • You can use a scriptJob and listen for the DagObjectCreated event: http://help.autodesk.com/cloudhelp/2018/ENU/Maya-Tech-Docs/CommandsPython/scriptJob.html

    However, unless your script is calling external dependencies that you have little/no control over the output from, I think you'd be much better off simply recording the actions your script is taking. And if necessary, persisting this data as JSON or similar to use between sessions or modules.

    Edit: As per comments, to detect created or modified non-dag objects it may be more useful to list scene contents with cmds.ls(): http://help.autodesk.com/cloudhelp/2018/ENU/Maya-Tech-Docs/CommandsPython/ls.html

    Eg:

    import maya.cmds as cmds
    
    before = cmds.ls()
    cmds.polySphere()
    cmds.polyCube()
    after = cmds.ls()
    
    diff = set(before).symmetric_difference(set(after))
    print(diff)