-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfileFormat.cpp
190 lines (157 loc) · 5.69 KB
/
fileFormat.cpp
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
//
// Copyright © 2024 Weta Digital Limited
//
// SPDX-License-Identifier: Apache-2.0
//
#include "fileFormat.h"
#include <pxr/pxr.h>
#include <pxr/usd/pcp/dynamicFileFormatContext.h>
#include <pxr/usd/usd/usdaFileFormat.h>
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usdGeom/mesh.h>
#include <fstream>
#include <string>
PXR_NAMESPACE_OPEN_SCOPE
// Default value of the triangle side length.
static const double defaultSideLengthValue = 1.0;
// Convenience function to extract the length value from a file format context.
static double
_ExtractSideLengthFromContext(const PcpDynamicFileFormatContext& context)
{
// Default sideLength.
double sideLength = defaultSideLengthValue;
VtValue value;
if (!context.ComposeValue(UsdTriangleFileFormatTokens->SideLength,
&value) ||
value.IsEmpty()) {
return sideLength;
}
if (!value.IsHolding<double>()) {
TF_CODING_ERROR("Expected '%s' value to hold a double, got '%s'",
UsdTriangleFileFormatTokens->SideLength.GetText(),
TfStringify(value).c_str());
return sideLength;
}
return value.UncheckedGet<double>();
}
static double
_ExtractSideLengthFromArgs(const SdfFileFormat::FileFormatArguments& args)
{
// Default sideLength.
double sideLength = defaultSideLengthValue;
// Find "sideLength" file format argument.
auto it = args.find(UsdTriangleFileFormatTokens->SideLength);
if (it == args.end()) {
return sideLength;
}
// Try to convert the string value to the actual output value type.
double extractVal;
bool success = true;
extractVal = TfUnstringify<double>(it->second, &success);
if (!success) {
TF_CODING_ERROR(
"Could not convert arg string '%s' to value of type double",
UsdTriangleFileFormatTokens->SideLength.GetText());
return sideLength;
}
sideLength = extractVal;
return sideLength;
}
TF_DEFINE_PUBLIC_TOKENS(UsdTriangleFileFormatTokens,
USD_TRIANGLE_FILE_FORMAT_TOKENS);
TF_REGISTRY_FUNCTION(TfType)
{
SDF_DEFINE_FILE_FORMAT(UsdTriangleFileFormat, SdfFileFormat);
}
UsdTriangleFileFormat::UsdTriangleFileFormat()
: SdfFileFormat(UsdTriangleFileFormatTokens->Id,
UsdTriangleFileFormatTokens->Version,
UsdTriangleFileFormatTokens->Target,
UsdTriangleFileFormatTokens->Extension)
{
}
UsdTriangleFileFormat::~UsdTriangleFileFormat() {}
bool
UsdTriangleFileFormat::CanRead(const std::string& filePath) const
{
return true;
}
bool
UsdTriangleFileFormat::Read(SdfLayer* layer,
const std::string& resolvedPath,
bool metadataOnly) const
{
if (!TF_VERIFY(layer)) {
return false;
}
// Extract file format arguments.
FileFormatArguments args;
std::string layerPath;
SdfLayer::SplitIdentifier(layer->GetIdentifier(), &layerPath, &args);
double sideLength = _ExtractSideLengthFromArgs(args);
// Create a new anonymous layer and wrap a stage around it.
SdfLayerRefPtr newLayer = SdfLayer::CreateAnonymous(".usd");
UsdStageRefPtr stage = UsdStage::Open(newLayer);
// Define a Mesh, set as default prim.
UsdGeomMesh mesh = UsdGeomMesh::Define(stage, SdfPath("/Triangle"));
stage->SetDefaultPrim(mesh.GetPrim());
// Author mesh attributes (describing a length-scaled equilateral triangle).
VtIntArray faceVertexCounts(1, 3);
mesh.CreateFaceVertexCountsAttr(VtValue(faceVertexCounts));
VtIntArray faceVertexIndices{ 0, 1, 2 };
mesh.CreateFaceVertexIndicesAttr(VtValue(faceVertexIndices));
VtVec3fArray points{
GfVec3f(0.0f, 0.57735027f * sideLength, 0.0f),
GfVec3f(-0.5f * sideLength, -0.28867513f * sideLength, 0.0f),
GfVec3f(0.5f * sideLength, -0.28867513f * sideLength, 0.0f)
};
mesh.CreatePointsAttr(VtValue(points));
// Copy contents into output layer.
layer->TransferContent(newLayer);
return true;
}
bool
UsdTriangleFileFormat::WriteToString(const SdfLayer& layer,
std::string* str,
const std::string& comment) const
{
// Write the generated contents in usda text format.
return SdfFileFormat::FindById(UsdUsdaFileFormatTokens->Id)
->WriteToString(layer, str, comment);
}
bool
UsdTriangleFileFormat::WriteToStream(const SdfSpecHandle& spec,
std::ostream& out,
size_t indent) const
{
// Write the generated contents in usda text format.
return SdfFileFormat::FindById(UsdUsdaFileFormatTokens->Id)
->WriteToStream(spec, out, indent);
}
void
UsdTriangleFileFormat::ComposeFieldsForFileFormatArguments(
const std::string& assetPath,
const PcpDynamicFileFormatContext& context,
FileFormatArguments* args,
VtValue* contextDependencyData) const
{
double sideLength = _ExtractSideLengthFromContext(context);
(*args)[UsdTriangleFileFormatTokens->SideLength] = TfStringify(sideLength);
}
bool
UsdTriangleFileFormat::CanFieldChangeAffectFileFormatArguments(
const TfToken& field,
const VtValue& oldValue,
const VtValue& newValue,
const VtValue& contextDependencyData) const
{
// Check if the "sideLength" argument changed.
double oldLength = oldValue.IsHolding<double>()
? oldValue.UncheckedGet<double>()
: defaultSideLengthValue;
double newLength = newValue.IsHolding<double>()
? newValue.UncheckedGet<double>()
: defaultSideLengthValue;
return oldLength != newLength;
}
PXR_NAMESPACE_CLOSE_SCOPE