1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2020 Blender Foundation.
* All rights reserved.
*/
#include "usd_reader_xformable.h"
#include "usd_import_util.h"
#include "BKE_lib_id.h"
#include "BKE_object.h"
#include "DNA_object_types.h"
#include "BLI_listbase.h"
#include "BLI_math_geom.h"
#include "BLI_string.h"
#include "BLI_utildefines.h"
#include <pxr/base/gf/matrix4d.h>
#include <pxr/base/gf/matrix4f.h>
#include <pxr/usd/usdGeom/xformable.h>
#include <iostream>
namespace blender::io::usd {
USDXformableReader::USDXformableReader(const pxr::UsdPrim &prim, const USDImporterContext &context)
: USDPrimReader(prim, context), object_(nullptr), parent_(nullptr), merged_with_parent_(false)
{
}
USDXformableReader::~USDXformableReader()
{
}
Object *USDXformableReader::object() const
{
return object_;
}
void USDXformableReader::set_object_transform(const double time)
{
if (!this->object_) {
return;
}
float transform_from_usd[4][4];
this->read_matrix(transform_from_usd, time, this->context_.import_params.scale);
/* Apply the matrix to the object. */
BKE_object_apply_mat4(object_, transform_from_usd, true, false);
BKE_object_to_mat4(object_, object_->obmat);
/* TODO(makowalski): Set up transform constraint if not constant. */
}
void USDXformableReader::read_matrix(float r_mat[4][4] /* local matrix */,
const double time,
const float scale) const
{
pxr::UsdGeomXformable xformable(prim_);
if (!xformable) {
unit_m4(r_mat);
return;
}
pxr::GfMatrix4d usd_local_xf;
bool reset_xform_stack;
xformable.GetLocalTransformation(&usd_local_xf, &reset_xform_stack, time);
if (merged_with_parent_) {
/* Take into account the parent's local xform. */
pxr::UsdGeomXformable parent_xformable(prim_.GetParent());
if (parent_xformable) {
pxr::GfMatrix4d usd_parent_local_xf;
parent_xformable.GetLocalTransformation(&usd_parent_local_xf, &reset_xform_stack, time);
usd_local_xf = usd_local_xf * usd_parent_local_xf;
}
}
// Convert the result to a float matrix.
pxr::GfMatrix4f mat4f = pxr::GfMatrix4f(usd_local_xf);
mat4f.Get(r_mat);
if (this->context_.stage_up_axis == pxr::UsdGeomTokens->y) {
/* Swap the matrix from y-up to z-up. */
copy_m44_axis_swap(r_mat, r_mat, USD_ZUP_FROM_YUP);
}
/* Apply scaling only to root objects, parenting will propagate it. */
if (!this->parent_) {
float scale_mat[4][4];
scale_m4_fl(scale_mat, scale);
mul_m4_m4m4(r_mat, scale_mat, r_mat);
}
}
} /* namespace blender::io::usd */
|