Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.8 kB
1
Indexable
Never
import nuke
import math

def getWorldMatrix_localMatrix(node):
    t = node['translate'].value()
    r = node['rotate'].value()
    sc = node['scaling'].value()
    sk = node['skew'].value()
    p = node['pivot'].value()
    if node.knob('pivot_rotate') is not None:
        pr = node['pivot_rotate'].value()
    else:
        pr = None
    
    #pivot
    pivot = nuke.math.Matrix4()
    pivot.makeIdentity()
    
    if pr is not None:
        pivot.rotateY((math.pi/180)*pr[1])
        pivot.rotateX((math.pi/180)*pr[0])
        pivot.rotateZ((math.pi/180)*pr[2])
    
    pivot.translate(-p[0],-p[1],-p[2])
    pivot.transpose()
    inv = pivot.inverse()
    
    #translate
    translate = nuke.math.Matrix4()
    translate.makeIdentity()
    translate.translate(t[0],t[1],t[2])
    translate.transpose()
    
    #rotate
    rotate = nuke.math.Matrix4()
    rotate.makeIdentity()
    
    rotateOrder = node['rot_order'].value()[::-1]
    for i in rotateOrder:
        if i == 'X':
            rotate.rotateX((math.pi/180)*r[0])
        elif i == 'Y':
            rotate.rotateY((math.pi/180)*r[1])
        elif i == 'Z':
            rotate.rotateZ((math.pi/180)*r[2])
    rotate.transpose()
    
    #scale
    scale = nuke.math.Matrix4()
    scale.makeIdentity()
    scale.scale(sc[0],sc[1],sc[2])
    scale.transpose()
    
    #build matrix
    matrix = nuke.math.Matrix4()
    matrix.makeIdentity()
    
    matrix = matrix*pivot
    
    xform = node['xform_order'].value()
    for i in xform:
        if i == 'S':
            matrix = matrix*scale
        elif i == 'R':
            matrix = matrix*rotate
        elif i == 'T':
            matrix = matrix*translate
    
    matrix = matrix*inv

    return matrix


def getWorldMatrix_ConcatMatrix(node):
    ThreeDNodes = ['Axis', 'Axis2', 'Axis3', 'Camera', 'Camera2', 'Camera3', 'Camera4']
    skipNodes = ['Dot', 'Noop']

    currentNode = node.input(0)
    matrixStack = []

    while True:
        if currentNode is None:
            break

        nodeClass = currentNode.Class()
        if nodeClass is None:
            break
        elif nodeClass in skipNodes:
            currentNode = currentNode.input(0)
        elif nodeClass == 'Switch':
            sv = currentNode['which'].value()
            currentNode = currentNode.input(sv)
        elif nodeClass in ThreeDNodes:
            matrixStack.append(getWorldMatrix_localMatrix(currentNode))
            currentNode = currentNode.input(0)
        elif nodeClass == 'Input':
            ni = int( currentNode['number'].value() )
            currentNode = currentNode.parent().input(ni)
        else:
            break

    #combine matrixes
    matrix = nuke.math.Matrix4()
    matrix.makeIdentity()

    for i in matrixStack:
        matrix = matrix*i

    return matrix