[maya][python] delete unused nodes?

What’s the python equivalent of hypersahde edit>delete unused nodes?

Hi.

This is a UI Command. You can have a look at “hyperShadePanel.mel” to see what going on exactly.
Otherwise you can just wrap the MEL command into Python

mel.eval('hyperShadePanelMenuCommand("hyperShadePanel1", "deleteUnusedNodes");')
1 Like

thanks!

// mel
MLdeleteUnused;

python

import maya.mel as mel
mel.eval(‘MLdeleteUnused;’)

3 Likes

i’ve had to write to my own Delete Unused Nodes because there are some kinds of nodes that can slip through the cracks.

For example, if you end up with a construction history chain that does not feed into a shape node, e.g. polyCube > bevel > polyProjectUV, those “orphaned” construction history nodes will not be removed by Clean Scene, Delete Un-used Nodes, or any of Maya’s other various tools for keeping your files clean.

1 Like

This is from years ago, but might you have that Delete Unused Nodes tool you made yourself? Interested ins how it works to know what to delete.

I ended up with a pymel command

import pyeml.core as pm
pm.mel.MLdeleteUnused()

If you want to truly remove all unused nodes and do it quickly, one of the fastest ways to do this is to select all top level transforms and “export selected” them into a new scene. You will want to make sure that the export options are setup correctly.