Blender V5.3
usd_hook.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "usd_hook.hh"
6
7#include "usd.hh"
8#include "usd_api_hook.hh"
9#include "usd_asset_utils.hh"
11#include "usd_reader_prim.hh"
12#include "usd_reader_stage.hh"
14
15#include "BLI_map.hh"
16#include "BLI_utildefines.hh"
17#include "BLI_vector.hh"
18
19#include "BKE_lib_id.hh"
20#include "BKE_report.hh"
21
22#include "DNA_material_types.h"
23#include "DNA_object_types.h"
24
25#include "RNA_access.hh"
26#include "RNA_prototypes.hh"
27#include "RNA_types.hh"
28#include "bpy_rna.hh"
29
30#include <list>
31#include <memory>
32#include <string>
33
34#include <pxr/external/boost/python/call_method.hpp>
35#include <pxr/external/boost/python/class.hpp>
36#include <pxr/external/boost/python/dict.hpp>
37#include <pxr/external/boost/python/import.hpp>
38#include <pxr/external/boost/python/list.hpp>
39#include <pxr/external/boost/python/ref.hpp>
40#include <pxr/external/boost/python/return_value_policy.hpp>
41#include <pxr/external/boost/python/to_python_converter.hpp>
42#include <pxr/external/boost/python/tuple.hpp>
43
44namespace blender {
45
46using namespace pxr::pxr_boost;
47
48namespace io::usd {
49
50using USDHookList = std::list<std::unique_ptr<USDHook>>;
52
53/* USD hook type declarations */
55{
56 static USDHookList hooks{};
57 return hooks;
58}
59
60void USD_register_hook(std::unique_ptr<USDHook> hook)
61{
62 if (USD_find_hook_name(hook->idname)) {
63 /* The hook is already in the list. */
64 return;
65 }
66
67 /* Add hook type to the list. */
68 hook_list().push_back(std::move(hook));
69}
70
72{
73 hook_list().remove_if(
74 [hook](const std::unique_ptr<USDHook> &item) { return item.get() == hook; });
75}
76
77USDHook *USD_find_hook_name(const char idname[])
78{
79 /* sanity checks */
80 if (hook_list().empty() || (idname == nullptr) || (idname[0] == 0)) {
81 return nullptr;
82 }
83
84 USDHookList::iterator hook_iter = std::find_if(
85 hook_list().begin(), hook_list().end(), [idname](const std::unique_ptr<USDHook> &item) {
86 return STREQ(item->idname, idname);
87 });
88
89 return (hook_iter == hook_list().end()) ? nullptr : hook_iter->get();
90}
91
92/* Convert PointerRNA to a PyObject*. */
94
95 /* We pass the argument by value because we need
96 * to obtain a non-const pointer to it. */
97 static PyObject *convert(PointerRNA ptr)
98 {
99 return pyrna_struct_CreatePyObject(&ptr);
100 }
101};
102
103/* Encapsulate arguments for scene export. */
105 private:
106 pxr::UsdStageRefPtr stage_;
107 PointerRNA depsgraph_ptr_;
108 const USDHierarchyIterator *hierarchy_iterator_ = nullptr;
109
110 public:
111 USDSceneExportContext(const USDHierarchyIterator *iter, Depsgraph *depsgraph)
112 : stage_(iter->get_stage()), hierarchy_iterator_(iter)
113 {
114 depsgraph_ptr_ = RNA_pointer_create_discrete(nullptr, RNA_Depsgraph, depsgraph);
115 }
116
117 pxr::UsdStageRefPtr get_stage() const
118 {
119 return stage_;
120 }
121
123 {
124 return depsgraph_ptr_;
125 }
126
127 python::dict get_prim_map() const
128 {
129 python::dict result;
130
131 const auto &exported_prim_map = hierarchy_iterator_->get_exported_prim_map();
132 exported_prim_map.foreach_item([&](const pxr::SdfPath &path, const Vector<ID *> &ids) {
133 python::list id_list;
134 for (ID *id : ids) {
135 if (id) {
136 PointerRNA ptr_rna = RNA_id_pointer_create(id);
137 id_list.append(ptr_rna);
138 }
139 }
140 result[path] = id_list;
141 });
142 return result;
143 }
144};
145
146/* Encapsulate arguments for scene import. */
148 private:
149 pxr::UsdStageRefPtr stage_;
150 ImportedPrimMap prim_map_;
151 python::dict *prim_map_dict_ = nullptr;
152
153 public:
154 USDSceneImportContext(pxr::UsdStageRefPtr in_stage, const ImportedPrimMap &in_prim_map)
155 : stage_(in_stage), prim_map_(in_prim_map)
156 {
157 }
158
159 void release()
160 {
161 delete prim_map_dict_;
162 }
163
164 pxr::UsdStageRefPtr get_stage() const
165 {
166 return stage_;
167 }
168
169 python::dict get_prim_map()
170 {
171 if (!prim_map_dict_) {
172 prim_map_dict_ = new python::dict;
173
174 prim_map_.foreach_item([&](const pxr::SdfPath &path, const Vector<PointerRNA> &ids) {
175 if (!prim_map_dict_->has_key(path)) {
176 (*prim_map_dict_)[path] = python::list();
177 }
178
179 python::list list = python::extract<python::list>((*prim_map_dict_)[path]);
180 for (const auto &ptr_rna : ids) {
181 list.append(ptr_rna);
182 }
183 });
184 }
185
186 return *prim_map_dict_;
187 }
188};
189
190/* Encapsulate arguments for material export. */
192 private:
193 pxr::UsdStageRefPtr stage_;
194 USDExportParams params_ = {};
195 ReportList *reports_ = nullptr;
196
197 public:
198 USDMaterialExportContext(pxr::UsdStageRefPtr stage,
199 const USDExportParams &params,
200 ReportList *reports)
201 : stage_(stage), params_(params), reports_(reports)
202 {
203 }
204
205 pxr::UsdStageRefPtr get_stage() const
206 {
207 return stage_;
208 }
209
215 std::string export_texture(python::object obj) const
216 {
217 ID *id;
218 if (!pyrna_id_FromPyObject(obj.ptr(), &id)) {
219 return "";
220 }
221
222 if (!id) {
223 return "";
224 }
225
226 if (GS(id->name) != ID_IM) {
227 return "";
228 }
229
230 Image *ima = reinterpret_cast<Image *>(id);
231
232 std::string asset_path = get_tex_image_asset_filepath(ima, stage_, params_);
233
234 if (params_.export_textures) {
235 io::usd::export_texture(ima, stage_, params_.overwrite_textures, reports_);
236 }
237
238 return asset_path;
239 }
240};
241
242/* Encapsulate arguments for material import. */
244 private:
245 pxr::UsdStageRefPtr stage_;
246 USDImportParams params_ = {};
247 ReportList *reports_ = nullptr;
248
249 public:
250 USDMaterialImportContext(pxr::UsdStageRefPtr stage,
251 const USDImportParams &params,
252 ReportList *reports)
253 : stage_(stage), params_(params), reports_(reports)
254 {
255 }
256
257 pxr::UsdStageRefPtr get_stage() const
258 {
259 return stage_;
260 }
261
270 python::tuple import_texture(const std::string &asset_path) const
271 {
272 if (!should_import_asset(asset_path)) {
273 /* This path does not need to be imported, so return it unchanged. */
274 return python::make_tuple(asset_path, false);
275 }
276
277 const char *textures_dir = params_.import_textures_mode == TexImportMode::Pack ?
279 params_.import_textures_dir;
280
281 const TexNameCollisionMode name_collision_mode = params_.import_textures_mode ==
284 params_.tex_name_collision_mode;
285
286 std::string import_path = import_asset(
287 asset_path, textures_dir, name_collision_mode, reports_);
288
289 if (import_path == asset_path) {
290 /* Path is unchanged. */
291 return python::make_tuple(asset_path, false);
292 }
293
294 const bool is_temporary = params_.import_textures_mode == TexImportMode::Pack;
295 return python::make_tuple(import_path, is_temporary);
296 }
297};
298
300{
301 static bool registered = false;
302
303 /* No need to register if there are no hooks. */
304 if (hook_list().empty()) {
305 return;
306 }
307
308 if (registered) {
309 return;
310 }
311
312 registered = true;
313
314 PyGILState_STATE gilstate = PyGILState_Ensure();
315
316 /* We must import these modules for the USD type converters to work. */
317 python::import("pxr.Usd");
318 python::import("pxr.UsdShade");
319
320 /* Register converter from PointerRNA to a PyObject*. */
321 python::to_python_converter<PointerRNA, PointerRNAToPython>();
322
323 /* Register context class converters. */
324 python::class_<USDSceneExportContext>("USDSceneExportContext", python::no_init)
325 .def("get_stage", &USDSceneExportContext::get_stage)
326 .def("get_depsgraph",
328 python::return_value_policy<python::return_by_value>())
329 .def("get_prim_map", &USDSceneExportContext::get_prim_map);
330
331 python::class_<USDMaterialExportContext>("USDMaterialExportContext", python::no_init)
332 .def("get_stage", &USDMaterialExportContext::get_stage)
333 .def("export_texture", &USDMaterialExportContext::export_texture);
334
335 python::class_<USDSceneImportContext>("USDSceneImportContext", python::no_init)
336 .def("get_stage", &USDSceneImportContext::get_stage)
337 .def("get_prim_map", &USDSceneImportContext::get_prim_map);
338
339 python::class_<USDMaterialImportContext>("USDMaterialImportContext", python::no_init)
340 .def("get_stage", &USDMaterialImportContext::get_stage)
341 .def("import_texture", &USDMaterialImportContext::import_texture);
342
343 PyGILState_Release(gilstate);
344}
345
346/* Retrieve and report the current Python error. */
347static void handle_python_error(USDHook *hook, ReportList *reports)
348{
349 if (!PyErr_Occurred()) {
350 return;
351 }
352
353 PyErr_Print();
354
355 BKE_reportf(reports,
356 RPT_ERROR,
357 "An exception occurred invoking USD hook '%s'. Please see the console for details",
358 hook->name);
359}
360
361/* Abstract base class to facilitate calling a function with a given
362 * signature defined by the registered USDHook classes. Subclasses
363 * override virtual methods to specify the hook function name and to
364 * call the hook with the required arguments.
365 */
367 private:
368 ReportList *reports_;
369
370 public:
371 explicit USDHookInvoker(ReportList *reports) : reports_(reports) {}
372 virtual ~USDHookInvoker() = default;
373
374 /* Attempt to call the function, if defined by the registered hooks. */
375 void call()
376 {
377 if (hook_list().empty()) {
378 return;
379 }
380
381 PyGILState_STATE gilstate = PyGILState_Ensure();
382 init_in_gil();
383
384 /* Iterate over the hooks and invoke the hook function, if it's defined. */
385 USDHookList::const_iterator hook_iter = hook_list().begin();
386 while (hook_iter != hook_list().end()) {
387
388 /* XXX: Not sure if this is necessary:
389 * Advance the iterator before invoking the callback, to guard
390 * against the unlikely error where the hook is de-registered in
391 * the callback. This would prevent a crash due to the iterator
392 * getting invalidated. */
393 USDHook *hook = hook_iter->get();
394 ++hook_iter;
395
396 if (!hook->rna_ext.data) {
397 continue;
398 }
399
400 try {
401 PyObject *hook_obj = static_cast<PyObject *>(hook->rna_ext.data);
402
403 if (!PyObject_HasAttrString(hook_obj, function_name())) {
404 continue;
405 }
406
407 call_hook(hook_obj);
408 }
409 catch (python::error_already_set const &) {
410 handle_python_error(hook, reports_);
411 }
412 catch (...) {
414 reports_, RPT_ERROR, "An exception occurred invoking USD hook '%s'", hook->name);
415 }
416 }
417
419 PyGILState_Release(gilstate);
420 }
421
422 protected:
423 /* Override to specify the name of the function to be called. */
424 virtual const char *function_name() const = 0;
425 /* Override to call the function of the given object with the
426 * required arguments, e.g.,
427 *
428 * python::call_method<void>(hook_obj, function_name(), arg1, arg2); */
429 virtual void call_hook(PyObject *hook_obj) = 0;
430
431 virtual void init_in_gil() {};
432 virtual void release_in_gil() {};
433};
434
436 private:
437 USDSceneExportContext hook_context_;
438
439 public:
440 OnExportInvoker(const USDHierarchyIterator *iter, Depsgraph *depsgraph, ReportList *reports)
441 : USDHookInvoker(reports), hook_context_(iter, depsgraph)
442 {
443 }
444
445 private:
446 const char *function_name() const override
447 {
448 return "on_export";
449 }
450
451 void call_hook(PyObject *hook_obj) override
452 {
453 python::call_method<bool>(hook_obj, function_name(), python::ref(hook_context_));
454 }
455};
456
458 private:
459 USDMaterialExportContext hook_context_;
460 pxr::UsdShadeMaterial usd_material_;
461 PointerRNA material_ptr_;
462
463 public:
464 OnMaterialExportInvoker(pxr::UsdStageRefPtr stage,
465 const Material *material,
466 const pxr::UsdShadeMaterial &usd_material,
467 const USDExportParams &export_params,
468 ReportList *reports)
469 : USDHookInvoker(reports),
470 hook_context_(stage, export_params, reports),
471 usd_material_(usd_material)
472 {
473 material_ptr_ = RNA_pointer_create_discrete(
474 nullptr, RNA_Material, const_cast<Material *>(material));
475 }
476
477 private:
478 const char *function_name() const override
479 {
480 return "on_material_export";
481 }
482
483 void call_hook(PyObject *hook_obj) override
484 {
485 python::call_method<bool>(
486 hook_obj, function_name(), python::ref(hook_context_), material_ptr_, usd_material_);
487 }
488};
489
491 private:
492 USDSceneImportContext hook_context_;
493
494 public:
495 OnImportInvoker(pxr::UsdStageRefPtr stage, const ImportedPrimMap &prim_map, ReportList *reports)
496 : USDHookInvoker(reports), hook_context_(stage, prim_map)
497 {
498 }
499
500 private:
501 const char *function_name() const override
502 {
503 return "on_import";
504 }
505
506 void call_hook(PyObject *hook_obj) override
507 {
508 python::call_method<bool>(hook_obj, function_name(), python::ref(hook_context_));
509 }
510
511 void release_in_gil() override
512 {
513 hook_context_.release();
514 }
515};
516
518 private:
519 USDMaterialImportContext hook_context_;
520 pxr::UsdShadeMaterial usd_material_;
521 bool result_ = false;
522
523 public:
524 MaterialImportPollInvoker(pxr::UsdStageRefPtr stage,
525 const pxr::UsdShadeMaterial &usd_material,
526 const USDImportParams &import_params,
527 ReportList *reports)
528 : USDHookInvoker(reports),
529 hook_context_(stage, import_params, reports),
530 usd_material_(usd_material)
531 {
532 }
533
534 bool result() const
535 {
536 return result_;
537 }
538
539 private:
540 const char *function_name() const override
541 {
542 return "material_import_poll";
543 }
544
545 void call_hook(PyObject *hook_obj) override
546 {
547 /* If we already know that one of the registered hook classes can import the material
548 * because it returned true in a previous invocation of the callback, we skip the call. */
549 if (!result_) {
550 result_ = python::call_method<bool>(
551 hook_obj, function_name(), python::ref(hook_context_), usd_material_);
552 }
553 }
554};
555
557 private:
558 USDMaterialImportContext hook_context_;
559 pxr::UsdShadeMaterial usd_material_;
560 PointerRNA material_ptr_;
561 bool result_ = false;
562
563 public:
564 OnMaterialImportInvoker(pxr::UsdStageRefPtr stage,
565 Material *material,
566 const pxr::UsdShadeMaterial &usd_material,
567 const USDImportParams &import_params,
568 ReportList *reports)
569 : USDHookInvoker(reports),
570 hook_context_(stage, import_params, reports),
571 usd_material_(usd_material)
572 {
573 material_ptr_ = RNA_pointer_create_discrete(nullptr, RNA_Material, material);
574 }
575
576 bool result() const
577 {
578 return result_;
579 }
580
581 private:
582 const char *function_name() const override
583 {
584 return "on_material_import";
585 }
586
587 void call_hook(PyObject *hook_obj) override
588 {
589 result_ |= python::call_method<bool>(
590 hook_obj, function_name(), python::ref(hook_context_), material_ptr_, usd_material_);
591 }
592};
593
594void call_export_hooks(Depsgraph *depsgraph, const USDHierarchyIterator *iter, ReportList *reports)
595{
596 if (hook_list().empty()) {
597 return;
598 }
599
600 OnExportInvoker on_export(iter, depsgraph, reports);
601 on_export.call();
602}
603
604void call_material_export_hooks(pxr::UsdStageRefPtr stage,
605 const Material *material,
606 const pxr::UsdShadeMaterial &usd_material,
607 const USDExportParams &export_params,
608 ReportList *reports)
609{
610 if (hook_list().empty()) {
611 return;
612 }
613
614 OnMaterialExportInvoker on_material_export(
615 stage, material, usd_material, export_params, reports);
616 on_material_export.call();
617}
618
620{
621 if (hook_list().empty()) {
622 return;
623 }
624
625 const Vector<USDPrimReader *> &readers = archive->readers();
626 const ImportSettings &settings = archive->settings();
627 ImportedPrimMap prim_map;
628
629 /* Resize based on the typical scenario where there will be both Object and Data entries
630 * in the map in addition to each material. */
631 prim_map.reserve((readers.size() * 2) + settings.usd_path_to_mat.size());
632
633 for (const USDPrimReader *reader : readers) {
634 Object *ob = reader->object();
635
636 prim_map.lookup_or_add_default(reader->object_prim_path())
637 .append(RNA_id_pointer_create(&ob->id));
638 if (ob->data) {
639 prim_map.lookup_or_add_default(reader->data_prim_path())
640 .append(RNA_id_pointer_create(ob->data));
641 }
642 }
643
644 settings.usd_path_to_mat.foreach_item([&prim_map](const pxr::SdfPath &path, Material *mat) {
645 prim_map.lookup_or_add_default(path).append(RNA_id_pointer_create(&mat->id));
646 });
647
648 OnImportInvoker on_import(archive->stage(), prim_map, reports);
649 on_import.call();
650}
651
652bool have_material_import_hook(pxr::UsdStageRefPtr stage,
653 const pxr::UsdShadeMaterial &usd_material,
654 const USDImportParams &import_params,
655 ReportList *reports)
656{
657 if (hook_list().empty()) {
658 return false;
659 }
660
661 MaterialImportPollInvoker poll(stage, usd_material, import_params, reports);
662 poll.call();
663
664 return poll.result();
665}
666
667bool call_material_import_hooks(pxr::UsdStageRefPtr stage,
668 Material *material,
669 const pxr::UsdShadeMaterial &usd_material,
670 const USDImportParams &import_params,
671 ReportList *reports)
672{
673 if (hook_list().empty()) {
674 return false;
675 }
676
677 OnMaterialImportInvoker on_material_import(
678 stage, material, usd_material, import_params, reports);
679 on_material_import.call();
680 return on_material_import.result();
681}
682
683} // namespace io::usd
684} // namespace blender
#define final(a, b, c)
Definition BLI_hash_c.hh:21
Object is a sort of wrapper for general info.
#define STREQ(a, b)
Value & lookup_or_add_default(const Key &key)
Definition BLI_map.hh:639
void reserve(int64_t n)
Definition BLI_map.hh:1028
int64_t size() const
MaterialImportPollInvoker(pxr::UsdStageRefPtr stage, const pxr::UsdShadeMaterial &usd_material, const USDImportParams &import_params, ReportList *reports)
Definition usd_hook.cc:524
OnExportInvoker(const USDHierarchyIterator *iter, Depsgraph *depsgraph, ReportList *reports)
Definition usd_hook.cc:440
OnImportInvoker(pxr::UsdStageRefPtr stage, const ImportedPrimMap &prim_map, ReportList *reports)
Definition usd_hook.cc:495
OnMaterialExportInvoker(pxr::UsdStageRefPtr stage, const Material *material, const pxr::UsdShadeMaterial &usd_material, const USDExportParams &export_params, ReportList *reports)
Definition usd_hook.cc:464
OnMaterialImportInvoker(pxr::UsdStageRefPtr stage, Material *material, const pxr::UsdShadeMaterial &usd_material, const USDImportParams &import_params, ReportList *reports)
Definition usd_hook.cc:564
USDHookInvoker(ReportList *reports)
Definition usd_hook.cc:371
virtual ~USDHookInvoker()=default
virtual const char * function_name() const =0
virtual void call_hook(PyObject *hook_obj)=0
pxr::UsdStageRefPtr get_stage() const
Definition usd_hook.cc:205
USDMaterialExportContext(pxr::UsdStageRefPtr stage, const USDExportParams &params, ReportList *reports)
Definition usd_hook.cc:198
std::string export_texture(python::object obj) const
Definition usd_hook.cc:215
pxr::UsdStageRefPtr get_stage() const
Definition usd_hook.cc:257
USDMaterialImportContext(pxr::UsdStageRefPtr stage, const USDImportParams &params, ReportList *reports)
Definition usd_hook.cc:250
python::tuple import_texture(const std::string &asset_path) const
Definition usd_hook.cc:270
pxr::UsdStageRefPtr get_stage() const
Definition usd_hook.cc:117
const PointerRNA & get_depsgraph() const
Definition usd_hook.cc:122
USDSceneExportContext(const USDHierarchyIterator *iter, Depsgraph *depsgraph)
Definition usd_hook.cc:111
USDSceneImportContext(pxr::UsdStageRefPtr in_stage, const ImportedPrimMap &in_prim_map)
Definition usd_hook.cc:154
pxr::UsdStageRefPtr get_stage() const
Definition usd_hook.cc:164
const ImportSettings & settings() const
const Vector< USDPrimReader * > & readers() const
const char * temp_textures_dir()
void USD_unregister_hook(const USDHook *hook)
Definition usd_hook.cc:71
bool should_import_asset(const std::string &path)
bool have_material_import_hook(pxr::UsdStageRefPtr stage, const pxr::UsdShadeMaterial &usd_material, const USDImportParams &import_params, ReportList *reports)
Definition usd_hook.cc:652
void USD_register_hook(std::unique_ptr< USDHook > hook)
Definition usd_hook.cc:60
static USDHookList & hook_list()
Definition usd_hook.cc:54
void export_texture(Image *ima, const pxr::UsdStageRefPtr stage, const bool allow_overwrite, ReportList *reports)
std::list< std::unique_ptr< USDHook > > USDHookList
Definition usd_hook.cc:50
std::string get_tex_image_asset_filepath(Image *ima, const pxr::UsdStageRefPtr stage, const USDExportParams &export_params)
void call_import_hooks(USDStageReader *archive, ReportList *reports)
Definition usd_hook.cc:619
Map< pxr::SdfPath, Vector< PointerRNA > > ImportedPrimMap
Definition usd_hook.cc:51
std::string import_asset(const std::string &src, const char *import_dir, TexNameCollisionMode name_collision_mode, ReportList *reports)
bool call_material_import_hooks(pxr::UsdStageRefPtr stage, Material *material, const pxr::UsdShadeMaterial &usd_material, const USDImportParams &import_params, ReportList *reports)
Definition usd_hook.cc:667
void call_material_export_hooks(pxr::UsdStageRefPtr stage, const Material *material, const pxr::UsdShadeMaterial &usd_material, const USDExportParams &export_params, ReportList *reports)
Definition usd_hook.cc:604
void call_export_hooks(Depsgraph *depsgraph, const USDHierarchyIterator *iter, ReportList *reports)
Definition usd_hook.cc:594
USDHook * USD_find_hook_name(const char idname[])
Definition usd_hook.cc:77
static void handle_python_error(USDHook *hook, ReportList *reports)
Definition usd_hook.cc:347
void register_hook_converters()
Definition usd_hook.cc:299
BMesh Iterators.
PointerRNA RNA_id_pointer_create(ID *id)
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:8874
bool pyrna_id_FromPyObject(PyObject *obj, ID **id)
Definition bpy_rna.cc:8988
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
iter begin(iter)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
Map< pxr::SdfPath, Material * > usd_path_to_mat
static PyObject * convert(PointerRNA ptr)
Definition usd_hook.cc:97