And, just to clarify, you can also use nuke.OutputContext() in 6.1 too (and
even in 6.0). I got that mixed up with something else, I think


On Wed, Jul 6, 2011 at 11:19 AM, Ivan Busquets <[email protected]>wrote:

> If you're using a version before 6.2, you can do the "CurveTool" trick to
> force evaluation at each step of your loop:
>
> ######################
> c = nuke.nodes.CurveTool()
>
> for frame in range(firstframe, lastframe):
>     nuke.execute(c, frame, frame)
>
>     camMatrix = nukescripts.snap3d.cameraProjectionMatrix(cameranode)
>     print frame, camMatrix
> nuke.delete(c)
> ######################
>
> If you're on 6.2 or above, you could write your version of
> nukescripts.snap3d.cameraProjectionMatrix(), as you were already trying to
> do, and use an OutputContext object to feed into the value() method of the
> camera's transform knob, which I suppose is the only knob you're having
> trouble with.
>
> Here's a basic version of the function that will take a frame number as an
> argument, but you could expand it to include a "view" argument too.
>
>
> ###############################################################
> #    Note: this is mostly the same function provided by The
> Foundry          #
> #    in nukescripts.snap3d.cameraProjectionMatrix(), with
> only                  #
> #    a few modifications to accept a "frame"
> argument                             #
> ##############################################################
>
> def cameraProjectionMatrixAt(cameraNode, frame):
>   '''Calculate the projection matrix for the camera, based on it's knob
> values.'''
>
>   # Matrix to transform points into camera-relative coords.
>   ctx = nuke.OutputContext()
>   ctx.setFrame(frame)
>   camTransform = cameraNode['transform'].value(ctx).inverse()
>
>   # Matrix to take the camera projection knobs into account
>   roll = float(cameraNode['winroll'].getValueAt(frame))
>   scale_x, scale_y = [float(v) for v in
> cameraNode['win_scale'].getValueAt(frame)]
>   translate_x, translate_y = [float(v) for v in
> cameraNode['win_translate'].getValueAt(frame)]
>   m = _nukemath.Matrix4()
>   m.makeIdentity()
>   m.rotateZ(math.radians(roll))
>   m.scale(1.0 / scale_x, 1.0 / scale_y, 1.0)
>   m.translate(-translate_x, -translate_y, 0.0)
>
>   # Projection matrix based on the focal length, aperture and clipping
> planes of the camera
>   focal_length = float(cameraNode['focal'].getValueAt(frame))
>   h_aperture = float(cameraNode['haperture'].getValueAt(frame))
>   near = float(cameraNode['near'].getValueAt(frame))
>   far = float(cameraNode['far'].getValueAt(frame))
>   projection_mode = int(cameraNode['projection_mode'].getValueAt(frame))
>   p = _nukemath.Matrix4()
>   p.projection(focal_length / h_aperture, near, far, projection_mode == 0)
>
>   # Matrix to translate the projected points into normalised pixel coords
>   format = nuke.root()['format'].value()
>   imageAspect = float(format.height()) / float(format.width())
>   t = _nukemath.Matrix4()
>   t.makeIdentity()
>   t.translate( 1.0, 1.0 - (1.0 - imageAspect /
> float(format.pixelAspect())), 0.0 )
>
>   # Matrix to scale normalised pixel coords into actual pixel coords.
>   x_scale = float(format.width()) / 2.0
>   y_scale = x_scale * format.pixelAspect()
>   s = _nukemath.Matrix4()
>   s.makeIdentity()
>   s.scale(x_scale, y_scale, 1.0)
>
>   # The projection matrix transforms points into camera coords, modifies
> based
>   # on the camera knob values, projects points into clip coords, translates
> the
>   # clip coords so that they lie in the range 0,0 - 2,2 instead of -1,-1 -
> 1,1,
>   # then scales the clip coords to proper pixel coords.
>   return s * t * p * m * camTransform
> ##################################################################
>
> Hope that helps.
> Cheers,
> Ivan
>
>
> On Wed, Jul 6, 2011 at 10:19 AM, Brogan Ross <[email protected]> wrote:
>
>> Hello everyone,
>>     I'm trying to run cameraProjectionMatrix for every frame, but I'm
>> having some issues with nuke.frame() updating.  It's odd, it will iterate
>> through each frame but cameraProjectionMatrix only gets the value for the
>> frame you are on when you start the function.  I tried
>> rewriting cameraProjectionMatrix so it uses getValueAt() instead of just
>> getValue().  But the key value seems to be the Camera's transform inverted,
>> and if you try getValueAt on that one it changes the output to a float
>> equaling 0.0, when it's suppose to give you a Matrix4.
>>    Anyone ever run into this before?  Any ideas on how to get
>> cameraProjectionMatrix to run on every frame?
>>
>>
>> for frame in range(firstframe, lastframe):
>>     nuke.frame(frame)
>>     camMatrix = nukescripts.snap3d.cameraProjectionMatrix(cameranode)
>>     print frame, camMatrix
>>
>> _______________________________________________
>> Nuke-python mailing list
>> [email protected], http://forums.thefoundry.co.uk/
>> http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python
>>
>>
>
_______________________________________________
Nuke-python mailing list
[email protected], http://forums.thefoundry.co.uk/
http://support.thefoundry.co.uk/cgi-bin/mailman/listinfo/nuke-python

Reply via email to