Hi,
I am trying to convert camera node from using the matrix to using its
scale translate and rotate knobs but I can't seem to get it to line
up. Is there an easy way to do this?
Here's my code:
import logging
import math

import nuke
import nukescripts

def makeSRTNodeOfSelectedNodes():
    selectedNodes = nuke.selectedNodes()
    if len(selectedNodes)!=1:
        nuke.message("Please select one camera node")
        return False
    nukescripts.edit.node_copypaste()
    cameraNode = nuke.selectedNode()
    convertCameraFromMatrixToSRT(cameraNode)

def convertCameraFromMatrixToSRT(cameraNode, startFrame=None, endFrame=None):
    if startFrame==None:
        startFrame = int(nuke.root()["first_frame"].value())
    if endFrame==None:
        endFrame = int(nuke.root()["last_frame"].value())
    for frame in xrange(startFrame, endFrame+1):
        setScaleFromMatrix(cameraNode, frame)
        setRotationFromMatrix(cameraNode, frame)
        setTranslationFromMatrix(cameraNode, frame)
    cameraNode['useMatrix'].setValue(False)

def setTranslationFromMatrix(node, frame):
    node['translate'].setKeyAt(frame)
    tx = node['matrix'].valueAt(frame, converToIndex(0, 3))
    node['translate'].setValueAt(tx, frame, 0)
    #
    ty = node['matrix'].valueAt(frame, converToIndex(1, 3))
    node['translate'].setValueAt(ty, frame, 1)
    #
    tz = node['matrix'].valueAt(frame, converToIndex(2, 3))
    node['translate'].setValueAt(tz, frame, 2)

def setScaleFromMatrix(node, frame):
    node['scaling'].setKeyAt(frame)
    sx = node['matrix'].valueAt(frame, converToIndex(0, 0))
    node['scaling'].setValueAt(sx, frame, 0)
    #
    sy = node['matrix'].valueAt(frame, converToIndex(1, 1))
    node['scaling'].setValueAt(sy, frame, 1)
    #
    sz = node['matrix'].valueAt(frame, converToIndex(2, 2))
    node['scaling'].setValueAt(sz, frame, 2)

def setRotationFromMatrix(node, frame):
    import _nukemath
    m4 = _nukemath.Matrix4()
    for i in xrange(16):
        v = node['matrix'].valueAt(frame,i)
        logging.debug("Matrix value at %d=%f", i, v)
        m4[i] = v
    m4.rotationOnly()
    ZXYRotation = m4.rotationsZXY()
    logging.debug("ZXY rotation=%s", ZXYRotation)
    node['rot_order'].setValue("ZXY")
    node['rotate'].setKeyAt(frame)
    for i in xrange(3):
        r = ZXYRotation[i]
        d = math.degrees(r)
        node['rotate'].setValueAt(d, frame, i)

def converToIndex(x, y, d=4):
    return y*d+x
_______________________________________________
Nuke-python mailing list
Nuke-python@support.thefoundry.co.uk, http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python

Reply via email to