Using the nuke.math Python module to do Vector and Matrix operations

Written by Ivan Busquets on .

Nuke has its own python math module (nuke.math) that provides support for Matrix, Vector, and Quaternion classes, and useful functions to operate with them.

If you need to do any maths involving vector operations in Nuke (like adding or substracting vectors, transforming vectors with transformation matrices, calculating vector reflections, inverse-projecting a 3d vector into 2d screen coordinates, etc), then the nuke.math module can be of great help.

Unfortunately, at this time there is no way to write out pixel data using python in Nuke, so the use of this module will be rather limited. However, it can still prove very useful to do any kind of vector and matrix operations between knobs, 3d objects, and even on sampled pixels (just not writing them out, unfortunately)

The documentation is a bit scarce on the nuke.math module at the moment, and the little there is is mostly a direct port from the same classes in C++. When first facing this module, I found myself having to learn how to use it the hard way, so here's some notes, examples, tips and extended documentation on the nuke.math module, hoping it will make it a bit more accessible to anyone trying to use it for the first time.

Note: Part of the descriptions of some classes and methods is taken from the C++ Plugin development documentation, but I've tried to expand a little more to give a better insight on what the different methods can be used for.

Comments   

 
# Frank Rueter 2010-09-14 15:09
awesome tutorial! Thanks so much for sharing and putting in that much effort!
 
 
# Carlos Trijueque 2010-09-16 00:55
Ivan, thanks for sharing this terrific piece of info.Very helpful and, as your other tutorials, very well written and explainied.
 
 
# Michael Garrett 2010-09-17 11:29
Brilliant! Good info on nuke.math is long overdue!
 
 
+1 # Ivan Busquets 2010-09-17 12:06
Thanks for all the comments, guys!

Yes, Michael, completely agree. It's one of those I had to learn the hard way, so I figured it would be useful to share. Plus, I learnt a bit more while writing it. Glad you liked it. ;-)
 
 
# Pete O'Connell 2010-09-20 19:33
Thanks Ivan, that is one of the most awesome tutorials I have ever read!
 
 
# Zach Lewis 2011-08-06 10:12
Wanted to chime in and say that this is awesome. A lot of care went into preparing this, and it's much appreciated.
 
 
# Diego Piccinato 2011-09-08 10:53
Awesome stuff, thanks Ivan!
 
 
# Marco Leone 2013-08-24 00:25
Hey Ivan, thanks a lot for the tutorial, it opened my mind!!!

one quick question thou, I was testing out some of those function on Matrix and I found a little problem.

What I am trying to do is to get the XYZ rotation( in degrees ) from the world matrix of a Camera.

Following you tutorial everythign works fine but the final result of the rotation is a bit different from the original rotation.

what I am doing is really simple :

########################################

import math

matrix = nuke.math.Matri x4() #creating a matrix

matrixValues = nuke.toNode('Ca mera1')['world_ matrix'].getVal ue() # getting the value from the camera world matrix

print "matrixValues :", matrixValues

### adding the value from the camera to the matrix just created ###

for VAL in matrixValues :
print "VAL",VAL
matrix[matrixVa lues.index(VAL) ] = VAL



matrix.rotationOnly() #getting sure I have only the rotation values

print math.degrees(ma trix.rotationsZ XY()[0])
print math.degrees(matrix.rotationsZXY()[1])
print math.degrees(matrix.rotationsZXY()[2])

### check if values r the same

if nuke.toNode('Ca mera1')['rotate '].value(0) == math.degrees(ma trix.rotationsZ XY()[0]) : print True
else: print False


########################################


as u may see it seems that there is an approximation and we loose the last 5 digits on every value when we are adding the worldMatrix of the camera to our Matrix4, and that may be causing the little difference in the result.

Do you know by chance if that is something I did wrong or maybe if there is a work around it ?
thanks for your time
 

You have no rights to post comments

We have 2205 guests and 46 members online