storing vert selection and setting position API


#1

hello,

i have tried to get and set vert points from one mesh (‘neutral’) to another with mesh (‘target’) before using this code:

import maya.OpenMaya as om
   

def getPoints(geo):

    sel = om.MSelectionList()
    dag = om.MDagPath()
   
    sel.add(geo)
    sel.getDagPath(0,dag)
  
    mesh = om.MFnMesh(dag)
  
    vts=om.MFloatPointArray()
    mesh.getPoints(vts, om.MSpace.kObject)
   
    return mesh,vts

def setPoints(geo, finalPos):	
    geo.setPoints(finalPos, om.MSpace.kObject) 

#store the vert positions for each mesh
mFnMesh, mesh1Vts = getPoints("neutral")
mFnMeshTarget, mesh2Vts = getPoints("target")


#set the points from mesh 1 to mesh 2
setPoints(mFnMeshTarget,mesh1Vts)

But my problem is i need to adapt this so that it can work on a vert selection of mesh one, rather than all the verts. Is there a way of just storing the specific selected verts from mesh1 and copying they positions to mesh2. Each mesh has matching topology but their shapes are different

if anyone could help my adapt this, i would appreciate it

thanks,
Sam


#2

Have you thought about blendShape + deformer weights + collapse history?


#3

im not sure how that works, but i need this script to work very fast, as it is dealing with big meshes. So using api stuff i think will be ideal.

thanks,
Sam


#4

If you grab the full set of verts from both meshes you will have two lists ordered by vert id. Then you just need to get the id’s from your vert selection and use them to update the 2nd list with values from the first. Then dump the updated list back onto the mesh.

David


#5

thanks djx. I will try this tonight.


#6

ok i just realised i dont know how to do that in the api. Can i update the vert id’s with normal python cmds? or would that take longer.

I think i will need to study api longer to understand how to manipulate its data

Sam


#7

If you want to capture the selected components, you would need to reference the MSelectionList.getDagPath function (or getComponent in Python API 2.0). Here’s a short example:

# Note this is using Python API 2.0
import maya.api.OpenMaya as om

sel_list = om.MGlobal.getActiveSelectionList()
(path, comp) = sel_list.getComponent(0)

if comp.apiType() == om.MFn.kMeshVertComponent:
    comp_fn = om.MFnSingleIndexedComponent(comp)
    indices = comp_fn.getElements()
    print indices

# Result:  [290, 573, 749, 750, 751, 753, 756, 757, 768, 769, 770, 771, 773, 774, 964, 965, 1040, 1041, 1059, 1060, 1780]

From there I think you can then set the verts accordingly. I would caution you that it is a lot faster to use getPoints/setPoints than setting them one at a time. So I would retrieve all the points, set the vertices that were selected and then run setPoints back if you have any significant amount of selected verts.

Also I would recommend Python API 2.0 for tasks like this where you’re traversing meshes and the like as Python API 1.0 was notably slow in its bindings for repetitive tasks.


#8

thanks alot!.