24#include <pxr/usd/usd/primRange.h>
25#include <pxr/usd/usdGeom/camera.h>
26#include <pxr/usd/usdGeom/capsule.h>
27#include <pxr/usd/usdGeom/capsule_1.h>
28#include <pxr/usd/usdGeom/cone.h>
29#include <pxr/usd/usdGeom/cube.h>
30#include <pxr/usd/usdGeom/cylinder.h>
31#include <pxr/usd/usdGeom/cylinder_1.h>
32#include <pxr/usd/usdGeom/mesh.h>
33#include <pxr/usd/usdGeom/metrics.h>
34#include <pxr/usd/usdGeom/nurbsCurves.h>
35#include <pxr/usd/usdGeom/plane.h>
36#include <pxr/usd/usdGeom/pointInstancer.h>
37#include <pxr/usd/usdGeom/points.h>
38#include <pxr/usd/usdGeom/scope.h>
39#include <pxr/usd/usdGeom/sphere.h>
40#include <pxr/usd/usdGeom/tokens.h>
41#include <pxr/usd/usdGeom/xform.h>
42#include <pxr/usd/usdLux/boundableLightBase.h>
43#include <pxr/usd/usdLux/domeLight.h>
44#include <pxr/usd/usdLux/domeLight_1.h>
45#include <pxr/usd/usdLux/nonboundableLightBase.h>
46#include <pxr/usd/usdShade/material.h>
72#include <fmt/format.h>
109 if (!instance_reader) {
113 pxr::SdfPath proto_path = instance_reader->
proto_path();
116 if (collection !=
nullptr) {
121 "Couldn't find prototype collection for %s",
122 instance_reader->
prim_path().GetAsString().c_str());
130 if (!stage || pxr::UsdGeomGetStageUpAxis(stage) == pxr::UsdGeomTokens->
z) {
150 pxr::TfToken generated_key(
"Blender:generated");
151 pxr::SdfPath path(
"/");
152 auto prim = stage->GetPseudoRoot();
156 for (
auto child : prim.GetChildren()) {
157 if (child_count == 0) {
158 prim = child.GetPrim();
163 if (child_count != 1) {
171 if (!prim.HasCustomDataKey(generated_key)) {
174 path = path.AppendChild(prim.GetName());
178 if (path == pxr::SdfPath(
"/")) {
179 path = pxr::SdfPath();
190 const std::string doc = stage->GetRootLayer()->GetDocumentation();
193 if (doc.starts_with(
"Blender v")) {
201 const std::function<
CacheFile *()> &get_cache_file_fn)
225bool USDStageReader::is_primitive_prim(
const pxr::UsdPrim &prim)
const
227 return (prim.IsA<pxr::UsdGeomCapsule>() || prim.IsA<pxr::UsdGeomCapsule_1>() ||
228 prim.IsA<pxr::UsdGeomCylinder>() || prim.IsA<pxr::UsdGeomCylinder_1>() ||
229 prim.IsA<pxr::UsdGeomCone>() || prim.IsA<pxr::UsdGeomCube>() ||
230 prim.IsA<pxr::UsdGeomSphere>() || prim.IsA<pxr::UsdGeomPlane>());
246 if (prim.IsA<pxr::UsdGeomPointInstancer>()) {
262 (prim.IsA<pxr::UsdLuxDomeLight>() || prim.IsA<pxr::UsdLuxDomeLight_1>()))
268 (prim.IsA<pxr::UsdLuxBoundableLightBase>() || prim.IsA<pxr::UsdLuxNonboundableLightBase>()))
275 if (prim.IsA<pxr::UsdVolParticleField3DGaussianSplat>()) {
284 if (prim.IsA<pxr::UsdGeomImageable>()) {
296 if (is_primitive_prim(prim)) {
299 if (prim.IsA<pxr::UsdGeomCamera>()) {
302 if (prim.IsA<pxr::UsdGeomBasisCurves>()) {
305 if (prim.IsA<pxr::UsdGeomNurbsCurves>()) {
308 if (prim.IsA<pxr::UsdGeomMesh>()) {
311 if (prim.IsA<pxr::UsdLuxDomeLight>() || prim.IsA<pxr::UsdLuxDomeLight_1>()) {
315 if (prim.IsA<pxr::UsdLuxBoundableLightBase>() || prim.IsA<pxr::UsdLuxNonboundableLightBase>()) {
318 if (prim.IsA<pxr::UsdVolVolume>()) {
321 if (prim.IsA<pxr::UsdVolParticleField3DGaussianSplat>()) {
324 if (prim.IsA<pxr::UsdSkelSkeleton>()) {
327 if (prim.IsA<pxr::UsdGeomPoints>()) {
330 if (prim.IsA<pxr::UsdGeomPointInstancer>()) {
333 if (prim.IsA<pxr::UsdGeomImageable>()) {
339bool USDStageReader::include_by_visibility(
const pxr::UsdGeomImageable &imageable)
const
346 pxr::UsdAttribute visibility_attr = imageable.GetVisibilityAttr();
348 if (!visibility_attr) {
355 if (visibility_attr.ValueMightBeTimeVarying()) {
359 pxr::TfToken visibility;
360 visibility_attr.Get(&visibility);
361 return visibility != pxr::UsdGeomTokens->invisible;
364bool USDStageReader::include_by_purpose(
const pxr::UsdGeomImageable &imageable)
const
376 pxr::UsdAttribute purpose_attr = imageable.GetPurposeAttr();
383 pxr::TfToken purpose;
384 purpose_attr.Get(&purpose);
386 if (purpose == pxr::UsdGeomTokens->guide) {
389 if (purpose == pxr::UsdGeomTokens->proxy) {
392 if (purpose == pxr::UsdGeomTokens->render) {
399bool USDStageReader::merge_with_parent(
USDPrimReader *reader)
const
406 USDXformReader *xform_reader =
dynamic_cast<USDXformReader *
>(reader);
413 if (xform_reader->use_parent_xform()) {
418 if (!xform_reader->prim().GetParent().IsA<pxr::UsdGeomXform>()) {
423 if (xform_reader->prim().IsA<pxr::UsdGeomXform>() ||
424 xform_reader->prim().IsA<pxr::UsdGeomScope>())
430 if (xform_reader->prim_has_xform_ops()) {
435 xform_reader->set_use_parent_xform(
true);
442 const bool defined_prims_only,
443 Vector<USDPrimReader *> &r_readers)
445 if (prim.IsA<pxr::UsdGeomImageable>()) {
446 pxr::UsdGeomImageable imageable(prim);
448 if (!include_by_purpose(imageable)) {
452 if (!include_by_visibility(imageable)) {
457 if (prim.IsA<pxr::UsdLuxDomeLight>() || prim.IsA<pxr::UsdLuxDomeLight_1>()) {
463 pxr::Usd_PrimFlagsConjunction filter_flags = pxr::UsdPrimIsActive && pxr::UsdPrimIsLoaded &&
464 !pxr::UsdPrimIsAbstract;
466 if (defined_prims_only) {
467 filter_flags &= pxr::UsdPrimIsDefined;
470 pxr::Usd_PrimFlagsPredicate filter_predicate(filter_flags);
472 filter_predicate = pxr::UsdTraverseInstanceProxies(filter_predicate);
475 Vector<USDPrimReader *> child_readers;
477 pxr::UsdPrimSiblingRange children = prim.GetFilteredChildren(filter_predicate);
479 for (
const auto &child_prim : children) {
480 if (pruned_prims.contains(child_prim.GetPath())) {
484 child_prim, pruned_prims, defined_prims_only, r_readers))
486 child_readers.append(child_reader);
490 if (prim.IsPseudoRoot()) {
503 if (child_readers.size() == 1) {
505 USDPrimReader *child_reader = child_readers.first();
507 if (merge_with_parent(child_reader)) {
512 if (prim.IsA<pxr::UsdShadeMaterial>()) {
526 if (!reader->valid()) {
530 r_readers.append(reader);
534 for (USDPrimReader *child_reader : child_readers) {
535 child_reader->parent(reader);
551 UsdPathSet instancer_proto_paths = collect_point_instancer_proto_paths();
554 pxr::UsdPrim root =
stage_->GetPseudoRoot();
556 stage_->SetInterpolationType(pxr::UsdInterpolationType::UsdInterpolationTypeHeld);
563 std::vector<pxr::UsdPrim> protos =
stage_->GetPrototypes();
565 for (
const pxr::UsdPrim &proto_prim : protos) {
567 collect_readers(proto_prim, instancer_proto_paths,
true, proto_readers);
577 if (!instancer_proto_paths.
is_empty()) {
578 create_point_instancer_proto_readers(instancer_proto_paths);
590 usd_path_to_armature.
add(reader->prim_path(), reader->object());
596 if (!reader->object()) {
614 if (
object ==
nullptr) {
617 "%s: Couldn't find armature object corresponding to USD skeleton %s",
619 skel_path.GetAsString().c_str());
634 Object *mesh_object = reader->object();
659 pxr::UsdPrim prim =
stage_->GetPrimAtPath(mtl_path);
661 pxr::UsdShadeMaterial usd_mtl(prim);
690 if (have_import_hook) {
703 if (mat->id.us == 0) {
711 pxr::UsdPrimRange range =
stage_->Traverse();
712 for (pxr::UsdPrim prim : range) {
713 if (prim.IsA<pxr::UsdShadeMaterial>()) {
714 pxr::UsdShadeMaterial usd_mat(prim);
730 pxr::UsdPrim prim =
stage_->GetPrimAtPath(item.key);
732 pxr::UsdShadeMaterial usd_mtl(prim);
743 "USD hook 'on_material_import' for material %s failed, attempting to convert USD "
744 "Preview Surface material",
745 usd_mtl.GetPath().GetAsString().c_str());
784 int result = BLI_strcasecmp(a->name().c_str(), b->name().c_str());
789 return a->prim_path() < b->prim_path();
804 if (all_protos_collection) {
807 if (parent_collection) {
817 proto_collection_map.
add(path, proto_collection);
832 if (collection ==
nullptr) {
834 "Couldn't find collection when adding objects for prototype %s",
835 item.key.GetAsString().c_str());
867 if (!instancer_reader) {
871 pxr::SdfPathVector proto_paths = instancer_reader->
proto_paths();
872 const pxr::SdfPath &instancer_path = reader->prim().GetPath();
874 bmain, all_protos_collection, instancer_path.GetName().c_str());
882 for (
const pxr::SdfPath &proto_path : proto_paths) {
886 std::string coll_name = fmt::format(
"proto_{0:0{1}}", proto_index, max_index_digits);
893 Object *ob = proto->object();
906void USDStageReader::create_point_instancer_proto_readers(
const UsdPathSet &proto_paths)
912 for (
const pxr::SdfPath &path : proto_paths) {
914 pxr::UsdPrim proto_prim = stage_->GetPrimAtPath(path);
924 collect_readers(proto_prim, proto_paths,
false , proto_readers);
926 instancer_proto_readers_.add(path, proto_readers);
929 reader->set_is_in_instancer_proto(
true);
930 readers_.append(reader);
936void USDStageReader::collect_point_instancer_proto_paths(
const pxr::UsdPrim &prim,
942 pxr::Usd_PrimFlagsConjunction filter_flags = pxr::UsdPrimIsActive && pxr::UsdPrimIsLoaded &&
943 !pxr::UsdPrimIsAbstract;
945 pxr::UsdPrimSiblingRange children = prim.GetFilteredChildren(filter_flags);
947 for (
const auto &child_prim : children) {
951 if (child_prim.IsDefined()) {
952 const pxr::UsdGeomImageable imageable = pxr::UsdGeomImageable(child_prim);
960 if (!include_by_purpose(imageable)) {
964 if (!include_by_visibility(imageable)) {
971 if (!params_.support_scene_instancing && child_prim.IsInPrototype()) {
975 if (pxr::UsdGeomPointInstancer instancer = pxr::UsdGeomPointInstancer(child_prim)) {
976 pxr::SdfPathVector paths;
977 instancer.GetPrototypesRel().GetTargets(&paths);
978 for (
const pxr::SdfPath &path : paths) {
983 collect_point_instancer_proto_paths(child_prim, r_paths);
987UsdPathSet USDStageReader::collect_point_instancer_proto_paths()
const
995 collect_point_instancer_proto_paths(stage_->GetPseudoRoot(),
result);
997 std::vector<pxr::UsdPrim> protos = stage_->GetPrototypes();
999 for (
const pxr::UsdPrim &proto_prim : protos) {
1000 collect_point_instancer_proto_paths(proto_prim,
result);
General operations, lookup, etc. for blender objects.
#define CLOG_WARN(clg_ref,...)
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
#define BLI_assert_msg(a, msg)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
bool add(const Key &key, const Value &value)
Value lookup_default(const Key &key, const Value &default_value) const
ItemIterator items() const &
bool contains(const Key &key) const
USDBasisCurvesReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
USDCameraReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
USDDomeLightReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
pxr::SdfPath proto_path() const
void set_instance_collection(Collection *coll)
USDInstanceReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
USDLightReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
Material * add_material(const pxr::UsdShadeMaterial &usd_material, bool read_usd_preview=true) const
void import_usd_preview(Material *mtl, const pxr::UsdShadeMaterial &usd_material) const
USDMeshReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
pxr::SdfPath get_skeleton_path() const
USDNurbsReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
USDParticleFieldReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
USDPointInstancerReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
pxr::SdfPathVector proto_paths() const
void set_collection(Main *bmain, Collection &coll)
USDPointsReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
pxr::SdfPath prim_path() const
USDShapeReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
USDSkeletonReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
ReportList * reports() const
ProtoReaderMap proto_readers_
USDPrimReader * create_reader(const pxr::UsdPrim &prim)
void fake_users_for_unused_materials()
void call_material_import_hooks(struct Main *bmain) const
Vector< pxr::SdfPath > material_paths_
pxr::UsdStageRefPtr stage()
const USDImportParams & params() const
USDPrimReader * create_reader_if_allowed(const pxr::UsdPrim &prim)
pxr::UsdStageRefPtr stage_
void find_material_import_hook_sources()
USDStageReader(pxr::UsdStageRefPtr stage, const USDImportParams ¶ms, const std::function< CacheFile *()> &get_cache_file_fn={})
ProtoReaderMap instancer_proto_readers_
void create_proto_collections(Main *bmain, Collection *parent_collection)
void process_armature_modifiers() const
Vector< USDDomeLightReader * > dome_light_readers_
Vector< USDPrimReader * > readers_
void import_all_materials(struct Main *bmain)
USDVolumeReader(const pxr::UsdPrim &prim, const USDImportParams &import_params, const ImportSettings &settings)
bool have_material_import_hook(pxr::UsdStageRefPtr stage, const pxr::UsdShadeMaterial &usd_material, const USDImportParams &import_params, ReportList *reports)
static void set_instance_collection(USDInstanceReader *instance_reader, const Map< pxr::SdfPath, Collection * > &proto_collection_map)
static void decref(USDPrimReader *reader)
static void convert_to_z_up(pxr::UsdStageRefPtr stage, ImportSettings &settings)
static Collection * create_collection(Main *bmain, Collection *parent, const char *name)
bool call_material_import_hooks(pxr::UsdStageRefPtr stage, Material *material, const pxr::UsdShadeMaterial &usd_material, const USDImportParams &import_params, ReportList *reports)
static void find_prefix_to_skip(pxr::UsdStageRefPtr stage, ImportSettings &settings)
static void determine_blender_compat(pxr::UsdStageRefPtr stage, ImportSettings &settings)
Material * find_existing_material(const pxr::SdfPath &usd_mat_path, const USDImportParams ¶ms, const Map< std::string, Material * > &mat_map, const Map< pxr::SdfPath, Material * > &usd_path_to_mat)
Set< pxr::SdfPath > UsdPathSet
void build_material_map(const Main *bmain, Map< std::string, Material * > &r_mat_map)
EulerXYZBase< float > EulerXYZ
MatT from_rotation(const RotationT &rotation)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE int integer_digits_i(int i)
void id_fake_user_set(ID *id)
bool BKE_object_parent_loop_check(const Object *parent, const Object *ob)
void parallel_sort(RandomAccessIterator begin, RandomAccessIterator end)
void DEG_id_tag_update(ID *id, unsigned int flags)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
void unit_m4(float m[4][4])
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
@ COLLECTION_HIDE_VIEWPORT
Collection * BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
Set< pxr::SdfPath > mat_import_hook_sources
Map< pxr::SdfPath, Material * > usd_path_to_mat_for_hook
Map< std::string, Material * > mat_name_to_mat
double stage_meters_per_unit
Map< pxr::SdfPath, Material * > usd_path_to_mat
bool blender_stage_version_prior_44
std::function< CacheFile *()> get_cache_file
MtlNameCollisionMode mtl_name_collision_mode
bool apply_unit_conversion_scale
wmJobWorkerStatus * worker_status
bool support_scene_instancing