maya api storing selected verts in mFloatPointArray and setting points

178 views
Skip to first unread message

alessia...@gmail.com

unread,
Jan 28, 2017, 6:30:53 AM1/28/17
to Python Programming for Autodesk Maya
Hello,

i had a similar problem i posted before where i needed to get all the points of a particular mesh and 'set' the position for another mesh.

in that problem i used getPoints, like this:

\\\\\\\\\\\\\\\\\\\\\\\\\\\\
def getPoints(geo):

sel = om.MSelectionList()
dag = om.MDagPath()

sel.add(geo)
sel.getDagPath(0,dag)

mesh = om.MFnMesh(dag)

vts=om.MPointArray()
mesh.getPoints(vts, om.MSpace.kObject)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

In my current problem i have two identical spheres 'pSphere1' and 'pSphere2', the first sphere has been deformed all over. What i want to do is select a group of verts on that surface and copy them onto the second sphere, not all of them, just the selection.

Im assuming i first need to do something like below, which is not written correctly, and then use the vert locations somehow to set them onto the sphere 2:

\\\\

import maya.OpenMaya as om

vertList = om.MFloatPointArray()

for v in (cmds.ls(sl=True)):
p1 = om.MFloatPoint(v)
vertList.append(p1)

\set locations from vertList onto 'pSphere2'

\\\\\

is this is simple thing to setup, does anyone know?, using maya api 1 by the way

thanks,
Sam




kevco...@gmail.com

unread,
Feb 2, 2017, 7:44:08 PM2/2/17
to Python Programming for Autodesk Maya, alessia...@gmail.com
you're almost there.

This is just me thinking/coding out loud, untested, but maybe it helps slightly

import maya.OpenMaya as om


def getPointArray(dagNode, components):
dagNode.extendToShape()
fn = om.MFnMesh(dagNode)

pts = om.MPointArray()
fn.getPoints(pts, om.MSpace.kWorld)

return pts


def implantPtPosition(dagNode, components, pts):
dagNode.extendToShape()
fn = om.MFnMesh(dagNode)

indices = om.MIntArray()
fnSingle = om.MFnSingleIndexedComponent(components)
fnSingle.getElements(indices)

for i in xrange(indices.length()):
fnMesh.setPoint(indices[i], pts[i], om.MSpace.kWorld)


sel = om.MSelectionList()

srcDagNode = om.MDagPath()
tgtDagNode = om.MDagPath()
components = om.MObject()

om.MGlobal.getActiveSelectionList(sel)

sel.getDagPath(0, srcDagNode, components)
sel.getDagPath(1, tgtDagNode)


pts = getPointArray(srcDagNode, components)
implantPtPosition(tgtDagNode, components, pts)




s...@weacceptyou.com

unread,
Feb 3, 2017, 4:43:46 AM2/3/17
to Python Programming for Autodesk Maya, alessia...@gmail.com, kevco...@gmail.com
thanks alot Kev, its taking a long while to get help with this problem. I will check this out tonight,

thanks,
Sam

s...@weacceptyou.com

unread,
Feb 4, 2017, 12:36:13 PM2/4/17
to Python Programming for Autodesk Maya, alessia...@gmail.com, kevco...@gmail.com
the only thing i dont get with this is. How do i initially store a vert selection, rather than just the transform node itself. Do i need to get the dagPath of each piece of geo as well as provide a list off all the verts i want to copy across?

thanks,
Sam

33th...@gmail.com

unread,
Feb 4, 2017, 9:27:01 PM2/4/17
to Python Programming for Autodesk Maya, alessia...@gmail.com, kevco...@gmail.com, s...@weacceptyou.com
the vertex selection is being stored in the component MObject.

calling MSelectionList.getActiveSelectionList(MDagPath, MObject) fills the MObject with those components.

But you need to use a Fn set to get the Indices, in order to use them in the way you want to.

The transform is being returned in the MDagPath, however. calling extendToShape() on the dagPath, extends it to the shapeNode.

There's a mistake in my code above, that you would actually need to call MFnMesh(dagNode.node()) since the constructor takes an MObject and not a dagPath.

33th...@gmail.com

unread,
Feb 4, 2017, 9:35:22 PM2/4/17
to Python Programming for Autodesk Maya, alessia...@gmail.com, kevco...@gmail.com, s...@weacceptyou.com, 33th...@gmail.com
sorry, I meant MSelectionList.getDagPath(DagPath, MObject) ;) doh!


s...@weacceptyou.com

unread,
Feb 5, 2017, 5:01:05 PM2/5/17
to Python Programming for Autodesk Maya, kevco...@gmail.com, s...@weacceptyou.com, 33th...@gmail.com
ok i think im getting it. is that covered in this line:

sel.getDagPath(0, srcDagNode, components)

?

Because at the moment the error is 'Index not in valid range'

'Don't i need to provide the name of 'pSphere1' and 'pSphere2' to the srcDagNode and the tgtDagNode ?

sorry, im just trying to get my head around this. Im almost there

thanks,
Sam

kevco...@gmail.com

unread,
Feb 5, 2017, 5:27:27 PM2/5/17
to Python Programming for Autodesk Maya, kevco...@gmail.com, s...@weacceptyou.com, 33th...@gmail.com
You can add the objects to the MSelectionList by their name, sure. Or you can work with what you have selected in the viewport. I think you should read the docs.

but, MGlobal.getActiveSelectionList(MSelectionList) will fill that selection list with what you have selected in the viewport.


s...@weacceptyou.com

unread,
Feb 6, 2017, 12:56:20 PM2/6/17
to Python Programming for Autodesk Maya, kevco...@gmail.com, s...@weacceptyou.com, 33th...@gmail.com
ok thanks for the help

Sam
Reply all
Reply to author
Forward
0 new messages