I'm trying to put a python function in a expression node expression. The following syntax works perfectly well:
[python testMe()]
My question is about how giving my function some arguments.
For example, how can I give the pixel red color, like in tcl : r(x,y)
Unfortunately, I think what you're trying to do is impossible because of the evaluation order of knob expressions vs. the pixel processing expressions.
tl;dr: The TCL expressions in the Expression node's knobs are being evaluated before the variables you're after are actually available.
This glosses over or otherwise simplifies many things about Nuke's operator evaluation in order to try and address the original question more concisely.
Expressions on knobs (i.e. those in square brackets, or set using right-click -> "Add Expression...") are evaluated at the beginning of the operator's processing (during what you can call the "knob store" phase) before any kind of pixel processing starts. This step generates the actual (numeric) values that are stored by the internal C++ Knob
objects, in order to make the values available for use during pixel processing. This expansion is done transparently by most built-in knob types so that a plugin developer doesn't have to worry about doing it themselves in their operator code.
One way to see a little of what's going on is to evaluate a Nuke expression in the context of one of these knobs outside of any rendering context:
nuke.tcl('in Expression1.expr2 {return [expression x]}')
This uses the Nuke TCL command in
to set the evaluation context for an expression to one of the pixel expression knobs. Then, the expression
command (not to be confused with TCL's expr
command), evaluates the given string as a normal Nuke knob expression (knob expressions use what is essentially a restricted subset of TCL's expr
grammar that only operates on float values and does knob and variable resolution for strings like Grade1.blackpoint
).
You'll notice that this nuke.tcl
call does actually return a value, but unfortunately, that value is the current frame number. If you replace x
with y
in the expression... you get 0. In other words, the pixel coordinates are not available in the knob's expression context during the "knob store" phase (when Nuke will be expanding your TCL expression(s)).
When the Expression node is actually processing pixels, the text values from each expression knob are expanded by the operator in an expression context that has the x
and y
variables set to the coordinates of the pixel that is currently being processed.
Anyway, I know this may be a bit technical and long-winded, but I hope it helps explain what's going on here.