View Full Version : maya script delete empty groups?
malcolm
03-30-2010, 03:16 PM
Anyone got a script that deletes groups with nothing in them or groups that have groups in them.
Shawner
03-30-2010, 03:40 PM
Run this in the python tab in your script editor.
import maya.cmds as cmds
transforms = cmds.ls(type='transform')
deleteList = []
for tran in transforms:
if cmds.nodeType(tran) == 'transform':
children = cmds.listRelatives(tran, c=True)
if children == None:
print '%s, has no childred' %(tran)
deleteList.append(tran)
cmds.delete(deleteList)
THNKR
03-30-2010, 05:41 PM
or use File->Optimize Scene Size [options]
Remove empty: (check) Transforms.
malcolm
03-30-2010, 08:48 PM
THNKR, that rarely works. Not sure what maya considers empty transforms but it's pretty picky, I think even a group within a group doesn't qualify for delete.
sama.van
03-30-2010, 09:50 PM
THNKR, that rarely works. Not sure what maya considers empty transforms but it's pretty picky, I think even a group within a group doesn't qualify for delete.
Yeah but if your group is in another group and the last group in the hierarchy doesn't have any child, do you want to delete this group or to keep it?
Example :
-group1
--group2
---group3
Do you want to delete group3 or not?
or :
- cube01
--group1
---group2
Do you want to delete group2 or not?
I think Maya will clean one time.... after that "oh damn this grp is empty! ok.. then // I will delete it..."
claydough
03-31-2010, 12:34 AM
Anyone got a script that deletes groups with nothing in them or groups that have groups in them.
So any transform without any other node children ( shape node? etc ) other than transform nodes??
I usually found Optimise Scene Size works pretty well, provided you've deleted history.
If you don't delete history on all your objects, the transforms which appear "empty" in the outliner may still have connections to valid objects in your scene (eg. after a Combine operation, the previous transforms are now empty but still affect the position of the combined objects).
If you first Delete History on all objects (or Non-Deformer history to be safer), then do Optimise Scene Size, it should always get rid of all empty groups.
malcolm
03-31-2010, 11:00 PM
Did a delete all by type history before running delete unused transform did not fix the problem.
sama.van
04-01-2010, 12:00 AM
lol it starts to be confuse to me...
Do you have a scene example, or screen of your complete outliner ?
Then you could explain what are the node you would like to delete or not?
I also remember some bugs with the Optimize Scene Size in Maya but before the 2008 version.
What version are you using right now?
malcolm
04-21-2010, 02:57 PM
Shawner that script works great thanks a lot man.
Shawner
04-21-2010, 03:59 PM
Awsome good to here.
Toast
04-21-2010, 04:54 PM
In 2011, running that script I get this error
# Error: TypeError: file <maya console> line 12: Object [] is invalid #
Shawner
04-21-2010, 05:32 PM
try
import maya.cmds as cmds
transforms = cmds.ls(type='transform')
deleteList = []
for tran in transforms:
if cmds.nodeType(tran) == 'transform':
children = cmds.listRelatives(tran, c=True)
if children == None:
print '%s, has no childred' %(tran)
deleteList.append(tran)
if len(deleteList) > 0:
cmds.delete(deleteList)
Toast
04-21-2010, 06:56 PM
Perfect!
funshark
04-22-2010, 03:29 AM
Never had any issue with the Optimize Scene Size.
If the empty groups don't disappear, that means they're connected to something and I recommend you to double check if it's something important or not.
vBulletin® v3.8.4, Copyright ©2000-2013, Jelsoft Enterprises Ltd.