Recreate PreviewEngine when render_delegate_name is changed #36

Merged
Bogdan Nagirniak merged 6 commits from BLEN-404 into hydra-render 2023-05-05 14:49:30 +02:00
7 changed files with 57 additions and 24 deletions
Showing only changes of commit 8bbd49cf52 - Show all commits

View File

@ -38,7 +38,6 @@ __all__ = (
import os import os
import platform import platform
import traceback
from pathlib import Path from pathlib import Path
import bpy import bpy
@ -107,21 +106,14 @@ def export_mtlx(material):
""" Exports material to .mtlx file. It is called from Blender source code. """ """ Exports material to .mtlx file. It is called from Blender source code. """
try: try:
import materialx.utils as mx_utils import materialx.utils as mx_utils
doc = mx_utils.export(material, None)
if not doc:
return ""
mtlx_file = mx_utils.get_temp_file(".mtlx", f"mat_{material.as_pointer():016x}")
mx_utils.export_to_file(doc, mtlx_file, export_deps=True, copy_deps=False)
return str(mtlx_file)
except ImportError: except ImportError:
print("ERROR: no MaterialX addon available") print("ERROR: no MaterialX addon available")
return ""
except: doc = mx_utils.export(material, None)
# This is a placeholder, this code will be moved to C part later if not doc:
# This code shouldn't raise any exception due to breaking refcounts on RenderEngine return ""
traceback.print_exc()
return "" mtlx_file = mx_utils.get_temp_file(".mtlx", f"mat_{material.as_pointer():016x}")
mx_utils.export_to_file(doc, mtlx_file, export_deps=True, copy_deps=False)
return str(mtlx_file)

View File

@ -21,7 +21,7 @@ namespace blender::render::hydra {
extern struct CLG_LogRef *LOG_RENDER_HYDRA_SCENE; /* BSD - Blender Scene Delegate */ extern struct CLG_LogRef *LOG_RENDER_HYDRA_SCENE; /* BSD - Blender Scene Delegate */
class BlenderSceneDelegate : public pxr::HdSceneDelegate { class BlenderSceneDelegate : public pxr::HdSceneDelegate {
friend MeshData; /* has access to materials_*/ friend MeshData; /* has access to materials and instances */
public: public:
enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW }; enum class EngineType { VIEWPORT = 1, FINAL, PREVIEW };

View File

@ -51,9 +51,6 @@ void InstancerData::insert()
{ {
ID_LOG(2, ""); ID_LOG(2, "");
scene_delegate_->GetRenderIndex().InsertInstancer(scene_delegate_, prim_id); scene_delegate_->GetRenderIndex().InsertInstancer(scene_delegate_, prim_id);
for (auto &it : instances_) {
it.second.obj_data->insert();
}
} }
void InstancerData::remove() void InstancerData::remove()
@ -149,6 +146,7 @@ void InstancerData::check_update(Object *object)
} }
ObjectData *obj_data = it->second.obj_data.get(); ObjectData *obj_data = it->second.obj_data.get();
obj_data->update(); obj_data->update();
obj_data->transform = pxr::GfMatrix4d(1.0);
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean; pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
if (object->id.recalc & ID_RECALC_TRANSFORM) { if (object->id.recalc & ID_RECALC_TRANSFORM) {
@ -189,6 +187,13 @@ void InstancerData::available_materials(std::set<pxr::SdfPath> &paths) const
} }
} }
void InstancerData::update_as_parent()
{
set_instances();
scene_delegate_->GetRenderIndex().GetChangeTracker().MarkInstancerDirty(
prim_id, pxr::HdChangeTracker::AllDirty);
}
pxr::SdfPath InstancerData::object_prim_id(Object *object) const pxr::SdfPath InstancerData::object_prim_id(Object *object) const
{ {
/* Making id of object in form like <prefix>_<pointer in 16 hex digits format> */ /* Making id of object in form like <prefix>_<pointer in 16 hex digits format> */

View File

@ -38,6 +38,7 @@ class InstancerData : public ObjectData {
void check_update(Object *object); void check_update(Object *object);
void check_remove(std::set<std::string> &available_objects); void check_remove(std::set<std::string> &available_objects);
void available_materials(std::set<pxr::SdfPath> &paths) const; void available_materials(std::set<pxr::SdfPath> &paths) const;
void update_as_parent();
private: private:
pxr::SdfPath object_prim_id(Object *object) const; pxr::SdfPath object_prim_id(Object *object) const;

View File

@ -68,8 +68,28 @@ void MaterialData::init()
Py_DECREF(result); Py_DECREF(result);
} }
else { else {
CLOG_ERROR(LOG_RENDER_HYDRA_SCENE, "Export error for %s", id->name); /* Clearing and logging exception data */
PyErr_Print(); PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyErr_NormalizeException(&type, &value, &traceback);
std::string err_str = ((PyTypeObject *)type)->tp_name;
if (value) {
PyObject *pstr = PyObject_Str(value);
err_str += ": ";
err_str += PyUnicode_AsUTF8(pstr);
Py_DECREF(pstr);
}
CLOG_ERROR(LOG_RENDER_HYDRA_SCENE,
"Export error for %s (%s): %s",
prim_id.GetText(),
id->name,
err_str.c_str());
if (traceback) {
PyTraceBack_Print(traceback, PySys_GetObject("stderr"));
}
Py_XDECREF(traceback);
Py_XDECREF(value);
Py_DECREF(type);
} }
Py_DECREF(module); Py_DECREF(module);

View File

@ -17,7 +17,7 @@ namespace blender::render::hydra {
MeshData::MeshData(BlenderSceneDelegate *scene_delegate, MeshData::MeshData(BlenderSceneDelegate *scene_delegate,
Object *object, Object *object,
pxr::SdfPath const &prim_id) pxr::SdfPath const &prim_id)
: ObjectData(scene_delegate, object, prim_id) : ObjectData(scene_delegate, object, prim_id), parent_(object->parent)
{ {
} }
@ -65,8 +65,22 @@ void MeshData::remove()
void MeshData::update() void MeshData::update()
{ {
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
Object *object = (Object *)id; Object *object = (Object *)id;
if (parent_ != object->parent) {
parent_ = object->parent;
/* Looking for corresponded instancer and update it as parent */
for (Object *ob = parent_; ob != nullptr; ob = ob->parent) {
pxr::SdfPath i_id = scene_delegate_->instancer_prim_id(ob);
InstancerData *i_data = scene_delegate_->instancer_data(i_id);
if (i_data) {
i_data->update_as_parent();
break;
}
}
}
pxr::HdDirtyBits bits = pxr::HdChangeTracker::Clean;
if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) { if ((id->recalc & ID_RECALC_GEOMETRY) || (((ID *)object->data)->recalc & ID_RECALC_GEOMETRY)) {
init(); init();
bits = pxr::HdChangeTracker::AllDirty; bits = pxr::HdChangeTracker::AllDirty;

View File

@ -29,7 +29,7 @@ class MeshData : public ObjectData {
pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const; pxr::HdPrimvarDescriptorVector primvar_descriptors(pxr::HdInterpolation interpolation) const;
pxr::SdfPath material_id() const; pxr::SdfPath material_id() const;
protected: private:
void write_mesh(Mesh *mesh); void write_mesh(Mesh *mesh);
void write_material(); void write_material();
void write_uv_maps(Mesh *mesh); void write_uv_maps(Mesh *mesh);
@ -43,6 +43,7 @@ class MeshData : public ObjectData {
pxr::VtMatrix4dArray instances_; pxr::VtMatrix4dArray instances_;
MaterialData *mat_data_ = nullptr; MaterialData *mat_data_ = nullptr;
Object *parent_;
}; };
} // namespace blender::render::hydra } // namespace blender::render::hydra