Skip to content

Commit

Permalink
blender: Hook up background shader
Browse files Browse the repository at this point in the history
Blender 3.6 started crashing in the blender UI code after these changes,
no idea why, I'll go test on 4.0 on my desktop.
  • Loading branch information
vkoskiv committed Dec 16, 2023
1 parent b0fc7fa commit 19ab99f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions bindings/blender_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ def sync_scene(self, renderer, depsgraph, b_scene):
facebuf = (c_ray.cr_face * len(faces))(*faces)
cr_mesh.bind_faces(bytearray(facebuf), len(faces))
cr_mesh.bind_vertex_buf(cr_vertex_buf(cr_scene, me))

# Set background shader
bl_nodetree = bpy.data.worlds[0].node_tree
cr_scene.set_background(convert_background(bl_nodetree))
return cr_scene

def render(self, depsgraph):
Expand Down
9 changes: 8 additions & 1 deletion bindings/c_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,14 @@ def material_set_new(self):
def instance_new(self, object, type):
return instance(self.cr_ptr, object, type)
def set_background(self, material):
return _lib.scene_set_background(self.cr_ptr, material)
if material is None:
return _lib.scene_set_background(self.cr_ptr, material)
self.background = material
ct.pythonapi.PyCapsule_New.argtypes = [ct.c_void_p, ct.c_char_p, ct.c_void_p]
ct.pythonapi.PyCapsule_New.restype = ct.py_object
name = b'cray.shader_node'
capsule = ct.pythonapi.PyCapsule_New(ct.byref(material.cr_struct), name, None)
return _lib.scene_set_background(self.cr_ptr, capsule)
def vertex_buf_new(self, v, vn, n, nn, t, tn):
return vertex_buf(self.cr_ptr, v, vn, n, nn, t, tn)

Expand Down
8 changes: 6 additions & 2 deletions bindings/cray_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,12 @@ static PyObject *py_cr_scene_set_background(PyObject *self, PyObject *args) {
return NULL;
}
struct cr_scene *s = PyCapsule_GetPointer(s_ext, "cray.cr_scene");
// TODO
bool ret = cr_scene_set_background(s, NULL);
if (!PyCapsule_IsValid(node_desc, "cray.shader_node")) {
cr_scene_set_background(s, NULL);
Py_RETURN_NONE;
}
struct cr_shader_node *desc = PyCapsule_GetPointer(node_desc, "cray.shader_node");
bool ret = cr_scene_set_background(s, desc);
return PyBool_FromLong(ret);
}

Expand Down
24 changes: 22 additions & 2 deletions bindings/nodes/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
from . value import *
from . vector import *

def convert_background(nt):
if len(nt.nodes) < 1:
print("No nodes found for background, bailing out")
return None
if not 'World Output' in nt.nodes:
print("No World Output node found in tree for background, bailing out")
return None
root = nt.nodes['World Output']
return parse_node(root.inputs['Surface'].links[0].from_node)


def convert_node_tree(bl_depsgraph, mat, nt):
if len(nt.nodes) < 1:
print("No nodes found for material {}, bailing out".format(mat.name))
Expand All @@ -27,8 +38,11 @@ def parse_color(input):
return NodeColorConstant(cr_color(vals[0], vals[1], vals[2], vals[3]))
# case 'ShaderNodeTexImage':
# return warning_color
# case 'ShaderNodeTexChecker':
# return warning_color
case 'ShaderNodeTexChecker':
color1 = parse_color(input.inputs['Color1'])
color2 = parse_color(input.inputs['Color2'])
scale = parse_value(input.inputs['Scale'])
return NodeColorCheckerboard(color1, color2, scale)
# case 'ShaderNodeBlackbody':
# return warning_color
# case 'ShaderNodeCombineRGB':
Expand Down Expand Up @@ -85,6 +99,12 @@ def parse_node(input):
case 'ShaderNodeBsdfTranslucent':
color = parse_color(input.inputs['Color'])
return NodeShaderTranslucent(color)
case 'ShaderNodeBackground':
color = parse_color(input.inputs['Color'])
# Blender doesn't specify the pose here
pose = NodeVectorConstant(cr_vector(0.0, 0.0, 0.0))
strength = parse_value(input.inputs['Strength'])
return NodeShaderBackground(color, pose, strength)
case _:
print("Unknown shader node of type {}, maybe fix.".format(input.bl_idname))
return warning_shader
Expand Down

0 comments on commit 19ab99f

Please sign in to comment.