Last active
January 10, 2017 14:08
-
-
Save taikomatsu/bb0417980db51e50cef9 to your computer and use it in GitHub Desktop.
Stick points to closest point on a specified mesh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from maya import OpenMaya | |
import pymel.core as pm | |
def mdagpath_from_name(name, extend_to_shape=False): | |
if not pm.objExists(name): | |
raise MayaNodeError, name | |
slist = OpenMaya.MSelectionList() | |
slist.add(name) | |
dagpath = OpenMaya.MDagPath() | |
slist.getDagPath(0, dagpath) | |
if extend_to_shape: | |
dagpath.extendToShape() | |
return dagpath | |
def stick_points_to_closest_mesh(tgt_obj, dfm_obj): | |
# target object to stick | |
meshfn = OpenMaya.MFnMesh(mdagpath_from_name(tgt_obj, True)) | |
result = OpenMaya.MPoint() | |
space = OpenMaya.MSpace.kWorld | |
# source points | |
vtxs = pm.ls('%s.vtx[*]' % dfm_obj, fl=True) | |
npoints = len(vtxs) | |
for i, vtx in enumerate(vtxs): | |
if i % 10 == 0: | |
print('[PROGRESS] %04d / %04d' % (i+1, npoints)) | |
p = vtx.getPosition(space='world') | |
meshfn.getClosestPoint(OpenMaya.MPoint(p.x, p.y, p.z), result, space) | |
vtx.setPosition(pm.dt.Point(result.x, result.y, result.z), space='world') | |
print('# Done') | |
stick_points_to_closest_mesh('pCube1', 'pSphere1') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment