MEL - Select Geometry ONLY within a selection

MEL - Select Geometry ONLY within a selection

Anonymous
Not applicable
7,520 Views
6 Replies
Message 1 of 7

MEL - Select Geometry ONLY within a selection

Anonymous
Not applicable

Hi Everyone,

 

I'm having trouble writing a MEL script that only selects geometry within a selection. For example: I have a hierarchy with groups, geometry, locators, 3d nodes, distanceDimensions, etc. I want it to select only the geometry within that selected hierarchy and nothing else, otherwise the script will error and won't continue on to the next portion.  

 

Here's what I have so far:

 

// primary visibility TEST
string $selection[] = `ls -sl -s -dag`;
for ($node in $selection)
{
editRenderLayerGlobals -currentRenderLayer TEST;
setAttr ( $node + ".primaryVisibility") 0;
}

 

If I use 'ls -geometry'; it selects all the geometry in the scene, but I only want it to affect what is inside my selected hierarchy. 

 

Thanks in advance,

 

 

0 Likes
7,521 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

The command you want is the listRelatives command.

0 Likes
Message 3 of 7

Anonymous
Not applicable

Rallion, thanks for responding. I tried listRelatives briefly but it wasn't working. Once I have more free time I'll try it again. 

0 Likes
Message 4 of 7

Anonymous
Not applicable

Not sure with mel, but you could use list comprehension in Python.

 

Create 2 lists:

 

1)Selected Objects.

2)Geometry in the scene.

3)Combine the two if the match. 

 

 

import pymel.core as pm

sel = pm.ls(sl=True)
meshes = pm.listTransforms(type='mesh')
selectedMeshes = [x for x in sel if x in meshes]

0 Likes
Message 5 of 7

Anonymous
Not applicable

Hey there,

 

did you ever manage to create something that worked?  This is such a simple concept, I'm surprised I couldn't find this anywhere else on Google, for all specified type objects within hierarchies.

0 Likes
Message 6 of 7

Anonymous
Not applicable

Yes I resolved it with the filterExpand command: https://help.autodesk.com/cloudhelp/2017/CHS/Maya-Tech-Docs/Commands/filterExpand.html

MEL Example:

 

string $selection[] = `ls -sl`;
string $polyGons[] = `filterExpand -sm 12 $selection`;
select -r $polyGons;

 

You can also do the same thing via Python:

 

selection = cmds.ls(sl = True)
polyGons = cmds.filterExpand(selection, sm = 12)
cmds.select(polyGons, r = True)

 

Hope it helps.

Message 7 of 7

Anonymous
Not applicable

Awesome I will try them out.

 

A coworker also quickly cooked up this python script and it seemed to work fine:

 

import maya.cmds as mc
mc.select(hi=True, r=True)
hierarchy = mc.ls(sl=True, long=True)
shapes =  filter(lambda x: mc.nodeType(x) == 'mesh', hierarchy)
transforms = [mc.listRelatives(shape, p=True, fullPath=True)[0] for shape in shapes]
mc.select(transforms, r=True) 

 

I believe the script pasted correctly.  Ot gave me an error when I tried to post about invalid html.

0 Likes