So I've been trying hard to understand how to perform the 'lookAt' stuff with the API, which I think is the basics of what I need to understand to achieve what I want.
This is what I got so far. Use it in a scene with a sphere and two cubes with their default names. ;)
###########
import maya.cmds as mc
import maya.OpenMaya as om
def getDependNodeByName( name ):
selList = om.MSelectionList ()
selList.add (name)
node = om.MObject()
selList.getDependNode (0, node)
return node
srcNode = getDependNodeByName("pSphere1")
targetNode = getDependNodeByName("pCube1")
upNode = getDependNodeByName("pCube2")
fnSrc = om.MFnTransform(srcNode)
fnTarget = om.MFnTransform(targetNode)
fnUp = om.MFnTransform(upNode)
srcTransformMatrix = fnSrc.transformation()
srcTranslate = srcTransformMatrix.getTranslation(om.MSpace.kWorld)
srcQuatRotation = srcTransformMatrix.rotation()
targetTransformMatrix = fnTarget.transformation()
targetTranslate = targetTransformMatrix.getTranslation(om.MSpace.kWorld)
upTransformMatrix = fnUp.transformation()
upTranslate = upTransformMatrix.getTranslation(om.MSpace.kWorld)
# Aim Axis (hardcoded to x)
basisAimVec = om.MVector(0,0,0)
basisAimVec.x = 1
basisAimVec = basisAimVec.rotateBy(srcQuatRotation)
aimVec = targetTranslate - srcTranslate
quat = basisAimVec.rotateTo(aimVec)
srcTransformMatrix.addRotationQuaternion( quat.x, quat.y, quat.z, quat.w, om.MSpace().kTransform )
srcQuatRotation = srcTransformMatrix.rotation()
# Up Axis (hardcoded to y)
"""
basisUpVec = om.MVector(0,0,0)
basisUpVec.y = 1
basisUpVec = basisUpVec.rotateBy(srcQuatRotation)
upVec = upTranslate - srcTranslate
quat = basisUpVec.rotateTo(upVec)
srcTransformMatrix.addRotationQuaternion( quat.x, quat.y, quat.z, quat.w, om.MSpace().kTransform );
"""
# Set the adjusted matrix
fnSrc.set(srcTransformMatrix)
mc.refresh()
###########
So I haven't been able to implement the up axis in this. Any tips would be greatly appreciated.
I also had to refresh at the end otherwise Maya ended up not updating the viewport, anybody knows what that's all about?
Love to get into this more so thanks in advance for any help!
Regards,
Roy