14#include <pxr/base/gf/rotation.h>
15#include <pxr/base/gf/vec3f.h>
16#include <pxr/usd/usdGeom/metrics.h>
17#include <pxr/usd/usdGeom/tokens.h>
18#include <pxr/usd/usdGeom/xformCache.h>
19#include <pxr/usd/usdGeom/xformCommonAPI.h>
20#include <pxr/usd/usdLux/domeLight.h>
21#include <pxr/usd/usdLux/tokens.h>
55static const pxr::TfToken
pole_axis_z(
"Z", pxr::TfToken::Immortal);
60struct WorldNtreeSearchPayload {
62 pxr::UsdStageRefPtr stage;
65 : params(in_params), stage(in_stage)
84 std::string imported_file_source_path = tex_path;
86 if (import_textures) {
98 tex_path =
import_asset(tex_path, textures_dir, name_collision_mode,
nullptr);
106 if (import_textures && imported_file_source_path != tex_path) {
146 pxr::UsdStageRefPtr stage)
148 if (!(stage && scene && scene->
world)) {
160 std::string image_filepath;
164 if (image_filepath.empty()) {
174 pxr::UsdLuxDomeLight dome_light = pxr::UsdLuxDomeLight::Define(stage, env_light_path);
179 dome_light.CreateTextureFileAttr().Set(pxr::SdfAssetPath(image_filepath));
184 dome_light.CreateColorAttr().Set(color_val);
188 pxr::GfVec3d angles = res.
transform.DecomposeRotation(
189 pxr::GfVec3d::ZAxis(), pxr::GfVec3d::YAxis(), pxr::GfVec3d::XAxis());
190 pxr::GfVec3f rot_vec(angles[2], angles[1], angles[0]);
191 pxr::UsdGeomXformCommonAPI xform_api(dome_light);
192 xform_api.SetRotate(rot_vec, pxr::UsdGeomXformCommonAPI::RotationOrderXYZ);
197 dome_light.CreateIntensityAttr().Set(res.
intensity);
200 const std::string base_path = stage->GetRootLayer()->GetRealPath();
212 if (
BLI_copy(source_path.c_str(), dest_path) != 0) {
213 CLOG_WARN(&
LOG,
"USD Export: Couldn't write world color image to %s", dest_path);
218 dome_light.CreateTextureFileAttr().Set(pxr::SdfAssetPath(dest_path));
229 const pxr::UsdPrim &prim,
230 const pxr::UsdTimeCode time)
232 if (!(scene && scene->
world && prim)) {
239 bNode *bgshader =
nullptr;
254 node->location[1] += 300;
261 output->location[0] = 300.0f;
262 output->location[1] = 300.0f;
271 &scene->
world->horr);
277 if (shader_input && shader_input->
link) {
287 if (!dome_light_data.
has_tex) {
293 dome_light_data.
color.data());
313 vec_sock = vec_sock->
next;
318 dome_light_data.
color.data());
321 CLOG_WARN(&
LOG,
"Couldn't find vector multiply second vector socket");
325 bNode *tex =
nullptr;
341 const std::string &resolved_path = dome_light_data.
tex_path.GetResolvedPath();
342 if (resolved_path.empty()) {
344 "Couldn't get resolved path for asset %s",
345 dome_light_data.
tex_path.GetAssetPath().c_str());
351 CLOG_WARN(&
LOG,
"Couldn't load image file %s", resolved_path.c_str());
358 pxr::UsdGeomXformCache xf_cache(time);
359 pxr::GfMatrix4d xf = xf_cache.GetLocalToWorldTransform(prim);
361 pxr::UsdStageRefPtr stage = prim.GetStage();
364 CLOG_WARN(&
LOG,
"Couldn't get stage for dome light %s", prim.GetPath().GetText());
371 const pxr::TfToken stage_up = pxr::UsdGeomGetStageUpAxis(stage);
372 const bool needs_stage_z_adjust = stage_up == pxr::UsdGeomTokens->z &&
374 pxr::UsdLuxTokens->Z,
375 pxr::UsdLuxTokens->scene);
376 const bool needs_stage_y_adjust = stage_up == pxr::UsdGeomTokens->y &&
378 if (needs_stage_z_adjust || needs_stage_y_adjust) {
379 xf *= pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(0.0, 1.0, 0.0), 90.0));
381 else if (stage_up == pxr::UsdGeomTokens->
y) {
383 xf *= pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), 90.0));
387 xf = pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), -90.0)) *
388 pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -90.0)) * xf;
390 pxr::GfVec3d angles = xf.DecomposeRotation(
391 pxr::GfVec3d::XAxis(), pxr::GfVec3d::YAxis(), pxr::GfVec3d::ZAxis());
392 pxr::GfVec3f rot_vec(-angles[0], -angles[1], -angles[2]);
395 rot_vec *=
M_PI / 180.0f;
399 socket->default_value);
410 if (!(userdata && fromnode)) {
419 *fromnode,
SOCK_IN,
"Color"_ustr)
439 pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), 90.0)) *
440 pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), 90.0));
448 vec_sock = vec_sock->
next;
465 pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(0.0, 0.0, 1.0), -
rot[2])) *
466 pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(0.0, 1.0, 0.0), -
rot[1])) *
467 pxr::GfMatrix4d().SetRotate(pxr::GfRotation(pxr::GfVec3d(1.0, 0.0, 0.0), -
rot[0]));
476 scene->
world->nodetree->ensure_topology_cache();
478 "ShaderNodeOutputWorld"_ustr);
#define SH_NODE_OUTPUT_WORLD
#define SH_NODE_TEX_COORD
#define SH_NODE_VECTOR_MATH
#define SH_NODE_TEX_ENVIRONMENT
#define SH_NODE_BACKGROUND
File and directory operations.
#define BLI_path_join(...)
#define CLOG_WARN(clg_ref,...)
#define ID_BLEND_PATH(_bmain, _id)
void node_remove_link(bNodeTree *ntree, bNodeLink &link)
void node_chain_iterator(const bNodeTree *ntree, const bNode *node_start, bool(*callback)(bNode *, bNode *, void *, const bool), void *userdata, bool reversed)
bNode * node_add_static_node(const bContext *C, bNodeTree &ntree, int type)
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
bNodeSocket * node_find_socket(bNode &node, eNodeSocketInOut in_out, UString identifier)
void node_set_active(bNodeTree &ntree, bNode &node)
void world_material_to_dome_light(const USDExportParams ¶ms, const Scene *scene, pxr::UsdStageRefPtr stage)
const char * temp_textures_dir()
bool should_import_asset(const std::string &path)
static Image * load_image(std::string tex_path, Main *bmain, const USDImportParams ¶ms)
pxr::SdfPath get_unique_path(pxr::UsdStageRefPtr stage, const std::string &path)
void dome_light_to_world_material(const USDImportParams ¶ms, Scene *scene, Main *bmain, const USDImportDomeLightData &dome_light_data, const pxr::UsdPrim &prim, const pxr::UsdTimeCode time)
std::string cache_image_color(const float color[4])
static bool node_search(bNode *fromnode, bNode *, void *userdata, bool)
void export_texture(Image *ima, const pxr::UsdStageRefPtr stage, const bool allow_overwrite, ReportList *reports)
std::string get_tex_image_asset_filepath(Image *ima, const pxr::UsdStageRefPtr stage, const USDExportParams &export_params)
std::string import_asset(const std::string &src, const char *import_dir, TexNameCollisionMode name_collision_mode, ReportList *reports)
void colorspace_apply_to_prim(const pxr::UsdPrim &prim)
void ensure_usd_source_path_prop(const std::string &path, ID *id)
static bNode * append_node(bNode *dst_node, int new_node_type, const StringRef out_sock, const StringRef in_sock, bNodeTree *ntree, float offset)
static const pxr::TfToken pole_axis_z("Z", pxr::TfToken::Immortal)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void copy_v3_v3(float r[3], const float a[3])
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
void BLI_string_replace_char(char *str, char src, char dst) ATTR_NONNULL(1)
size_t size_t BLI_path_append_dir(char *__restrict dst, size_t dst_maxncpy, const char *__restrict dir) ATTR_NONNULL(1
size_t BLI_path_append(char *__restrict dst, size_t dst_maxncpy, const char *__restrict file) ATTR_NONNULL(1
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
Image * BKE_image_load_exists(Main *bmain, const char *filepath, bool check_open=true, bool *r_exists=nullptr)
void DEG_id_tag_update(ID *id, unsigned int flags)
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL()
void BKE_image_packfiles(ReportList *reports, Image *ima, const char *basepath)
void BKE_ntree_update_after_single_tree_change(Main &bmain, bNodeTree &modified_tree, const NodeTreeUpdateExtraParams ¶ms={})
bool BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
@ NODE_VECTOR_MATH_MULTIPLY
bool BKE_image_has_packedfile(const Image *image)
struct bNodeSocket * next
std::string root_prim_path
pxr::SdfAssetPath tex_path
TexImportMode import_textures_mode
TexNameCollisionMode tex_name_collision_mode
float light_intensity_scale
char import_textures_dir[768]
pxr::GfMatrix4d transform