-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathprimWriter.cpp
More file actions
307 lines (251 loc) · 9.99 KB
/
primWriter.cpp
File metadata and controls
307 lines (251 loc) · 9.99 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "primWriter.h"
#include <mayaUsd/fileio/jobs/jobArgs.h>
#include <mayaUsd/fileio/translators/translatorGprim.h>
#include <mayaUsd/fileio/utils/adaptor.h>
#include <mayaUsd/fileio/utils/writeUtil.h>
#include <mayaUsd/fileio/writeJobContext.h>
#include <mayaUsd/utils/util.h>
#include <pxr/base/tf/diagnostic.h>
#include <pxr/base/tf/staticTokens.h>
#include <pxr/base/tf/token.h>
#include <pxr/base/vt/value.h>
#include <pxr/pxr.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/usd/prim.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usd/timeCode.h>
#include <pxr/usd/usdGeom/gprim.h>
#include <pxr/usd/usdGeom/imageable.h>
#include <pxr/usd/usdGeom/tokens.h>
#include <pxr/usd/usdUtils/sparseValueWriter.h>
#include <maya/MDagPath.h>
#include <maya/MFnDagNode.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MObject.h>
#include <maya/MStatus.h>
#include <maya/MString.h>
#include <string>
#include <typeinfo>
#include <vector>
PXR_NAMESPACE_OPEN_SCOPE
PXRUSDMAYA_REGISTER_ADAPTOR_ATTRIBUTE_ALIAS(UsdGeomTokens->purpose, "USD_purpose");
// clang-format off
TF_DEFINE_PRIVATE_TOKENS(
_tokens,
(USD_inheritClassNames)
);
// clang-format on
static bool _IsAnimated(const UsdMayaJobExportArgs& args, const MObject& obj)
{
if (!args.timeSamples.empty()) {
return UsdMayaUtil::isAnimated(obj);
}
return false;
}
UsdMayaPrimWriter::UsdMayaPrimWriter(
const MFnDependencyNode& depNodeFn,
const SdfPath& usdPath,
UsdMayaWriteJobContext& jobCtx)
: _writeJobCtx(jobCtx)
, _dagPath(UsdMayaUtil::getDagPath(depNodeFn))
, _mayaObject(depNodeFn.object())
, _usdPath(usdPath)
, _baseDagToUsdPaths(UsdMayaUtil::getDagPathMap(depNodeFn, usdPath))
, _exportVisibility(jobCtx.GetArgs().exportVisibility)
, _hasAnimCurves(_IsAnimated(jobCtx.GetArgs(), depNodeFn.object()))
{
}
/* virtual */
UsdMayaPrimWriter::~UsdMayaPrimWriter() { }
bool UsdMayaPrimWriter::_IsMergedTransform() const
{
return _writeJobCtx.IsMergedTransform(GetDagPath());
}
bool UsdMayaPrimWriter::_IsMergedShape() const
{
// For DG nodes, popping an invalid path will silently fail leaving the
// path invalid, and IsMergedTransform() returns false for invalid paths.
MDagPath parentPath = GetDagPath();
parentPath.pop();
return _writeJobCtx.IsMergedTransform(parentPath);
}
// In the future, we'd like to make this a plugin point.
static bool _GetClassNamesToWrite(const MObject& mObj, std::vector<std::string>* outClassNames)
{
return UsdMayaWriteUtil::ReadMayaAttribute(
MFnDependencyNode(mObj), MString(_tokens->USD_inheritClassNames.GetText()), outClassNames);
}
/* virtual */
void UsdMayaPrimWriter::Write(const UsdTimeCode& usdTime)
{
MStatus status;
const MFnDependencyNode depNodeFn(GetMayaObject(), &status);
if (status != MS::kSuccess) {
return;
}
// Note that the prim may not actually conform to this schema, so we must
// check it for validity before using it below.
UsdGeomImageable imageable(_usdPrim);
// Visibility is unfortunately special when merging transforms and shapes
// in that visibility is "pruning" and cannot be overridden by descendants.
// Thus, we arbitrarily say that when merging transforms and shapes, the
// _shape_ writer always writes visibility.
if (imageable && _exportVisibility && !_IsMergedTransform()) {
bool isVisible = true;
bool isVisAnimated = false;
UsdMayaUtil::getPlugValue(depNodeFn, "visibility", &isVisible, &isVisAnimated);
if (_IsMergedShape()) {
MDagPath parentDagPath = GetDagPath();
parentDagPath.pop();
const MFnDependencyNode parentDepNodeFn(parentDagPath.node());
bool parentIsVisible = true;
bool parentIsVisAnimated = false;
UsdMayaUtil::getPlugValue(
parentDepNodeFn, "visibility", &parentIsVisible, &parentIsVisAnimated);
// If BOTH the shape AND the transform are visible, then the
// prim is visible.
isVisible = isVisible && parentIsVisible;
// If the visibility of EITHER the shape OR the transform is
// animated, then the prim's visibility is animated.
isVisAnimated = isVisAnimated || parentIsVisAnimated;
}
// We write out the current visibility value to the default, regardless
// if it is animated or not. If we're not writing to default, we only
// write visibility if it's animated.
if (usdTime.IsDefault() || isVisAnimated) {
const TfToken& visibilityTok
= (isVisible ? UsdGeomTokens->inherited : UsdGeomTokens->invisible);
UsdMayaWriteUtil::SetAttribute(
imageable.CreateVisibilityAttr(VtValue(), true),
visibilityTok,
usdTime,
_GetSparseValueWriter());
}
}
if (usdTime.IsDefault()) {
// There is no Gprim abstraction in this module, so process the few
// Gprim attrs here.
// We imagine that many, but not all, prim writers will write Gprims,
// so it's OK to skip writing if this isn't a Gprim.
UsdGeomGprim gprim(_usdPrim);
if (gprim) {
GeomSidedness sidedness = GeomSidedness::Derived;
const TfToken sidednessArg = _writeJobCtx.GetArgs().geomSidedness;
if (sidednessArg == UsdMayaJobExportArgsTokens->derived) {
sidedness = GeomSidedness::Derived;
} else if (sidednessArg == UsdMayaJobExportArgsTokens->single) {
sidedness = GeomSidedness::Single;
} else if (sidednessArg == UsdMayaJobExportArgsTokens->double_) {
sidedness = GeomSidedness::Double;
} else {
TF_CODING_ERROR("Unsupported sidedness: %s", sidednessArg.GetString().c_str());
}
UsdMayaTranslatorGprim::Write(GetMayaObject(), gprim, nullptr, sidedness);
}
// Only write class inherits once at default time.
std::vector<std::string> classNames;
if (_GetClassNamesToWrite(GetMayaObject(), &classNames)) {
UsdMayaWriteUtil::WriteClassInherits(_usdPrim, classNames);
}
if (imageable) {
// Write UsdGeomImageable typed schema attributes.
// Currently only purpose, which is uniform, so only export at
// default time.
UsdMayaWriteUtil::WriteSchemaAttributesToPrim<UsdGeomImageable>(
GetMayaObject(),
_usdPrim,
{ UsdGeomTokens->purpose },
usdTime,
_GetSparseValueWriter());
}
// Write API schema attributes and strongly-typed metadata.
// We currently only support these at default time.
UsdMayaWriteUtil::WriteMetadataToPrim(GetMayaObject(), _usdPrim);
}
UsdMayaWriteUtil::WriteAPISchemaAttributesToPrim(
GetMayaObject(), _usdPrim, _GetExportArgs(), usdTime, _GetSparseValueWriter());
// Write out user-tagged attributes, which are supported at default time
// and at animated time-samples.
UsdMayaWriteUtil::WriteUserExportedAttributes(
GetMayaObject(), _usdPrim, usdTime, _GetSparseValueWriter());
}
/* virtual */
bool UsdMayaPrimWriter::ExportsGprims() const { return false; }
/* virtual */
bool UsdMayaPrimWriter::ShouldPruneChildren() const { return false; }
/* virtual */
void UsdMayaPrimWriter::PostExport() { MakeSingleSamplesStatic(); }
void UsdMayaPrimWriter::SetExportVisibility(const bool exportVis) { _exportVisibility = exportVis; }
bool UsdMayaPrimWriter::GetExportVisibility() const { return _exportVisibility; }
/* virtual */
const SdfPathVector& UsdMayaPrimWriter::GetModelPaths() const
{
static const SdfPathVector empty;
return empty;
}
/* virtual */
const UsdMayaUtil::MDagPathMap<SdfPath>& UsdMayaPrimWriter::GetDagToUsdPathMapping() const
{
return _baseDagToUsdPaths;
}
const MDagPath& UsdMayaPrimWriter::GetDagPath() const { return _dagPath; }
const MObject& UsdMayaPrimWriter::GetMayaObject() const { return _mayaObject; }
const SdfPath& UsdMayaPrimWriter::GetUsdPath() const { return _usdPath; }
const UsdPrim& UsdMayaPrimWriter::GetUsdPrim() const { return _usdPrim; }
void UsdMayaPrimWriter::_SetUsdPrim(const UsdPrim& usdPrim) { _usdPrim = usdPrim; }
const UsdStageRefPtr& UsdMayaPrimWriter::GetUsdStage() const { return _writeJobCtx.GetUsdStage(); }
const UsdMayaJobExportArgs& UsdMayaPrimWriter::_GetExportArgs() const
{
return _writeJobCtx.GetArgs();
}
UsdUtilsSparseValueWriter* UsdMayaPrimWriter::_GetSparseValueWriter()
{
if (_writeJobCtx.GetArgs().disableSparseSamples)
return nullptr;
return &_valueWriter;
}
void UsdMayaPrimWriter::MakeSingleSamplesStatic()
{
auto exportArgs = _GetExportArgs();
if (!exportArgs.staticSingleSample) {
return;
}
UsdPrim prim = GetUsdPrim();
if (!prim.IsValid()) {
return;
}
for (auto& attr : prim.GetAttributes()) {
MakeSingleSamplesStatic(attr);
}
};
void UsdMayaPrimWriter::MakeSingleSamplesStatic(UsdAttribute attr)
{
std::vector<double> samples;
if (attr.GetNumTimeSamples() != 1) {
return;
}
attr.GetTimeSamples(&samples);
VtValue sample;
attr.Get(&sample, samples[0]);
// Clear all time samples and then sets a default
attr.Clear();
attr.Set(sample);
}
/* virtual */
bool UsdMayaPrimWriter::_HasAnimCurves() const { return _hasAnimCurves; }
PXR_NAMESPACE_CLOSE_SCOPE