Skip to content

Commit

Permalink
SceneParameter: fix update parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
rtabbara committed Aug 14, 2024
1 parent b3a7401 commit 06a4859
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/core/python/object_v.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ MI_PY_EXPORT(Object) {
},
"ptr"_a, "type"_a, "value"_a);

m.def(
"set_property",
[](nb::handle dst, nb::handle src) {
nb::handle h = dst.type();
if (nb::type_check(h)) {
nb::inst_replace_copy(dst, h(src));
return;
}
Throw("set_property(): Target property type isn't a nanobind type!");
},
"dst"_a, "src"_a);

if constexpr (dr::is_array_v<ObjectPtr>) {
dr::ArrayBinding b;
auto object_ptr = dr::bind_array_t<ObjectPtr>(b, m, "ObjectPtr");
Expand Down
15 changes: 14 additions & 1 deletion src/python/python/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ class MyTexture(mi.Texture):

def __init__(self, props):
mi.Texture.__init__(self, props)
self.tex_param_nondiff = mi.Float(3)

def traverse(self, callback):
callback.put_parameter("tex_param_diff", mi.Point3f(1, 1, 1), mi.ParamFlags.Differentiable)
callback.put_parameter("tex_param_scalar", mi.ScalarFloat(2), mi.ParamFlags.NonDifferentiable)
callback.put_parameter("tex_param_nondiff", mi.Float(3), mi.ParamFlags.NonDifferentiable)
callback.put_parameter("tex_param_nondiff", self.tex_param_nondiff, mi.ParamFlags.NonDifferentiable)

def parameters_changed(self, keys):
MyTexture.update_keys = keys
Expand Down Expand Up @@ -173,6 +174,12 @@ def reset_update_keys():
), "Updated parameters should propagate to their parents!"
reset_update_keys()

# Propagate changes to plugin variable
params["bsdf_tex_diff.tex_param_nondiff"] = 0.2
params.update()
assert dr.allclose(bsdf.tex1.tex_param_nondiff, 0.2)
reset_update_keys()

params["bsdf_tex_diff.tex_param_diff"] = mi.Point3f(0, 0, 0)
params.update()
assert (
Expand Down Expand Up @@ -263,6 +270,12 @@ def test04_scene_parameters_update(variants_all_ad_rgb):
assert dr.allclose(params['clearcoat.value'], 0.5)
assert dr.allclose(params['flatness.value'], 0.4)

# Check parameter changes have been applied to plugin
si = dr.zeros(mi.SurfaceInteraction3f)
si.uv = [0.5, 0.5]
assert dr.allclose(bsdf.eval_attribute_1('clearcoat', si), 0.5)
assert dr.allclose(bsdf.eval_attribute_1('flatness', si), 0.4)

assert ret[-1][-1] == {'clearcoat', 'flatness'}


Expand Down
6 changes: 3 additions & 3 deletions src/python/python/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def __setitem__(self, key: str, value):

if value_type is None:
try:
cur.assign(value)
except AttributeError as e:
if "has no attribute 'assign'" in str(e):
self.set_property(cur, value)
except Exception as e:
if "Target property type isn't a nanobind type" in str(e):
mi.Log(
mi.LogLevel.Warn,
f"Parameter '{key}' cannot be modified! This usually "
Expand Down

0 comments on commit 06a4859

Please sign in to comment.