WIP: Brush assets project #106303
|
@ -33,7 +33,7 @@ void BKE_brush_system_exit();
|
||||||
|
|
||||||
/* TODO: Should be somewhere else not specific to brushes. */
|
/* TODO: Should be somewhere else not specific to brushes. */
|
||||||
Brush *BKE_brush_asset_runtime_ensure(Main *bmain,
|
Brush *BKE_brush_asset_runtime_ensure(Main *bmain,
|
||||||
const AssetWeakReference *brush_asset_reference);
|
const AssetWeakReference &brush_asset_reference);
|
||||||
|
|
||||||
/* Data-block functions. */
|
/* Data-block functions. */
|
||||||
|
|
||||||
|
|
|
@ -218,7 +218,7 @@ bool BKE_paint_brush_is_valid_asset(const Brush *brush);
|
||||||
*/
|
*/
|
||||||
bool BKE_paint_brush_asset_set(Paint *paint,
|
bool BKE_paint_brush_asset_set(Paint *paint,
|
||||||
Brush *brush,
|
Brush *brush,
|
||||||
AssetWeakReference *weak_asset_reference);
|
const AssetWeakReference &weak_asset_reference);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the active brush of given paint struct, together with its weak asset reference.
|
* Get the active brush of given paint struct, together with its weak asset reference.
|
||||||
|
|
|
@ -526,15 +526,13 @@ static void brush_defaults(Brush *brush)
|
||||||
#undef FROM_DEFAULT_PTR
|
#undef FROM_DEFAULT_PTR
|
||||||
}
|
}
|
||||||
|
|
||||||
Brush *BKE_brush_asset_runtime_ensure(Main *bmain, const AssetWeakReference *brush_asset_reference)
|
Brush *BKE_brush_asset_runtime_ensure(Main *bmain, const AssetWeakReference &brush_asset_reference)
|
||||||
{
|
{
|
||||||
BLI_assert(brush_asset_reference != nullptr);
|
|
||||||
|
|
||||||
char asset_full_path_buffer[FILE_MAX_LIBEXTRA];
|
char asset_full_path_buffer[FILE_MAX_LIBEXTRA];
|
||||||
char *asset_lib_path, *asset_group, *asset_name;
|
char *asset_lib_path, *asset_group, *asset_name;
|
||||||
|
|
||||||
AS_asset_full_path_explode_from_weak_ref(
|
AS_asset_full_path_explode_from_weak_ref(
|
||||||
brush_asset_reference, asset_full_path_buffer, &asset_lib_path, &asset_group, &asset_name);
|
&brush_asset_reference, asset_full_path_buffer, &asset_lib_path, &asset_group, &asset_name);
|
||||||
|
|
||||||
if (asset_lib_path == nullptr && asset_group == nullptr && asset_name == nullptr) {
|
if (asset_lib_path == nullptr && asset_group == nullptr && asset_name == nullptr) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
|
@ -685,30 +685,26 @@ bool BKE_paint_brush_is_valid_asset(const Brush *brush)
|
||||||
|
|
||||||
static void paint_brush_asset_update(Paint &paint,
|
static void paint_brush_asset_update(Paint &paint,
|
||||||
Brush *brush,
|
Brush *brush,
|
||||||
AssetWeakReference *brush_asset_reference)
|
const AssetWeakReference &brush_asset_reference)
|
||||||
{
|
{
|
||||||
if (paint.brush_asset_reference != nullptr) {
|
MEM_delete(paint.brush_asset_reference);
|
||||||
BKE_asset_weak_reference_free(&paint.brush_asset_reference);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (brush == nullptr || brush != paint.brush || !ID_IS_OVERRIDE_LIBRARY_REAL(paint.brush) ||
|
if (brush == nullptr || brush != paint.brush || !ID_IS_OVERRIDE_LIBRARY_REAL(paint.brush) ||
|
||||||
!(ID_IS_ASSET(paint.brush) || ID_IS_ASSET(paint.brush->id.override_library->reference)))
|
!(ID_IS_ASSET(paint.brush) || ID_IS_ASSET(paint.brush->id.override_library->reference)))
|
||||||
{
|
{
|
||||||
BKE_asset_weak_reference_free(&brush_asset_reference);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
paint.brush_asset_reference = brush_asset_reference;
|
paint.brush_asset_reference = MEM_new<AssetWeakReference>(__func__, brush_asset_reference);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BKE_paint_brush_asset_set(Paint *paint,
|
bool BKE_paint_brush_asset_set(Paint *paint,
|
||||||
Brush *brush,
|
Brush *brush,
|
||||||
AssetWeakReference *weak_asset_reference)
|
const AssetWeakReference &weak_asset_reference)
|
||||||
{
|
{
|
||||||
/* Should not happen for users if brush assets are properly filtered by mode, but still protect
|
/* Should not happen for users if brush assets are properly filtered by mode, but still protect
|
||||||
* against it in case of invalid API usage. */
|
* against it in case of invalid API usage. */
|
||||||
if (brush && paint->runtime.ob_mode != brush->ob_mode) {
|
if (brush && paint->runtime.ob_mode != brush->ob_mode) {
|
||||||
BKE_asset_weak_reference_free(&weak_asset_reference);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -742,14 +738,14 @@ void BKE_paint_brush_asset_restore(Main *bmain, Paint *paint)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
AssetWeakReference *brush_asset_reference = paint->brush_asset_reference;
|
AssetWeakReference weak_ref = std::move(*paint->brush_asset_reference);
|
||||||
paint->brush_asset_reference = nullptr;
|
MEM_delete(paint->brush_asset_reference);
|
||||||
|
|
||||||
Brush *brush_asset = BKE_brush_asset_runtime_ensure(bmain, brush_asset_reference);
|
Brush *brush_asset = BKE_brush_asset_runtime_ensure(bmain, weak_ref);
|
||||||
|
|
||||||
/* Will either re-assign the brush_asset_reference to `paint`, or free it if loading a brush ID
|
/* Will either re-assign the brush_asset_reference to `paint`, or free it if loading a brush ID
|
||||||
* from it failed. */
|
* from it failed. */
|
||||||
BKE_paint_brush_asset_set(paint, brush_asset, brush_asset_reference);
|
BKE_paint_brush_asset_set(paint, brush_asset, weak_ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BKE_paint_runtime_init(const ToolSettings *ts, Paint *paint)
|
void BKE_paint_runtime_init(const ToolSettings *ts, Paint *paint)
|
||||||
|
@ -1290,9 +1286,7 @@ void BKE_paint_free(Paint *paint)
|
||||||
{
|
{
|
||||||
BKE_curvemapping_free(paint->cavity_curve);
|
BKE_curvemapping_free(paint->cavity_curve);
|
||||||
MEM_SAFE_FREE(paint->tool_slots);
|
MEM_SAFE_FREE(paint->tool_slots);
|
||||||
if (paint->brush_asset_reference != nullptr) {
|
MEM_delete(paint->brush_asset_reference);
|
||||||
BKE_asset_weak_reference_free(&paint->brush_asset_reference);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BKE_paint_copy(const Paint *src, Paint *tar, const int flag)
|
void BKE_paint_copy(const Paint *src, Paint *tar, const int flag)
|
||||||
|
|
|
@ -102,7 +102,7 @@ AssetView::AssetView(const AssetLibraryReference &library_ref, const AssetShelf
|
||||||
|
|
||||||
AssetView::~AssetView()
|
AssetView::~AssetView()
|
||||||
{
|
{
|
||||||
BKE_asset_weak_reference_free(&active_asset_);
|
MEM_delete(active_asset_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetView::build_items()
|
void AssetView::build_items()
|
||||||
|
@ -235,10 +235,9 @@ std::optional<bool> AssetViewItem::should_be_active() const
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const asset_system::AssetRepresentation *asset = handle_get_representation(&asset_);
|
const asset_system::AssetRepresentation *asset = handle_get_representation(&asset_);
|
||||||
AssetWeakReference *weak_ref = asset->make_weak_reference();
|
AssetWeakReference weak_ref = asset->make_weak_reference();
|
||||||
const bool matches = *asset_view.active_asset_ == *weak_ref;
|
const bool matches = *asset_view.active_asset_ == weak_ref;
|
||||||
|
|
||||||
BKE_asset_weak_reference_free(&weak_ref);
|
|
||||||
return matches;
|
return matches;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1002,7 +1002,7 @@ static int brush_asset_select_exec(bContext *C, wmOperator *op)
|
||||||
return OPERATOR_CANCELLED;
|
return OPERATOR_CANCELLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
AssetWeakReference *brush_asset_reference = asset->make_weak_reference();
|
AssetWeakReference brush_asset_reference = asset->make_weak_reference();
|
||||||
Brush *brush = BKE_brush_asset_runtime_ensure(CTX_data_main(C), brush_asset_reference);
|
Brush *brush = BKE_brush_asset_runtime_ensure(CTX_data_main(C), brush_asset_reference);
|
||||||
|
|
||||||
Paint *paint = BKE_paint_get_active_from_context(C);
|
Paint *paint = BKE_paint_get_active_from_context(C);
|
||||||
|
@ -1034,18 +1034,19 @@ static void BRUSH_OT_asset_select(wmOperatorType *ot)
|
||||||
/* FIXME Quick dirty hack to generate a weak ref from 'raw' paths.
|
/* FIXME Quick dirty hack to generate a weak ref from 'raw' paths.
|
||||||
* This needs to be properly implemented in assetlib code.
|
* This needs to be properly implemented in assetlib code.
|
||||||
*/
|
*/
|
||||||
static AssetWeakReference *brush_asset_create_weakref_hack(const bUserAssetLibrary *user_asset_lib,
|
static AssetWeakReference brush_asset_create_weakref_hack(const bUserAssetLibrary *user_asset_lib,
|
||||||
std::string &file_path)
|
std::string &file_path)
|
||||||
{
|
{
|
||||||
AssetWeakReference *asset_weak_ref = MEM_new<AssetWeakReference>(__func__);
|
AssetWeakReference asset_weak_ref{};
|
||||||
|
|
||||||
StringRef asset_root_path = user_asset_lib->dirpath;
|
StringRef asset_root_path = user_asset_lib->dirpath;
|
||||||
BLI_assert(file_path.find(asset_root_path) == 0);
|
BLI_assert(file_path.find(asset_root_path) == 0);
|
||||||
std::string relative_asset_path = file_path.substr(size_t(asset_root_path.size()) + 1);
|
std::string relative_asset_path = file_path.substr(size_t(asset_root_path.size()) + 1);
|
||||||
|
|
||||||
asset_weak_ref->asset_library_type = ASSET_LIBRARY_CUSTOM;
|
asset_weak_ref.asset_library_type = ASSET_LIBRARY_CUSTOM;
|
||||||
asset_weak_ref->asset_library_identifier = BLI_strdup(user_asset_lib->name);
|
asset_weak_ref.asset_library_identifier = BLI_strdup(user_asset_lib->name);
|
||||||
asset_weak_ref->relative_asset_identifier = BLI_strdup(relative_asset_path.c_str());
|
asset_weak_ref.relative_asset_identifier = BLI_strdupn(relative_asset_path.c_str(),
|
||||||
|
relative_asset_path.size());
|
||||||
|
|
||||||
return asset_weak_ref;
|
return asset_weak_ref;
|
||||||
}
|
}
|
||||||
|
@ -1302,7 +1303,7 @@ static int brush_asset_save_as_exec(bContext *C, wmOperator *op)
|
||||||
return OPERATOR_CANCELLED;
|
return OPERATOR_CANCELLED;
|
||||||
}
|
}
|
||||||
|
|
||||||
AssetWeakReference *new_brush_weak_ref = brush_asset_create_weakref_hack(
|
AssetWeakReference new_brush_weak_ref = brush_asset_create_weakref_hack(
|
||||||
user_library, final_full_asset_filepath);
|
user_library, final_full_asset_filepath);
|
||||||
|
|
||||||
/* TODO: maybe not needed, even less so if there is more visual confirmation of change. */
|
/* TODO: maybe not needed, even less so if there is more visual confirmation of change. */
|
||||||
|
|
Loading…
Reference in New Issue