Refactor: Anim: simplify ANIM_remove_driver()

Simplify `ANIM_remove_driver()` by removing unused parameters and handling
simple cases first.

No functional changes.

Pull Request: https://projects.blender.org/blender/blender/pulls/121655
This commit is contained in:
Sybren A. Stüvel
2024-05-10 15:43:31 +02:00
parent 40f2df1a81
commit 45bf2eae98
5 changed files with 39 additions and 49 deletions

View File

@@ -520,49 +520,39 @@ int ANIM_add_driver(
return done_tot;
}
bool ANIM_remove_driver(
ReportList * /*reports*/, ID *id, const char rna_path[], int array_index, short /*flag*/)
bool ANIM_remove_driver(ID *id, const char rna_path[], int array_index)
{
AnimData *adt;
FCurve *fcu;
bool success = false;
/* we don't check the validity of the path here yet, but it should be ok... */
adt = BKE_animdata_from_id(id);
if (adt) {
if (array_index == -1) {
/* step through all drivers, removing all of those with the same base path */
FCurve *fcu_iter = static_cast<FCurve *>(adt->drivers.first);
while ((fcu = BKE_fcurve_iter_step(fcu_iter, rna_path)) != nullptr) {
/* Store the next fcurve for looping. */
fcu_iter = fcu->next;
/* remove F-Curve from driver stack, then free it */
BLI_remlink(&adt->drivers, fcu);
BKE_fcurve_free(fcu);
/* done successfully */
success = true;
}
}
else {
/* find the matching driver and remove it only
* NOTE: here is one of the places where we don't want new F-Curve + Driver added!
* so 'add' var must be 0
*/
fcu = verify_driver_fcurve(id, rna_path, array_index, DRIVER_FCURVE_LOOKUP_ONLY);
if (fcu) {
BLI_remlink(&adt->drivers, fcu);
BKE_fcurve_free(fcu);
success = true;
}
}
AnimData *adt = BKE_animdata_from_id(id);
if (!adt) {
return false;
}
return success;
if (array_index >= 0) {
/* Simple case: Find the matching driver and remove it. */
FCurve *fcu = verify_driver_fcurve(id, rna_path, array_index, DRIVER_FCURVE_LOOKUP_ONLY);
if (!fcu) {
return false;
}
BLI_remlink(&adt->drivers, fcu);
BKE_fcurve_free(fcu);
return true;
}
/* Step through all drivers, removing all of those with the same RNA path. */
bool any_driver_removed = false;
FCurve *fcu_iter = static_cast<FCurve *>(adt->drivers.first);
FCurve *fcu;
while ((fcu = BKE_fcurve_iter_step(fcu_iter, rna_path)) != nullptr) {
/* Store the next fcurve for looping. */
fcu_iter = fcu->next;
BLI_remlink(&adt->drivers, fcu);
BKE_fcurve_free(fcu);
any_driver_removed = true;
}
return any_driver_removed;
}
/* ************************************************** */
@@ -1101,7 +1091,7 @@ static int remove_driver_button_exec(bContext *C, wmOperator *op)
if (ptr.owner_id && ptr.data && prop) {
if (const std::optional<std::string> path = RNA_path_from_ID_to_property(&ptr, prop)) {
changed = ANIM_remove_driver(op->reports, ptr.owner_id, path->c_str(), index, 0);
changed = ANIM_remove_driver(ptr.owner_id, path->c_str(), index);
}
}

View File

@@ -353,10 +353,11 @@ int ANIM_add_driver(
/**
* \brief Main Driver Management API calls.
*
* Remove the driver for the specified property on the given ID block (if available).
* Remove the driver for the specified property on the given ID block.
*
* \return Whether any driver was removed.
*/
bool ANIM_remove_driver(
ReportList *reports, ID *id, const char rna_path[], int array_index, short flag);
bool ANIM_remove_driver(ID *id, const char rna_path[], int array_index);
/* -------- */

View File

@@ -3295,7 +3295,7 @@ void GRAPH_OT_driver_variables_paste(wmOperatorType *ot)
/** \name Delete Invalid Drivers Operator
* \{ */
static int graph_driver_delete_invalid_exec(bContext *C, wmOperator *op)
static int graph_driver_delete_invalid_exec(bContext *C, wmOperator * /*op*/)
{
bAnimContext ac;
ListBase anim_data = {nullptr, nullptr};
@@ -3326,7 +3326,7 @@ static int graph_driver_delete_invalid_exec(bContext *C, wmOperator *op)
continue;
}
ok |= ANIM_remove_driver(op->reports, ale->id, fcu->rna_path, fcu->array_index, 0);
ok |= ANIM_remove_driver(ale->id, fcu->rna_path, fcu->array_index);
if (!ok) {
break;
}

View File

@@ -1832,8 +1832,7 @@ static void do_outliner_drivers_editop(SpaceOutliner *space_outliner,
break;
}
case DRIVERS_EDITMODE_REMOVE: {
/* remove driver matching the information obtained (only if valid) */
ANIM_remove_driver(reports, id, path, array_index, dflags);
ANIM_remove_driver(id, path, array_index);
break;
}
}

View File

@@ -669,7 +669,7 @@ PyObject *pyrna_struct_driver_remove(BPy_StructRNA *self, PyObject *args)
BKE_reports_init(&reports, RPT_STORE);
result = ANIM_remove_driver(&reports, (ID *)self->ptr.owner_id, path_full, index, 0);
result = ANIM_remove_driver(self->ptr.owner_id, path_full, index);
if (path != path_full) {
MEM_freeN((void *)path_full);