Skip to content

Commit

Permalink
python: Wrap renderer prefs into a _prefs class
Browse files Browse the repository at this point in the history
Now we can just do `renderer.prefs.threads = x`
or `current_cam = renderer.prefs.cam_idx`
  • Loading branch information
vkoskiv committed Dec 4, 2023
1 parent 6d1675a commit 4328b3b
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 33 deletions.
14 changes: 7 additions & 7 deletions include/c-ray/c-ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ struct cr_renderer;
CR_EXPORT struct cr_renderer *cr_new_renderer(void);

enum cr_renderer_param {
// Num
cr_renderer_threads = 0,
cr_renderer_samples,
cr_renderer_bounces,
cr_renderer_tile_width,
cr_renderer_tile_height,

cr_renderer_tile_order,
//TODO: Renderer shouldn't know these
cr_renderer_output_path,
cr_renderer_asset_path,
cr_renderer_output_name,
cr_renderer_output_filetype,
cr_renderer_output_num,
cr_renderer_override_width,
cr_renderer_override_height,
cr_renderer_should_save, //FIXME: Remove
cr_renderer_override_cam,
cr_renderer_node_list,
cr_renderer_is_iterative,
// String
cr_renderer_output_path,
cr_renderer_asset_path,
cr_renderer_output_name,
cr_renderer_output_filetype,
cr_renderer_node_list,
};

