Blender V4.3
usd_writer_transform.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
6
7#include <pxr/base/gf/matrix4d.h>
8#include <pxr/base/gf/matrix4f.h>
9#include <pxr/usd/usdGeom/xform.h>
10
11#include "BKE_object.hh"
12
13#include "BLI_math_matrix.h"
14#include "BLI_math_rotation.h"
15#include "BLI_string.h"
16#include "BLI_vector.hh"
17
18#include "CLG_log.h"
19static CLG_LogRef LOG = {"io.usd"};
20
21namespace blender::io::usd {
22
24
25pxr::UsdGeomXformable USDTransformWriter::create_xformable() const
26{
27 pxr::UsdGeomXform xform;
28
29 /* If prim exists, cast to #UsdGeomXform
30 * (Solves merge transform and shape issue for animated exports). */
31 pxr::UsdPrim existing_prim = usd_export_context_.stage->GetPrimAtPath(
33 if (existing_prim.IsValid() && existing_prim.IsA<pxr::UsdGeomXform>()) {
34 xform = pxr::UsdGeomXform(existing_prim);
35 }
36 else {
37 xform = pxr::UsdGeomXform::Define(usd_export_context_.stage, usd_export_context_.usd_path);
38 }
39
40 return pxr::UsdGeomXformable(xform.GetPrim());
41}
42
44{
46 return false;
47 }
48
50 return false;
51 }
52
53 if (context.export_parent != nullptr) {
54 return false;
55 }
56
57 return true;
58}
59
61{
62 constexpr float UNIT_M4[4][4] = {
63 {1, 0, 0, 0},
64 {0, 1, 0, 0},
65 {0, 0, 1, 0},
66 {0, 0, 0, 1},
67 };
68
69 pxr::UsdGeomXformable xform = create_xformable();
70
71 if (!xform) {
72 CLOG_ERROR(&LOG, "USDTransformWriter: couldn't create xformable");
73 return;
74 }
75
76 float parent_relative_matrix[4][4]; /* The object matrix relative to the parent. */
77
78 if (should_apply_root_xform(context)) {
79 float matrix_world[4][4];
80 copy_m4_m4(matrix_world, context.matrix_world);
81
83 float mrot[3][3];
84 float mat[4][4];
89 mrot);
90 transpose_m3(mrot);
91 copy_m4_m3(mat, mrot);
92 mul_m4_m4m4(matrix_world, mat, context.matrix_world);
93 }
94
95 mul_m4_m4m4(parent_relative_matrix, context.parent_matrix_inv_world, matrix_world);
96 }
97 else {
98 mul_m4_m4m4(parent_relative_matrix, context.parent_matrix_inv_world, context.matrix_world);
99 }
100
101 /* USD Xforms are by default the identity transform; only write if necessary when static. */
102 if (is_animated_ || !compare_m4m4(parent_relative_matrix, UNIT_M4, 0.000000001f)) {
103 set_xform_ops(parent_relative_matrix, xform);
104 }
105
106 if (context.object) {
107 auto prim = xform.GetPrim();
108 write_id_properties(prim, context.object->id, get_export_time_code());
109 }
110}
111
113{
114 if (context.duplicator != nullptr) {
115 /* This object is being duplicated, so could be emitted by a particle system and thus
116 * influenced by forces. TODO(Sybren): Make this more strict. Probably better to get from the
117 * depsgraph whether this object instance has a time source. */
118 return true;
119 }
120 if (check_has_physics(context)) {
121 return true;
122 }
123 return BKE_object_moves_in_time(context.object, context.animation_check_include_parent);
124}
125
126void USDTransformWriter::set_xform_ops(float xf_matrix[4][4], const pxr::UsdGeomXformable &xf)
127{
128 if (!xf) {
129 return;
130 }
131
133
134 if (xformOps_.is_empty()) {
135 switch (xfOpMode) {
136 case USD_XFORM_OP_TRS:
137 xformOps_.append(xf.AddTranslateOp());
138 xformOps_.append(xf.AddRotateXYZOp());
139 xformOps_.append(xf.AddScaleOp());
140 break;
141 case USD_XFORM_OP_TOS:
142 xformOps_.append(xf.AddTranslateOp());
143 xformOps_.append(xf.AddOrientOp());
144 xformOps_.append(xf.AddScaleOp());
145 break;
146 case USD_XFORM_OP_MAT:
147 xformOps_.append(xf.AddTransformOp());
148 break;
149 default:
150 CLOG_WARN(&LOG, "Unknown XformOp type");
151 xformOps_.append(xf.AddTransformOp());
152 break;
153 }
154 }
155
156 if (xformOps_.is_empty()) {
157 /* Shouldn't happen. */
158 return;
159 }
160
161 pxr::UsdTimeCode time_code = get_export_time_code();
162
163 if (xformOps_.size() == 1) {
164 pxr::GfMatrix4d mat_val(xf_matrix);
165 usd_value_writer_.SetAttribute(xformOps_[0].GetAttr(), mat_val, time_code);
166 }
167 else if (xformOps_.size() == 3) {
168
169 float loc[3];
170 float quat[4];
171 float scale[3];
172
173 mat4_decompose(loc, quat, scale, xf_matrix);
174
175 if (xfOpMode == USD_XFORM_OP_TRS) {
176 float rot[3];
177 quat_to_eul(rot, quat);
178 rot[0] *= 180.0 / M_PI;
179 rot[1] *= 180.0 / M_PI;
180 rot[2] *= 180.0 / M_PI;
181
182 pxr::GfVec3d loc_val(loc);
183 usd_value_writer_.SetAttribute(xformOps_[0].GetAttr(), loc_val, time_code);
184
185 pxr::GfVec3f rot_val(rot);
186 usd_value_writer_.SetAttribute(xformOps_[1].GetAttr(), rot_val, time_code);
187
188 pxr::GfVec3f scale_val(scale);
189 usd_value_writer_.SetAttribute(xformOps_[2].GetAttr(), scale_val, time_code);
190 }
191 else if (xfOpMode == USD_XFORM_OP_TOS) {
192 pxr::GfVec3d loc_val(loc);
193 usd_value_writer_.SetAttribute(xformOps_[0].GetAttr(), loc_val, time_code);
194
195 pxr::GfQuatf quat_val(quat[0], quat[1], quat[2], quat[3]);
196 usd_value_writer_.SetAttribute(xformOps_[1].GetAttr(), quat_val, time_code);
197
198 pxr::GfVec3f scale_val(scale);
199 usd_value_writer_.SetAttribute(xformOps_[2].GetAttr(), scale_val, time_code);
200 }
201 }
202}
203
204} // namespace blender::io::usd
General operations, lookup, etc. for blender objects.
bool BKE_object_moves_in_time(const Object *object, bool recurse_parent)
#define M_PI
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void mat4_decompose(float loc[3], float quat[4], float size[3], const float wmat[4][4])
void copy_m4_m3(float m1[4][4], const float m2[3][3])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
void transpose_m3(float R[3][3])
bool compare_m4m4(const float mat1[4][4], const float mat2[4][4], float limit)
void quat_to_eul(float eul[3], const float quat[4])
bool mat3_from_axis_conversion(int src_forward, int src_up, int dst_forward, int dst_up, float r_mat[3][3])
int char char int int int int size_t BLI_strnlen(const char *str, size_t maxlen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition string.c:909
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:182
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:181
@ IO_AXIS_Y
@ IO_AXIS_Z
int64_t size() const
void append(const T &value)
bool is_empty() const
static bool check_has_physics(const HierarchyContext &context)
pxr::UsdTimeCode get_export_time_code() const
pxr::UsdUtilsSparseValueWriter usd_value_writer_
void write_id_properties(const pxr::UsdPrim &prim, const ID &id, pxr::UsdTimeCode=pxr::UsdTimeCode::Default()) const
const USDExporterContext usd_export_context_
USDTransformWriter(const USDExporterContext &ctx)
bool check_is_animated(const HierarchyContext &context) const override
void do_write(HierarchyContext &context) override
void set_xform_ops(float parent_relative_matrix[4][4], const pxr::UsdGeomXformable &xf)
virtual pxr::UsdGeomXformable create_xformable() const
bool should_apply_root_xform(const HierarchyContext &context) const
#define rot(x, k)
#define LOG(severity)
Definition log.h:33
@ USD_XFORM_OP_TRS
Definition usd.hh:92
@ USD_XFORM_OP_TOS
Definition usd.hh:93
@ USD_XFORM_OP_MAT
Definition usd.hh:94
eUSDXformOpMode xform_op_mode
Definition usd.hh:162
static CLG_LogRef LOG