import maya.cmds as mc
import maya.OpenMaya as OpenMaya
mesh = "pSphereShape1"
source = "locatorShape1"
deformer = "deltaMush1"
# get object and attach it to the MFnDependencyNode
# we can access mesh data with Dependency node function set
selection = OpenMaya.MSelectionList()
selection.add(mesh)
selection.add(source)
selection.add(deformer)
meshDag = OpenMaya.MDagPath()
sourceDag = OpenMaya.MDagPath()
defObj = OpenMaya.MObject()
selection.getDagPath(0, meshDag)
selection.getDagPath(1, sourceDag)
selection.getDependNode(2, defObj)
defFn = OpenMaya.MFnDependencyNode(defObj)
# accessing weightList and query the values
weightsPlug = defFn.findPlug("weightList")
for i in range(weightsPlug.numElements()):
weightIdxPlug = weightsPlug.elementByPhysicalIndex(i)
for j in range(weightIdxPlug.numChildren()):
weights = weightIdxPlug.child(j) # "cluster1.weightList[0].weights"
weightList = {}
for i in range(weights.numElements()):
val = weights.elementByPhysicalIndex(i).asFloat()
weightList[i] = val
# get the source object position
mMatrix = sourceDag.exclusiveMatrix()
locMatrix = OpenMaya.MTransformationMatrix( mMatrix )
locPoint = OpenMaya.MPoint( locMatrix.getTranslation( OpenMaya.MSpace.kWorld ))
# find the closest point on mesh from our source
meshIntersector = OpenMaya.MMeshIntersector()
pointInfo = OpenMaya.MPointOnMesh()
meshIntersector.create(meshDag.node(), meshDag.exclusiveMatrix())
meshIntersector.getClosestPoint(locPoint, pointInfo)
# create the list pointer
uUtil = OpenMaya.MScriptUtil(0.0)
uPtr = uUtil.asFloatPtr()
vUtil = OpenMaya.MScriptUtil(0.0)
vPtr = vUtil.asFloatPtr()
pointerList = OpenMaya.MScriptUtil()
ListIntPtr = pointerList.asIntPtr()
# get BarycentricCoords weights
pointInfo.getBarycentricCoords(uPtr,vPtr)
u = uUtil.getFloat(uPtr)
v = vUtil.getFloat(vPtr)
w = 1-u-v
faceId = pointInfo.faceIndex()
triId = pointInfo.triangleIndex()
# find the current face from pointed vertices
currentFace = OpenMaya.MItMeshPolygon(meshDag)
pointArray = OpenMaya.MPointArray()
vertIdList = OpenMaya.MIntArray()
currentFace.setIndex(faceId, ListIntPtr)
currentFace.getTriangle(triId, pointArray, vertIdList, OpenMaya.MSpace.kWorld )
# get the weights & multipy them by barycentric weights
P0 = weightList[vertIdList[0]]
P1 = weightList[vertIdList[1]]
P2 = weightList[vertIdList[2]]
# final weight for the source
source_weight = u*P0 + v*P1 + w*P2
from maya import cmds as mc, OpenMaya
import math
# get joints positions
sel = mc.ls(sl = 1)
start = mc.xform(sel[0] ,q= 1 ,ws = 1,t =1 )
mid = mc.xform(sel[1] ,q= 1 ,ws = 1,t =1 )
end = mc.xform(sel[2] ,q= 1 ,ws = 1,t =1 )
startV = OpenMaya.MVector(start[0] ,start[1],start[2])
midV = OpenMaya.MVector(mid[0] ,mid[1],mid[2])
endV = OpenMaya.MVector(end[0] ,end[1],end[2])
# get the vector from start to mid and project it on start to end vector
startEnd = endV - startV
startMid = midV - startV
dotP = startMid * startEnd
proj = float(dotP) / float(startEnd.length())
startEndN = startEnd.normal()
projV = startEndN * proj
arrowV = startMid - projV
arrowV*= 0.5
finalV = arrowV + midV
# find the proper rotation with using cross product from
# start to end vector to the arrow product we just found
cross1 = startEnd ^ startMid
cross1.normalize()
cross2 = cross1 ^ arrowV
cross2.normalize()
arrowV.normalize()
matrixV = [arrowV.x , arrowV.y , arrowV.z , 0 ,
cross1.x ,cross1.y , cross1.z , 0 ,
cross2.x , cross2.y , cross2.z , 0,
0,0,0,1]
matrixM = OpenMaya.MMatrix()
OpenMaya.MScriptUtil.createMatrixFromList(matrixV , matrixM)
matrixFn = OpenMaya.MTransformationMatrix(matrixM)
rot = matrixFn.eulerRotation()
# create a locator and apply translation and rotation
loc = mc.spaceLocator()[0]
mc.xform(loc , ws =1 , t= (finalV.x , finalV.y ,finalV.z))
mc.xform ( loc , ws = 1 , rotation = ((rot.x/math.pi*180.0),
(rot.y/math.pi*180.0),
(rot.z/math.pi*180.0)))