api 2.0 copying vert points from one list to another

257 views
Skip to first unread message

s...@weacceptyou.com

unread,
Feb 9, 2017, 5:31:54 PM2/9/17
to Python Programming for Autodesk Maya
hello,

i've stored a selection of verts from one mesh. And i've stored all the verts from another mesh.

im trying to copy the selection of verts from the first mesh into the list of the second mesh. So all the corresponding vert points get implanted into the second mesh's vert list.

Then i just want to use setPoints to set all the vert points back onto the second mesh. Effectively copying the relative position of the selected verts onto the other mesh.

i think i'm very nearly there with this code so far:

########################################################
import maya.api.OpenMaya as om


def getPoints(geo):

sel = om.MSelectionList()
sel.add(geo)
selObj = sel.getDagPath(0)
mesh = om.MFnMesh(selObj)

return mesh, mesh.getPoints()


def implantPtPositions(targetMesh,target_mesh_verts,indices):

#cycle through selected verts and copy them into the second mesh verts list
# HERE HERE HERE

#then use this new list to set the points of the second mesh
targetMesh.setPoints(newVerts,om.MSpace.kObject)


#store the vert positions for the second mesh
(targetMesh, target_mesh_verts) = getPoints("target")

#store selected vert points
sel_list = om.MGlobal.getActiveSelectionList()
(mDagPath, component) = sel_list.getComponent(0)
comp_fn = om.MFnSingleIndexedComponent(component)
indices = comp_fn.getElements()

#set the mesh difference to the target mesh
implantPtPositions(targetMesh,target_mesh_verts,indices)

########################################################

I just need to know how to cycle through the selected verts and copy them over to the second mesh ("target") list. so i can then use that new verts list in the setPoints function.

can anyone help me out here,thanks

Sam

Michael Boon

unread,
Feb 16, 2017, 6:17:27 PM2/16/17
to Python Programming for Autodesk Maya, s...@weacceptyou.com
I'm not really understanding what you want to do but I'll take a stab at it.

You've got a list of vertex positions in your target mesh (target_mesh_verts)
You've got a list of component indices from your first mesh (indices)
I'm not clear on how you want to use them together.

You will probably want to do something like
for i in indices:
    target_mesh_verts
[i] = something
target_mesh
.setPoints(target_mesh_verts)

Note that can fail (IndexError) if there are more verts in your first mesh than there are in your target mesh.

s...@weacceptyou.com

unread,
Feb 17, 2017, 4:45:34 AM2/17/17
to Python Programming for Autodesk Maya, s...@weacceptyou.com
i just want to use getpoints to get the points from both meshes. Then from my selection of verts on one mesh, copy those points into the other points list of the other mesh.

Then when i set points i will effectively have set the points on the second mesh in the shape of the selected points from the first mesh

thanks,
Sam

kevco...@gmail.com

unread,
Feb 19, 2017, 9:15:03 AM2/19/17
to Python Programming for Autodesk Maya, s...@weacceptyou.com
there's really no need to get the points on both meshes, and depending on vertex counts, you may wanna avoid getting all the vertices opposed to just the ones you have selected.

It would be strange to loop over the full set of points on the target mesh, updating just the indices of the selected vertices on the src mesh, then setting ALL the points on the tgt. you can do it, but its strange imo.

But anyways, The code below doesn't store any vertices/MPoints. It simply loops over the indices of the vertices you have selected on the src mesh, and sets the point on the tgt mesh with the equivalent point on the src mesh in local space.

import maya.api.OpenMaya as om2

# get MSelectionList of actively selected objects in the viewport
sel = om2.MGlobal.getActiveSelectionList()

# the first item is the src, along with its selected vertices
src, components = sel.getComponent(0)

# the second selected item is the target
tgt = sel.getDagPath(1)

# create function sets
fnMeshSrc = om2.MFnMesh(src)
fnMeshTgt = om2.MFnMesh(tgt)
fnSingle = om2.MFnSingleIndexedComponent(components)

indices = fnSingle.getElements()
for index in indices:
fnMeshTgt.setPoint(index, fnMeshSrc.getPoint(index, om2.MSpace.kObject), om2.MSpace.kObject)


It may be better to get the entire point array of the src mesh, and index into that instead of calling 'getPoint()' inside the loop. It depends on object vertex counts, and how many vertices you have selected. you'll have to test it.

srcPnts = fnMeshSrc.getPoints(om2.MSpace.kObject)
indices = fnSingle.getElements()
for index in indices:
fnMeshTgt.setPoint(index, srcPnts[index], om2.MSpace.kObject)


goodluck!

s...@weacceptyou.com

unread,
Feb 20, 2017, 10:32:20 AM2/20/17
to Python Programming for Autodesk Maya, s...@weacceptyou.com, kevco...@gmail.com
thanks alot bruh! its taking a while to get this one figured out, but this is really handy

thanks,
Sam
Reply all
Reply to author
Forward
0 new messages