Skip to content

Commit

Permalink
lib+blender: Various fixes to make material converting more robust
Browse files Browse the repository at this point in the history
On the lib side, I detect and emit a warning if a shader buffer is empty
(which shouldn't happen, but can if the API is used 'incorrectly').
I also patch in a null material to make the renderer happy, since it
assumes that there is at least one material there.
On the blender side, I now convert the material trees once, instead of
repeating the conversion for every mesh, since meshes often share
materials.
  • Loading branch information
vkoskiv committed Dec 18, 2023
1 parent 405bf37 commit 00d9e30
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions bindings/blender_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ def sync_scene(self, renderer, depsgraph, b_scene):
else:
cr_cam.set_param(c_ray.cam_param.focus_distance, bl_cam.dof.focus_distance)

cr_materials = {}
for bl_mat in bpy.data.materials:
print("Converting {}".format(bl_mat.name))
cr_materials[bl_mat.name] = convert_node_tree(depsgraph, bl_mat, bl_mat.node_tree)

# Sync meshes
for idx, ob_main in enumerate(objects):
if ob_main.type != 'MESH':
Expand All @@ -209,12 +214,15 @@ def sync_scene(self, renderer, depsgraph, b_scene):
continue
# FIXME: Parse & convert these to an array before parsing meshes, then pick from there
# We're doing duplicate work for many materials
if len(me.materials) < 1:
cr_mat_set.add(None)
for bl_mat in me.materials:
if not bl_mat:
print("WTF, array contains NoneType?")
cr_mat_set.add(None)
elif bl_mat.use_nodes:
print("Converting material {}".format(bl_mat.name))
cr_mat_set.add(convert_node_tree(depsgraph, bl_mat, bl_mat.node_tree))
print("Fetching material {}".format(bl_mat.name))
cr_mat_set.add(cr_materials[bl_mat.name])
else:
print("Material {} doesn't use nodes, do something about that".format(bl_mat.name))
cr_mat_set.add(None)
Expand Down
3 changes: 3 additions & 0 deletions bindings/nodes/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def convert_background(nt):


def convert_node_tree(bl_depsgraph, mat, nt):
if not nt:
print("No node tree in material {}, bailing out".format(mat.name))
return None
if len(nt.nodes) < 1:
print("No nodes found for material {}, bailing out".format(mat.name))
return None
Expand Down
6 changes: 6 additions & 0 deletions src/lib/renderer/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ struct cr_bitmap *renderer_render(struct renderer *r) {

struct tile_set set = tile_quantize(camera.width, camera.height, r->prefs.tileWidth, r->prefs.tileHeight, r->prefs.tileOrder);

for (size_t i = 0; i < r->scene->shader_buffers.count; ++i) {
if (!r->scene->shader_buffers.items[i].bsdfs.count) {
logr(warning, "bsdf buffer %zu is empty, patching in placeholder\n", i);
bsdf_node_ptr_arr_add(&r->scene->shader_buffers.items[i].bsdfs, build_bsdf_node((struct cr_scene *)r->scene, NULL));
}
}
// Bind object buffers to instances
for (size_t i = 0; i < r->scene->instances.count; ++i) {
struct instance *inst = &r->scene->instances.items[i];
Expand Down

0 comments on commit 00d9e30

Please sign in to comment.