Blender V4.5
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_listbase.h"
16#include "BLI_path_utils.hh"
17#include "BLO_readfile.hh"
18
19#include "DEG_depsgraph.hh"
20
21#include "usd.hh"
22
23namespace blender::io::usd {
24
25const StringRefNull usdz_export_test_filename = "usd/usdz_export_test.blend";
29
31 protected:
32 bContext *context = nullptr;
33
34 public:
36 const eEvaluationMode eval_mode = DAG_EVAL_VIEWPORT)
37 {
38 if (!blendfile_load(filepath.c_str())) {
39 return false;
40 }
41 depsgraph_create(eval_mode);
42
46
47 return true;
48 }
49
50 void SetUp() override
51 {
52 BlendfileLoadingBaseTest::SetUp();
53
54 BKE_tempdir_init(nullptr);
55 const char *temp_base_dir = BKE_tempdir_base();
56
57 BLI_path_join(temp_dir, FILE_MAX, temp_base_dir, "usdz_test_temp_dir");
59
60 BLI_path_join(temp_output_dir, FILE_MAX, temp_base_dir, "usdz_test_output_dir");
62
63 BLI_path_join(output_filepath, FILE_MAX, temp_output_dir, "output_новый.usdz");
64 }
65
66 void TearDown() override
67 {
70 context = nullptr;
71
72 BLI_delete(temp_dir, true, true);
73 BLI_delete(temp_output_dir, true, true);
74 }
75};
76
78{
79 if (!load_file_and_depsgraph(usdz_export_test_filename)) {
80 ADD_FAILURE();
81 return;
82 }
83
84 /* File sanity check. */
85 ASSERT_EQ(BLI_listbase_count(&bfile->main->objects), 4)
86 << "Blender scene should have 4 objects.";
87
88 char original_cwd_buff[FILE_MAX];
89 char *original_cwd = BLI_current_working_dir(original_cwd_buff, sizeof(original_cwd_buff));
90 /* Buffer is expected to be returned by #BLI_current_working_dir, although in theory other
91 * returns are possible on some platforms, this is not handled by this code. */
92 ASSERT_EQ(original_cwd, original_cwd_buff)
93 << "BLI_current_working_dir is not expected to return a different value than the given char "
94 "buffer.";
95
97 params.export_materials = false;
98 params.convert_world_material = false;
99 params.visible_objects_only = false;
100
101 bool result = USD_export(context, output_filepath, &params, false, nullptr);
102 ASSERT_TRUE(result) << "usd export to " << output_filepath << " failed.";
103
104 pxr::UsdStageRefPtr stage = pxr::UsdStage::Open(output_filepath);
105 ASSERT_TRUE(bool(stage)) << "unable to open stage for the exported usdz file.";
106
107 std::string prim_name = pxr::TfMakeValidIdentifier("Cube");
108 pxr::UsdPrim test_prim = stage->GetPrimAtPath(pxr::SdfPath("/Cube/" + prim_name));
109 EXPECT_TRUE(bool(test_prim)) << "Cube prim should exist in exported usdz file.";
110
111 prim_name = pxr::TfMakeValidIdentifier("Cylinder");
112 test_prim = stage->GetPrimAtPath(pxr::SdfPath("/Cylinder/" + prim_name));
113 EXPECT_TRUE(bool(test_prim)) << "Cylinder prim should exist in exported usdz file.";
114
115 prim_name = pxr::TfMakeValidIdentifier("Icosphere");
116 test_prim = stage->GetPrimAtPath(pxr::SdfPath("/Icosphere/" + prim_name));
117 EXPECT_TRUE(bool(test_prim)) << "Icosphere prim should exist in exported usdz file.";
118
119 prim_name = pxr::TfMakeValidIdentifier("Sphere");
120 test_prim = stage->GetPrimAtPath(pxr::SdfPath("/Sphere/" + prim_name));
121 EXPECT_TRUE(bool(test_prim)) << "Sphere prim should exist in exported usdz file.";
122
123 char final_cwd_buff[FILE_MAX];
124 char *final_cwd = BLI_current_working_dir(final_cwd_buff, sizeof(final_cwd_buff));
125 /* Buffer is expected to be returned by #BLI_current_working_dir, although in theory other
126 * returns are possible on some platforms, this is not handled by this code. */
127 ASSERT_EQ(final_cwd, final_cwd_buff) << "BLI_current_working_dir is not expected to return "
128 "a different value than the given char buffer.";
129 EXPECT_TRUE(STREQ(original_cwd, final_cwd))
130 << "Final CWD should be the same as the original one.";
131}
132
133} // namespace blender::io::usd
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1197
const char * BKE_tempdir_base() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1249
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 ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:524
#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)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
char temp_output_dir[FILE_MAX]
TEST_F(UsdExportTest, usd_export_rain_mesh)
char output_filepath[FILE_MAX]
bool USD_export(const bContext *C, const char *filepath, const USDExportParams *params, bool as_background_job, ReportList *reports)
const StringRefNull usdz_export_test_filename