How Can We Help?

Script Clips for Nuke

You are here:
< Back to Wiki

TLC and Python

https://learn.foundry.com/nuke/8.0/content/user_guide/expressions/converting_expressions.html

# to use a Nuke expression in Python code.
nuke.expression()# to use a Nuke expression in TCL.
expression
# to run TCL code in Python.
nuke.tcl()

# to run Python code in TCL
python

# (square brackets) to embed TCL in a Nuke expression (or a string knob)
[ ]

# to embed Python in a Nuke expression.
[python {…}]

Variables & Expressions

# This makes a decimal number a whole number ( 2.75 = 2 )
int(x)

# This makes negative numbers positive.
abs(x)

# Make s a value a string, good for text
str(x)

TLC

[value variable]

# Get the frame variables
frame first_frame last_frame

format.x format.y – xxx

# Get the horizontal and vertical number
width height

# Get the left and bottom bounding box location
bbox.x bbox.y

# Get the right and top bounding box location
bbox.r bbox.t

# Get the width and height bounding box location
bbox.w bbox.h

Strings

string = “One, Two, Three, Four”

Conditions

IF THEN

Python:

if expression:
     statement(s)
else:
     statement(s)

TLC:
A > B ? True : False
[if {A > B} {True} {False}]

FOR LOOPS

for x in range(0, 10):
     print x

string = ['one', Two, '3', 'four']
for x in string:
     print x

WHILE LOOPS

loops = 10
while (loops <= 10):
     loops = loops + 1

Expressions

http://www.nukepedia.com/written-tutorials/expressions-101

Gradient x / width     y / height     1 - (y / height)

Lines  sin(x)     sin(x) + 1     ( sin(x) + 1 ) / 2     ( sin(x / 4 ) + 1 ) / 2

Radial Gradient sqrt(x * x + y * y )      300 - sqrt(x * x + y * y )     (300 - sqrt( x * x + y * y ) ) / 300

Radial Rings sin(sqrt(x * x + y * y ))    sin(sqrt(x * x + y * y ) / 4)

Random

(random(123,frame*0.1)*0.5)+0.5 | (random(seed,frame*frequency)*amplitude)+valueOffset

https://www.cameroncarson.com/nuke-wave-expressions

RANDOM WAVE

random((frame+offset)/waveLength) * (maxVal-minVal) + minVal 

NOISE WAVE

(noise((frame+offset)/waveLength)+1)/2 * (maxVal-minVal) + minVal

SINE WAVE

(sin(2*pi*(frame+offset)/waveLength)+1)/2 * (maxVal-minVal) + minVal

TRIANGLE WAVE

(asin(sin(2*pi*(frame+offset)/waveLength))/pi+0.5) * (maxVal-minVal) + minVal

SQUARE WAVE

int(sin(2*pi*(frame+offset)/waveLength)+1) * (maxVal-minVal) + minVal

SAWTOOTH WAVE

((frame+offset) % waveLength)/waveLength * (maxVal-minVal) + minVal

SAWTOOTH (PARABOLIC) WAVE

sin((pi*(frame+offset)/(2*waveLength)) % (pi/2)) * (maxVal-minVal) + minVal

SAWTOOTH (PARABOLIC REVERSED) WAVE

cos((pi*(frame+offset)/(2*waveLength)) % (pi/2)) * (maxVal-minVal) + minVal

SAWTOOTH (EXPONENTIAL) WAVE

(exp(2*pi*((frame+offset) % waveLength)/waveLength)-1)/exp(2*pi) * (maxVal-minVal) + minVal

BOUNCE WAVE

abs(sin(pi*(frame + offset)/waveLength))* (maxVal-minVal) + minVal

BLIP

((frame+(offset+waveLength)) % (waveLength+blipLength)/(waveLength)) *(waveLength/blipLength) - (waveLength/blipLength) >= 0 ? maxVal : minVal

SINEBLIP

((int((frame+offset) % waveLength)) >= 0 ? ((int((frame+offset) % waveLength)) <= (0+(blipLength-1)) ? ((sin(pi*((frame+offset) % waveLength)/blipLength)/2+1/2) * (2*maxVal-2*minVal) + (2*minVal-maxVal)) : minVal) : minVal)

 

