Blender V5.3
usd_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "usd_utils.hh"
6
7#include "BLI_array.hh"
8#include "BLI_string_ref.hh"
9#include "BLI_string_utf8.hh"
10
11#include <pxr/base/tf/stringUtils.h>
12#include <pxr/base/tf/unicodeUtils.h>
13#include <pxr/usd/usd/prim.h>
14#include <pxr/usd/usd/stage.h>
15
16namespace blender::io::usd {
17
18static bool is_safe_char(const pxr::TfUtf8CodePoint cp, bool is_first, bool allow_unicode)
19{
20 constexpr pxr::TfUtf8CodePoint cp_underscore = pxr::TfUtf8CodePointFromAscii('_');
21
22 if (cp == cp_underscore) {
23 return true;
24 }
25
26 if (allow_unicode) {
27 return is_first ? pxr::TfIsUtf8CodePointXidStart(cp) : pxr::TfIsUtf8CodePointXidContinue(cp);
28 }
29
30 constexpr uint32_t cp_A = pxr::TfUtf8CodePointFromAscii('A').AsUInt32();
31 constexpr uint32_t cp_Z = pxr::TfUtf8CodePointFromAscii('Z').AsUInt32();
32 constexpr uint32_t cp_a = pxr::TfUtf8CodePointFromAscii('a').AsUInt32();
33 constexpr uint32_t cp_z = pxr::TfUtf8CodePointFromAscii('z').AsUInt32();
34 constexpr uint32_t cp_0 = pxr::TfUtf8CodePointFromAscii('0').AsUInt32();
35 constexpr uint32_t cp_9 = pxr::TfUtf8CodePointFromAscii('9').AsUInt32();
36
37 const uint32_t cp_u32 = cp.AsUInt32();
38 const bool is_letter = (cp_u32 >= cp_A && cp_u32 <= cp_Z) || (cp_u32 >= cp_a && cp_u32 <= cp_z);
39 const bool is_digit = cp_u32 >= cp_0 && cp_u32 <= cp_9;
40 return is_first ? is_letter : (is_letter || is_digit);
41}
42
43static std::string make_safe_identifier(const StringRef name, bool allow_unicode)
44{
45 if (name.is_empty()) {
46 return "_";
47 }
48
49 const bool has_leading_digit = std::isdigit(name[0]);
50 const bool need_leading_underscore = has_leading_digit;
51
52 /* Create temporary buffer using the original incoming string size, which can be larger than
53 * required if unicode characters are converted to '_'. This size serves as the upper limit of
54 * what might be produced. */
55 const int64_t adjust = (need_leading_underscore ? 1 : 0);
56 Array<char, 256> storage(name.size() + adjust);
58
59 /* Insert a leading '_' to account for invalid starting characters. */
60 size_t offset = 0;
61 bool first = true;
62 if (need_leading_underscore) {
63 buf[0] = '_';
64 offset = 1;
65 first = false;
66 }
67
68 for (auto cp : pxr::TfUtf8CodePointView{name}) {
69 const bool cp_allowed = is_safe_char(cp, first, allow_unicode);
70 if (!cp_allowed) {
71 offset += BLI_str_utf8_from_unicode(uint32_t('_'), buf.data() + offset, buf.size() - offset);
72 }
73 else {
74 offset += BLI_str_utf8_from_unicode(cp.AsUInt32(), buf.data() + offset, buf.size() - offset);
75 }
76
77 first = false;
78 }
79
80 return {buf.data(), offset};
81}
82
83std::string make_safe_name(const StringRef name, bool allow_unicode)
84{
85 return make_safe_identifier(name, allow_unicode);
86}
87
88std::string make_safe_primvar_name(const StringRef name, bool allow_unicode)
89{
90 /* Allow namespaced identifiers, separated by ':'. */
91 const std::string original(name);
92 std::vector<std::string> tokens = pxr::TfStringSplit(original, ":");
93 if (tokens.empty()) {
94 return "_";
95 }
96
97 std::string safe_name;
98 for (size_t i = 0; i < tokens.size(); i++) {
99 const std::string &token = tokens[i];
100 safe_name += make_safe_identifier(token, allow_unicode);
101 if (i != tokens.size() - 1) {
102 safe_name += ":";
103 }
104 }
105
106 return safe_name;
107}
108
109pxr::SdfPath get_unique_path(pxr::UsdStageRefPtr stage, const std::string &path)
110{
111 std::string unique_path = path;
112 int suffix = 2;
113 while (stage->GetPrimAtPath(pxr::SdfPath(unique_path)).IsValid()) {
114 unique_path = path + std::to_string(suffix++);
115 }
116
117 return pxr::SdfPath(unique_path);
118}
119
120} // namespace blender::io::usd
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr T * data() const
Definition BLI_span.hh:545
constexpr bool is_empty() const
constexpr int64_t size() const
#define storage(slot, qualifiers)
static bool is_safe_char(const pxr::TfUtf8CodePoint cp, bool is_first, bool allow_unicode)
Definition usd_utils.cc:18
pxr::SdfPath get_unique_path(pxr::UsdStageRefPtr stage, const std::string &path)
Definition usd_utils.cc:109
std::string make_safe_name(const StringRef name, bool allow_unicode)
Definition usd_utils.cc:83
static std::string make_safe_identifier(const StringRef name, bool allow_unicode)
Definition usd_utils.cc:43
std::string make_safe_primvar_name(const StringRef name, bool allow_unicode)
Definition usd_utils.cc:88
__int64 int64_t
Definition BKE_text.h:90
size_t BLI_str_utf8_from_unicode(unsigned int c, char *dst, size_t dst_maxncpy) ATTR_NONNULL(2)
unsigned int uint32_t
Definition BKE_text.h:81