Store component selection in variable


#1

Hey guys,

I’m new here and new to programming in Maya.
I’m learning Python, and I was to be able to select a bunch of faces and have that selection stored in a variable.
Seems easy enough, but I can’t figure it out. I’ve had a scout around google and can’t seem to find any answers.
Would appreciate any help!
Cheers,

Tom


#2

Hello and welcome. I’d encourage you to post the code you are having trouble with. That way we can correct and guide you.

To get you started, I have written a small example. Try to run each line one at a time so you can see what the individual lines do.

import pymel.core as pm
import json

# get a flattened list of selected faces on pCube1
faces = pm.selected(fl=True)
print 'faces:', faces

idxList = [f.index() for f in faces]
print 'idxList:', idxList

# create locator with string attr
locShape = pm.createNode('locator', ss=True)
loc = locShape.getParent()
loc.addAttr('componentIndexList', dt='string')

# use json to dump the list into the string attr
loc.componentIndexList.set(json.dumps(idxList))


# get index list using json to load the data from the string attr
idxList2 = json.loads(loc.componentIndexList.get())
print 'idxList2:', idxList2

# reconstruct faces list
faces2 = ['pCube1.f[%i]'%idx for idx in idxList2]
print 'faces2:', faces2

# reselct faces
pm.select(faces2)

David