GPv3: Optional automatic conversion of legacy GP objects on fileload. #118705

Merged
Bastien Montagne merged 1 commits from mont29/blender:tmp-gp-conversion-fileload into main 2024-03-23 03:29:08 +01:00
7 changed files with 113 additions and 8 deletions

View File

@ -2674,6 +2674,7 @@ class USERPREF_PT_experimental_prototypes(ExperimentalPanel, Panel):
({"property": "use_sculpt_texture_paint"}, ("blender/blender/issues/96225", "#96225")),
({"property": "use_experimental_compositors"}, ("blender/blender/issues/88150", "#88150")),
({"property": "use_grease_pencil_version3"}, ("blender/blender/projects/6", "Grease Pencil 3.0")),
({"property": "use_grease_pencil_version3_convert_on_load"}, ("blender/blender/projects/6", "Grease Pencil 3.0")),
({"property": "use_new_matrix_socket"}, ("blender/blender/issues/116067", "Matrix Socket")),
({"property": "enable_overlay_next"}, ("blender/blender/issues/102179", "#102179")),
({"property": "use_extension_repos"}, ("/blender/blender/issues/117286", "#117286")),

View File

@ -10,6 +10,7 @@
struct bGPdata;
struct bGPDframe;
struct BlendFileReadReport;
struct GreasePencil;
struct GreasePencilDrawing;
struct ListBase;
@ -27,6 +28,9 @@ void legacy_gpencil_to_grease_pencil(Main &bmain, GreasePencil &grease_pencil, b
void legacy_gpencil_object(Main &bmain, Object &object);
/** Main entry point to convert all legacy GPData into GreasePencil data and objects. */
void legacy_main(Main &bmain, BlendFileReadReport &reports);
void thickness_factor_to_modifier(const bGPdata &src_object_data, Object &dst_object);
void layer_adjustments_to_modifiers(Main &bmain,
const bGPdata &src_object_data,

View File

@ -36,6 +36,7 @@
#include "BLT_translation.hh"
#include "BKE_grease_pencil_legacy_convert.hh"
#include "BKE_idtype.hh"
#include "BKE_key.hh"
#include "BKE_layer.hh"
@ -1379,6 +1380,14 @@ void BKE_blendfile_append(BlendfileLinkAppendContext *lapp_context, ReportList *
blendfile_link_append_proxies_convert(bmain, reports);
BKE_main_mesh_legacy_convert_auto_smooth(*bmain);
if (U.experimental.use_grease_pencil_version3 &&
U.experimental.use_grease_pencil_version3_convert_on_load)
{
BlendFileReadReport bf_reports{};
bf_reports.reports = reports;
blender::bke::greasepencil::convert::legacy_main(*bmain, bf_reports);
}
}
void BKE_blendfile_link(BlendfileLinkAppendContext *lapp_context, ReportList *reports)
@ -1496,6 +1505,14 @@ void BKE_blendfile_link(BlendfileLinkAppendContext *lapp_context, ReportList *re
if ((lapp_context->params->flag & FILE_LINK) != 0) {
blendfile_link_append_proxies_convert(lapp_context->params->bmain, reports);
BKE_main_mesh_legacy_convert_auto_smooth(*lapp_context->params->bmain);
if (U.experimental.use_grease_pencil_version3 &&
U.experimental.use_grease_pencil_version3_convert_on_load)
{
BlendFileReadReport bf_reports{};
bf_reports.reports = reports;
blender::bke::greasepencil::convert::legacy_main(*lapp_context->params->bmain, bf_reports);
}
}
BKE_main_namemap_clear(lapp_context->params->bmain);

View File

@ -6,6 +6,8 @@
* \ingroup bke
*/
#include <optional>
#include <fmt/format.h>
#include "BKE_anim_data.hh"
@ -19,15 +21,20 @@
#include "BKE_grease_pencil_legacy_convert.hh"
#include "BKE_idprop.hh"
#include "BKE_lib_id.hh"
#include "BKE_lib_remap.hh"
#include "BKE_main.hh"
#include "BKE_material.h"
#include "BKE_modifier.hh"
#include "BKE_node.hh"
#include "BKE_node_tree_update.hh"
#include "BKE_object.hh"
#include "BLO_readfile.hh"
#include "BLI_color.hh"
#include "BLI_function_ref.hh"
#include "BLI_listbase.h"
#include "BLI_map.hh"
#include "BLI_math_matrix.h"
#include "BLI_math_vector_types.hh"
#include "BLI_string.h"
@ -2024,21 +2031,41 @@ static void legacy_object_modifiers(Main & /*bmain*/, Object &object)
}
}
void legacy_gpencil_object(Main &bmain, Object &object)
static void legacy_gpencil_object_ex(
Main &bmain,
Object &object,
std::optional<blender::Map<bGPdata *, GreasePencil *>> legacy_to_greasepencil_data)
{
bGPdata *gpd = static_cast<bGPdata *>(object.data);
BLI_assert((GS(static_cast<ID *>(object.data)->name) == ID_GD_LEGACY));
bGPdata *gpd = static_cast<bGPdata *>(object.data);
GreasePencil *new_grease_pencil = nullptr;
bool do_gpencil_data_conversion = true;
if (legacy_to_greasepencil_data) {
new_grease_pencil = legacy_to_greasepencil_data->lookup_default(gpd, nullptr);
do_gpencil_data_conversion = (new_grease_pencil == nullptr);
}
if (!new_grease_pencil) {
new_grease_pencil = static_cast<GreasePencil *>(
BKE_id_new_in_lib(&bmain, gpd->id.lib, ID_GP, gpd->id.name + 2));
id_us_min(&new_grease_pencil->id);
}
GreasePencil *new_grease_pencil = static_cast<GreasePencil *>(
BKE_id_new(&bmain, ID_GP, gpd->id.name + 2));
object.data = new_grease_pencil;
object.type = OB_GREASE_PENCIL;
/* NOTE: Could also use #BKE_id_free_us, to also free the legacy GP if not used anymore? */
id_us_min(&gpd->id);
/* No need to increase user-count of `new_grease_pencil`,
* since ID creation already set it to 1. */
id_us_plus(&new_grease_pencil->id);
legacy_gpencil_to_grease_pencil(bmain, *new_grease_pencil, *gpd);
if (do_gpencil_data_conversion) {
legacy_gpencil_to_grease_pencil(bmain, *new_grease_pencil, *gpd);
if (legacy_to_greasepencil_data) {
legacy_to_greasepencil_data->add(gpd, new_grease_pencil);
}
}
legacy_object_modifiers(bmain, object);
@ -2050,6 +2077,45 @@ void legacy_gpencil_object(Main &bmain, Object &object)
BKE_object_free_derived_caches(&object);
}
void legacy_gpencil_object(Main &bmain, Object &object)
{
legacy_gpencil_object_ex(bmain, object, std::nullopt);
}
void legacy_main(Main &bmain, BlendFileReadReport & /*reports*/)
{
/* Allows to convert a legacy GPencil data only once, in case it's used by several objects. */
blender::Map<bGPdata *, GreasePencil *> legacy_to_greasepencil_data;
LISTBASE_FOREACH (Object *, object, &bmain.objects) {
if (object->type != OB_GPENCIL_LEGACY) {
continue;
}
legacy_gpencil_object_ex(bmain, *object, std::make_optional(legacy_to_greasepencil_data));
}
/* Potential other usages of legacy bGPdata IDs also need to be remapped to their matching new
* GreasePencil counterparts. */
blender::bke::id::IDRemapper gpd_remapper;
/* Allow remapping from legacy bGPdata IDs to new GreasePencil ones. */
gpd_remapper.allow_idtype_mismatch = true;
LISTBASE_FOREACH (bGPdata *, legacy_gpd, &bmain.gpencils) {
GreasePencil *new_grease_pencil = legacy_to_greasepencil_data.lookup_default(legacy_gpd,
nullptr);
if (!new_grease_pencil) {
new_grease_pencil = static_cast<GreasePencil *>(
BKE_id_new_in_lib(&bmain, legacy_gpd->id.lib, ID_GP, legacy_gpd->id.name + 2));
id_us_min(&new_grease_pencil->id);
legacy_gpencil_to_grease_pencil(bmain, *new_grease_pencil, *legacy_gpd);
legacy_to_greasepencil_data.add(legacy_gpd, new_grease_pencil);
}
gpd_remapper.add(&legacy_gpd->id, &new_grease_pencil->id);
}
BKE_libblock_remap_multiple(&bmain, gpd_remapper, ID_REMAP_ALLOW_IDTYPE_MISMATCH);
}
void lineart_wrap_v3(const LineartGpencilModifierData *lmd_legacy,
GreasePencilLineartModifierData *lmd)
{

View File

@ -19,6 +19,7 @@
#include "BLI_string_ref.hh"
#include "BKE_animsys.h"
#include "BKE_grease_pencil_legacy_convert.hh"
#include "BKE_idprop.h"
#include "BKE_ipo.h"
#include "BKE_lib_id.hh"
@ -537,4 +538,10 @@ void do_versions_after_setup(Main *new_bmain, BlendFileReadReport *reports)
* been made idempotent. */
BKE_main_mesh_legacy_convert_auto_smooth(*new_bmain);
}
if (U.experimental.use_grease_pencil_version3 &&
U.experimental.use_grease_pencil_version3_convert_on_load)
{
blender::bke::greasepencil::convert::legacy_main(*new_bmain, *reports);
}
}

View File

@ -722,8 +722,9 @@ typedef struct UserDef_Experimental {
char use_shader_node_previews;
char use_extension_repos;
char use_extension_utils;
char use_grease_pencil_version3_convert_on_load;
char _pad[2];
char _pad[1];
/** `makesdna` does not allow empty structs. */
} UserDef_Experimental;

View File

@ -7162,6 +7162,15 @@ static void rna_def_userdef_experimental(BlenderRNA *brna)
RNA_def_property_flag(prop, PROP_CONTEXT_UPDATE);
RNA_def_property_update(prop, 0, "rna_userdef_use_grease_pencil_version3_update");
prop = RNA_def_property(
srna, "use_grease_pencil_version3_convert_on_load", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, nullptr, "use_grease_pencil_version3_convert_on_load", 1);
RNA_def_property_ui_text(prop,
"Grease Pencil 3.0 Automatic Conversion",
"Enable automatic conversion to grease pencil 3.0 data when opening a "
"blendfile (only active if 'Grease Pencil 3.0' is enabled)");
RNA_def_property_update(prop, 0, "rna_userdef_ui_update");
prop = RNA_def_property(srna, "use_new_matrix_socket", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, nullptr, "use_new_matrix_socket", 1);
RNA_def_property_ui_text(