Nuke 5: sample tcl command -> python

911 views
Skip to first unread message

michaelg

unread,
Mar 31, 2008, 2:10:03 AM3/31/08
to nukeuser
Can anyone tell me what the exact python syntax is when using the
"sample" script command in a knob's expression field? At the moment
if I want to sample the pixel value for another node I do this:
[sample othernode red 1 1]

...works well (except for some update issues) but would like to know
the "python way"...

Thanks

Michael

Michael Morehouse

unread,
Mar 31, 2008, 11:51:15 AM3/31/08
to nuke...@googlegroups.com
From what I've been able to figure out so far that is the 'right' way
to do it for now.

Python doesn't appear to have been enabled for expressions except for
as a way of running a TCL expression. In other words to do it the
Python way inside knob's expression field you'd end up having to write
a script to run in the script editor that would, in the end, set the
expression value to what you're already typing in.

I doubt that makes any sense at all, but Python seems to be there to
overcome scripting difficulties, like setting the values of nodes
recursively, or creating new nodes (or entire trees), or parsing text
files, etc., but it doesn't seem to replace the (arguably limited)
expression syntax within each node, which I guess make sense for
backwards compatibility purposes.

To use the sample directly in the Script Editor or a python script,
say in the context of building a tool that asked input from the user
and then returned the value (all of which could be used for a huge
range of purposes) you'd write a script sort of like this and run it
in the Script Editor.

nodetotest = nuke.selectedNode()
R = str(nuke.channels(nodetotest)[0])
G = str(nuke.channels(nodetotest)[1])
B = str(nuke.channels(nodetotest)[2])
chandict = {}
chandict['r'] = R
chandict['g'] = G
chandict['b'] = B
trychan = nuke.getInput("What channel? (enter r for Red, g for Green,
or b for Blue)")
tryx = int(nuke.getInput("What x pixel value? (must be an integer)"))
tryy = int(nuke.getInput("What y pixel value? (must be an integer)"))
trydx = int(nuke.getInput("How wide is the sample? (must be an integer
and NOT 0)"))
trydy = int(nuke.getInput("How tall is the sample? (must be an integer
and NOT 0)"))
nuke.display(str(nuke.sample(nodetotest, chandict[trychan], tryx,
tryy, trydx, trydy)),nodetotest,"Result")

Copy and paste that into the script editor window and run it, then
make sure you follow the directions, and you should see what I mean.

Michael M

michaelg

unread,
Mar 31, 2008, 2:24:00 PM3/31/08
to nukeuser
Thanks for the reply! I initially thought of trying the python
"version" of sample because I was finding the tcl "version" didn't
update when I changed the frame (the source node colour was animated),
unless you forced a refresh in some way. But obviously doing it with
Python isn't going to make any difference and isn't approriate in this
case....perhaps it's a bug I'm experiencing.

I'll want to look at Python in Nuke more closely and the example
you've written helps. It's more complex than the simple examples in
the docs but simpler than the example Python scripts....which is good.

Michael

Michael Morehouse

unread,
Mar 31, 2008, 2:41:24 PM3/31/08
to nuke...@googlegroups.com
Yeah, there's a lot of sort of brute-forcing your head into it, cause it's not that simple and in places I don't think it's really been fully implemented.

I've actually put a couple python scripts up on fxshare that are getting quite a bit of use, digging through them might help you see more as well.

The autobackdrop.py especially is basically just a translation into python of the autobackdrop.tcl that Frank Reuter also has on fxshare, comparing those two might help.

Also, to the original problem, I've just plugged your expression into the color node of a text field and have it looking at an arbitrary pixel in the read node, and it's updating frame by frame.  What I did notice is that when I first added the expression it automatically set a keyframe, and then I deleted it, clicked elsewhere in the parameter window, and made sure it didn't go back to keyframe before changing frames.  Seems to work just fine, so where and how are you getting different results?

I could see it maybe being a problem if you're not on a float knob (ie an Integer Knob or a 2d position knob, etc, cause the float RGB values will all just floor to 0 or ceiling to 1).  Also the keyframes keep trying to reestablish themselves if you ever even look at the expression again.

A silly trick you might also do to force a refresh is add "+ (frame * 0)" to the expression.

Michael

michaelg

unread,
Apr 2, 2008, 6:49:05 PM4/2/08
to nukeuser
Thanks for the suggestion about comparing autobackdrop.py with the tcl
version...it's on the to do list.

Re: the initial problem, I've tried to isolate it but for some reason
it's working OK now and I'm not sure why. Before, not only was it not
updating but it was quite unstable. I was careful to delete the
keyframes that kept on appearing whenever you look at the
expression...that's a pretty annoying Nuke 5 "feature".

In essence, what I was doing was linking the 3D position of an Axis to
the rgb of a constant. I can use the Constant to store the location
of the Axis then process it with various merges and expressions, then
use [sample etc] to get that info back to say a camera, or to effect
an image.
Maybe not the best way of doing things but I'm not much of a
programmer.
I've actually rebuilt it a bit differently and it seems good, updates
fine...but as I said the older version started updating without being
forced and I'm not sure what I did to make it work...I didn't change
anything to my knowledge.

Michael (G)
Reply all
Reply to author
Forward
0 new messages