Fix #124103: Build error when using WITH_USD but not WITH_HYDRA

Move a few functions to a common location so that they're all accessible
from both USD and Hydra.

Pull Request: https://projects.blender.org/blender/blender/pulls/124119
This commit is contained in:
Jesse Yurkovich 2024-07-03 22:02:53 +02:00 committed by Jesse Yurkovich
parent 71159872a4
commit e9ba414799
7 changed files with 67 additions and 59 deletions

@ -3,6 +3,7 @@
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "image.hh"
#include "usd_private.hh"
#include <pxr/imaging/hio/imageRegistry.h>
@ -24,25 +25,6 @@
namespace blender::io::hydra {
std::string image_cache_file_path()
{
char dir_path[FILE_MAX];
BLI_path_join(dir_path, sizeof(dir_path), BKE_tempdir_session(), "hydra", "image_cache");
return dir_path;
}
static std::string get_cache_file(const std::string &file_name, bool mkdir = true)
{
std::string dir_path = image_cache_file_path();
if (mkdir) {
BLI_dir_create_recursive(dir_path.c_str());
}
char file_path[FILE_MAX];
BLI_path_join(file_path, sizeof(file_path), dir_path.c_str(), file_name.c_str());
return file_path;
}
static std::string cache_image_file(
Main *bmain, Scene *scene, Image *image, ImageUser *iuser, bool check_exist)
{
@ -58,7 +40,7 @@ static std::string cache_image_file(
SNPRINTF(file_name, "img_%p%s", image, r_ext);
file_path = get_cache_file(file_name);
file_path = blender::io::usd::get_image_cache_file(file_name);
if (check_exist && BLI_exists(file_path.c_str())) {
return file_path;
}
@ -87,7 +69,7 @@ std::string cache_or_get_image_file(Main *bmain, Scene *scene, Image *image, Ima
}
else if (BKE_image_has_packedfile(image)) {
do_check_extension = true;
std::string dir_path = image_cache_file_path();
std::string dir_path = blender::io::usd::image_cache_file_path();
char *cached_path;
char subfolder[FILE_MAXDIR];
SNPRINTF(subfolder, "unpack_%p", image);
@ -126,33 +108,4 @@ std::string cache_or_get_image_file(Main *bmain, Scene *scene, Image *image, Ima
return file_path;
}
std::string cache_image_color(float color[4])
{
char name[128];
SNPRINTF(name,
"color_%02d%02d%02d.hdr",
int(color[0] * 255),
int(color[1] * 255),
int(color[2] * 255));
std::string file_path = get_cache_file(name);
if (BLI_exists(file_path.c_str())) {
return file_path;
}
ImBuf *ibuf = IMB_allocImBuf(4, 4, 32, IB_rectfloat);
IMB_rectfill(ibuf, color);
ibuf->ftype = IMB_FTYPE_RADHDR;
if (IMB_saveiff(ibuf, file_path.c_str(), IB_rectfloat)) {
CLOG_INFO(LOG_HYDRA_SCENE, 1, "%s", file_path.c_str());
}
else {
CLOG_ERROR(LOG_HYDRA_SCENE, "Can't save %s", file_path.c_str());
file_path = "";
}
IMB_freeImBuf(ibuf);
return file_path;
}
} // namespace blender::io::hydra

@ -13,9 +13,6 @@ struct ImageUser;
namespace blender::io::hydra {
std::string image_cache_file_path();
std::string cache_or_get_image_file(Main *bmain, Scene *Scene, Image *image, ImageUser *iuser);
std::string cache_image_color(float color[4]);
} // namespace blender::io::hydra

@ -3,6 +3,7 @@
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "material.hh"
#include "usd_private.hh"
#include <Python.h>
#include <unicodeobject.h>
@ -80,7 +81,7 @@ void MaterialData::init()
material_library_path,
get_time_code,
export_params,
image_cache_file_path(),
blender::io::usd::image_cache_file_path(),
cache_or_get_image_file};
/* Create USD material. */
pxr::UsdShadeMaterial usd_material;

@ -3,6 +3,7 @@
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "world.hh"
#include "usd_private.hh"
#include <pxr/base/gf/rotation.h>
#include <pxr/base/gf/vec2f.h>
@ -112,7 +113,7 @@ void WorldData::init()
if (texture_file.GetAssetPath().empty()) {
float fill_color[4] = {color[0], color[1], color[2], 1.0f};
std::string image_path = cache_image_color(fill_color);
std::string image_path = blender::io::usd::cache_image_color(fill_color);
texture_file = pxr::SdfAssetPath(image_path, image_path);
}
}

@ -48,6 +48,9 @@
#include "BLI_string.h"
#include "BLI_timeit.hh"
#include <IMB_imbuf.hh>
#include <IMB_imbuf_types.hh>
#include "WM_api.hh"
#include "WM_types.hh"
@ -329,6 +332,54 @@ static bool perform_usdz_conversion(const ExportJobData *data)
return true;
}
std::string image_cache_file_path()
{
char dir_path[FILE_MAX];
BLI_path_join(dir_path, sizeof(dir_path), BKE_tempdir_session(), "usd", "image_cache");
return dir_path;
}
std::string get_image_cache_file(const std::string &file_name, bool mkdir)
{
std::string dir_path = image_cache_file_path();
if (mkdir) {
BLI_dir_create_recursive(dir_path.c_str());
}
char file_path[FILE_MAX];
BLI_path_join(file_path, sizeof(file_path), dir_path.c_str(), file_name.c_str());
return file_path;
}
std::string cache_image_color(float color[4])
{
char name[128];
SNPRINTF(name,
"color_%02d%02d%02d.hdr",
int(color[0] * 255),
int(color[1] * 255),
int(color[2] * 255));
std::string file_path = get_image_cache_file(name);
if (BLI_exists(file_path.c_str())) {
return file_path;
}
ImBuf *ibuf = IMB_allocImBuf(4, 4, 32, IB_rectfloat);
IMB_rectfill(ibuf, color);
ibuf->ftype = IMB_FTYPE_RADHDR;
if (IMB_saveiff(ibuf, file_path.c_str(), IB_rectfloat)) {
CLOG_INFO(&LOG, 1, "%s", file_path.c_str());
}
else {
CLOG_ERROR(&LOG, "Can't save %s", file_path.c_str());
file_path = "";
}
IMB_freeImBuf(ibuf);
return file_path;
}
pxr::UsdStageRefPtr export_to_stage(const USDExportParams &params,
Depsgraph *depsgraph,
const char *filepath)

