Blender V4.3
usd_usdz_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/usd/usd/prim.h>
9#include <pxr/usd/usd/stage.h>
10
11#include "BKE_appdir.hh"
12#include "BKE_context.hh"
13#include "BKE_main.hh"
14#include "BLI_fileops.h"
15#include "BLI_path_utils.hh"
16#include "BLO_readfile.hh"
17
18#include "DEG_depsgraph.hh"
19
20#include "WM_api.hh"
21
22#include "usd.hh"
23
24namespace blender::io::usd {
25
26const StringRefNull usdz_export_test_filename = "usd/usdz_export_test.blend";
30
32 protected:
33 bContext *context = nullptr;
34
35 public:
37 const eEvaluationMode eval_mode = DAG_EVAL_VIEWPORT)
38 {
39 if (!blendfile_load(filepath.c_str())) {
40 return false;
41 }
42 depsgraph_create(eval_mode);
43
44 context = CTX_create();
45 CTX_data_main_set(context, bfile->main);
47
48 return true;
49 }
50
51 virtual void SetUp() override
52 {
53 BlendfileLoadingBaseTest::SetUp();
54
55 BKE_tempdir_init(nullptr);
56 const char *temp_base_dir = BKE_tempdir_base();
57
58 BLI_path_join(temp_dir, FILE_MAX, temp_base_dir, "usdz_test_temp_dir");
60
61 BLI_path_join(temp_output_dir, FILE_MAX, temp_base_dir, "usdz_test_output_dir");
63
64 BLI_path_join(output_filepath, FILE_MAX, temp_output_dir, "output_новый.usdz");
65 }
66
67 virtual void TearDown() override
68 {
70 CTX_free(context);
71 context = nullptr;
72
73 BLI_delete(temp_dir, true, true);
74 BLI_delete(temp_output_dir, true, true);
75 }
76};
77
79{
80 if (!load_file_and_depsgraph(usdz_export_test_filename)) {
81 ADD_FAILURE();
82 return;
83 }
84
85 /* File sanity check. */
86 ASSERT_EQ(BLI_listbase_count(&bfile->main->objects), 4)
87 << "Blender scene should have 4 objects.";
88
89 char original_cwd_buff[FILE_MAX];
90 char *original_cwd = BLI_current_working_dir(original_cwd_buff, sizeof(original_cwd_buff));
91 /* Buffer is expected to be returned by #BLI_current_working_dir, although in theory other
92 * returns are possible on some platforms, this is not handled by this code. */
93 ASSERT_EQ(original_cwd, original_cwd_buff)
94 << "BLI_current_working_dir is not expected to return a different value than the given char "
95 "buffer.";
96
98 params.export_materials = false;
99 params.convert_world_material = false;
100 params.visible_objects_only = false;
101
102 bool result = USD_export(context, output_filepath, &params, false, nullptr);
103 ASSERT_TRUE(result) << "usd export to " << output_filepath << " failed.";
104
105 pxr::UsdStageRefPtr stage = pxr::UsdStage::Open(output_filepath);
106 ASSERT_TRUE(bool(stage)) << "unable to open stage for the exported usdz file.";
107
108 std::string prim_name = pxr::TfMakeValidIdentifier("Cube");
109 pxr::UsdPrim test_prim = stage->GetPrimAtPath(pxr::SdfPath("/Cube/" + prim_name));
110 EXPECT_TRUE(bool(test_prim)) << "Cube prim should exist in exported usdz file.";
111
112 prim_name = pxr::TfMakeValidIdentifier("Cylinder");
113 test_prim = stage->GetPrimAtPath(pxr::SdfPath("/Cylinder/" + prim_name));
114 EXPECT_TRUE(bool(test_prim)) << "Cylinder prim should exist in exported usdz file.";
115
116 prim_name = pxr::TfMakeValidIdentifier("Icosphere");
117 test_prim = stage->GetPrimAtPath(pxr::SdfPath("/Icosphere/" + prim_name));
118 EXPECT_TRUE(bool(test_prim)) << "Icosphere prim should exist in exported usdz file.";
119
120 prim_name = pxr::TfMakeValidIdentifier("Sphere");
121 test_prim = stage->GetPrimAtPath(pxr::SdfPath("/Sphere/" + prim_name));
122 EXPECT_TRUE(bool(test_prim)) << "Sphere prim should exist in exported usdz file.";
123
124 char final_cwd_buff[FILE_MAX];
125 char *final_cwd = BLI_current_working_dir(final_cwd_buff, sizeof(final_cwd_buff));
126 /* Buffer is expected to be returned by #BLI_current_working_dir, although in theory other
127 * returns are possible on some platforms, this is not handled by this code. */
128 ASSERT_EQ(final_cwd, final_cwd_buff) << "BLI_current_working_dir is not expected to return "
129 "a different value than the given char buffer.";
130 EXPECT_TRUE(STREQ(original_cwd, final_cwd))
131 << "Final CWD should be the same as the original one.";
132}
133
134} // namespace blender::io::usd
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1190
const char * BKE_tempdir_base() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1211
void CTX_data_main_set(bContext *C, Main *bmain)
void CTX_free(bContext *C)
void CTX_data_scene_set(bContext *C, Scene *scene)
bContext * CTX_create()
File and directory operations.
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:391
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
char * BLI_current_working_dir(char *dir, size_t maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:85
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define FILE_MAX
#define BLI_path_join(...)
#define STREQ(a, b)
external readfile function prototypes.
eEvaluationMode
@ DAG_EVAL_VIEWPORT
virtual void depsgraph_create(eEvaluationMode depsgraph_evaluation_mode)
bool blendfile_load(const char *filepath)
constexpr const char * c_str() const
bool load_file_and_depsgraph(const StringRefNull &filepath, const eEvaluationMode eval_mode=DAG_EVAL_VIEWPORT)
EvaluationStage stage
Definition deg_eval.cc:83
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
char temp_output_dir[FILE_MAX]
char output_filepath[FILE_MAX]
bool USD_export(const bContext *C, const char *filepath, const USDExportParams *params, bool as_background_job, ReportList *reports)
TEST_F(UsdCurvesTest, usd_export_curves)
const StringRefNull usdz_export_test_filename