enum cr_tile_state {
Expand Down
159 changes: 133 additions & 26 deletions wrappers/c_ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,151 @@ class cr_renderer(ct.Structure):
c_ray.cr_new_renderer.restype = ct.POINTER(cr_renderer)
c_ray.cr_destroy_renderer.argtypes = [ct.POINTER(cr_renderer)]

class cr_rparam(IntEnum):
class _cr_rparam(IntEnum):
threads = 0
samples = 1
bounces = 2
tile_width = 3
tile_height = 4
tile_order = 5
output_path = 6
asset_path = 7
output_name = 8
output_filetype = 9
output_num = 10
override_width = 11
override_height = 12
should_save = 13
override_cam = 14
node_list = 15
is_iterative = 16
output_num = 6
override_width = 7
override_height = 8
should_save = 9
override_cam = 10
is_iterative = 11
output_path = 12
asset_path = 13
output_name = 14
output_filetype = 15
node_list = 16

c_ray.cr_renderer_set_num_pref.restype = ct.c_bool
c_ray.cr_renderer_set_num_pref.argtypes = [ct.POINTER(cr_renderer), ct.c_int, ct.c_uint64]

c_ray.cr_renderer_get_num_pref.restype = ct.c_uint64
c_ray.cr_renderer_get_num_pref.argtypes = [ct.POINTER(cr_renderer), ct.c_int]

def _r_set_num(ptr, param, value):
c_ray.cr_renderer_set_num_pref(ptr, param, value)

def _r_get_num(ptr, param):
return c_ray.cr_renderer_get_num_pref(ptr, param)

class _pref:
def __init__(self, r_ptr):
self.r_ptr = r_ptr

def _get_threads(self):
return _r_get_num(self.r_ptr, _cr_rparam.threads)
def _set_threads(self, value):
_r_set_num(self.r_ptr, _cr_rparam.threads, value)
threads = property(_get_threads, _set_threads, None, "Local thread count, defaults to nproc + 2")

def _get_samples(self):
return _r_get_num(self.r_ptr, _cr_rparam.samples)
def _set_samples(self, value):
_r_set_num(self.r_ptr, _cr_rparam.samples, value)
samples = property(_get_samples, _set_samples, None, "Amount of samples to render")

def _get_bounces(self):
return _r_get_num(self.r_ptr, _cr_rparam.bounces)
def _set_bounces(self, value):
_r_set_num(self.r_ptr, _cr_rparam.bounces, value)
bounces = property(_get_bounces, _set_bounces, None, "Max times a light ray can bounce in the scene")

def _get_tile_x(self):
return _r_get_num(self.r_ptr, _cr_rparam.tile_width)
def _set_tile_x(self, value):
_r_set_num(self.r_ptr, _cr_rparam.tile_width, value)
tile_x = property(_get_tile_x, _set_tile_x, None, "Tile width")

def _get_tile_y(self):
return _r_get_num(self.r_ptr, _cr_rparam.tile_height)
def _set_tile_y(self, value):
_r_set_num(self.r_ptr, _cr_rparam.tile_height, value)
tile_y = property(_get_tile_y, _set_tile_y, None, "Tile height")

def _get_tile_order(self):
return _r_get_num(self.r_ptr, _cr_rparam.tile_order)
def _set_tile_order(self, value):
_r_set_num(self.r_ptr, _cr_rparam.tile_order, value)
tile_order = property(_get_tile_order, _set_tile_order, None, "Order to render tiles in")

def _get_output_idx(self):
return _r_get_num(self.r_ptr, _cr_rparam.output_num)
def _set_output_idx(self, value):
_r_set_num(self.r_ptr, _cr_rparam.output_num, value)
output_idx = property(_get_output_idx, _set_output_idx, None, "Number for output file name")

def _get_img_width(self):
return _r_get_num(self.r_ptr, _cr_rparam.override_width)
def _set_img_width(self, value):
_r_set_num(self.r_ptr, _cr_rparam.override_width, value)
img_width = property(_get_img_width, _set_img_width, None, "Image width in pixels")

def _get_img_height(self):
return _r_get_num(self.r_ptr, _cr_rparam.override_height)
def _set_img_height(self, value):
_r_set_num(self.r_ptr, _cr_rparam.override_height, value)
img_height = property(_get_img_height, _set_img_height, None, "Image height in pixels")

def _get_should_save(self):
return _r_get_num(self.r_ptr, _cr_rparam.should_save)
def _set_should_save(self, value):
_r_set_num(self.r_ptr, _cr_rparam.should_save, value)
should_save = property(_get_should_save, _set_should_save, None, "0 = don't save, 1 = save")

def _get_cam_idx(self):
return _r_get_num(self.r_ptr, _cr_rparam.override_cam)
def _set_cam_idx(self, value):
_r_set_num(self.r_ptr, _cr_rparam.override_cam, value)
cam_idx = property(_get_cam_idx, _set_cam_idx, None, "Select camera")

def _get_is_iterative(self):
return _r_get_num(self.r_ptr, _cr_rparam.is_iterative)
def _set_is_iterative(self, value):
_r_set_num(self.r_ptr, _cr_rparam.is_iterative, value)
is_iterative = property(_get_is_iterative, _set_is_iterative, None, "")

def _get_threads(self):
return _r_get_num(self.r_ptr, _cr_rparam.threads)
def _set_threads(self, value):
_r_set_num(self.r_ptr, _cr_rparam.threads, value)
threads = property(_get_threads, _set_threads, None, "")

def _get_threads(self):
return _r_get_num(self.r_ptr, _cr_rparam.threads)
def _set_threads(self, value):
_r_set_num(self.r_ptr, _cr_rparam.threads, value)
threads = property(_get_threads, _set_threads, None, "")

def _get_threads(self):
return _r_get_num(self.r_ptr, _cr_rparam.threads)
def _set_threads(self, value):
_r_set_num(self.r_ptr, _cr_rparam.threads, value)
threads = property(_get_threads, _set_threads, None, "")

def _get_threads(self):
return _r_get_num(self.r_ptr, _cr_rparam.threads)
def _set_threads(self, value):
_r_set_num(self.r_ptr, _cr_rparam.threads, value)
threads = property(_get_threads, _set_threads, None, "")

def _get_threads(self):
return _r_get_num(self.r_ptr, _cr_rparam.threads)
def _set_threads(self, value):
_r_set_num(self.r_ptr, _cr_rparam.threads, value)
threads = property(_get_threads, _set_threads, None, "")

class Renderer:
def __init__(self):
self.obj_ptr = c_ray.cr_new_renderer()
self.prefs = _pref(self.obj_ptr)

def close(self):
c_ray.cr_destroy_renderer(self.obj_ptr)

def get_threads(self):
return c_ray.cr_renderer_get_num_pref(self.obj_ptr, cr_rparam.threads)
def set_threads(self, value):
c_ray.cr_renderer_set_num_pref(self.obj_ptr, cr_rparam.threads, value)

threads = property(get_threads, set_threads, None, "Local thread count, defaults to nproc + 2")

@classmethod
def from_param(cls, param):
Expand All @@ -73,13 +180,13 @@ def __exit__(self, exc_type, exc_value, traceback):
self.close()

if __name__ == "__main__":
print("Hello, c-ray!");
print("libc-ray version {} ({})".format(cr_get_version().decode(), cr_get_git_hash().decode()))
with Renderer() as r:
print("Initial value {}".format(r.threads))
r.threads = 42
print("Got value {}".format(r.threads))
r.threads = 69
print("Got value {}".format(r.threads))
r.threads = 1234
print("Got value {}".format(r.threads))
print("Initial value {}".format(r.prefs.threads))
r.prefs.threads = 42
print("Got value {}".format(r.prefs.threads))
r.prefs.threads = 69
print("Got value {}".format(r.prefs.threads))
r.prefs.threads = 1234
print("Got value {}".format(r.prefs.threads))
r.prefs.asdf = 1234

0 comments on commit 4328b3b

Please sign in to comment.