Blender V5.3
usd_reader_xform.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Tangent Animation. All rights reserved.
2 * SPDX-FileCopyrightText: 2023 Blender Authors
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 *
6 * Adapted from the Blender Alembic importer implementation. */
7
8#include "usd_reader_xform.hh"
9
10#include "BKE_constraint.h"
11#include "BKE_lib_id.hh"
12#include "BKE_object.hh"
13
14#include "BLI_math_matrix.hh"
16#include "BLI_string.hh"
17
18#include "DNA_cachefile_types.h"
20#include "DNA_object_types.h"
21
22#include <pxr/base/gf/matrix4f.h>
23#include <pxr/usd/usdGeom/xformable.h>
24
25#include <string>
26
27namespace blender::io::usd {
28
35
36void USDXformReader::read_object_data(Main * /*bmain*/, const pxr::UsdTimeCode time)
37{
38 bool is_constant;
39 float4x4 transform_from_usd;
40
41 read_matrix(transform_from_usd, time, settings_->scene_scale, &is_constant);
42
43 if (!is_constant && settings_->get_cache_file) {
47
48 pxr::SdfPath object_path = use_parent_xform_ ? prim_.GetParent().GetPath() : this->prim_path();
49
50 STRNCPY(data->object_path, object_path.GetAsString().c_str());
51
52 data->cache_file = settings_->get_cache_file();
53 id_us_plus(&data->cache_file->id);
54 }
55
56 BKE_object_apply_mat4(object_, transform_from_usd.ptr(), true, false);
57
58 /* Make sure to collect custom attributes */
60}
61
63{
64 return get_xformable().GetPrim().GetPath();
65}
66
67void USDXformReader::read_matrix(float4x4 &r_mat /* local matrix */,
68 const pxr::UsdTimeCode time,
69 const float scale,
70 bool *r_is_constant) const
71{
72 BLI_assert(r_is_constant);
73
74 *r_is_constant = true;
75 r_mat = float4x4::identity();
76
77 std::optional<XformResult> xf_result = get_local_usd_xform(time);
78 if (!xf_result) {
79 return;
80 }
81
82 std::get<0>(*xf_result).Get(r_mat.ptr());
83 *r_is_constant = std::get<1>(*xf_result);
84
85 /* Apply global scaling and rotation only to root objects, parenting
86 * will propagate it. */
87 if (is_root_xform_prim()) {
88 if (scale != 1.0f) {
90 r_mat = mat_scale * r_mat;
91 }
92
94 r_mat = settings_->conversion_mat * r_mat;
95 }
96 }
97}
98
100{
101 const pxr::UsdGeomXformable xformable(prim_);
102
103 if (!xformable) {
104 /* This might happen if the prim is a Scope. */
105 return false;
106 }
107
108 bool reset_xform_stack = false;
109
110 return !xformable.GetOrderedXformOps(&reset_xform_stack).empty();
111}
112
114{
115 if (!prim_.IsValid()) {
116 return false;
117 }
118
119 if (is_in_proto()) {
120 /* We don't consider prototypes to be root prims,
121 * because we never want to apply global scaling
122 * or rotations to the prototypes themselves. */
123 return false;
124 }
125
126 if (prim_.IsA<pxr::UsdGeomXformable>()) {
127 /* If this prim doesn't have an ancestor that's a
128 * UsdGeomXformable, then it's a root prim. Note
129 * that it's not sufficient to only check the immediate
130 * parent prim, since the immediate parent could be a
131 * UsdGeomScope that has an xformable ancestor. */
132 pxr::UsdPrim cur_parent = prim_.GetParent();
133
134 if (use_parent_xform_) {
135 cur_parent = cur_parent.GetParent();
136 }
137
138 while (cur_parent && !cur_parent.IsPseudoRoot()) {
139 if (cur_parent.IsA<pxr::UsdGeomXformable>()) {
140 return false;
141 }
142 cur_parent = cur_parent.GetParent();
143 }
144
145 /* We didn't find an xformable ancestor. */
146 return true;
147 }
148
149 return false;
150}
151
152std::optional<XformResult> USDXformReader::get_local_usd_xform(const pxr::UsdTimeCode time) const
153{
154 const pxr::UsdGeomXformable xformable = get_xformable();
155
156 if (!xformable) {
157 /* This might happen if the prim is a Scope. */
158 return std::nullopt;
159 }
160
161 bool is_constant = !xformable.TransformMightBeTimeVarying();
162
163 bool reset_xform_stack;
164 pxr::GfMatrix4d xform;
165 if (!xformable.GetLocalTransformation(&xform, &reset_xform_stack, time)) {
166 return std::nullopt;
167 }
168
169 /* The USD bind transform is a matrix of doubles,
170 * but we cast it to GfMatrix4f because Blender expects
171 * a matrix of floats. */
172 return XformResult(pxr::GfMatrix4f(xform), is_constant);
173}
174
175pxr::UsdGeomXformable USDXformReader::get_xformable() const
176{
177 pxr::UsdPrim prim = use_parent_xform_ ? prim_.GetParent() : prim_;
178 return pxr::UsdGeomXformable(prim);
179}
180} // namespace blender::io::usd
General operations, lookup, etc. for blender objects.
Object is a sort of wrapper for general info.
constexpr const char * c_str() const
const pxr::UsdPrim & prim() const
const ImportSettings * settings_
void set_props(bool merge_with_parent=false, pxr::UsdTimeCode time=pxr::UsdTimeCode::Default())
void create_object(Main *bmain) override
void read_object_data(Main *bmain, pxr::UsdTimeCode time) override
virtual std::optional< XformResult > get_local_usd_xform(pxr::UsdTimeCode time) const
void read_matrix(float4x4 &r_mat, pxr::UsdTimeCode time, float scale, bool *r_is_constant) const
virtual bool is_root_xform_prim() const
pxr::SdfPath object_prim_path() const override
float3x3 scale(float3x3 mat, float2 scale)
std::tuple< pxr::GfMatrix4f, bool > XformResult
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * STRNCPY(char(&dst)[N], const char *src)
@ CONSTRAINT_TYPE_TRANSFORM_CACHE
void id_us_plus(ID *id)
Definition lib_id.cc:433
MatBase< float, 4, 4 > float4x4
struct bConstraint * BKE_constraint_add_for_object(struct Object *ob, const char *name, eBConstraint_Types type)
void BKE_object_apply_mat4(Object *ob, const float mat[4][4], bool use_compat, bool use_parent)
Definition object.cc:3663
BMesh const char void * data
VecBase< float, 3 > float3
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
const c_style_mat & ptr() const
std::function< CacheFile *()> get_cache_file