Skip to content

Commit

Permalink
lib+blender: Tiny cleanups
Browse files Browse the repository at this point in the history
Just revisiting this again after a while of being away, tidying up some
things.
  • Loading branch information
vkoskiv committed Jun 18, 2024
1 parent c1bd15e commit ece6d60
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
20 changes: 11 additions & 9 deletions bindings/blender_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ class CrayRender(bpy.types.RenderEngine):
bl_label = "c-ray for Blender"
bl_use_preview = True
bl_use_shading_nodes_custom = False
cr_interactive_running = False
cr_renderer = None
old_mtx = None
old_dims = None
Expand All @@ -205,14 +204,14 @@ class CrayRender(bpy.types.RenderEngine):
def __init__(self):
print("c-ray initialized")
self.cr_scene = None
self.cr_interactive_running = False
c_ray.log_level_set(c_ray.log_level.Debug)

def __del__(self):
if self.cr_interactive_running:
self.cr_renderer.stop()
self.cr_interactive_running = False
# FIXME: This never gets called when leaving rendered viewport shading mode
# which ends up leaving zombie instances of c-ray running in the background
if self.cr_renderer:
if self.cr_renderer.interactive:
self.cr_renderer.stop()
self.cr_renderer.close()
print("c-ray deleted")

Expand Down Expand Up @@ -262,7 +261,7 @@ def sync_scene(self, depsgraph):
else:
cr_cam.opts.focus_distance = bl_cam.dof.focus_distance

# Convert materials
# Convert Cycles materials into c-ray node graphs
cr_materials = {}
for bl_mat in bpy.data.materials:
print("Converting {}".format(bl_mat.name))
Expand Down Expand Up @@ -302,7 +301,7 @@ def sync_scene(self, depsgraph):
cr_mat_set.add(None)
for bl_mat in me.materials:
if not bl_mat:
print("WTF, array contains NoneType?")
print("Huh, array contains NoneType?")
cr_mat_set.add(None)
elif bl_mat.use_nodes:
print("Fetching material {}".format(bl_mat.name))
Expand All @@ -314,6 +313,7 @@ def sync_scene(self, depsgraph):
else:
print("Material {} doesn't use nodes, do something about that".format(bl_mat.name))
cr_mat_set.add(None)
# c-ray only supports triangles
mesh_triangulate(me)
verts = me.vertices[:]
# me.calc_normals_split()
Expand Down Expand Up @@ -357,6 +357,7 @@ def render(self, depsgraph):
bm = self.cr_renderer.get_result()
self.display_bitmap(bm)

# This is still very buggy, don't resize the viewport
def view_draw(self, context, depsgraph):
zoom = context.region_data.view_camera_zoom
mtx = context.region_data.view_matrix.inverted()
Expand All @@ -382,11 +383,13 @@ def view_draw(self, context, depsgraph):
def partial_update_mesh(self, update):
mesh = update.id
print("Mesh {} was updated".format(mesh.name))
# For now, we only handle transforms.
if update.is_updated_transform:
# FIXME: How do I get the actual instance index from Blender?
# Just grabbing the first one for now.
inst = self.cr_scene.meshes[mesh.name].instances[-1]
inst.set_transform(to_cr_matrix(mesh.matrix_world))

def partial_update(self, updates):
for update in updates:
# Kind of annoying that seemingly every update has 'type', except if it's an
Expand Down Expand Up @@ -440,13 +443,12 @@ def view_update(self, context, depsgraph):
cr_cam.opts.res_y = context.region.height
cr_cam.opts.blender_coord = 1

if self.cr_interactive_running == True:
if self.cr_renderer.interactive == True:
self.cr_renderer.restart()
else:
print("Kicking off background renderer")
self.cr_renderer.callbacks.on_interactive_pass_finished = (status_update_interactive, (self.tag_redraw, self.update_stats, self))
self.cr_renderer.start_interactive()
self.cr_interactive_running = True

def display_bitmap(self, bm):
# Get float array from libc-ray
Expand Down
18 changes: 2 additions & 16 deletions bindings/c_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ def __init__(self, scene_ptr, object, type):
self.cr_idx = _lib.instance_new(self.scene_ptr, self.object.cr_idx, self.type)

def set_transform(self, matrix):
if self.matrix == matrix:
return
self.matrix = matrix
_lib.instance_set_transform(self.scene_ptr, self.cr_idx, self.matrix)

Expand Down Expand Up @@ -458,22 +460,6 @@ def set_background(self, material):
def vertex_buf_new(self, v, vn, n, nn, t, tn):
return vertex_buf(self.cr_ptr, v, vn, n, nn, t, tn)

class cr_cb_info(ct.Structure):
_fields_ = [
("fb", ct.POINTER(cr_bitmap)),
("tiles", ct.POINTER(ct.c_void_p)), # TODO
("tiles_count", ct.c_size_t),
("active_threads", ct.c_size_t),
("avg_per_ray_us", ct.c_double),
("samples_per_sec", ct.c_int64),
("eta_ms", ct.c_int64),
("finished_passes", ct.c_size_t),
("completion", ct.c_double),
("paused", ct.c_bool),
]

cr_cb_func = ct.CFUNCTYPE(ct.c_void_p, ct.POINTER(cr_cb_info), ct.POINTER(ct.c_void_p))

class renderer:
def __init__(self, path = None):
self.obj_ptr = _lib.new_renderer()
Expand Down

0 comments on commit ece6d60

Please sign in to comment.