SHARE
TWEET

Untitled

a guest May 24th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from maya import cmds
  2. import maya.api.OpenMaya as om
  3.  
  4. def rad_to_deg(rad = [0,0,0]):
  5.      deg = om.MAngle.kDegrees
  6.      ang = om.MAngle
  7.      angles = [angle.asRadians() for angle in (ang(rad[0],deg), ang(rad[1],deg), ang(rad[2],deg))]
  8.      return om.MVector(angles)
  9.  
  10. def deg_to_rad(deg = [0,0,0]):
  11.      rad = om.MAngle.kRadians
  12.      ang = om.MAngle
  13.      rotate = [angle.asDegrees() for angle in (ang(deg[0],rad), ang(deg[1],rad), ang(deg[2],rad))]
  14.      return om.MVector(rotate)
  15.  
  16.  
  17. def compNames(name, components, attr='vtx'):
  18.     results = []
  19.     for c in components:
  20.         results.append(name+'.%s[%s]'%(attr, str(c)))
  21.     return results
  22.  
  23. def getFaceMatrix(mesh, faceId, point):
  24.     meshFn = getMeshFn(mesh)
  25.     space = om.MSpace.kWorld
  26.  
  27.     itMeshPly = getMItMeshPolygon(mesh)
  28.     itMeshPly.setIndex(faceId)
  29.     vtxIds = itMeshPly.getVertices()
  30.  
  31.     normal = om.MVector()
  32.     tangent = om.MVector()
  33.     binormal = om.MVector()
  34.  
  35.     print(compNames(mesh, vtxIds, attr='vtx'))
  36.     print(compNames(mesh, [faceId], attr='f'))
  37.    
  38.     for vtx in vtxIds:
  39.         normal += meshFn.getVertexNormal(vtx, space)
  40.         tangent += meshFn.getFaceVertexTangent(faceId, vtx, space)
  41.         binormal += meshFn.getFaceVertexBinormal(faceId, vtx, space)
  42.  
  43.     normal.normalize()
  44.     tangent.normalize()
  45.     binormal.normalize()
  46.  
  47.  
  48.     pt = point
  49.     if not isinstance(point, om.MPoint):
  50.         pt = om.MPoint(*point)
  51.      
  52.     mm=[binormal.x, binormal.y, binormal.z, 0,
  53.         normal.x,   normal.y,   normal.z,   0,
  54.         tangent.x,  tangent.y,  tangent.z,  0,
  55.         pt.x,  pt.y,  pt.z,  1]
  56.          
  57.     return om.MMatrix(mm)
  58.  
  59. def getMItMeshPolygon(mesh):
  60.     selList = om.MSelectionList()
  61.     selList.add(mesh)
  62.     dagPath = selList.getDagPath(0)
  63.     dagPath.extendToShape()
  64.    
  65.     if not dagPath.hasFn( om.MFn.kMesh ):
  66.         return None
  67.  
  68.     itMeshPly  = om.MItMeshPolygon(dagPath)
  69.     return itMeshPly
  70.  
  71. def getMeshFn(mesh):
  72.     selList = om.MSelectionList()
  73.     selList.add(mesh)
  74.     dagPath = selList.getDagPath(0)
  75.     dagPath.extendToShape()
  76.    
  77.     if not dagPath.hasFn( om.MFn.kMesh ):
  78.         return None
  79.  
  80.     meshFn  = om.MFnMesh(dagPath)
  81.     return meshFn
  82.  
  83.  
  84. def getPointAtUV(mesh, u, v, uvset=None):
  85.     meshFn = getMeshFn(mesh)
  86.     space = om.MSpace.kWorld
  87.     if not uvset:
  88.         uvset = meshFn.getUVSetNames()
  89.         uvset = uvset[0]
  90.     for i in range(meshFn.numPolygons):
  91.         try:
  92.             point = meshFn.getPointAtUV(i,u,v, space=space, uvSet=uvset, tolerance=0.0)
  93.             return point, i
  94.         except:
  95.             pass
  96.     return None, None
  97.  
  98.  
  99. def getUVAtPoint(mesh, position):
  100.     meshFn = getMeshFn(mesh)
  101.     space = om.MSpace.kWorld
  102.     pos = om.MPoint(*position)
  103.     uvset = meshFn.getUVSetNames()
  104.     u,v,faceId = meshFn.getUVAtPoint(pos,space, uvSet=uvset[0])
  105.     return u,v,faceId, uvset[0]
  106.  
  107.  
  108.  
  109. comp = cmds.ls(sl=1, fl=1)
  110. mesh = comp[0].split('.')[0]
  111. bpos = cmds.xform(comp, q=1, ws=1, bb=1)
  112. pos = [(bpos[0]+bpos[3])/2, (bpos[1]+bpos[4])/2, (bpos[2]+bpos[5])/2]
  113.  
  114. node = cmds.createNode('transform', n= mesh+'_loc_#')
  115. cmds.createNode('locator', n=node+'Shape', p=node)
  116.  
  117. u,v,faceids,uvset = getUVAtPoint(mesh, pos)
  118.  
  119. # setPointAtUV transform
  120. point, faceId = getPointAtUV(mesh, u, v, uvset=uvset)
  121. facemx = getFaceMatrix(mesh, faceId, point)
  122. mt = om.MTransformationMatrix(facemx)
  123. mt.reorderRotation(om.MEulerRotation.kXYZ+1)
  124. space = om.MSpace.kWorld
  125. cmds.setAttr(node+'.t', *mt.translation(space))
  126. cmds.setAttr(node+'.r', *deg_to_rad(deg = mt.rotation()))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top