Blender V5.3
usd_export_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "testing/testing.h"
7
8#include <pxr/base/tf/stringUtils.h>
9#include <pxr/base/vt/types.h>
10#include <pxr/base/vt/value.h>
11#include <pxr/usd/sdf/types.h>
12#include <pxr/usd/usd/common.h>
13#include <pxr/usd/usd/prim.h>
14#include <pxr/usd/usd/stage.h>
15#include <pxr/usd/usdGeom/mesh.h>
16
17#include "DNA_image_types.h"
18#include "DNA_material_types.h"
19#include "DNA_mesh_types.h"
20#include "DNA_node_types.h"
21
22#include "BKE_context.hh"
23#include "BKE_lib_id.hh"
24#include "BKE_main.hh"
25#include "BKE_mesh_types.hh"
26
27#include "BLI_fileops.hh"
28#include "BLI_listbase.hh"
30#include "BLI_path_utils.hh"
31
32#include "BLO_readfile.hh"
33
34#include "BKE_node_runtime.hh"
35
36#include "DEG_depsgraph.hh"
37
38#include "usd.hh"
39#include "usd_utils.hh"
41
42namespace blender::io::usd {
43
44const StringRefNull simple_scene_filename = "usd/usd_simple_scene.blend";
45const StringRefNull materials_filename = "usd/usd_materials_export.blend";
46const StringRefNull output_filename = "output.usd";
47
48static const bNode *find_node_for_type_in_graph(const bNodeTree *nodetree,
49 const UString type_idname);
50
52 protected:
53 bContext *context = nullptr;
54
55 public:
57 const eEvaluationMode eval_mode = DAG_EVAL_VIEWPORT)
58 {
59 if (!blendfile_load(filepath.c_str())) {
60 return false;
61 }
62 depsgraph_create(eval_mode);
63
67
68 return true;
69 }
70
71 void SetUp() override
72 {
73 BlendfileLoadingBaseTest::SetUp();
74 }
75
76 void TearDown() override
77 {
80 context = nullptr;
81
83 BLI_delete(output_filename.c_str(), false, false);
84 }
85 }
86
87 pxr::UsdPrim get_first_child_mesh(const pxr::UsdPrim prim)
88 {
89 for (auto child : prim.GetChildren()) {
90 if (child.IsA<pxr::UsdGeomMesh>()) {
91 return child;
92 }
93 }
94 return pxr::UsdPrim();
95 }
96
101 void compare_blender_node_to_usd_prim(const bNode *bsdf_node, const pxr::UsdPrim &bsdf_prim)
102 {
103 ASSERT_NE(bsdf_node, nullptr);
104 ASSERT_TRUE(bool(bsdf_prim));
105
106 for (const auto *socket : bsdf_node->input_sockets()) {
107 const pxr::TfToken attribute_token = io::usd::token_for_input(socket->name);
108 if (attribute_token.IsEmpty()) {
109 /* This socket is not translated between Blender and USD. */
110 continue;
111 }
112
113 const pxr::UsdAttribute bsdf_attribute = bsdf_prim.GetAttribute(attribute_token);
114 pxr::SdfPathVector paths;
115 bsdf_attribute.GetConnections(&paths);
116 if (!paths.empty() || !bsdf_attribute.IsValid()) {
117 /* Skip if the attribute is connected or has an error. */
118 continue;
119 }
120
121 const float socket_value_f = *socket->default_value_typed<float>();
122 const float3 socket_value_3f = *socket->default_value_typed<float3>();
123 float attribute_value_f;
124 pxr::GfVec3f attribute_value_3f;
125
126 switch (socket->type) {
127 case SOCK_FLOAT:
128 bsdf_attribute.Get(&attribute_value_f, 0.0);
129 EXPECT_FLOAT_EQ(socket_value_f, attribute_value_f);
130 break;
131
132 case SOCK_VECTOR:
133 bsdf_attribute.Get(&attribute_value_3f, 0.0);
134 EXPECT_FLOAT_EQ(socket_value_3f[0], attribute_value_3f[0]);
135 EXPECT_FLOAT_EQ(socket_value_3f[1], attribute_value_3f[1]);
136 EXPECT_FLOAT_EQ(socket_value_3f[2], attribute_value_3f[2]);
137 break;
138
139 case SOCK_RGBA:
140 bsdf_attribute.Get(&attribute_value_3f, 0.0);
141 EXPECT_FLOAT_EQ(socket_value_3f[0], attribute_value_3f[0]);
142 EXPECT_FLOAT_EQ(socket_value_3f[1], attribute_value_3f[1]);
143 EXPECT_FLOAT_EQ(socket_value_3f[2], attribute_value_3f[2]);
144 break;
145
146 default:
147 FAIL() << "Socket " << socket->name << " has unsupported type " << socket->type;
148 break;
149 }
150 }
151 }
152
154 const pxr::UsdPrim &image_prim)
155 {
156 const Image *image = reinterpret_cast<Image *>(image_node->id);
157
158 const pxr::UsdShadeShader image_shader(image_prim);
159 const pxr::UsdShadeInput file_input = image_shader.GetInput(pxr::TfToken("file"));
160 EXPECT_TRUE(bool(file_input));
161
162 pxr::VtValue file_val;
163 EXPECT_TRUE(file_input.Get(&file_val));
164 EXPECT_TRUE(file_val.IsHolding<pxr::SdfAssetPath>());
165
166 pxr::SdfAssetPath image_prim_asset = file_val.Get<pxr::SdfAssetPath>();
167
168 /* The path is expected to be relative, but that means in Blender the
169 * path will start with //.
170 */
171 EXPECT_EQ(
172 BLI_path_cmp_normalized(image->filepath + 2, image_prim_asset.GetAssetPath().c_str()), 0);
173 }
174
175 /*
176 * Determine if a Blender Mesh matches a UsdGeomMesh prim by checking counts
177 * on vertices, faces, face indices, and normals.
178 */
179 void compare_blender_mesh_to_usd_prim(const Mesh *mesh, const pxr::UsdGeomMesh &mesh_prim)
180 {
181 pxr::VtIntArray face_indices;
182 pxr::VtIntArray face_counts;
183 pxr::VtVec3fArray positions;
184 pxr::VtVec3fArray normals;
185
186 /* Our export doesn't use `primvars:normals` so we're not
187 * looking for that to be written here. */
188 mesh_prim.GetFaceVertexIndicesAttr().Get(&face_indices, 0.0);
189 mesh_prim.GetFaceVertexCountsAttr().Get(&face_counts, 0.0);
190 mesh_prim.GetPointsAttr().Get(&positions, 0.0);
191 mesh_prim.GetNormalsAttr().Get(&normals, 0.0);
192
193 EXPECT_EQ(mesh->verts_num, positions.size());
194 EXPECT_EQ(mesh->faces_num, face_counts.size());
195 EXPECT_EQ(mesh->corners_num, face_indices.size());
196 switch (mesh->normals_domain()) {
198 EXPECT_EQ(mesh_prim.GetNormalsInterpolation(), pxr::UsdGeomTokens->vertex);
199 EXPECT_EQ(mesh->verts_num, normals.size());
200 break;
201 }
203 EXPECT_EQ(mesh_prim.GetNormalsInterpolation(), pxr::UsdGeomTokens->uniform);
204 EXPECT_EQ(mesh->faces_num, normals.size());
205 break;
206 }
208 EXPECT_EQ(mesh_prim.GetNormalsInterpolation(), pxr::UsdGeomTokens->faceVarying);
209 EXPECT_EQ(mesh->corners_num, normals.size());
210 break;
211 }
212 }
213 }
214};
215
216TEST_F(UsdExportTest, usd_export_rain_mesh)
217{
218 if (!load_file_and_depsgraph(simple_scene_filename)) {
219 FAIL() << "Unable to load file: " << simple_scene_filename;
220 return;
221 }
222
223 /* File sanity check. */
224 EXPECT_EQ(bfile->main->objects.count(), 3);
225
226 USDExportParams params;
227 params.export_materials = false;
228 params.export_normals = true;
229 params.export_uvmaps = false;
230
231 bool result = USD_export(context, output_filename.c_str(), &params, false, nullptr);
232 ASSERT_TRUE(result) << "Writing to " << output_filename << " failed!";
233
234 pxr::UsdStageRefPtr stage = pxr::UsdStage::Open(output_filename);
235 ASSERT_TRUE(bool(stage)) << "Unable to load Stage from " << output_filename;
236
237 /*
238 * Run the mesh comparison for all Meshes in the original scene.
239 */
240 for (Object &object : bfile->main->objects) {
241 const Mesh *mesh = id_cast<Mesh *>(object.data);
242 const StringRefNull object_name(object.id.name + 2);
243
244 const pxr::SdfPath sdf_path("/" + pxr::TfMakeValidIdentifier(object_name.c_str()));
245 pxr::UsdPrim prim = stage->GetPrimAtPath(sdf_path);
246 EXPECT_TRUE(bool(prim));
247
248 const pxr::UsdGeomMesh mesh_prim(get_first_child_mesh(prim));
249 EXPECT_TRUE(bool(mesh_prim));
250
251 compare_blender_mesh_to_usd_prim(mesh, mesh_prim);
252 }
253}
254
255static const bNode *find_node_for_type_in_graph(const bNodeTree *nodetree,
256 const UString type_idname)
257{
258 auto found_nodes = nodetree->nodes_by_type(type_idname);
259 if (found_nodes.size() == 1) {
260 return found_nodes[0];
261 }
262
263 return nullptr;
264}
265
266/*
267 * Export Material test-- export a scene with a material, then read it back
268 * in and check that the BSDF and Image Texture nodes translated correctly
269 * by comparing values between the exported USD stage and the objects in
270 * memory.
271 */
272TEST_F(UsdExportTest, usd_export_material)
273{
274 if (!load_file_and_depsgraph(materials_filename)) {
275 FAIL() << "Unable to load file: " << materials_filename;
276 return;
277 }
278
279 /* File sanity checks. */
280 EXPECT_EQ(bfile->main->objects.count(), 6);
281 /* There is 1 additional material because of the "Dots Stroke". */
282 EXPECT_EQ(bfile->main->materials.count(), 7);
283
284 Material *material = reinterpret_cast<Material *>(
285 BKE_libblock_find_name(bfile->main, ID_MA, "Material"));
286
287 EXPECT_TRUE(bool(material));
288
289 USDExportParams params;
290 params.export_materials = true;
291 params.export_normals = true;
292 params.export_textures = false;
293 params.export_uvmaps = true;
294 params.generate_preview_surface = true;
295 params.generate_materialx_network = false;
296 params.convert_world_material = false;
297 params.relative_paths = false;
298
299 const bool result = USD_export(context, output_filename.c_str(), &params, false, nullptr);
300 ASSERT_TRUE(result) << "Unable to export stage to " << output_filename;
301
302 pxr::UsdStageRefPtr stage = pxr::UsdStage::Open(output_filename);
303 ASSERT_NE(stage, nullptr) << "Unable to open exported stage: " << output_filename;
304
305 material->nodetree->ensure_topology_cache();
306 const bNode *bsdf_node = find_node_for_type_in_graph(material->nodetree,
307 "ShaderNodeBsdfPrincipled"_ustr);
308
309 const std::string prim_name = pxr::TfMakeValidIdentifier(bsdf_node->name);
310 const pxr::UsdPrim bsdf_prim = stage->GetPrimAtPath(
311 pxr::SdfPath("/_materials/Material/" + prim_name));
312
313 compare_blender_node_to_usd_prim(bsdf_node, bsdf_prim);
314
315 const bNode *image_node = find_node_for_type_in_graph(material->nodetree,
316 "ShaderNodeTexImage"_ustr);
317 ASSERT_NE(image_node, nullptr);
318 ASSERT_NE(image_node->storage, nullptr);
319
320 const std::string image_prim_name = pxr::TfMakeValidIdentifier(image_node->name);
321
322 const pxr::UsdPrim image_prim = stage->GetPrimAtPath(
323 pxr::SdfPath("/_materials/Material/" + image_prim_name));
324
325 ASSERT_TRUE(bool(image_prim)) << "Unable to find Material prim from exported stage "
327
328 compare_blender_image_to_usd_image_shader(image_node, image_prim);
329}
330
332{
333 /* ASCII variations. */
334 ASSERT_EQ(make_safe_name("", false), std::string("_"));
335 ASSERT_EQ(make_safe_name("|", false), std::string("_"));
336 ASSERT_EQ(make_safe_name("1", false), std::string("_1"));
337 ASSERT_EQ(make_safe_name("1Test", false), std::string("_1Test"));
338
339 ASSERT_EQ(make_safe_name(":", false), std::string("_"));
340 ASSERT_EQ(make_safe_name("test:", false), std::string("test_"));
341 ASSERT_EQ(make_safe_name(":test", false), std::string("_test"));
342 ASSERT_EQ(make_safe_name("test:test", false), std::string("test_test"));
343
344 ASSERT_EQ(make_safe_name("Test", false), std::string("Test"));
345 ASSERT_EQ(make_safe_name("Test|$bézier @ world", false), std::string("Test__b_zier___world"));
346 ASSERT_EQ(make_safe_name("Test|ハローワールド", false), std::string("Test________"));
347 ASSERT_EQ(make_safe_name("Test|Γεια σου κόσμε", false), std::string("Test_______________"));
348 ASSERT_EQ(make_safe_name("Test|∧hello ○ wórld", false), std::string("Test__hello___w_rld"));
349
350 /* Unicode variations. */
351 ASSERT_EQ(make_safe_name("", true), std::string("_"));
352 ASSERT_EQ(make_safe_name("|", true), std::string("_"));
353 ASSERT_EQ(make_safe_name("1", true), std::string("_1"));
354 ASSERT_EQ(make_safe_name("1Test", true), std::string("_1Test"));
355
356 ASSERT_EQ(make_safe_name(":", true), std::string("_"));
357 ASSERT_EQ(make_safe_name("test:", true), std::string("test_"));
358 ASSERT_EQ(make_safe_name(":test", true), std::string("_test"));
359 ASSERT_EQ(make_safe_name("test:test", true), std::string("test_test"));
360
361 ASSERT_EQ(make_safe_name("Test", true), std::string("Test"));
362 ASSERT_EQ(make_safe_name("Test|$bézier @ world", true), std::string("Test__bézier___world"));
363 ASSERT_EQ(make_safe_name("Test|ハローワールド", true), std::string("Test_ハローワールド"));
364 ASSERT_EQ(make_safe_name("Test|Γεια σου κόσμε", true), std::string("Test_Γεια_σου_κόσμε"));
365 ASSERT_EQ(make_safe_name("Test|∧hello ○ wórld", true), std::string("Test__hello___wórld"));
366}
367
369{
370 /* ASCII variations. */
371 ASSERT_EQ(make_safe_primvar_name("", false), std::string("_"));
372 ASSERT_EQ(make_safe_primvar_name("|", false), std::string("_"));
373 ASSERT_EQ(make_safe_primvar_name("1", false), std::string("_1"));
374 ASSERT_EQ(make_safe_primvar_name("1Test", false), std::string("_1Test"));
375
376 ASSERT_EQ(make_safe_primvar_name(":", false), std::string("_:_"));
377 ASSERT_EQ(make_safe_primvar_name("test:", false), std::string("test:_"));
378 ASSERT_EQ(make_safe_primvar_name(":test", false), std::string("_:test"));
379 ASSERT_EQ(make_safe_primvar_name("test:test", false), std::string("test:test"));
380 ASSERT_EQ(make_safe_primvar_name("1test:2test", false), std::string("_1test:_2test"));
381
382 ASSERT_EQ(make_safe_primvar_name("tést", false), std::string("t_st"));
383 ASSERT_EQ(make_safe_primvar_name("tést:tést", false), std::string("t_st:t_st"));
384 ASSERT_EQ(make_safe_primvar_name("tést:tést:tést", false), std::string("t_st:t_st:t_st"));
385
386 /* Unicode variations. */
387 ASSERT_EQ(make_safe_primvar_name("", true), std::string("_"));
388 ASSERT_EQ(make_safe_primvar_name("|", true), std::string("_"));
389 ASSERT_EQ(make_safe_primvar_name("1", true), std::string("_1"));
390 ASSERT_EQ(make_safe_primvar_name("1Test", true), std::string("_1Test"));
391
392 ASSERT_EQ(make_safe_primvar_name(":", true), std::string("_:_"));
393 ASSERT_EQ(make_safe_primvar_name("test:", true), std::string("test:_"));
394 ASSERT_EQ(make_safe_primvar_name(":test", true), std::string("_:test"));
395 ASSERT_EQ(make_safe_primvar_name("test:test", true), std::string("test:test"));
396 ASSERT_EQ(make_safe_primvar_name("1test:2test", true), std::string("_1test:_2test"));
397
398 ASSERT_EQ(make_safe_primvar_name("tést", true), std::string("tést"));
399 ASSERT_EQ(make_safe_primvar_name("tést:tést", true), std::string("tést:tést"));
400 ASSERT_EQ(make_safe_primvar_name("tést:tést:tést", true), std::string("tést:tést:tést"));
401}
402
403} // namespace blender::io::usd
File and directory operations.
external readfile function prototypes.
virtual void depsgraph_create(eEvaluationMode depsgraph_evaluation_mode)
constexpr const char * c_str() const
pxr::UsdPrim get_first_child_mesh(const pxr::UsdPrim prim)
void compare_blender_image_to_usd_image_shader(const bNode *image_node, const pxr::UsdPrim &image_prim)
void compare_blender_node_to_usd_prim(const bNode *bsdf_node, const pxr::UsdPrim &bsdf_prim)
void compare_blender_mesh_to_usd_prim(const Mesh *mesh, const pxr::UsdGeomMesh &mesh_prim)
bool load_file_and_depsgraph(const StringRefNull &filepath, const eEvaluationMode eval_mode=DAG_EVAL_VIEWPORT)
#define EXPECT_TRUE(result)
static const bNode * find_node_for_type_in_graph(const bNodeTree *nodetree, const UString type_idname)
pxr::TfToken token_for_input(const StringRef input_name)
TEST_F(UsdExportTest, usd_export_rain_mesh)
TEST(utilities, make_safe_name)
std::string make_safe_name(const StringRef name, bool allow_unicode)
Definition usd_utils.cc:83
const StringRefNull simple_scene_filename
const StringRefNull materials_filename
bool USD_export(const bContext *C, const char *filepath, const USDExportParams *params, bool as_background_job, ReportList *reports)
const StringRefNull output_filename
std::string make_safe_primvar_name(const StringRef name, bool allow_unicode)
Definition usd_utils.cc:88
ID * BKE_libblock_find_name(Main *bmain, short type, const char *name, const std::optional< Library * > lib=std::nullopt) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition lib_id.cc:1808
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
bContext * CTX_create()
BMesh const char void * data
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void CTX_free(bContext *C)
@ DAG_EVAL_VIEWPORT
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void CTX_data_main_set(bContext *C, Main *bmain)
VecBase< float, 3 > float3
void CTX_data_scene_set(bContext *C, Scene *scene)