Advertisement
bradon

create joints v1

Feb 16th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.30 KB | None | 0 0
  1. import maya.cmds as mc
  2. import maya.OpenMaya as om
  3. import pymel.core as pm
  4. import math
  5.  
  6. def createHierarchy(nodeList=['object1,object2']):
  7.    
  8.     #reverse the list
  9.     nodeList.reverse()
  10.    
  11.     # get the length of the list
  12.     selectionLength = len(nodeList)
  13.    
  14.     # Loop through the selection list
  15.     for index, node in enumerate(nodeList):
  16.         # Check to make sure its not the last element in the list
  17.         if index < (selectionLength - 1):
  18.             # parent the node to the previously selected node
  19.             mc.parent(node, nodeList[index + 1])            
  20.             # Debuggin
  21.             #print "node", node
  22.             #print "child", nodeList[index + 1]
  23.             #print "index", index
  24.  
  25.  
  26. def calculateEdgeCenter(edge='polyObject.e[0]'):
  27.     '''
  28.    @summary:
  29.        gets the worldspace center given edge
  30.        libraries pymel, openMaya
  31.    @arguments:
  32.        edge = string of the edge name
  33.    @returns:
  34.        worldspace XYZ coordinates of the edge
  35.   '''
  36.     edge = pm.MeshEdge(edge)
  37.    
  38.     # get the centerpoint of the edge
  39.     edgept = edge.__apimfn__().center(om.MSpace.kWorld)
  40.     edgecenterPoint = pm.datatypes.Point(edgept)
  41.    
  42.     print 'EDGECENTER:', edgecenterPoint
  43.     return edgecenterPoint
  44.  
  45. def createLineVector(point1 = om.MVector(0,0,0), point2 = om.MVector(1,1,0), name='vector'):
  46.     '''
  47.    @summary:
  48.        given two point MVectors create a line - good for visualizing normals and angles
  49.    @arguments:
  50.        Point1 = MVector
  51.        point2 = MVector
  52.        name = string, names the created line
  53.    @returns:
  54.        curve name
  55.    '''
  56.  
  57.     newCurve = mc.curve(name=name, worldSpace=True, degree=1,
  58.                         point=[(point1.x, point1.y, point1.z),
  59.                                (point2.x, point2.y, point2.z)])
  60.     return newCurve
  61.  
  62. def calculateRotations(pointOrigin=[0,0,0], pointX=[1,0,0], pointUP=[0,1,0]):
  63.     '''
  64.    @summary:
  65.        calculates Euler rotation values 3 point locations (mVectors)
  66.    @arguments:
  67.        pointOrigin = position of origin point
  68.        pointX = position of x point
  69.        pointUP = position of up point
  70.  
  71.    @returns:
  72.        Euler rotation to rotate the object back
  73.        [x,y,z]
  74.    '''
  75.     #rotOrder = mc.getAttr('%s.rotateOrder'%node)
  76.     # hard code rotation order to xyz
  77.     rotOrder = 0
  78.    
  79.     # Get Point Positions
  80.     originPosition = pointOrigin
  81.     XDirection = pointX
  82.     UPDirection = pointUP
  83.     #worldOrigin = om.MVector(0,0,0)
  84.    
  85.     # Subtract Positions for Vector
  86.     UpVector = originPosition - UPDirection
  87.          
  88.     XAxis = XDirection - originPosition
  89.     ZAxis = UpVector^XAxis
  90.     YAxis = ZAxis^XAxis
  91.    
  92.     # Debugging
  93.     #print 'XAxis:', XAxis.x, XAxis.y, XAxis.z
  94.     #print 'UpVector:', UpVector.x, UpVector.y, UpVector.z
  95.     #print 'ZAxis:', ZAxis.x, ZAxis.y, ZAxis.z
  96.     #print 'YAxis:', YAxis.x, YAxis.y, YAxis.z
  97.        
  98.     XAxisNormalize = XAxis.normal()
  99.     YAxisNormalize = YAxis.normal()
  100.     ZAxisNormalize = ZAxis.normal()
  101.        
  102.     # Create lines in space for each axis: visual aid
  103.     #xNormalCurve = createLineVector(point1=worldOrigin, point2=XAxisNormalize,  name='XAxisNormalize')
  104.     #yNormalCurve = createLineVector(point1=worldOrigin, point2=YAxisNormalize,  name='YAxisNormalize')
  105.     #zNormalCurve = createLineVector(point1=worldOrigin, point2=ZAxisNormalize,  name='ZAxisNormalize')
  106.    
  107.     # Create axis group and move to origin point: visual aid
  108.     #waxisGroup = mc.group([xNormalCurve,yNormalCurve,zNormalCurve], name='{node}_axisGroup'.format(node=node))
  109.    
  110.     # Debugging            
  111.     #print 'XAxisNormalize:', XAxisNormalize.x, XAxisNormalize.y, XAxisNormalize.z
  112.     #print 'YAxisNormalize:', YAxisNormalize.x, YAxisNormalize.y, YAxisNormalize.z
  113.     #print 'ZAxisNormalize:', ZAxisNormalize.x, ZAxisNormalize.y, ZAxisNormalize.z
  114.        
  115.     # pack values into matrix      
  116.     matrix = om.MMatrix()
  117.        
  118.     om.MScriptUtil.setDoubleArray(matrix[0], 0, XAxisNormalize.x) # Sets the first row, first column
  119.     om.MScriptUtil.setDoubleArray(matrix[0], 1, XAxisNormalize.y) # Sets the first row, second column
  120.     om.MScriptUtil.setDoubleArray(matrix[0], 2, XAxisNormalize.z) # Sets the first row, third column
  121.        
  122.     om.MScriptUtil.setDoubleArray(matrix[1], 0, YAxisNormalize.x) # Sets the second row, first column
  123.     om.MScriptUtil.setDoubleArray(matrix[1], 1, YAxisNormalize.y) # Sets the second row, second column
  124.     om.MScriptUtil.setDoubleArray(matrix[1], 2, YAxisNormalize.z) # Sets the second row, third column
  125.        
  126.     om.MScriptUtil.setDoubleArray(matrix[2], 0, ZAxisNormalize.x) # Sets the third row, first column
  127.     om.MScriptUtil.setDoubleArray(matrix[2], 1, ZAxisNormalize.y) # Sets the third row, second column
  128.     om.MScriptUtil.setDoubleArray(matrix[2], 2, ZAxisNormalize.z) # Sets the third row, third column            
  129.        
  130.     # converts inverse matrix to center rotate points to 0
  131.     matrixInverse = matrix.inverse()
  132.        
  133.     # Convert to MTransformationMatrix to extract rotations
  134.     mTransformMtx = om.MTransformationMatrix(matrix)
  135.     mTransformMtxInverse = om.MTransformationMatrix(matrixInverse)
  136.                
  137.     # Get the euler rotations
  138.     eulerRot = mTransformMtx.eulerRotation()
  139.     eulerRotInverse = mTransformMtxInverse.eulerRotation()
  140.        
  141.     # sort to the proper rotate order
  142.     eulerRot.reorderIt(rotOrder)
  143.     eulerRotInverse.reorderIt(rotOrder)
  144.  
  145.     # convert radians to degrees
  146.     rotAngle = [math.degrees(angle) for angle in (eulerRot.x, eulerRot.y, eulerRot.z)]
  147.     rotAngleInverse = [math.degrees(angle) for angle in (eulerRotInverse.x, eulerRotInverse.y, eulerRotInverse.z)]
  148.        
  149.     print 'ANGLES: ', rotAngle
  150.     #print 'ANGLES INVERSE:', rotAngleInverse
  151.        
  152.     #return rotAngleInverse, rotAngle
  153.     return rotAngle
  154.  
  155.  
  156. def createJointControls():
  157.     '''
  158.    @summary:
  159.        creates a joint at the center of every selected edge
  160.    @arguments:
  161.        none but needs a selection of edges
  162.    @returns:
  163.        JointChain
  164.    '''
  165.  
  166.     # This code works to retain the selected orderd
  167.     selectedEdgeList = mc.ls(orderedSelection=True, long=True)
  168.     print 'EDGELIST:', selectedEdgeList
  169.    
  170.     mc.select(clear=True)
  171.     jointList = []
  172.    
  173.     # for each edge selected create a joint    
  174.     for selectedEdge in selectedEdgeList:
  175.         # get the centerPoint
  176.         # also need to get the orientation of the joint
  177.         edgeCenter = calculateEdgeCenter(selectedEdge)
  178.        
  179.         edgeEndPoints = mc.xform(selectedEdge, query=True, worldSpace=True, translation=True)
  180.         print "EDGEENDS", edgeEndPoints
  181.        
  182.         originPosition = om.MVector(edgeEndPoints[0],edgeEndPoints[1],edgeEndPoints[2])
  183.         XDirection = om.MVector(edgeEndPoints[3],edgeEndPoints[4],edgeEndPoints[5])
  184.         UPDirection = om.MVector(0,0,0)
  185.        
  186.         rotationAngle = calculateRotations(pointOrigin=originPosition, pointX=XDirection, pointUP=UPDirection)
  187.        
  188.        
  189.         joint = mc.joint(position=edgeCenter, orientation=rotationAngle)
  190.        
  191.         mc.select(clear=True)
  192.         #append joint name to list
  193.         jointList.append(joint)
  194.          
  195.     createHierarchy(nodeList=jointList)
  196.     #newBindSkin " -byClosestPoint -toSkeleton";
  197.        
  198.  
  199. createJointControls()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement