Delete unknown nodes in Python

Delete unknown nodes in Python

Anonymous
Not applicable
16,024 Views
3 Replies
Message 1 of 4

Delete unknown nodes in Python

Anonymous
Not applicable

Is there a way to delete unknown noeds from within Maya using Pyhton

 

#Something like...

for item in unknownNodes:
  if node.exists:
    delete node

print unknownNodes # []

0 Likes
Accepted solutions (1)
16,025 Views
3 Replies
Replies (3)
Message 2 of 4

haggi_master
Advocate
Advocate
import maya.cmds as cmds
cmds.delete(cmds.ls(type="unknown"))
Message 3 of 4

rajasekaransurjen
Collaborator
Collaborator
Accepted solution

import maya.cmds as cmds
unknownNodes=cmds.ls(type = "unknown")
for item in unknownNodes:
    if cmds.objExists(item):
        print item
        cmds.delete(item)

Message 4 of 4

ijdallas
Explorer
Explorer

Thanks, rajasekaransurjen, your code handles ALMOST all the unknown nodes.

 

It looks like there's another type, "unknownDag" that Maya also uses (eg for Mental Ray nodes if the plugin isn't loaded). I updated your code to include that type, and to unlock nodes before deleting them since Maya will refuse to delete any locked nodes:

 

import maya.cmds as cmds
unknownNodes=cmds.ls(type = "unknown")
unknownNodes+=cmds.ls(type = "unknownDag")
for item in unknownNodes:
    if cmds.objExists(item):
        print item
        cmds.lockNode(item, lock=False)         cmds.delete(item)

 

A recent Autodesk post also discusses a few more ways to remove unknown nodes, for example by using the Optimize Scene Size command: https://knowledge.autodesk.com/support/maya/troubleshooting/caas/sfdcarticles/sfdcarticles/Unable-to...