Fix crash when hydra render plugin isn't loaded #60

Merged
Bogdan Nagirniak merged 1 commits from BLEN-443 into hydra-render 2023-07-04 10:22:36 +02:00
3 changed files with 28 additions and 14 deletions

View File

@ -63,7 +63,6 @@ class HydraRenderEngine(bpy.types.RenderEngine):
return
_bpy_hydra.engine_free(self.engine_ptr)
del self.engine_ptr
@classmethod
def register(cls):
@ -95,6 +94,8 @@ class HydraRenderEngine(bpy.types.RenderEngine):
def update(self, data, depsgraph):
engine_type = 'PREVIEW' if self.is_preview else 'FINAL'
self.engine_ptr = _bpy_hydra.engine_create(self.as_pointer(), engine_type, self.delegate_id)
if not self.engine_ptr:
return
for key, val in self.get_sync_settings(engine_type).items():
_bpy_hydra.engine_set_sync_setting(self.engine_ptr, key, val)
@ -114,6 +115,8 @@ class HydraRenderEngine(bpy.types.RenderEngine):
def view_update(self, context, depsgraph):
if not self.engine_ptr:
self.engine_ptr = _bpy_hydra.engine_create(self.as_pointer(), 'VIEWPORT', self.delegate_id)
if not self.engine_ptr:
return
for key, val in self.get_sync_settings('VIEWPORT').items():
_bpy_hydra.engine_set_sync_setting(self.engine_ptr, key, val)

View File

@ -23,7 +23,6 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name)
pxr::HdRendererPluginRegistry &registry = pxr::HdRendererPluginRegistry::GetInstance();
pxr::TF_PY_ALLOW_THREADS_IN_SCOPE();
render_delegate_ = registry.CreateRenderDelegate(pxr::TfToken(render_delegate_name));
/* USD has limited support for Vulkan. To make it works USD should be built
* with PXR_ENABLE_VULKAN_SUPPORT=TRUE which is not possible now */
@ -39,6 +38,11 @@ Engine::Engine(RenderEngine *bl_engine, const std::string &render_delegate_name)
hd_drivers.push_back(&hgi_driver_);
}
render_delegate_ = registry.CreateRenderDelegate(pxr::TfToken(render_delegate_name));
if (!render_delegate_) {
throw std::runtime_error("Cannot create render delegate: " + render_delegate_name);
}
render_index_.reset(pxr::HdRenderIndex::New(render_delegate_.Get(), hd_drivers));
free_camera_delegate_ = std::make_unique<pxr::HdxFreeCameraSceneDelegate>(

View File

@ -65,23 +65,30 @@ static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args)
RenderEngine *bl_engine = (RenderEngine *)PyLong_AsVoidPtr(pyengine);
Engine *engine;
if (STREQ(engine_type, "VIEWPORT")) {
engine = new ViewportEngine(bl_engine, render_delegate_id);
}
else if (STREQ(engine_type, "PREVIEW")) {
engine = new PreviewEngine(bl_engine, render_delegate_id);
}
else {
if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) {
engine = new FinalEngineGL(bl_engine, render_delegate_id);
Engine *engine = nullptr;
try {
if (STREQ(engine_type, "VIEWPORT")) {
engine = new ViewportEngine(bl_engine, render_delegate_id);
}
else if (STREQ(engine_type, "PREVIEW")) {
engine = new PreviewEngine(bl_engine, render_delegate_id);
}
else {
engine = new FinalEngine(bl_engine, render_delegate_id);
if (bl_engine->type->flag & RE_USE_GPU_CONTEXT) {
engine = new FinalEngineGL(bl_engine, render_delegate_id);
}
else {
engine = new FinalEngine(bl_engine, render_delegate_id);
}
}
}
catch (std::runtime_error &e) {
CLOG_ERROR(LOG_RENDER_HYDRA, "%s", e.what());
}
CLOG_INFO(LOG_RENDER_HYDRA, 1, "Engine %016llx %s", engine, engine_type);
if (engine) {
CLOG_INFO(LOG_RENDER_HYDRA, 1, "Engine %016llx %s", engine, engine_type);
}
return PyLong_FromVoidPtr(engine);
}