Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import maya.cmds as mc
- import maya.OpenMaya as om
- import pymel.core as pm
- import math
- def createHierarchy(nodeList=['object1,object2']):
- #reverse the list
- nodeList.reverse()
- # get the length of the list
- selectionLength = len(nodeList)
- # Loop through the selection list
- for index, node in enumerate(nodeList):
- # Check to make sure its not the last element in the list
- if index < (selectionLength - 1):
- # parent the node to the previously selected node
- mc.parent(node, nodeList[index + 1])
- # Debuggin
- #print "node", node
- #print "child", nodeList[index + 1]
- #print "index", index
- def calculateEdgeCenter(edge='polyObject.e[0]'):
- '''
- @summary:
- gets the worldspace center given edge
- libraries pymel, openMaya
- @arguments:
- edge = string of the edge name
- @returns:
- worldspace XYZ coordinates of the edge
- '''
- edge = pm.MeshEdge(edge)
- # get the centerpoint of the edge
- edgept = edge.__apimfn__().center(om.MSpace.kWorld)
- edgecenterPoint = pm.datatypes.Point(edgept)
- print 'EDGECENTER:', edgecenterPoint
- return edgecenterPoint
- def createLineVector(point1 = om.MVector(0,0,0), point2 = om.MVector(1,1,0), name='vector'):
- '''
- @summary:
- given two point MVectors create a line - good for visualizing normals and angles
- @arguments:
- Point1 = MVector
- point2 = MVector
- name = string, names the created line
- @returns:
- curve name
- '''
- newCurve = mc.curve(name=name, worldSpace=True, degree=1,
- point=[(point1.x, point1.y, point1.z),
- (point2.x, point2.y, point2.z)])
- return newCurve
- def calculateRotations(pointOrigin=[0,0,0], pointX=[1,0,0], pointUP=[0,1,0]):
- '''
- @summary:
- calculates Euler rotation values 3 point locations (mVectors)
- @arguments:
- pointOrigin = position of origin point
- pointX = position of x point
- pointUP = position of up point
- @returns:
- Euler rotation to rotate the object back
- [x,y,z]
- '''
- #rotOrder = mc.getAttr('%s.rotateOrder'%node)
- # hard code rotation order to xyz
- rotOrder = 0
- # Get Point Positions
- originPosition = pointOrigin
- XDirection = pointX
- UPDirection = pointUP
- #worldOrigin = om.MVector(0,0,0)
- # Subtract Positions for Vector
- UpVector = originPosition - UPDirection
- XAxis = XDirection - originPosition
- ZAxis = UpVector^XAxis
- YAxis = ZAxis^XAxis
- # Debugging
- #print 'XAxis:', XAxis.x, XAxis.y, XAxis.z
- #print 'UpVector:', UpVector.x, UpVector.y, UpVector.z
- #print 'ZAxis:', ZAxis.x, ZAxis.y, ZAxis.z
- #print 'YAxis:', YAxis.x, YAxis.y, YAxis.z
- XAxisNormalize = XAxis.normal()
- YAxisNormalize = YAxis.normal()
- ZAxisNormalize = ZAxis.normal()
- # Create lines in space for each axis: visual aid
- #xNormalCurve = createLineVector(point1=worldOrigin, point2=XAxisNormalize, name='XAxisNormalize')
- #yNormalCurve = createLineVector(point1=worldOrigin, point2=YAxisNormalize, name='YAxisNormalize')
- #zNormalCurve = createLineVector(point1=worldOrigin, point2=ZAxisNormalize, name='ZAxisNormalize')
- # Create axis group and move to origin point: visual aid
- #waxisGroup = mc.group([xNormalCurve,yNormalCurve,zNormalCurve], name='{node}_axisGroup'.format(node=node))
- # Debugging
- #print 'XAxisNormalize:', XAxisNormalize.x, XAxisNormalize.y, XAxisNormalize.z
- #print 'YAxisNormalize:', YAxisNormalize.x, YAxisNormalize.y, YAxisNormalize.z
- #print 'ZAxisNormalize:', ZAxisNormalize.x, ZAxisNormalize.y, ZAxisNormalize.z
- # pack values into matrix
- matrix = om.MMatrix()
- om.MScriptUtil.setDoubleArray(matrix[0], 0, XAxisNormalize.x) # Sets the first row, first column
- om.MScriptUtil.setDoubleArray(matrix[0], 1, XAxisNormalize.y) # Sets the first row, second column
- om.MScriptUtil.setDoubleArray(matrix[0], 2, XAxisNormalize.z) # Sets the first row, third column
- om.MScriptUtil.setDoubleArray(matrix[1], 0, YAxisNormalize.x) # Sets the second row, first column
- om.MScriptUtil.setDoubleArray(matrix[1], 1, YAxisNormalize.y) # Sets the second row, second column
- om.MScriptUtil.setDoubleArray(matrix[1], 2, YAxisNormalize.z) # Sets the second row, third column
- om.MScriptUtil.setDoubleArray(matrix[2], 0, ZAxisNormalize.x) # Sets the third row, first column
- om.MScriptUtil.setDoubleArray(matrix[2], 1, ZAxisNormalize.y) # Sets the third row, second column
- om.MScriptUtil.setDoubleArray(matrix[2], 2, ZAxisNormalize.z) # Sets the third row, third column
- # converts inverse matrix to center rotate points to 0
- matrixInverse = matrix.inverse()
- # Convert to MTransformationMatrix to extract rotations
- mTransformMtx = om.MTransformationMatrix(matrix)
- mTransformMtxInverse = om.MTransformationMatrix(matrixInverse)
- # Get the euler rotations
- eulerRot = mTransformMtx.eulerRotation()
- eulerRotInverse = mTransformMtxInverse.eulerRotation()
- # sort to the proper rotate order
- eulerRot.reorderIt(rotOrder)
- eulerRotInverse.reorderIt(rotOrder)
- # convert radians to degrees
- rotAngle = [math.degrees(angle) for angle in (eulerRot.x, eulerRot.y, eulerRot.z)]
- rotAngleInverse = [math.degrees(angle) for angle in (eulerRotInverse.x, eulerRotInverse.y, eulerRotInverse.z)]
- print 'ANGLES: ', rotAngle
- #print 'ANGLES INVERSE:', rotAngleInverse
- #return rotAngleInverse, rotAngle
- return rotAngle
- def createJointControls():
- '''
- @summary:
- creates a joint at the center of every selected edge
- @arguments:
- none but needs a selection of edges
- @returns:
- JointChain
- '''
- # This code works to retain the selected orderd
- selectedEdgeList = mc.ls(orderedSelection=True, long=True)
- print 'EDGELIST:', selectedEdgeList
- mc.select(clear=True)
- jointList = []
- # for each edge selected create a joint
- for selectedEdge in selectedEdgeList:
- # get the centerPoint
- # also need to get the orientation of the joint
- edgeCenter = calculateEdgeCenter(selectedEdge)
- edgeEndPoints = mc.xform(selectedEdge, query=True, worldSpace=True, translation=True)
- print "EDGEENDS", edgeEndPoints
- originPosition = om.MVector(edgeEndPoints[0],edgeEndPoints[1],edgeEndPoints[2])
- XDirection = om.MVector(edgeEndPoints[3],edgeEndPoints[4],edgeEndPoints[5])
- UPDirection = om.MVector(0,0,0)
- rotationAngle = calculateRotations(pointOrigin=originPosition, pointX=XDirection, pointUP=UPDirection)
- joint = mc.joint(position=edgeCenter, orientation=rotationAngle)
- mc.select(clear=True)
- #append joint name to list
- jointList.append(joint)
- createHierarchy(nodeList=jointList)
- #newBindSkin " -byClosestPoint -toSkeleton";
- createJointControls()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement