Shape Keys: replace the BKE_keyblock_from_key function with find_index.

The function for retrieving a shape key by its index is named
somewhat confusingly, and effectively reimplements BLI_findlink.

However, more importantly, for some reason it is coded to return
null for the index 0 instead of the basis shape key. This severely
limits its usability in some cases.

This refactor replaces the function with a simple strongly typed
wrapper around BLI_findlink, using a different name, and updating
the call sites to check that the index is not 0 where necessary.
This commit is contained in:
Alexander Gavrilov
2023-07-14 20:57:28 +03:00
committed by Alexander Gavrilov
parent 1ba2c933b4
commit f5602becde
5 changed files with 13 additions and 26 deletions

View File

@@ -88,9 +88,9 @@ struct KeyBlock *BKE_keyblock_add(struct Key *key, const char *name);
*/
struct KeyBlock *BKE_keyblock_add_ctime(struct Key *key, const char *name, bool do_force);
/**
* Get the appropriate #KeyBlock given an index.
* Get the appropriate #KeyBlock given an index (0 refers to the basis key). Key may be null.
*/
struct KeyBlock *BKE_keyblock_from_key(struct Key *key, int index);
struct KeyBlock *BKE_keyblock_find_by_index(struct Key *key, int index);
/**
* Get the appropriate #KeyBlock given a name to search for.
*/

View File

@@ -357,9 +357,9 @@ static float (*get_orco_coords(Object *ob, BMEditMesh *em, int layer, int *free)
if (!em) {
ClothModifierData *clmd = (ClothModifierData *)BKE_modifiers_findby_type(
ob, eModifierType_Cloth);
if (clmd) {
KeyBlock *kb = BKE_keyblock_from_key(BKE_key_from_object(ob),
clmd->sim_parms->shapekey_rest);
if (clmd && clmd->sim_parms->shapekey_rest) {
KeyBlock *kb = BKE_keyblock_find_by_index(BKE_key_from_object(ob),
clmd->sim_parms->shapekey_rest);
if (kb && kb->data) {
return (float(*)[3])kb->data;

View File

@@ -443,7 +443,7 @@ static char *shapekey_adrcodes_to_paths(ID *id, int adrcode, int * /*r_array_ind
else {
/* Find the name of the ShapeKey (i.e. KeyBlock) to look for */
Key *key = (Key *)id;
KeyBlock *kb = BKE_keyblock_from_key(key, adrcode);
KeyBlock *kb = BKE_keyblock_find_by_index(key, adrcode);
/* setting that we alter is the "value" (i.e. keyblock.curval) */
if (kb) {

View File

@@ -1897,12 +1897,7 @@ KeyBlock *BKE_keyblock_from_object(Object *ob)
{
Key *key = BKE_key_from_object(ob);
if (key) {
KeyBlock *kb = static_cast<KeyBlock *>(BLI_findlink(&key->block, ob->shapenr - 1));
return kb;
}
return nullptr;
return BKE_keyblock_find_by_index(key, ob->shapenr - 1);
}
KeyBlock *BKE_keyblock_from_object_reference(Object *ob)
@@ -1916,21 +1911,13 @@ KeyBlock *BKE_keyblock_from_object_reference(Object *ob)
return nullptr;
}
KeyBlock *BKE_keyblock_from_key(Key *key, int index)
KeyBlock *BKE_keyblock_find_by_index(Key *key, int index)
{
if (key) {
KeyBlock *kb = static_cast<KeyBlock *>(key->block.first);
for (int i = 1; i < key->totkey; i++) {
kb = kb->next;
if (index == i) {
return kb;
}
}
if (!key) {
return nullptr;
}
return nullptr;
return static_cast<KeyBlock *>(BLI_findlink(&key->block, index));
}
KeyBlock *BKE_keyblock_find_name(Key *key, const char name[])

View File

@@ -100,8 +100,8 @@ static void deform_verts(ModifierData *md,
* Also hopefully new cloth system will arrive soon..
*/
if (mesh == nullptr && clmd->sim_parms->shapekey_rest) {
KeyBlock *kb = BKE_keyblock_from_key(BKE_key_from_object(ctx->object),
clmd->sim_parms->shapekey_rest);
KeyBlock *kb = BKE_keyblock_find_by_index(BKE_key_from_object(ctx->object),
clmd->sim_parms->shapekey_rest);
if (kb && kb->data != nullptr) {
float(*layerorco)[3];
if (!(layerorco = static_cast<float(*)[3]>(