Skip to content

Instantly share code, notes, and snippets.

@TSCG
Last active October 22, 2021 08:52
Show Gist options
  • Save TSCG/99b1175d64811010e78b to your computer and use it in GitHub Desktop.
Save TSCG/99b1175d64811010e78b to your computer and use it in GitHub Desktop.
#選択頂点のリラックス
import pymel.core as pm
import maya.cmds as cmds
def TsRelaxVtxs():
vtxs = cmds.ls(fl=True, sl=True)
if len(vtxs) == 0:
cmds.error("Please select some vertices.")
else:
if type( pm.selected(fl=1)[0] ) != pm.MeshVertex:
cmds.error("selection is not vertices.")
else:
#get Obj Name
obj = vtxs[0].split(".")[0]
#Create Tgt Obj
tgtObj = cmds.duplicate( obj, n=obj+"Tgt" )
cmds.makeIdentity( tgtObj, apply=True, t=1, r=1, s=1 )
#RelaxVtx cel
cmds.polyAverageVertex( vtxs, i=1, ch=False )
snapToClosestPointOnMesh( vtxs, tgtObj[0] )
#clean
cmds.delete( tgtObj )
def snapToClosestPointOnMesh( vtxList, tgtObj ):
tgtObjShape = pm.PyNode(tgtObj).getShapes()
tmpCpNode = cmds.createNode( 'closestPointOnMesh', ss=True )
cmds.connectAttr( tgtObjShape[0]+'.outMesh', tmpCpNode+'.inMesh' )
cmds.connectAttr( tgtObjShape[0]+'.worldMatrix[0]', tmpCpNode+'.inputMatrix' )
for tmp in vtxList:
tmpPos = cmds.pointPosition( tmp, w=True)
cmds.setAttr(tmpCpNode+".inPosition", tmpPos[0],tmpPos[1],tmpPos[2])
fixPos = cmds.getAttr(tmpCpNode+".result.position")
cmds.xform( tmp, worldSpace=True, translation=fixPos[0])
cmds.delete(tmpCpNode)
TsRelaxVtxs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment