Skip to content

Commit

Permalink
blender: Start implementing node support in add-on
Browse files Browse the repository at this point in the history
Not really 100% sure how I'll approach it in the end, but I'm just
sketching out the ctypes boilerplate for now.
  • Loading branch information
vkoskiv committed Dec 13, 2023
1 parent dbb790e commit c20251c
Show file tree
Hide file tree
Showing 5 changed files with 389 additions and 17 deletions.
29 changes: 12 additions & 17 deletions bindings/c_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from enum import IntEnum

from . import cray_wrap as _lib
import nodes.shader
import nodes.vector.cr_vector

class cr_renderer(ct.Structure):
pass
Expand Down Expand Up @@ -225,16 +227,6 @@ def set_param(self, param, value):
# Weird. Could just do this internally, no?
_lib.camera_update(self.scene_ptr, self.cr_idx)

class material_set:
def __init__(self, scene_ptr):
self.scene_ptr = scene_ptr
self.materials = []
self.cr_idx = _lib.scene_new_material_set(self.scene_ptr)

def add(self, material):
self.materials.append(material)
_lib.material_set_add(self.scene_ptr, self.cr_idx, material)

def inst_type(IntEnum):
mesh = 0
sphere = 1
Expand Down Expand Up @@ -263,13 +255,6 @@ def transform(self, matrix):
def bind_materials(self, material_set):
_lib.instance_bind_material_set(self.scene_ptr, self.cr_idx, material_set.cr_idx)

class cr_vector(ct.Structure):
_fields_ = [
("x", ct.c_float),
("y", ct.c_float),
("z", ct.c_float),
]

class cr_coord(ct.Structure):
_fields_ = [
("u", ct.c_float),
Expand All @@ -287,6 +272,16 @@ def __init__(self, scene_ptr, v, vn, n, nn, t, tn):
self.tn = tn
self.cr_idx = _lib.scene_vertex_buf_new(self.cr_ptr, self.v, self.vn, self.n, self.nn, self.t, self.tn)

class material_set:
def __init__(self, scene_ptr):
self.scene_ptr = scene_ptr
self.materials = []
self.cr_idx = _lib.scene_new_material_set(self.scene_ptr)

def add(self, material):
self.materials.append(material)
_lib.material_set_add(self.scene_ptr, self.cr_idx, material)

class scene:
def __init__(self, scene_ptr):
self.cr_ptr = scene_ptr
Expand Down
90 changes: 90 additions & 0 deletions bindings/nodes/color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import ctypes as ct
from vector import _vector
from value import _value

class _color_type(IntEnum):
unknown = 0
constant = 1
image = 2
checkerboard = 3
blackbody = 4
split = 5
rgb = 6
hsl = 7
vec_to_color = 8
gradient = 9

class _color(ct.Structure):
_fields_ = [
("type", _color_type)
("arg", _color_arg)
]

class cr_color(ct.Structure):
_fields_ = [
("r", ct.c_float),
("g", ct.c_float),
("b", ct.c_float),
("a", ct.c_float),
]

class _color_arg(ct.Union):
_fields_ = [
("constant", cr_color),
("image", _color_arg_image),
("checkerboard", _color_arg_checkerboard),
("blackbody", _color_arg_blackbody),
("split", _color_arg_split),
("rgb", _color_arg_rgb),
("hsl", _color_arg_hsl),
("vec_to_color", _color_arg_vec_to_color),
("gradient", _color_arg_gradient),
]

class _color_arg_image(ct.Structure):
_fields_ = [
("full_path", ct.c_char_p),
("options", ct.c_uint8)
]

class _color_arg_checkerboard(ct.Structure):
_fields_ = [
("a", ct.POINTER(_color)),
("b", ct.POINTER(_color)),
("scale", ct.POINTER(_value))
]

class _color_arg_blackbody(ct.Structure):
_fields_ = [
("degrees", ct.POINTER(_value))
]

class _color_arg_split(ct.Structure):
_fields_ = [
("value", ct.POINTER(_value))
]

class _color_arg_rgb(ct.Structure):
_fields_ = [
("red", ct.POINTER(_value)),
("green", ct.POINTER(_value)),
("blue", ct.POINTER(_value)),
]

class _color_arg_hsl(ct.Structure):
_fields_ = [
("H", ct.POINTER(_value)),
("S", ct.POINTER(_value)),
("L", ct.POINTER(_value)),
]

class _color_arg_vec_to_color(ct.Structure):
_fields_ = [
("vec", ct.POINTER(_vector))
]

class _color_arg_gradient(ct.Structure):
_fields_ = [
("a", ct.POINTER(_color)),
("b", ct.POINTER(_color)),
]
88 changes: 88 additions & 0 deletions bindings/nodes/shader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import ctypes as ct
from color import _color
from value import _value
from vector import _vector

class _shader_type(IntEnum):
unknown = 0
diffuse = 1
metal = 2
glass = 3
plastic = 4
mix = 5
add = 6
transparent = 7
emissive = 8
translucent = 9
background = 10

class _shader(ct.Structure):
_fields_ = [
("type", _shader_type),
("arg", _shader_arg)
]

class _shader_arg(ct.Union):
_fields_ = [
("diffuse", _shader_arg_diffuse)
]

class _shader_arg_diffuse(ct.Structure):
_fields_ = [
("color", ct.POINTER(_color))
]

class _shader_arg_metal(ct.Structure):
_fields_ = [
("color", ct.POINTER(_color)),
("roughness", ct.POINTER(_value)),
]

class _shader_arg_plastic(ct.Structure):
_fields_ = [
("color", ct.POINTER(_color)),
("roughness", ct.POINTER(_value)),
("IOR", ct.POINTER(_value)),
]

class _shader_arg_mix(ct.Structure):
_fields_ = [
("A", ct.POINTER(_shader)),
("B", ct.POINTER(_shader)),
("factor", ct.POINTER(_value))
]

class _shader_arg_add(ct.Structure):
_fields_ = [
("A", ct.POINTER(_shader)),
("B", ct.POINTER(_shader)),
]

class _shader_arg_transparent(ct.Structure):
_fields_ = [
("color", ct.POINTER(_color))
]

class _shader_arg_emissive(ct.Structure):
_fields_ = [
("color", ct.POINTER(_color)),
("strength", ct.POINTER(_value))
]

class _shader_arg_translucent(ct.Structure):
_fields_ = [
("color", ct.POINTER(_color))
]

class _shader_arg_background(ct.Structure):
_fields_ = [
("color", ct.POINTER(_color)),
("pose", ct.POINTER(_vector)),
("strength", ct.POINTER(_value))
]

class _shader_arg_diffuse(ct.Structure):
_fields_ = [
("color", ct.POINTER(_color))
]

118 changes: 118 additions & 0 deletions bindings/nodes/value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import ctypes as ct
from vector import _vector
from color import _color

class _value_type(IntEnum):
unknown = 1
constant = 2
fresnel = 3
map_range = 4
raylength = 5
alpha = 6
vec_to_value = 7
math = 8
grayscale = 9

class _value(ct.Structure):
_fields_ = [
("type", _value_type)
("arg", _value_arg)
]

class _value_arg(ct.Union):
_fields_ = [
("constant", ct.c_double),
("fresnel", _value_arg_fresnel),
("map_range", _value_arg_map_range)
]

class _value_arg_fresnel(ct.Structure):
_fields_ = [
("IOR", ct.POINTER(_value)),
("normal", ct.POINTER(_vector))
]

class _value_arg_map_range(ct.Structure):
_fields_ = [
("input_value", ct.POINTER(_value)),
("from_min", ct.POINTER(_value)),
("from_max", ct.POINTER(_value)),
("to_min", ct.POINTER(_value)),
("to_max", ct.POINTER(_value)),
]

class _value_arg_alpha(ct.Structure):
_fields_ = [
("color", ct.POINTER(_color))
]

class _component(IntEnum):
X = 0
Y = 1
Z = 3
U = 4
V = 5
F = 6

class _value_arg_vec_to_value(ct.Structure):
_fields_ = [
("comp", _component),
("vec", ct.POINTER(_vector))
]

# These are ripped off here:
# https:#docs.blender.org/manual/en/latest/render/shader_nodes/converter/math.html
# TODO: Commented ones need to be implemented to reach parity with Cycles. Feel free to do so! :^)
class _math_op(IntEnum):
Add = 0
Subtract = 1
Multiply = 2
Divide = 3
#MultiplyAdd =
Power = 4
Log = 5
SquareRoot = 6
InvSquareRoot = 7
Absolute = 8
#Exponent =
Min = 9
Max = 10
LessThan = 11
GreaterThan = 12
Sign = 13
Compare = 14
#SmoothMin =
#SmoothMax =
Round = 15
Floor = 16
Ceil = 17
Truncate = 18
Fraction = 19
Modulo = 20
#Wrap =
#Snap =
#PingPong =
Sine = 21
Cosine = 22
Tangent = 23
#ArcSine =
#ArcCosine =
#ArcTangent =
#ArcTan2 =
#HyperbolicSine =
#HyperbolicCosine =
#HyperbolicTangent =
ToRadians = 24
ToDegrees = 25

class _value_arg_math(ct.Structure):
_fields_ = [
("A", ct.POINTER(_value)),
("B", ct.POINTER(_value)),
("op", _math_op)
]

class _value_arg_grayscale(ct.Structure):
_fields_ = [
("color", ct.POINTER(_color))
]
Loading

0 comments on commit c20251c

Please sign in to comment.