11#include <pxr/base/tf/stringUtils.h>
12#include <pxr/usd/usdGeom/bboxCache.h>
13#include <pxr/usd/usdGeom/scope.h>
14#include <pxr/usd/usdUI/accessibilityAPI.h>
32static const pxr::TfToken
blender_ns(
"userProperties:blender", pxr::TfToken::Immortal);
36struct AccessibilityPropertyName {
37 pxr::TfToken property_namespace;
38 pxr::TfToken property_base_name;
45 std::vector<std::string> property_tokens = pxr::TfStringTokenize(prop->
name,
":");
48 if (property_tokens.size() == 3) {
49 pxr::TfToken accessibility_token(property_tokens[0]);
50 pxr::TfToken basename(property_tokens[2]);
51 if (accessibility_token == pxr::UsdUITokens->accessibility &&
52 pxr::UsdUIAccessibilityAPI::IsSchemaPropertyBaseName(basename))
54 AccessibilityPropertyName property_name;
58 property_name.property_namespace = pxr::TfToken(
60 property_name.property_base_name = basename;
70 return token == pxr::UsdUITokens->low || token == pxr::UsdUITokens->standard ||
71 token == pxr::UsdUITokens->high;
82 const AccessibilityPropertyName &property_name,
83 const std::string &value)
85 pxr::UsdUIAccessibilityAPI accessibility_api = pxr::UsdUIAccessibilityAPI::Apply(
86 prim, property_name.property_namespace);
87 if (!accessibility_api) {
91 if (property_name.property_base_name == pxr::UsdUITokens->label) {
92 accessibility_api.CreateLabelAttr().Set(value);
94 else if (property_name.property_base_name == pxr::UsdUITokens->description) {
95 accessibility_api.CreateDescriptionAttr().Set(value);
97 else if (property_name.property_base_name == pxr::UsdUITokens->priority) {
98 pxr::TfToken priority(value);
100 accessibility_api.CreatePriorityAttr().Set(priority);
112 return mesh->active_uv_map_name();
115template<
typename USDT>
118 const pxr::TfToken &prop_token,
119 const pxr::SdfValueTypeName &type_name,
120 const pxr::UsdTimeCode &time)
122 if (!prim || !prop || !prop->
data.
pointer || prop_token.IsEmpty() || !type_name) {
126 pxr::UsdAttribute vec_attr = prim.CreateAttribute(prop_token, type_name,
true);
130 "Couldn't create USD attribute for array property %s",
131 prop_token.GetString().c_str());
135 USDT vec_value(
static_cast<typename USDT::ScalarType *
>(prop->
data.
pointer));
137 return vec_attr.Set(vec_value, time);
144 const pxr::TfToken &prop_token,
145 const pxr::UsdTimeCode &time)
147 if (!prim || !prop || prop_token.IsEmpty()) {
153 "Property %s is not an array type and can't be converted to a vector attribute",
158 pxr::SdfValueTypeName type_name;
159 bool success =
false;
162 if (prop->
len == 2) {
163 type_name = pxr::SdfValueTypeNames->Float2;
166 else if (prop->
len == 3) {
167 type_name = pxr::SdfValueTypeNames->Float3;
170 else if (prop->
len == 4) {
171 type_name = pxr::SdfValueTypeNames->Float4;
176 if (prop->
len == 2) {
177 type_name = pxr::SdfValueTypeNames->Double2;
180 else if (prop->
len == 3) {
181 type_name = pxr::SdfValueTypeNames->Double3;
184 else if (prop->
len == 4) {
185 type_name = pxr::SdfValueTypeNames->Double4;
190 if (prop->
len == 2) {
191 type_name = pxr::SdfValueTypeNames->Int2;
194 else if (prop->
len == 3) {
195 type_name = pxr::SdfValueTypeNames->Int3;
198 else if (prop->
len == 4) {
199 type_name = pxr::SdfValueTypeNames->Int4;
206 "Couldn't determine USD type name for array property %s",
207 prop_token.GetString().c_str());
213 &
LOG,
"Couldn't set USD attribute from array property %s", prop_token.GetString().c_str());
241 return pxr::UsdTimeCode::Default();
272 static std::string material_library_path(
"/_materials");
276 if (!root_prim_path.empty()) {
277 return pxr::SdfPath(root_prim_path + material_library_path);
280 return pxr::SdfPath(material_library_path);
285 static std::string material_library_path(
"/_materials");
289 path_prefix += context.higher_up_export_path;
291 return pxr::SdfPath(path_prefix + material_library_path);
300 pxr::TfToken material_name(
304 .AppendChild(material_name);
305 pxr::UsdShadeMaterial usd_material = pxr::UsdShadeMaterial::Get(stage,
usd_path);
315 auto prim = usd_material.GetPrim();
334 return library_material;
343 .AppendChild(library_material.GetPath().GetNameToken());
345 pxr::UsdShadeMaterial proto_material = pxr::UsdShadeMaterial::Define(stage,
usd_path);
347 if (!proto_material.GetPrim().GetReferences().AddInternalReference(library_material.GetPath())) {
349 "Unable to add a material reference from %s to %s for prototype %s",
350 proto_material.GetPath().GetAsString().c_str(),
351 library_material.GetPath().GetAsString().c_str(),
352 context.export_path.c_str());
353 return library_material;
356 return proto_material;
360 const pxr::UsdTimeCode time,
361 const pxr::UsdGeomImageable &usd_geometry)
363 pxr::UsdAttribute attr_visibility = usd_geometry.CreateVisibilityAttr(pxr::VtValue(),
true);
365 const bool is_visible = context.is_object_visible(
367 const pxr::TfToken visibility = is_visible ? pxr::UsdGeomTokens->inherited :
368 pxr::UsdGeomTokens->invisible;
377 if (context.export_path == context.original_export_path) {
379 "Reference error: export path matches reference path: %s",
380 context.export_path.c_str());
385 BLI_assert(!context.original_export_path.empty());
386 BLI_assert(context.original_export_path.front() ==
'/');
389 ref_path_str += context.original_export_path;
391 pxr::SdfPath ref_path(ref_path_str);
396 if (!prim.GetReferences().AddInternalReference(ref_path)) {
401 "Unable to add reference from %s to %s, not instancing object for export",
402 context.export_path.c_str(),
403 context.original_export_path.c_str());
407 prim.SetInstanceable(
true);
414 pxr::UsdTimeCode time)
const
424 pxr::SdfValueTypeNames->
String,
426 .Set<std::string>(std::string(
id.name + 2));
430 pxr::SdfValueTypeNames->
String,
432 .Set<std::string>(std::string(
id.name + 2));
443 pxr::UsdTimeCode time)
const
445 if (properties ==
nullptr) {
453 const StringRef displayName_identifier =
"displayName";
455 const std::string default_namespace(
459 if (displayName_identifier == prop->name) {
460 if (prop->type ==
IDP_STRING && prop->data.pointer) {
461 prim.SetDisplayName(
static_cast<char *
>(prop->data.pointer));
469 if (prop->type ==
IDP_STRING && prop->data.pointer) {
471 prim, *accessibility_property_name,
static_cast<char *
>(prop->data.pointer));
476 std::vector<std::string> path_names = pxr::TfStringTokenize(prop->name,
":");
480 if (!default_namespace.empty() && path_names.size() < 2) {
481 path_names.insert(path_names.begin(), default_namespace);
484 std::vector<std::string> safe_names;
485 for (
const std::string &name : path_names) {
489 std::string full_prop_name = pxr::SdfPath::JoinIdentifier(safe_names);
490 pxr::TfToken prop_token = pxr::TfToken(full_prop_name);
492 if (prim.HasAttribute(prop_token)) {
498 switch (prop->type) {
500 if (pxr::UsdAttribute int_attr = prim.CreateAttribute(
501 prop_token, pxr::SdfValueTypeNames->Int,
true))
503 int_attr.Set<
int>(prop->data.val, time);
507 if (pxr::UsdAttribute float_attr = prim.CreateAttribute(
508 prop_token, pxr::SdfValueTypeNames->Float,
true))
510 float_attr.Set<
float>(*
reinterpret_cast<float *
>(&prop->data.val), time);
514 if (pxr::UsdAttribute double_attr = prim.CreateAttribute(
515 prop_token, pxr::SdfValueTypeNames->Double,
true))
517 double_attr.Set<
double>(*
reinterpret_cast<double *
>(&prop->data.val), time);
521 if (pxr::UsdAttribute str_attr = prim.CreateAttribute(
522 prop_token, pxr::SdfValueTypeNames->String,
true))
524 str_attr.Set<std::string>(
static_cast<const char *
>(prop->data.pointer), time);
528 if (pxr::UsdAttribute bool_attr = prim.CreateAttribute(
529 prop_token, pxr::SdfValueTypeNames->Bool,
true))
531 bool_attr.Set<
bool>(prop->data.val, time);
547 const pxr::UsdTimeCode time)
551 const bool useExtentsHint =
false;
552 const pxr::TfTokenVector includedPurposes{pxr::UsdGeomTokens->default_};
553 pxr::UsdGeomBBoxCache bboxCache(time, includedPurposes, useExtentsHint);
554 pxr::GfBBox3d
bounds = bboxCache.ComputeLocalBound(boundable.GetPrim());
557 pxr::VtArray<pxr::GfVec3f> extent{pxr::GfVec3f(
bounds.GetRange().GetMin()),
558 pxr::GfVec3f(
bounds.GetRange().GetMax())};
560 pxr::UsdAttribute attr_extent = boundable.CreateExtentAttr(pxr::VtValue(),
true);
566 const pxr::UsdTimeCode time)
568 pxr::VtArray<pxr::GfVec3f> extent(2);
570 extent[0].Set(
bounds->min);
571 extent[1].Set(
bounds->max);
574 pxr::UsdAttribute attr_extent = boundable.CreateExtentAttr(pxr::VtValue(),
true);
#define CLOG_ERROR(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
#define BLI_assert_msg(a, msg)
virtual bool check_is_animated(const HierarchyContext &context) const
pxr::UsdShadeMaterial ensure_usd_material(const HierarchyContext &context, Material *material) const
virtual void do_write(HierarchyContext &context)=0
pxr::SdfPath get_proto_material_root_path(const HierarchyContext &context) const
bool frame_has_been_written_
void author_extent(const pxr::UsdGeomBoundable &boundable, const pxr::UsdTimeCode time)
const pxr::SdfPath & usd_path() const
void add_to_prim_map(const pxr::SdfPath &usd_path, const ID *id) const
void write_user_properties(const pxr::UsdPrim &prim, IDProperty *properties, pxr::UsdTimeCode=pxr::UsdTimeCode::Default()) const
pxr::UsdShadeMaterial ensure_usd_material_created(const HierarchyContext &context, Material *material) const
pxr::SdfPath get_material_library_path() const
ReportList * reports() const
virtual bool mark_as_instance(const HierarchyContext &context, const pxr::UsdPrim &prim)
pxr::UsdTimeCode get_export_time_code() const
pxr::UsdUtilsSparseValueWriter usd_value_writer_
USDAbstractWriter(const USDExporterContext &usd_export_context)
void write_id_properties(const pxr::UsdPrim &prim, const ID &id, pxr::UsdTimeCode=pxr::UsdTimeCode::Default()) const
void write(HierarchyContext &context) override
void write_visibility(const HierarchyContext &context, const pxr::UsdTimeCode time, const pxr::UsdGeomImageable &usd_geometry)
virtual bool is_supported(const HierarchyContext *context) const
std::string get_export_file_path() const
const USDExporterContext usd_export_context_
void add_to_prim_map(const pxr::SdfPath &usd_path, const ID *id) const
static void create_vector_attrib(const pxr::UsdPrim &prim, const IDProperty *prop, const pxr::TfToken &prop_token, const pxr::UsdTimeCode &time)
std::string make_safe_name(const StringRef name, bool allow_unicode)
void colorspace_apply_to_prim(const pxr::UsdPrim &prim)
void set_attribute(const pxr::UsdAttribute &attr, const USDT value, pxr::UsdTimeCode time, pxr::UsdUtilsSparseValueWriter &value_writer)
pxr::UsdShadeMaterial create_usd_material(const USDExporterContext &usd_export_context, pxr::SdfPath usd_path, const Material *material, const std::string &active_uvmap_name, ReportList *reports)
static const pxr::TfToken blender_ns("userProperties:blender", pxr::TfToken::Immortal)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void write_accessibility_property(const pxr::UsdPrim &prim, const AccessibilityPropertyName &property_name, const std::string &value)
static bool is_valid_accessibility_priority(const pxr::TfToken &token)
static std::optional< AccessibilityPropertyName > parse_accessibility_property_name(IDProperty *prop, bool allow_unicode)
bool set_vec_attrib(const pxr::UsdPrim &prim, const IDProperty *prop, const pxr::TfToken &prop_token, const pxr::SdfValueTypeName &type_name, const pxr::UsdTimeCode &time)
ID_Type GS(const char *name)
static std::string get_mesh_active_uvlayer_name(const Object *ob)
ListBaseT< struct IDProperty > group
std::string root_prim_path
char custom_properties_namespace[MAX_IDPROP_NAME]
enum eEvaluationMode evaluation_mode
bool export_custom_properties
wmJobWorkerStatus * worker_status
std::function< pxr::UsdTimeCode()> get_time_code
const USDExportParams & export_params
USDHierarchyIterator * hierarchy_iterator
const pxr::SdfPath usd_path
std::string export_file_path
const pxr::UsdStageRefPtr stage