Refactor: keyingsets.cc #113666

Merged
Christoph Lendenfeld merged 2 commits from ChrisLend/blender:refactor_keyingsets into main 2023-10-13 12:56:02 +02:00
5 changed files with 133 additions and 125 deletions

View File

@ -95,7 +95,7 @@ void BKE_keyingsets_foreach_id(struct LibraryForeachIDData *data,
void BKE_keyingset_free_path(struct KeyingSet *ks, struct KS_Path *ksp);
/* Free data for KeyingSet but not set itself */
void BKE_keyingset_free(struct KeyingSet *ks);
void BKE_keyingset_free_paths(struct KeyingSet *ks);
/* Free all the KeyingSets in the given list */
void BKE_keyingsets_free(struct ListBase *list);

View File

@ -255,7 +255,7 @@ void BKE_keyingsets_foreach_id(LibraryForeachIDData *data, const ListBase *keyin
/* Freeing Tools --------------------------- */
void BKE_keyingset_free(KeyingSet *ks)
void BKE_keyingset_free_paths(KeyingSet *ks)
{
KS_Path *ksp, *kspn;
@ -281,11 +281,11 @@ void BKE_keyingsets_free(ListBase *list)
}
/* loop over KeyingSets freeing them
* - BKE_keyingset_free() doesn't free the set itself, but it frees its sub-data
* - BKE_keyingset_free_paths() doesn't free the set itself, but it frees its sub-data
*/
for (ks = static_cast<KeyingSet *>(list->first); ks; ks = ksn) {
ksn = ks->next;
BKE_keyingset_free(ks);
BKE_keyingset_free_paths(ks);
BLI_freelinkN(list, ks);
}
}

View File

@ -153,7 +153,7 @@ static int remove_active_keyingset_exec(bContext *C, wmOperator *op)
ks = static_cast<KeyingSet *>(BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1));
/* free KeyingSet's data, then remove it from the scene */
BKE_keyingset_free(ks);
BKE_keyingset_free_paths(ks);
BLI_freelinkN(&scene->keyingsets, ks);
/* the active one should now be the previously second-to-last one */
@ -229,26 +229,23 @@ static int remove_active_ks_path_exec(bContext *C, wmOperator *op)
BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1));
/* if there is a KeyingSet, find the nominated path to remove */
if (ks) {
KS_Path *ksp = static_cast<KS_Path *>(BLI_findlink(&ks->paths, ks->active_path - 1));
if (ksp) {
/* remove the active path from the KeyingSet */
BKE_keyingset_free_path(ks, ksp);
/* the active path should now be the previously second-to-last active one */
ks->active_path--;
}
else {
BKE_report(op->reports, RPT_ERROR, "No active Keying Set path to remove");
return OPERATOR_CANCELLED;
}
}
else {
if (!ks) {
BKE_report(op->reports, RPT_ERROR, "No active Keying Set to remove a path from");
return OPERATOR_CANCELLED;
}
KS_Path *ksp = static_cast<KS_Path *>(BLI_findlink(&ks->paths, ks->active_path - 1));
if (!ksp) {
BKE_report(op->reports, RPT_ERROR, "No active Keying Set path to remove");
return OPERATOR_CANCELLED;
}
/* remove the active path from the KeyingSet */
BKE_keyingset_free_path(ks, ksp);
/* the active path should now be the previously second-to-last active one */
ks->active_path--;
return OPERATOR_FINISHED;
}
@ -271,14 +268,9 @@ void ANIM_OT_keying_set_path_remove(wmOperatorType *ot)
static int add_keyingset_button_exec(bContext *C, wmOperator *op)
{
Scene *scene = CTX_data_scene(C);
KeyingSet *ks = nullptr;
PropertyRNA *prop = nullptr;
PointerRNA ptr = {nullptr};
char *path = nullptr;
bool changed = false;
int index = 0, pflag = 0;
const bool all = RNA_boolean_get(op->ptr, "all");
if (!UI_context_active_but_prop_get(C, &ptr, &prop, &index)) {
/* pass event on if no active button found */
@ -289,6 +281,8 @@ static int add_keyingset_button_exec(bContext *C, wmOperator *op)
* - use the active one for now (more control over this can be added later)
* - add a new one if it doesn't exist
*/
KeyingSet *ks = nullptr;
Scene *scene = CTX_data_scene(C);
if (scene->active_keyingset == 0) {
eKS_Settings flag = eKS_Settings(0);
eInsertKeyFlags keyingflag = eInsertKeyFlags(0);
@ -319,6 +313,9 @@ static int add_keyingset_button_exec(bContext *C, wmOperator *op)
}
/* check if property is able to be added */
const bool all = RNA_boolean_get(op->ptr, "all");
char *path = nullptr;
bool changed = false;
if (ptr.owner_id && ptr.data && prop && RNA_property_animateable(&ptr, prop)) {
path = RNA_path_from_ID_to_property(&ptr, prop);
@ -377,12 +374,8 @@ void ANIM_OT_keyingset_button_add(wmOperatorType *ot)
static int remove_keyingset_button_exec(bContext *C, wmOperator *op)
{
Scene *scene = CTX_data_scene(C);
KeyingSet *ks = nullptr;
PropertyRNA *prop = nullptr;
PointerRNA ptr = {nullptr};
char *path = nullptr;
bool changed = false;
int index = 0;
if (!UI_context_active_but_prop_get(C, &ptr, &prop, &index)) {
@ -394,6 +387,7 @@ static int remove_keyingset_button_exec(bContext *C, wmOperator *op)
* - use the active one for now (more control over this can be added later)
* - return error if it doesn't exist
*/
Scene *scene = CTX_data_scene(C);
if (scene->active_keyingset == 0) {
BKE_report(op->reports, RPT_ERROR, "No active Keying Set to remove property from");
return OPERATOR_CANCELLED;
@ -404,8 +398,11 @@ static int remove_keyingset_button_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
ks = static_cast<KeyingSet *>(BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1));
KeyingSet *ks = static_cast<KeyingSet *>(
BLI_findlink(&scene->keyingsets, scene->active_keyingset - 1));
bool changed = false;
char *path = nullptr;
if (ptr.owner_id && ptr.data && prop) {
path = RNA_path_from_ID_to_property(&ptr, prop);
@ -596,19 +593,20 @@ void ANIM_keyingset_info_unregister(Main *bmain, KeyingSetInfo *ksi)
ksn = ks->next;
/* remove if matching typeinfo name */
if (STREQ(ks->typeinfo, ksi->idname)) {
Scene *scene;
BKE_keyingset_free(ks);
BLI_remlink(&builtin_keyingsets, ks);
for (scene = static_cast<Scene *>(bmain->scenes.first); scene;
scene = static_cast<Scene *>(scene->id.next))
{
BLI_remlink_safe(&scene->keyingsets, ks);
}
MEM_freeN(ks);
if (!STREQ(ks->typeinfo, ksi->idname)) {
continue;
}
Scene *scene;
BKE_keyingset_free_paths(ks);
BLI_remlink(&builtin_keyingsets, ks);
for (scene = static_cast<Scene *>(bmain->scenes.first); scene;
scene = static_cast<Scene *>(scene->id.next))
{
BLI_remlink_safe(&scene->keyingsets, ks);
}
MEM_freeN(ks);
}
/* free the type info */
@ -710,9 +708,11 @@ KeyingSet *ANIM_get_keyingset_for_autokeying(const Scene *scene, const char *tra
{
return ANIM_scene_get_active_keyingset(scene);
}
if (blender::animrig::is_autokey_flag(scene, AUTOKEY_FLAG_INSERTAVAIL)) {
return ANIM_builtin_keyingset_get_named(nullptr, ANIM_KS_AVAILABLE_ID);
}
return ANIM_builtin_keyingset_get_named(nullptr, transformKSName);
}
@ -788,11 +788,7 @@ const EnumPropertyItem *ANIM_keying_sets_enum_itemf(bContext *C,
PropertyRNA * /*prop*/,
bool *r_free)
{
Scene *scene = CTX_data_scene(C);
KeyingSet *ks;
EnumPropertyItem *item = nullptr, item_tmp = {0};
int totitem = 0;
int i = 0;
int enum_index = 0;
if (C == nullptr) {
return rna_enum_dummy_DEFAULT_items;
@ -801,29 +797,33 @@ const EnumPropertyItem *ANIM_keying_sets_enum_itemf(bContext *C,
/* active Keying Set
* - only include entry if it exists
*/
Scene *scene = CTX_data_scene(C);
EnumPropertyItem *item = nullptr, item_tmp = {0};
int totitem = 0;
if (scene->active_keyingset) {
/* active Keying Set */
item_tmp.identifier = "__ACTIVE__";
item_tmp.name = "Active Keying Set";
item_tmp.value = i;
item_tmp.value = enum_index;
RNA_enum_item_add(&item, &totitem, &item_tmp);
/* separator */
RNA_enum_item_add_separator(&item, &totitem);
}
i++;
enum_index++;
/* user-defined Keying Sets
* - these are listed in the order in which they were defined for the active scene
*/
KeyingSet *ks;
if (scene->keyingsets.first) {
for (ks = static_cast<KeyingSet *>(scene->keyingsets.first); ks; ks = ks->next, i++) {
for (ks = static_cast<KeyingSet *>(scene->keyingsets.first); ks; ks = ks->next, enum_index++) {
if (ANIM_keyingset_context_ok_poll(C, ks)) {
item_tmp.identifier = ks->idname;
item_tmp.name = ks->name;
item_tmp.description = ks->description;
item_tmp.value = i;
item_tmp.value = enum_index;
RNA_enum_item_add(&item, &totitem, &item_tmp);
}
}
@ -833,14 +833,14 @@ const EnumPropertyItem *ANIM_keying_sets_enum_itemf(bContext *C,
}
/* builtin Keying Sets */
i = -1;
for (ks = static_cast<KeyingSet *>(builtin_keyingsets.first); ks; ks = ks->next, i--) {
enum_index = -1;
for (ks = static_cast<KeyingSet *>(builtin_keyingsets.first); ks; ks = ks->next, enum_index--) {
/* only show KeyingSet if context is suitable */
if (ANIM_keyingset_context_ok_poll(C, ks)) {
item_tmp.identifier = ks->idname;
item_tmp.name = ks->name;
item_tmp.description = ks->description;
item_tmp.value = i;
item_tmp.value = enum_index;
RNA_enum_item_add(&item, &totitem, &item_tmp);
}
}
@ -886,20 +886,20 @@ KeyingSet *ANIM_keyingset_get_from_idname(Scene *scene, const char *idname)
bool ANIM_keyingset_context_ok_poll(bContext *C, KeyingSet *ks)
{
if ((ks->flag & KEYINGSET_ABSOLUTE) == 0) {
KeyingSetInfo *ksi = ANIM_keyingset_info_find_name(ks->typeinfo);
/* get the associated 'type info' for this KeyingSet */
if (ksi == nullptr) {
return false;
}
/* TODO: check for missing callbacks! */
/* check if it can be used in the current context */
return ksi->poll(ksi, C);
if ((ks->flag & KEYINGSET_ABSOLUTE)) {
return true;
}
return true;
KeyingSetInfo *ksi = ANIM_keyingset_info_find_name(ks->typeinfo);
/* get the associated 'type info' for this KeyingSet */
if (ksi == nullptr) {
return false;
}
/* TODO: check for missing callbacks! */
/* check if it can be used in the current context */
return ksi->poll(ksi, C);
}
/* Special 'Overrides' Iterator for Relative KeyingSets ------ */
@ -962,52 +962,54 @@ eModifyKey_Returns ANIM_validate_keyingset(bContext *C, ListBase *dsources, Keyi
{
/* sanity check */
if (ks == nullptr) {
return eModifyKey_Returns(0);
return MODIFYKEY_SUCCESS;
}
/* if relative Keying Sets, poll and build up the paths */
if ((ks->flag & KEYINGSET_ABSOLUTE) == 0) {
KeyingSetInfo *ksi = ANIM_keyingset_info_find_name(ks->typeinfo);
if ((ks->flag & KEYINGSET_ABSOLUTE)) {
return MODIFYKEY_SUCCESS;
}
/* clear all existing paths
* NOTE: BKE_keyingset_free() frees all of the paths for the KeyingSet, but not the set itself
*/
BKE_keyingset_free(ks);
KeyingSetInfo *ksi = ANIM_keyingset_info_find_name(ks->typeinfo);
/* get the associated 'type info' for this KeyingSet */
if (ksi == nullptr) {
return MODIFYKEY_MISSING_TYPEINFO;
}
/* TODO: check for missing callbacks! */
/* clear all existing paths
* NOTE: BKE_keyingset_free_paths() frees all of the paths for the KeyingSet, but not the set
* itself
*/
BKE_keyingset_free_paths(ks);
/* check if it can be used in the current context */
if (ksi->poll(ksi, C)) {
/* if a list of data sources are provided, run a special iterator over them,
* otherwise, just continue per normal
*/
if (dsources) {
RKS_ITER_overrides_list(ksi, C, ks, dsources);
}
else {
ksi->iter(ksi, C, ks);
}
/* get the associated 'type info' for this KeyingSet */
if (ksi == nullptr) {
return MODIFYKEY_MISSING_TYPEINFO;
}
/* TODO: check for missing callbacks! */
/* if we don't have any paths now, then this still qualifies as invalid context */
/* FIXME: we need some error conditions (to be retrieved from the iterator why this failed!)
*/
if (BLI_listbase_is_empty(&ks->paths)) {
return MODIFYKEY_INVALID_CONTEXT;
}
}
else {
/* poll callback tells us that KeyingSet is useless in current context */
/* FIXME: the poll callback needs to give us more info why */
return MODIFYKEY_INVALID_CONTEXT;
}
/* check if it can be used in the current context */
if (!ksi->poll(ksi, C)) {
/* poll callback tells us that KeyingSet is useless in current context */
/* FIXME: the poll callback needs to give us more info why */
return MODIFYKEY_INVALID_CONTEXT;
}
/* if a list of data sources are provided, run a special iterator over them,
* otherwise, just continue per normal
*/
if (dsources) {
RKS_ITER_overrides_list(ksi, C, ks, dsources);
}
else {
ksi->iter(ksi, C, ks);
}
/* if we don't have any paths now, then this still qualifies as invalid context */
/* FIXME: we need some error conditions (to be retrieved from the iterator why this failed!)
*/
if (BLI_listbase_is_empty(&ks->paths)) {
return MODIFYKEY_INVALID_CONTEXT;
}
/* succeeded; return 0 to tag error free */
return eModifyKey_Returns(0);
return MODIFYKEY_SUCCESS;
}
/* Determine which keying flags apply based on the override flags */
@ -1044,41 +1046,41 @@ static eInsertKeyFlags keyingset_apply_keying_flags(const eInsertKeyFlags base_f
int ANIM_apply_keyingset(bContext *C, ListBase *dsources, KeyingSet *ks, short mode, float cfra)
{
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
ReportList *reports = CTX_wm_reports(C);
ListBase nla_cache = {nullptr, nullptr};
const eInsertKeyFlags base_kflags = ANIM_get_keyframing_flags(scene, true);
const char *groupname = nullptr;
eInsertKeyFlags kflag = eInsertKeyFlags(0);
int num_channels = 0;
char keytype = scene->toolsettings->keyframe_type;
/* sanity checks */
if (ks == nullptr) {
return 0;
}
/* get flags to use */
Scene *scene = CTX_data_scene(C);
const eInsertKeyFlags base_kflags = ANIM_get_keyframing_flags(scene, true);
eInsertKeyFlags kflag = INSERTKEY_NOFLAGS;
if (mode == MODIFYKEY_MODE_INSERT) {
/* use context settings as base */
kflag = keyingset_apply_keying_flags(
base_kflags, eInsertKeyFlags(ks->keyingoverride), eInsertKeyFlags(ks->keyingflag));
}
else if (mode == MODIFYKEY_MODE_DELETE) {
kflag = eInsertKeyFlags(0);
kflag = INSERTKEY_NOFLAGS;
}
/* if relative Keying Sets, poll and build up the paths */
{
const eModifyKey_Returns error = ANIM_validate_keyingset(C, dsources, ks);
if (error != 0) {
if (error != MODIFYKEY_SUCCESS) {
BLI_assert(error < 0);
/* return error code if failed */
return error;
}
}
Main *bmain = CTX_data_main(C);
ReportList *reports = CTX_wm_reports(C);
ListBase nla_cache = {nullptr, nullptr};
char keytype = scene->toolsettings->keyframe_type;
int num_channels = 0;
const char *groupname = nullptr;
/* apply the paths as specified in the KeyingSet now */
LISTBASE_FOREACH (KS_Path *, ksp, &ks->paths) {
int arraylen, i;

View File

@ -195,6 +195,7 @@ enum eModifyKey_Modes {
/* return codes for errors (with Relative KeyingSets) */
enum eModifyKey_Returns {
MODIFYKEY_SUCCESS = 0,
/** Context info was invalid for using the Keying Set. */
MODIFYKEY_INVALID_CONTEXT = -1,
/** There isn't any type-info for generating paths from context. */

View File

@ -31,17 +31,22 @@ static void rna_KeyingSet_context_refresh(KeyingSet *ks, bContext *C, ReportList
/* TODO: enable access to providing a list of overrides (dsources)? */
const eModifyKey_Returns error = ANIM_validate_keyingset(C, nullptr, ks);
if (error != 0) {
switch (error) {
case MODIFYKEY_INVALID_CONTEXT:
BKE_report(reports, RPT_ERROR, "Invalid context for keying set");
break;
if (error == MODIFYKEY_SUCCESS) {
return;
}
case MODIFYKEY_MISSING_TYPEINFO:
BKE_report(
reports, RPT_ERROR, "Incomplete built-in keying set, appears to be missing type info");
break;
}
switch (error) {
case MODIFYKEY_INVALID_CONTEXT:
BKE_report(reports, RPT_ERROR, "Invalid context for keying set");
break;
case MODIFYKEY_MISSING_TYPEINFO:
BKE_report(
reports, RPT_ERROR, "Incomplete built-in keying set, appears to be missing type info");
break;
default:
break;
}
}