Bounding box

bbox.x bbox.y bbox.r bbox.t

Get the input width and height

.width .height

Get input first and last frame

.first_frame and .last_frame

Name of the node

.name

Will look for the original node

.root

Python

Get Script Name

os.path.basename(nuke.root().name()).split('_v')[0]

Get Version

os.path.basename(nuke.root().name()).split(getUserVersion)[1].split('.nk')[0]

Get File

inspect.getfile(inspect.currentframe())

Get Folder

os.path.dirname(inspect.getfile(inspect.currentframe()))

Rendering tips

Adding “/5″ to the end of you render frames will render every 5th frame i.e. 1-100/5

Mathematical Functions

FunctionPurposeOperator UsageRelated FunctionsDeepExpression Compatible
abs (x)Returns the absolute value of the floating-point number x.xSee also: fabs.
acos (x)Calculates the arc cosine of x; that is the value whose cosine is x.If x is less than -1 or greater than 1, acos returns nan (not a number).See also: cos, cosh, asin, atan.
asin (x)Calculates the arc sine of x; that is the value whose sine is x.If x is less than -1 or greater than1, asin returns nan (not a number).See also: sin, sinh, acos, atan.
atan (x)Calculates the arc tangent of x; that is the value whose tangent is x. The return value is between -PI/2 and PI/2.xSee also: tan, tanh, acos, asin, atan2.
atan2 (x, y)Calculates the arc tangent of the two variables x and y. This function is useful to calculate the angle between two vectors.x, ySee also: sin, cos, tan, asin, acos, atan, hypot.
ceil (x)Round x up to the nearest integer.xSee also: floor, trunc, rint.
clamp (x, min, max)Return x clamped to [min … max].x, min, maxSee also: min, max.
clamp (x)Return x clamped to [0.0 … 1.0].xSee also: min, max.
cos (x)Returns the cosine of x.x in radiansSee also: acos, sin, tan, cosh.
cosh (x)Returns the hyperbolic cosine of x, which is defined mathematically as (exp(x) + exp(-x)) / 2.xSee also: cos, acos, sinh, tanh.
curve (frame)Returns the y value of the animation curve at the given frame.optional: frame, defaults to current frame.See also: value, y.
degrees (x)Convert the angle x from radians into degrees.xSee also: radians.
exp (x)Returns the value of e (the base of natural logarithms) raised to the power of x.xSee also: log, log10.
exponent (x)Exponent of x.xSee also: mantissa, ldexp.
fBm (x, y, z, octaves, lacunarity, gain)Fractional Brownian Motion. This is the sum of octaves calls to noise(). For each of them the input point is multiplied by pow(lacunarity,i) and the result is multiplied by pow(gain,i). For normal use, lacunarity should be greater than 1 and gain should be less than 1.x, y, z, octaves, lacunarity, gainSee also: noise, random, turbulence.
fabs (x)Returns the absolute value of the floating-point number x.xSee also: abs.
false ()Always returns 0See also: true.
floor (x)Round x down to the nearest integer.xSee also: ceil, trunc, rint.
fmod (x, y)Computes the remainder of dividing x by y. The return value is x – n y, where n is the quotient of x / y, rounded towards zero to an integer.x, ySee also: ceil, floor.
frame ()Return the current frame number.See also: x.
from_byte (color component)Converts an sRGB pixel value to a linear value.color_componentSee also: to_sRGB, to_rec709f, from_rec709f.
from_rec709f (color component)Converts a rec709 byte value to a linear brightnesscolor_componentSee also: form_sRGB, to_rec709f.
from_sRGB (color component)Converts an sRGB pixel value to a linear value.color_componentSee also: to_sRGB, to_rec709f, from_rec709f.
hypot (x, y)Returns the sqrt(x*x + y*y). This is the length of the hypotenuse of a right-angle triangle with sides of length x and y.x, ySee also: atan2.
int (x)Round x to the nearest integer not larger in absolute value.xSee also: ceil, floor, trunc, rint.
ldexp (x)Returns the result of multiplying the floating-point number x by 2 raised to the power exp.x, expSee also: exponent.
lerp (a, b, x)Returns a point on the line f(x) where f(0)==a and f(1)==b. Matches the lerp function in other shading languages.a, b, xSee also: step, smoothstep.
log (x)Returns the natural logarithm of x.xSee also: log10, exp.
log10 (x)Returns the base-10 logarithm of x.xSee also: log, exp.
logb (x)Same as exponent().xSee also: mantissa, exponent.
mantissa (x)Returns the normalized fraction. If the argument x is not zero, the normalized fraction is x times a power of two, and is always in the range 1/2 (inclusive) to 1 (exclusive). If x is zero, then the normalized fraction is zero and exponent() Returns zero.xSee also: exponent.
max (x, y, … )Return the greatest of all values.x, y, (…)See also: min, clamp.
min (x, y, … )Return the smallest of all values.x, y, (…)See also: max, clamp.
mix (a, b, x)Same as lerp().a, b, xSee also: step, smoothstep, lerp.
noise (x, y, z)Creates a 3D Perlin noise value. This produces a signed range centerd on zero. The absolute maximum range is from -1.0 to 1.0. This produces zero at all integers, so you should rotate the coordinates somewhat (add a fraction of y and z to x, etc.) if you want to use this for random number generation.x, optional y, optional zSee also: random, fBm, turbulence.
pi ()Returns the value for pi (3.141592654…).
pow (x, y)Returns the value of x raised to the power of y.x, ySee also: log, exp, pow.
pow2 (x)Returns the value of x raised to the power of 2.x, ySee also: pow.
radians (x)Convert the angle x from degrees into radians.xSee also: degrees.
random (x, y, z)Creates a pseudo random value between 0 and 1. It always generates the same value for the same x, y and z. Calling random with no arguments creates a different value on every invocation.optional x, optional y, optional zSee also: noise, fBm, turbulence.
rint (x)Round x to the nearest integer.xSee also: ceil, floor, int, trunc.
sin (x)Returns the sine of x.x in radiansSee also: asin, cos, tan, sinh.
sinh (x)Returns the hyperbolic sine of x, which is defined mathematically as (exp(x) – exp(-x)) / 2.xSee also: sin, asin, cosh, tanh.
smoothstep (a, b, x)Returns 0 if x is less than a, returns 1 if x is greater or equal to b, returns a smooth cubic interpolation otherwise. Matches the smoothstep function in other shading languages.a, b, xSee also: step, lerp.
sqrt (x)Returns the non-negative square root of x.xSee also: pow, pow2.
step (a, x)Returns 0 if x is less than a, returns 1 otherwise. Matches the step function other shading languages.a, xSee also: smoothstep, lerp.
tan (x)Returns the tangent of x.x in radiansSee also: atan, cos, sin, tanh, atan2.
tanh (x)Returns the hyperbolic tangent of x, which is defined mathematically as sinh(x) / cosh(x).xSee also: tan, atan, sinh, cosh.
to_byte (color component)Converts a floating point pixel value to an 8-bit value that represents that number in sRGB space.color_componentSee also: form_sRGB, to_rec709f, from_rec709f.
to_rec709f (color component)Converts a floating point pixel value to an 8-bit value that represents that brightness in the rec709 standard when that standard is mapped to the 0-255 range.color_componentSee also: form_sRGB, from_rec709f.
to_sRGB (color component)Converts a floating point pixel value to an 8-bit value that represents that number in sRGB space.color_componentSee also: form_sRGB, to_rec709f, from_rec709f.
true ()Always Returns 1.See also: false
trunc (x)Round x to the nearest integer not larger in absolute value.xSee also: ceil, floor, int, rint.
turbulence (x, y, z, octaves, lucanarity, gain)This is the same as fBm() except the absolute value of the noise() function is used.x, y, z, octaves, lucanarity, gainSee also: fBm, noise, random.
value (frame)Evaluates the y value for an animation at the given frame.optional: frame, defaults to current frame.See also: y, curve.
x ()Return the current frame number.See also: frame.
y (frame)Evaluates the y value for an animation at the given frame.optional: frame, defaults to current frame.See also: value, curve.
Tags:

Leave a Reply

Table of Contents