Blender V5.3
usd_blend_shape_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 NVIDIA Corporation. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6#include "usd_utils.hh"
7
8#include <pxr/usd/usdGeom/primvarsAPI.h>
9#include <pxr/usd/usdSkel/animMapper.h>
10#include <pxr/usd/usdSkel/animation.h>
11#include <pxr/usd/usdSkel/bindingAPI.h>
12#include <pxr/usd/usdSkel/blendShape.h>
13
14#include "DNA_key_types.h"
15#include "DNA_mesh_types.h"
16
17#include "BKE_key.hh"
18#include "BKE_mesh.hh"
19#include "BKE_object.hh"
20#include "DNA_object_types.h"
21
22#include "BLI_assert.hh"
24#include "BLI_math_vector_c.hh"
25#include "BLI_set.hh"
26#include "BLI_vector.hh"
27
28#include <string>
29#include <vector>
30
31#include "CLG_log.h"
32
33namespace blender {
34
35static CLG_LogRef LOG = {"io.usd"};
36
37namespace usdtokens {
38static const pxr::TfToken Anim("Anim", pxr::TfToken::Immortal);
39static const pxr::TfToken joint1("joint1", pxr::TfToken::Immortal);
40static const pxr::TfToken Skel("Skel", pxr::TfToken::Immortal);
41} // namespace usdtokens
42
43namespace {
44
45/* Helper struct to facilitate merging blend shape weights time
46 * samples from multiple meshes to a single skeleton animation. */
47struct BlendShapeMergeInfo {
48 pxr::VtTokenArray src_blend_shapes;
49 pxr::UsdAttribute src_weights_attr;
50 /* Remap blend shape weight array from the
51 * source order to the destination order. */
52 pxr::UsdSkelAnimMapper anim_map;
53
54 void init_anim_map(const pxr::VtTokenArray &dst_blend_shapes)
55 {
56 anim_map = pxr::UsdSkelAnimMapper(src_blend_shapes, dst_blend_shapes);
57 }
58};
59
60/* Helper function to avoid name collisions when merging blend shape names from
61 * multiple meshes to a single skeleton.
62 *
63 * Attempt to add the given name to the 'names' set as a unique entry, modifying
64 * the name with a numerical suffix if necessary, and return the unique name that
65 * was added to the set. */
66std::string add_unique_name(Set<std::string> &names, const std::string &name)
67{
68 std::string unique_name = name;
69 int suffix = 2;
70 while (names.contains(unique_name)) {
71 unique_name = name + std::to_string(suffix++);
72 }
73 names.add(unique_name);
74 return unique_name;
75}
76
77} // namespace
78
79namespace io::usd {
80
81pxr::TfToken TempBlendShapeWeightsPrimvarName("temp:weights", pxr::TfToken::Immortal);
82
83void ensure_blend_shape_skeleton(pxr::UsdStageRefPtr stage, pxr::UsdPrim &mesh_prim)
84{
85 if (!stage || !mesh_prim) {
86 return;
87 }
88
89 pxr::UsdSkelBindingAPI skel_api = pxr::UsdSkelBindingAPI::Apply(mesh_prim);
90
91 if (!skel_api) {
93 "Couldn't apply UsdSkelBindingAPI to mesh prim %s",
94 mesh_prim.GetPath().GetAsString().c_str());
95 return;
96 }
97
98 pxr::UsdSkelSkeleton skel;
99 if (!skel_api.GetSkeleton(&skel)) {
100 pxr::SdfPath skel_path = mesh_prim.GetParent().GetPath().AppendChild(usdtokens::Skel);
101 skel = pxr::UsdSkelSkeleton::Define(stage, skel_path);
102
103 if (!skel) {
104 CLOG_WARN(&LOG,
105 "Couldn't find or create skeleton bound to mesh prim %s",
106 mesh_prim.GetPath().GetAsString().c_str());
107 return;
108 }
109
110 skel_api.CreateSkeletonRel().AddTarget(skel.GetPath());
111
112 /* Initialize the skeleton. */
113 pxr::VtMatrix4dArray bind_transforms(1, pxr::GfMatrix4d(1.0));
114 pxr::VtMatrix4dArray rest_transforms(1, pxr::GfMatrix4d(1.0));
115 skel.CreateBindTransformsAttr().Set(bind_transforms);
116 skel.GetRestTransformsAttr().Set(rest_transforms);
117
118 /* Some DCCs seem to require joint names to bind the
119 * skeleton to blend-shapes. */
120 pxr::VtTokenArray joints({usdtokens::joint1});
121 skel.CreateJointsAttr().Set(joints);
122 }
123
124 pxr::UsdAttribute temp_weights_attr = pxr::UsdGeomPrimvarsAPI(mesh_prim).GetPrimvar(
126
127 if (!temp_weights_attr) {
128 /* No need to create the animation. */
129 return;
130 }
131
132 pxr::SdfPath anim_path = skel.GetPath().AppendChild(usdtokens::Anim);
133 pxr::UsdSkelAnimation anim = pxr::UsdSkelAnimation::Define(stage, anim_path);
134
135 if (!anim) {
136 CLOG_WARN(&LOG, "Couldn't define animation at path %s", anim_path.GetAsString().c_str());
137 return;
138 }
139
140 pxr::VtTokenArray blendshape_names;
141 skel_api.GetBlendShapesAttr().Get(&blendshape_names);
142 anim.CreateBlendShapesAttr().Set(blendshape_names);
143
144 std::vector<double> times;
145 temp_weights_attr.GetTimeSamples(&times);
146
147 pxr::UsdAttribute anim_weights_attr = anim.CreateBlendShapeWeightsAttr();
148
149 pxr::VtFloatArray weights;
150 for (const double time : times) {
151 if (temp_weights_attr.Get(&weights, time)) {
152 anim_weights_attr.Set(weights, time);
153 }
154 }
155
156 /* Next, set the animation source on the skeleton. */
157
158 skel_api = pxr::UsdSkelBindingAPI::Apply(skel.GetPrim());
159
160 if (!skel_api) {
161 CLOG_WARN(&LOG,
162 "Couldn't apply UsdSkelBindingAPI to skeleton prim %s",
163 skel.GetPath().GetAsString().c_str());
164 return;
165 }
166
167 if (!skel_api.CreateAnimationSourceRel().AddTarget(pxr::SdfPath(usdtokens::Anim))) {
168 CLOG_WARN(&LOG,
169 "Couldn't set animation source on skeleton %s",
170 skel.GetPath().GetAsString().c_str());
171 }
172
173 pxr::UsdGeomPrimvarsAPI(mesh_prim).RemovePrimvar(TempBlendShapeWeightsPrimvarName);
174}
175
177{
179
180 if (!obj->data || obj->type != OB_MESH) {
181 return nullptr;
182 }
183
184 const Mesh *mesh = id_cast<const Mesh *>(obj->data);
185
186 return mesh->key;
187}
188
190{
191 const Key *key = get_mesh_shape_key(obj);
192 return key && key->totkey > 0 && key->type == KEY_RELATIVE;
193}
194
195void create_blend_shapes(pxr::UsdStageRefPtr stage,
196 const Object *obj,
197 const pxr::UsdPrim &mesh_prim,
198 bool allow_unicode)
199{
200 const Key *key = get_mesh_shape_key(obj);
201
202 if (!(key && mesh_prim)) {
203 return;
204 }
205
206 pxr::UsdSkelBindingAPI skel_api = pxr::UsdSkelBindingAPI::Apply(mesh_prim);
207
208 if (!skel_api) {
209 CLOG_WARN(&LOG,
210 "Couldn't apply UsdSkelBindingAPI to mesh prim %s",
211 mesh_prim.GetPath().GetAsString().c_str());
212 return;
213 }
214
215 pxr::VtTokenArray blendshape_names;
216 std::vector<pxr::SdfPath> blendshape_paths;
217
218 /* Get the basis, which we'll use to calculate offsets. */
219 KeyBlock *basis_key = key->block.first();
220
221 if (!basis_key) {
222 return;
223 }
224
225 int basis_totelem = basis_key->totelem;
226
227 for (KeyBlock &kb : key->block) {
228 if (&kb == basis_key) {
229 /* Skip the basis. */
230 continue;
231 }
232
233 pxr::TfToken name(make_safe_name(kb.name, allow_unicode));
234 blendshape_names.push_back(name);
235
236 pxr::SdfPath path = mesh_prim.GetPath().AppendChild(name);
237 blendshape_paths.push_back(path);
238
239 pxr::UsdSkelBlendShape blendshape = pxr::UsdSkelBlendShape::Define(stage, path);
240
241 pxr::UsdAttribute offsets_attr = blendshape.CreateOffsetsAttr();
242
243 /* Some applications, like Houdini, don't render blend shapes unless the point
244 * indices are set, so we always create this attribute, even when every index
245 * is included. */
246 pxr::UsdAttribute point_indices_attr = blendshape.CreatePointIndicesAttr();
247
248 pxr::VtVec3fArray offsets(kb.totelem);
249 pxr::VtIntArray indices(kb.totelem);
250 std::iota(indices.begin(), indices.end(), 0);
251
252 const float (*fp)[3] = static_cast<float (*)[3]>(kb.data);
253
254 const float (*basis_fp)[3] = static_cast<float (*)[3]>(basis_key->data);
255
256 for (int i = 0; i < kb.totelem; ++i) {
257 /* Subtract the key positions from the
258 * basis positions to get the offsets. */
259 sub_v3_v3v3(offsets[i].data(), fp[i], basis_fp[i]);
260 }
261
262 offsets_attr.Set(offsets);
263 point_indices_attr.Set(indices);
264 }
265
266 /* Set the blend-shape names and targets on the shape. */
267 pxr::UsdAttribute blendshape_attr = skel_api.CreateBlendShapesAttr();
268 blendshape_attr.Set(blendshape_names);
269 skel_api.CreateBlendShapeTargetsRel().SetTargets(blendshape_paths);
270
271 /* Some DCCs seem to require joint indices and weights to
272 * bind the skeleton for blend-shapes, so we create these primvars, if needed. */
273
274 if (!skel_api.GetJointIndicesAttr().HasAuthoredValue()) {
275 pxr::VtArray<int> joint_indices(basis_totelem, 0);
276 skel_api.CreateJointIndicesPrimvar(false, 1).GetAttr().Set(joint_indices);
277 }
278
279 if (!skel_api.GetJointWeightsAttr().HasAuthoredValue()) {
280 pxr::VtArray<float> joint_weights(basis_totelem, 1.0f);
281 skel_api.CreateJointWeightsPrimvar(false, 1).GetAttr().Set(joint_weights);
282 }
283}
284
285pxr::VtFloatArray get_blendshape_weights(const Key *key)
286{
287 BLI_assert(key);
288
289 pxr::VtFloatArray weights;
290
291 for (KeyBlock &kb : key->block) {
292 if (&kb == key->block.first_) {
293 /* Skip the first key, which is the basis. */
294 continue;
295 }
296 weights.push_back(kb.curval);
297 }
298
299 return weights;
300}
301
302void remap_blend_shape_anim(pxr::UsdStageRefPtr stage,
303 const pxr::SdfPath &skel_path,
304 const pxr::SdfPathSet &mesh_paths)
305{
306 pxr::UsdSkelBindingAPI skel_api = pxr::UsdSkelBindingAPI::Get(stage, skel_path);
307
308 if (!skel_api) {
309 CLOG_WARN(&LOG, "Couldn't get skeleton from path %s", skel_path.GetAsString().c_str());
310 return;
311 }
312
313 /* Use existing animation if possible, otherwise create a new one. */
314 pxr::UsdPrim anim_prim;
315 pxr::UsdSkelAnimation anim;
316 if (skel_api.GetAnimationSource(&anim_prim)) {
317 anim = pxr::UsdSkelAnimation(anim_prim);
318 }
319 else {
320 pxr::SdfPath anim_path = skel_path.AppendChild(usdtokens::Anim);
321 anim = pxr::UsdSkelAnimation::Define(stage, anim_path);
322 }
323
324 if (!anim) {
325 CLOG_WARN(&LOG, "Couldn't get animation under skeleton %s", skel_path.GetAsString().c_str());
326 return;
327 }
328
330
331 /* We are merging blend shape names and weights from multiple
332 * meshes to a single animation. In case of name collisions,
333 * we must generate unique blend shape names for the merged
334 * result. This set keeps track of the unique names that will
335 * be combined on the animation. */
336 Set<std::string> merged_names;
337
338 /* Iterate over all the meshes, generate unique blend shape names in case of name
339 * collisions and set up the information we will need to merge the results. */
340 for (const pxr::SdfPath &mesh_path : mesh_paths) {
341
342 pxr::UsdPrim mesh_prim = stage->GetPrimAtPath(mesh_path);
343 pxr::UsdSkelBindingAPI mesh_skel_api = pxr::UsdSkelBindingAPI::Apply(mesh_prim);
344 if (!mesh_skel_api) {
345 CLOG_WARN(&LOG,
346 "Couldn't apply UsdSkelBindingAPI to mesh prim %s",
347 mesh_path.GetAsString().c_str());
348 continue;
349 }
350
351 /* Get the blend shape names for this mesh. */
352 pxr::UsdAttribute blend_shapes_attr = mesh_skel_api.GetBlendShapesAttr();
353
354 if (!blend_shapes_attr) {
355 continue;
356 }
357
358 pxr::VtTokenArray names;
359 if (!mesh_skel_api.GetBlendShapesAttr().Get(&names)) {
360 continue;
361 }
362
363 /* Ensure the names are unique. */
364 pxr::VtTokenArray unique_names;
365
366 for (const pxr::TfToken &name : names.AsConst()) {
367 std::string unique = add_unique_name(merged_names, name.GetString());
368 unique_names.push_back(pxr::TfToken(unique));
369 }
370
371 /* Set the unique names back on the mesh. */
372 mesh_skel_api.GetBlendShapesAttr().Set(unique_names);
373
374 /* Look up the temporary weights time sample we wrote to the mesh. */
375 const pxr::UsdAttribute temp_weights_attr = pxr::UsdGeomPrimvarsAPI(mesh_prim).GetPrimvar(
377
378 if (!temp_weights_attr) {
379 /* No need to create the animation. Shouldn't usually happen. */
380 return;
381 }
382
383 /* Generate information we will need to merge the weight samples below. */
384 merge_info.append(BlendShapeMergeInfo());
385 merge_info.last().src_blend_shapes = unique_names;
386 merge_info.last().src_weights_attr = temp_weights_attr;
387 }
388
389 if (merged_names.is_empty()) {
390 /* No blend shape names were collected. Shouldn't usually happen. */
391 return;
392 }
393
394 /* Copy the list of name strings to a list of tokens, since we need to work with tokens. */
395 pxr::VtTokenArray skel_blend_shape_names;
396 for (const std::string &name : merged_names) {
397 skel_blend_shape_names.push_back(pxr::TfToken(name));
398 }
399
400 /* Initialize the merge info structs with the list of names on the merged animation. */
401 for (BlendShapeMergeInfo &info : merge_info) {
402 info.init_anim_map(skel_blend_shape_names);
403 }
404
405 /* Set the names on the animation prim. */
406 anim.CreateBlendShapesAttr().Set(skel_blend_shape_names);
407
408 pxr::UsdAttribute dst_weights_attr = anim.CreateBlendShapeWeightsAttr();
409
410 /* Merge the weight time samples. */
411 std::vector<double> times;
412 merge_info.first().src_weights_attr.GetTimeSamples(&times);
413
414 if (times.empty()) {
415 /* Times may be empty if there is only a default value for the weights,
416 * so we read the default. */
417 times.push_back(pxr::UsdTimeCode::Default().GetValue());
418 }
419
420 pxr::VtFloatArray dst_weights;
421
422 for (const double time : times) {
423 for (const BlendShapeMergeInfo &info : merge_info) {
424 pxr::VtFloatArray src_weights;
425 if (info.src_weights_attr.Get(&src_weights, time)) {
426 if (!info.anim_map.Remap(src_weights.AsConst(), &dst_weights)) {
427 CLOG_WARN(&LOG, "Failed remapping blend shape weights");
428 }
429 }
430 }
431 /* Set the merged weights on the animation. */
432 dst_weights_attr.Set(dst_weights, time);
433 }
434}
435
437{
438 if (!obj || !obj->data || obj->type != OB_MESH) {
439 return nullptr;
440 }
441
442 /* If we're exporting blend shapes, we export the unmodified mesh with
443 * the verts in the basis key positions. */
445
446 if (!mesh || !mesh->key || !mesh->key->block.first_) {
447 return nullptr;
448 }
449
450 const KeyBlock *basis = reinterpret_cast<KeyBlock *>(mesh->key->block.first_);
451
452 if (mesh->verts_num != basis->totelem) {
453 CLOG_WARN(&LOG, "Vertex and shape key element count mismatch for mesh %s", obj->id.name + 2);
454 return nullptr;
455 }
456
457 /* Make a copy of the mesh so we can update the verts to the basis shape. */
458 Mesh *temp_mesh = BKE_mesh_copy_for_eval(*mesh);
459
460 /* Update the verts. */
461 BKE_keyblock_convert_to_mesh(basis, temp_mesh->vert_positions_for_write());
462
463 return temp_mesh;
464}
465
466} // namespace io::usd
467} // namespace blender
General operations, lookup, etc. for blender objects.
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Object is a sort of wrapper for general info.
bool is_empty() const
Definition BLI_set.hh:614
Set(Allocator allocator={}) noexcept
Definition BLI_set.hh:168
void append(const T &value)
const T & last(const int64_t n=0) const
const T & first() const
void remap_blend_shape_anim(pxr::UsdStageRefPtr stage, const pxr::SdfPath &skel_path, const pxr::SdfPathSet &mesh_paths)
void create_blend_shapes(pxr::UsdStageRefPtr stage, const Object *obj, const pxr::UsdPrim &mesh_prim, bool allow_unicode)
const Key * get_mesh_shape_key(const Object *obj)
std::string make_safe_name(const StringRef name, bool allow_unicode)
Definition usd_utils.cc:83
void ensure_blend_shape_skeleton(pxr::UsdStageRefPtr stage, pxr::UsdPrim &mesh_prim)
pxr::TfToken TempBlendShapeWeightsPrimvarName
pxr::VtFloatArray get_blendshape_weights(const Key *key)
Mesh * get_shape_key_basis_mesh(Object *obj)
bool is_mesh_with_shape_keys(const Object *obj)
static const pxr::TfToken joint1("joint1", pxr::TfToken::Immortal)
static const pxr::TfToken Anim("Anim", pxr::TfToken::Immortal)
static const pxr::TfToken Skel("Skel", pxr::TfToken::Immortal)
BMesh Iterators.
const Mesh * BKE_object_get_pre_modified_mesh(const Object *object)
Definition object.cc:4444
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
void BKE_keyblock_convert_to_mesh(const KeyBlock *kb, MutableSpan< float3 > vert_positions)
Definition key.cc:1697
BMesh const char void * data
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
static CLG_LogRef LOG
Definition logging.cpp:12
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< KeyBlock > block
ShapekeyContainerType type