@ -6,6 +6,7 @@
#include "usd.hh"
#include "usd_asset_utils.hh"
#include "usd_private.hh"
#include "usd_reader_prim.hh"
#include "usd_writer_material.hh"
@ -32,8 +33,6 @@
#include "DNA_scene_types.h"
#include "DNA_world_types.h"
#include "../hydra/image.hh"
#include <string>
#include "CLG_log.h"
@ -308,7 +307,7 @@ void world_material_to_dome_light(const USDExportParams &params,
float fill_color[4] = {res.world_color[0], res.world_color[1], res.world_color[2], 1.0f};
std::string source_path = blender::io::hydra::cache_image_color(fill_color);
std::string source_path = cache_image_color(fill_color);
const std::string base_path = stage->GetRootLayer()->GetRealPath();
/* It'll be short, coming from cache_image_color. */

@ -6,6 +6,8 @@
#include <pxr/usd/usd/common.h>
#include <string>
#include "usd.hh"
struct Depsgraph;
@ -16,4 +18,8 @@ pxr::UsdStageRefPtr export_to_stage(const USDExportParams &params,
Depsgraph *depsgraph,
const char *filepath);
};
std::string image_cache_file_path();
std::string get_image_cache_file(const std::string &file_name, bool mkdir = true);
std::string cache_image_color(float color[4]);
}; // namespace blender::io::usd