Deleting unused shape nodes from object

Hi,

I’m looking for a script to delete unused shape nodes from my objects in maya. I found scripts of people finding and deleting unused transform nodes, and the function in hypershade for deleting unused material nodes but not for shape nodes.

If anyone has a script or knows how to tackle this one it would be appreciated.

Cheers

This is what I came up with:
#delete unconected shape nodes
transforms = cmds.ls(type=‘mesh’);
deleteList = [];
for shape in transforms:
if cmds.nodeType(shape) == ‘mesh’:
connection = cmds.listConnections(shape, c=True, shapes=True)
if connection == None:
print ‘%s, has no Connections’ %(shape)
deleteList.append(shape)
if len(deleteList) > 0:
cmds.delete(deleteList)

I don’t think it is clear here what “unused” means. Are you searching for meshes which are not deformed? This script would even find things which are connected to the initialShadingGroup. For example, if I make a polySphere and it has no history, no deformations or anything, it will not be deleted by your script, as it is.

In this script, you are iterating over meshes, but if you were to run this script over nurbsCurves, it would delete control icons from a rig. Are those nurbsCurves “unused” because they don’t have incoming connections? I would say no.

Also, a mesh is not a transform. The “transforms” variable name is a bit misleading.

Anyway. I think you’d better carefully define what you mean “unused” to mean, and do some more tests before you consider this the final solution. If it works in this particular instance for you, cool.

By the way, you can format code by enclosing it in triple backticks or a code block.

```
Code here
```
1 Like

Unused shape node to me means a node which is not contributing any geometric data to the object.

I altered someone else’s script which deleted empty transform objects. That’s why it says transforms there, I should have removed that. It does seem to work for my particular problem but I haven’t tested it thoroughly you’re right.

Testing out code formatting

#delete unconnected shape nodes
shapeNodes = cmds.ls(type=‘mesh’);
deleteList = [];
for shape in shapeNodes:
if cmds.nodeType(shape) == ‘mesh’:
connection = cmds.listConnections(shape, c=True, shapes=True)
if connection == None:
print ‘%s, has no Connections’ %(shape)
deleteList.append(shape)
if len(deleteList) > 0:
cmds.delete(deleteList)