Complex UV Layout in Maya
Over the last couple of years UV layout in Maya has changed for the better. In this course we're going to be taking a look at some of those changes as we UV map an entire character
# 1 20-11-2015 , 05:00 PM
Registered User
Join Date: Oct 2015
Posts: 4

Python to MEL

Hi all

So still having trouble with the whole more from Python to MEL. All I want to do is convert this basic script into MEL but the different syntax is doing my head in!

I have a load of nodes in the scene called removeKids_xxxxx and all I want to do is find them and delete all the children. Simple I thought. Works fine in Python, just cant figure it out in MEL.

Thanks in advance user added image

import maya.cmds as cmds

cmds.select("removeKids_*")

meshes = cmds.ls(sl=True, typ="transform")

for x in range (len(meshes)):
children = cmds.listRelatives(meshes[x])
for i in range (len(children)-1):
cmds.delete(children[i+1])

# 2 21-11-2015 , 03:59 AM
NextDesign's Avatar
Technical Director
Join Date: Feb 2004
Posts: 2,988
Try:

Code:
select "removeKids_*";

string $transforms[] = `ls -sl -transforms`;

for ($x = 0; $x < size($transforms); $x++)
{
    string $children[] = `listRelatives $transforms[$x]`;
    
    for ($i = 0; $i < size($children) - 1; $i++)
    {
        delete $children[$i + 1];
    }
}
You can also simplify your python code to:
Code:
import maya.cmds as cmds

transforms = cmds.ls("removeKids_*", sl=True, type="transform")

for x in xrange(len(transforms)):
    children = cmds.listRelatives(transforms[x], type="transform")
    if children: cmds.delete(children)
Which removes the nested loop, as well as the extra selection call.

Notice that I changed the variable name from "meshes" to "transforms", as this is the type of object that you are storing. I also modified the call to listRelatives to only return transform nodes. This stops the program from deleting objects of any other type (such as the parent object's shape node). I also switched range to xrange, as it doesn't store all numbers from 0 to len(transforms) - 1 in memory at once. Instead it generates a single number per iteration, which reduces memory usage. I also added the if statement, even though it wouldn't fail without it, it would throw a warning stating that no object was to be deleted if there were no child transform nodes under the parent.


Imagination is more important than knowledge.

Last edited by NextDesign; 21-11-2015 at 04:22 AM.
# 3 23-11-2015 , 08:39 AM
Registered User
Join Date: Oct 2015
Posts: 4
Perfect, thanks so much for your help! Much appreciated.

I can go through the MEL now and work out where I was going wrong. (Thanks also for the tip about the Python).

Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads