Blender  V2.93
usd_stage_creation_test.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 #include "testing/testing.h"
20 
21 #include <pxr/base/plug/registry.h>
22 #include <pxr/usd/usd/stage.h>
23 
24 #include <string>
25 
26 #include "BLI_path_util.h"
27 #include "BLI_utildefines.h"
28 
29 #include "BKE_appdir.h"
30 
31 namespace blender::io::usd {
32 
33 class USDStageCreationTest : public testing::Test {
34 };
35 
36 TEST_F(USDStageCreationTest, JSONFileLoadingTest)
37 {
38  const std::string &release_dir = blender::tests::flags_test_release_dir();
39  if (release_dir.empty()) {
40  FAIL();
41  }
42 
43  char usd_datafiles_dir[FILE_MAX];
44  const size_t path_len = BLI_path_join(
45  usd_datafiles_dir, FILE_MAX, release_dir.c_str(), "datafiles", "usd", nullptr);
46 
47  /* #BLI_path_join removes trailing slashes, but the USD library requires one in order to
48  * recognize the path as directory. */
49  BLI_assert(path_len + 1 < FILE_MAX);
50  usd_datafiles_dir[path_len] = '/';
51  usd_datafiles_dir[path_len + 1] = '\0';
52 
53  pxr::PlugRegistry::GetInstance().RegisterPlugins(usd_datafiles_dir);
54 
55  /* Simply the ability to create a USD Stage for a specific filename means that the extension
56  * has been recognized by the USD library, and that a USD plugin has been loaded to write such
57  * files. Practically, this is a test to see whether the USD JSON files can be found and
58  * loaded. */
59  std::string filename = "usd-stage-creation-test.usdc";
60  pxr::UsdStageRefPtr usd_stage = pxr::UsdStage::CreateNew(filename);
61  if (usd_stage != nullptr) {
62  /* Even though we don't call `usd_stage->SaveFile()`, a file is still created on the
63  * file-system when we call CreateNew(). It's immediately closed, though,
64  * so we can safely call `unlink()` here. */
65  unlink(filename.c_str());
66  }
67  else {
68  FAIL() << "unable to find suitable USD plugin to write " << filename << "; looked in "
69  << usd_datafiles_dir;
70  }
71 }
72 
73 } // namespace blender::io::usd
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define FILE_MAX
size_t BLI_path_join(char *__restrict dst, const size_t dst_len, const char *path_first,...) ATTR_NONNULL(1
TEST_F(USDStageCreationTest, JSONFileLoadingTest)