Skip to content

Instantly share code, notes, and snippets.

@julik
Created August 23, 2011 08:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julik/1164618 to your computer and use it in GitHub Desktop.
Save julik/1164618 to your computer and use it in GitHub Desktop.
Nuke corner pin to matrix
# and here is the script in case anyone is interested :)
import nuke
def getTheCornerpinAsMatrix():
projectionMatrixTo = nuke.math.Matrix4()
projectionMatrixFrom = nuke.math.Matrix4()
#dir(projectionMatrix)
theCornerpinNode = nuke.selectedNode()
imageWidth = float(theCornerpinNode.width())
imageHeight = float(theCornerpinNode.height())
to1x, to1y = theCornerpinNode['to1'].value()
to2x, to2y = theCornerpinNode['to2'].value()
to3x, to3y = theCornerpinNode['to3'].value()
to4x, to4y = theCornerpinNode['to4'].value()
from1x, from1y = theCornerpinNode['from1'].value()
from2x, from2y = theCornerpinNode['from2'].value()
from3x, from3y = theCornerpinNode['from3'].value()
from4x, from4y = theCornerpinNode['from4'].value()
projectionMatrixTo.mapUnitSquareToQuad(to1x,to1y,to2x,to2y,to3x,to3y,to4x,to4y)
projectionMatrixFrom.mapUnitSquareToQuad(from1x,from1y,from2x,from2y,from3x,from3y,from4x,from4y)
theCornerpinAsMatrix = projectionMatrixTo*projectionMatrixFrom.inverse()
theCornerpinAsMatrix.transpose()
return theCornerpinAsMatrix
Copy link

ghost commented Oct 5, 2015

Hi dude... this is gonna be very very useful to me ... Thanks a lot.

@gasmasocet
Copy link

added inverse option and exxtra matrix knob to calculations :)


import nuke

def mat4():
    M = nuke.math.Matrix4()
    M.makeIdentity()
    return M

def cornerpin_to_matrix( node, frame ):
    # converts cornerpin to transform matrix

    format_width = float(node.width())
    format_height = float(node.height())

    # make matrices
    m_to = mat4()
    m_from = mat4()
    m_ex = mat4()
    
    # get extra matrix    
    extra_values = node["transform_matrix"].getValueAt(frame)
    for i in range(16): m_ex[i] = extra_values[i]
    m_ex.transpose()

    # get from to values
    to1_x, to1_y = node['to1'].getValueAt(frame)
    to2_x, to2_y = node['to2'].getValueAt(frame)
    to3_x, to3_y = node['to3'].getValueAt(frame)
    to4_x, to4_y = node['to4'].getValueAt(frame)
    
    from1_x, from1_y = node['from1'].getValueAt(frame)
    from2_x, from2_y = node['from2'].getValueAt(frame)
    from3_x, from3_y = node['from3'].getValueAt(frame)
    from4_x, from4_y = node['from4'].getValueAt(frame)
    
    # build matrces
    m_to.mapUnitSquareToQuad(   to1_x, to1_y,
                                to2_x, to2_y,
                                to3_x, to3_y,
                                to4_x, to4_y )

    m_from.mapUnitSquareToQuad( from1_x, from1_y,
                                from2_x, from2_y,
                                from3_x, from3_y,
                                from4_x, from4_y )
    # combine matrices
    out = m_to * m_from.inverse()

    # add extra matrix    
    out = m_ex * out

    # if inverted do it at the result
    if node['invert'].getValueAt(frame):
        out = out.inverse()   
    
    return out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment