Coding Blender Materials With Nodes & Python

Image for post
Image for post

Creating Node Groups

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Comparisons

Not Equals (neq)

Image for post
Image for post
neq(a, b): max(lt(a, b), gt(a, b))

In-Bounds

Image for post
Image for post
inbounds(v, l, u): min(lt(l, v), gt(u, v))

Out-of-Bounds

Image for post
Image for post
outofbounds(v, l, u): max(lt(v, l), gt(v, u))

Approximates (approx)

Image for post
Image for post
approx(a, b, tolerance): gt(tolerance, abs(sub(a, b)))

Logic Gates

And

Image for post
Image for post
and(a, b): min(neq(a, b), neq(a, b))

Inclusive Or

Image for post
Image for post
or(a, b): max(neq(a, 0.0), neq(b, 0.0))

Not And (nand)

Image for post
Image for post
nand(a, b): max(approx(a, 0.0, epsilon), approx(b, 0.0, epsilon))

Not Or (nor)

Image for post
Image for post
nor(a, b): min(approx(a, 0.0, epsilon), approx(b, 0.0, epsilon))

Exclusive Or (xor)

Image for post
Image for post
xor(a, b): mult(or(a, b), nand(a, b))

Real Number Operations

Clamp

Image for post
Image for post
clamp(v, min, max): min(max(value, min), max)

CosineSine

Image for post
Image for post
cossin(radians): (cos(radians), sin(radians))

Exponent (exp)

Image for post
Image for post

Hyperbolic Cosine (cosh)

Image for post
Image for post
cosh(v): mult(add(exp(v), exp(mult(v, -1.0))), 0.5)

Hyperbolic Sine (sinh)

Image for post
Image for post
sinh(v): mult(sub(exp(v), exp(mult(v, -1.0))), 0.5)

Hyperbolic Tangent (tanh)

Image for post
Image for post
tanh(v): div(sinh(v), cosh(v))

Map

Image for post
Image for post
map(v, lb0, ub0, lb1, ub1): lb1 + (ub1 —lb1) * (v — lb0) / (ub0 —lb0)

Mix

Image for post
Image for post
mix(a, b, t): add(mult(a, sub(1.0, t)), mult(b, t))

Mod / Floor Mod

Image for post
Image for post
mod(a, b): n = fmod(a, b); mix(n, add(n, b), lt(n, 0.0))

Quantize

Image for post
Image for post
quantize(v, d): mult(delta, floor(add(div(v, d), 0.5)))
Image for post
Image for post

Radians

Image for post
Image for post
radians(degrees): mult(degrees, div(pi, 180.0))

Sign

Image for post
Image for post
sign(v): sub(mult(sub(1.0, lt(v, 0.0)), gt(v, 0.0)), lt(v, 0.0))

Fraction (fract)

Image for post
Image for post
fract(v): fmod(v, sign(v))

SmoothStep

Image for post
Image for post
smoothstep(edge0, edge1, x): t: div(sub(x, edge0), sub(edge1, edge0)); mult(pow(t, 2.0), sub(3.0, mult(t, 2.0)))

Threshold

Image for post
Image for post
inbounds(v, l, u): mult(inbounds(v, l, u) ,v)

Integer Conversions

Ceil

Image for post
Image for post
ceil(v): sub(mix(v, add(v, 1.0), gt(v, 0.0)), fmod(v, 1.0))

Floor

Image for post
Image for post
ceil(v): sub(mix(v, sub(v, 1.0), lt(v, 0.0)), fmod(v, 1.0))

Truncate (trunc)

Image for post
Image for post
trunc(v): sub(v, fract(v))

Vector Operations

Magnitude (mag)

Image for post
Image for post
mag(v): pow(dot(v, v), 0.5)

Minkowski Distance

Image for post
Image for post
minkowski(a, b, p): diff: pow(abs(sub(a, b)), p), pow(add(diff.x, diff.y, diff.z), div(1, p))

Multiply by a Uniform Scale (mult)

Image for post
Image for post
mult(v, s): combine(mult(v.x, s), mult(v.y, s), mult(v.z, s))

Multiply by a Nonuniform Scale (scale)

Image for post
Image for post
scale(v, w, h, d): combine(mult(v.x, w), mult(v.y, h), mult(v.z, d))

Reflect

Image for post
Image for post
reflect(i, n): sub(i, mult(n, mult(2, dot(i, n))))

Refract

Image for post
Image for post
refract(i, n, e): idn: dot(i, n), k: sub(1, mult(pow(e, 2), sub(1, pow(idn, 2)))), mult(sub(mult(i, e), mult(n, add(mult(e, idn), pow(k, 0.5)))), lt(0, k))

Rescale

Image for post
Image for post
rescale(v, s): mult(norm(v), s)

Rotate

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Tile

Image for post
Image for post
tile(v, s): fract(mult(v, s))

Shader Operations

Shaped Absorb Volume

Image for post
Image for post
shapedabsorb(c, d, f, l, u): mix(threshold(f, l, u), holdout, absorb(c, d))

Shaped Light

Image for post
Image for post
shapedlight(temp, strength, f, l, u): mix(threshold(f, l, u), holdout, emission(blackbody(temp), strength))

Shaped Scatter Volume

Image for post
Image for post
shapedscatter(c, d, a, f, l, u): mix(threshold(f, l, u), holdout, scatter(hsv(1.0, 1.0, 1.0, 1.0, c), d, a))

Scripting Nodes With Python

Example 1. In-Bounds

Example 2. Shaped Scatter Volume

Arranging Nodes

Arranging By Type

Arranging By Connected Sockets

Putting It All Together

Image for post
Image for post
Image for post
Image for post

Tiled Ring Pattern

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Generating Swatches

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Conclusion

Creative coder from Wisconsin, USA.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store