Blender V5.3
key.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cmath>
11#include <cstddef>
12#include <cstring>
13#include <optional>
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_listbase.hh"
18#include "BLI_math_matrix_c.hh"
19#include "BLI_math_vector_c.hh"
20#include "BLI_string.hh"
21#include "BLI_string_utf8.hh"
22#include "BLI_string_utils.hh"
23#include "BLI_task.hh"
24#include "BLI_utildefines.hh"
25
26#include "BLT_translation.hh"
27
28/* Allow using deprecated functionality for .blend file I/O. */
29#define DNA_DEPRECATED_ALLOW
30
31#include "DNA_ID.h"
32#include "DNA_curve_types.h"
33#include "DNA_key_types.h"
34#include "DNA_lattice_types.h"
35#include "DNA_mesh_types.h"
36#include "DNA_meshdata_types.h"
37#include "DNA_object_types.h"
38
39#include "BKE_anim_data.hh"
40#include "BKE_animsys.hh"
41#include "BKE_attribute.hh"
42#include "BKE_curve.hh"
43#include "BKE_customdata.hh"
44#include "BKE_deform.hh"
45#include "BKE_editmesh.hh"
46#include "BKE_global.hh"
47#include "BKE_idtype.hh"
48#include "BKE_key.hh"
49#include "BKE_lattice.hh"
50#include "BKE_lib_id.hh"
51#include "BKE_lib_query.hh"
52#include "BKE_main.hh"
53#include "BKE_mesh.hh"
54#include "BKE_scene.hh"
55
56#include "RNA_access.hh"
57#include "RNA_path.hh"
58#include "RNA_prototypes.hh"
59
60#include "BLO_read_write.hh"
61
62namespace blender {
63
64static void shapekey_copy_data(Main * /*bmain*/,
65 std::optional<Library *> /*owner_library*/,
66 ID *id_dst,
67 const ID *id_src,
68 const int /*flag*/)
69{
70 Key *key_dst = id_cast<Key *>(id_dst);
71 const Key *key_src = id_cast<const Key *>(id_src);
72 BLI_duplicatelist(&key_dst->block, &key_src->block);
73
74 KeyBlock *kb_dst, *kb_src;
75 for (kb_src = key_src->block.first(), kb_dst = key_dst->block.first(); kb_dst;
76 kb_src = kb_src->next, kb_dst = kb_dst->next)
77 {
78 if (kb_dst->data) {
79 kb_dst->data = MEM_dupalloc_void(kb_dst->data);
80 }
81 if (kb_src == key_src->refkey) {
82 key_dst->refkey = kb_dst;
83 }
84 }
85}
86
87static void shapekey_free_data(ID *id)
88{
89 Key *key = id_cast<Key *>(id);
90 while (KeyBlock *kb = static_cast<KeyBlock *>(BLI_pophead(&key->block))) {
91 if (kb->data) {
92 MEM_delete_void(kb->data);
93 }
94 MEM_delete(kb);
95 }
96}
97
99{
100 Key *key = reinterpret_cast<Key *>(id);
102}
103
104static ID **shapekey_owner_pointer_get(ID *id, const bool debug_relationship_assert)
105{
106 Key *key = id_cast<Key *>(id);
107
108 if (debug_relationship_assert) {
109 BLI_assert(key->from != nullptr);
110 BLI_assert(BKE_key_from_id(key->from) == key);
111 }
112
113 return &key->from;
114}
115
116static void shapekey_blend_write(BlendWriter *writer, ID *id, const void *id_address)
117{
118 Key *key = id_cast<Key *>(id);
119 const bool is_undo = writer->is_undo();
120
121 /* Write LibData. */
122 writer->write_id_struct(id_address, key);
123 BKE_id_blend_write(writer, &key->id);
124
125 /* Direct data. */
126 for (KeyBlock &kb : key->block) {
127 /* Do not store actual geometry data in case this is a library override ID. */
128 if (ID_IS_OVERRIDE_LIBRARY(key) && !is_undo) {
129 writer->write_struct(&kb, [](BlendStructWriter<KeyBlock> &struct_writer) {
130 struct_writer.shallow_data.totelem = 0;
131 struct_writer.shallow_data.data = nullptr;
132 });
133 }
134 else {
135 writer->write_struct(&kb);
136 if (kb.data != nullptr) {
137 writer->write_raw(kb.totelem * key->elemsize, kb.data);
138 }
139 }
140 }
141}
142
143/* Old defines from DNA_ipo_types.h for data-type, stored in DNA - don't modify! */
144#define IPO_FLOAT 4
145#define IPO_BEZTRIPLE 100
146#define IPO_BPOINT 101
147
149{
150 Key *key = id_cast<Key *>(id);
151 BLO_read_struct_list(reader, KeyBlock, &(key->block));
152
153 BLO_read_struct(reader, KeyBlock, &key->refkey);
154
155 for (KeyBlock &kb : key->block) {
157 reader, reinterpret_cast<std::byte **>(&kb.data), &kb.totelem, key->elemsize);
158
159 /* NOTE: This is endianness-sensitive. */
160 /* Keyblock data would need specific endian switching depending of the exact type of data it
161 * contain. */
162 }
163}
164
166{
167 /* ShapeKeys should always only be linked indirectly through their user ID (mesh, Curve etc.), or
168 * be fully local data, or part of linked packed data. */
169 BLI_assert((id->tag & ID_TAG_EXTERN) == 0 || ID_IS_PACKED(id));
171}
172
174 .id_code = Key::id_type,
175 .id_filter = FILTER_ID_KE,
176 /* Warning! key->from, could be more types in future? */
177 .dependencies_id_types = FILTER_ID_ME | FILTER_ID_CU_LEGACY | FILTER_ID_LT,
178 .main_listbase_index = INDEX_ID_KE,
179 .struct_size = sizeof(Key),
180 .name = "Key",
181 .name_plural = N_("shape_keys"),
182 .translation_context = BLT_I18NCONTEXT_ID_SHAPEKEY,
184 .asset_type_info = nullptr,
185
186 .init_data = nullptr,
187 .copy_data = shapekey_copy_data,
188 .free_data = shapekey_free_data,
189 .make_local = nullptr,
190 .foreach_id = shapekey_foreach_id,
191 .foreach_cache = nullptr,
192 .foreach_path = nullptr,
193 .foreach_working_space_color = nullptr,
194 .foreach_asset_weak_reference = nullptr,
195 /* A bit weird, due to shape-keys not being strictly speaking embedded data... But they also
196 * share a lot with those (non linkable, only ever used by one owner ID, etc.). */
197 .owner_pointer_get = shapekey_owner_pointer_get,
198
199 .blend_write = shapekey_blend_write,
200 .blend_read_data = shapekey_blend_read_data,
201 .blend_read_after_liblink = shapekey_blend_read_after_liblink,
202
203 .blend_read_undo_preserve = nullptr,
204
205 .lib_override_apply_post = nullptr,
206};
207
208#define KEY_MODE_DUMMY 0 /* Use where mode isn't checked for. */
209#define KEY_MODE_BPOINT 1
210#define KEY_MODE_BEZTRIPLE 2
211
212/* Internal use only. */
217
219{
220 while (KeyBlock *kb = static_cast<KeyBlock *>(BLI_pophead(&key->block))) {
221 if (kb->data) {
222 MEM_delete_void(kb->data);
223 }
224 MEM_delete(kb);
225 }
226}
227
228Key *BKE_key_add(Main *bmain, ID *id) /* Common function. */
229{
230 Key *key = BKE_id_new<Key>(bmain, "Key");
231
232 key->type = KEY_NORMAL;
233 key->from = id;
234
235 key->uidgen = 1;
236
237 char *el;
238 /* XXX The code here uses some defines which will soon be deprecated... (comment probably Joshua
239 * in 2009). */
240 switch (GS(id->name)) {
241 case ID_ME:
242 el = key->elemstr;
243
245 el[1] = IPO_FLOAT;
246 el[2] = 0;
247
248 key->elemsize = sizeof(float[KEYELEM_FLOAT_LEN_COORD]);
249
250 break;
251 case ID_LT:
252 el = key->elemstr;
253
255 el[1] = IPO_FLOAT;
256 el[2] = 0;
257
258 key->elemsize = sizeof(float[KEYELEM_FLOAT_LEN_COORD]);
259
260 break;
261 case ID_CU_LEGACY:
262 el = key->elemstr;
263
265 el[1] = IPO_BPOINT;
266 el[2] = 0;
267
268 key->elemsize = sizeof(float[KEYELEM_ELEM_SIZE_CURVE]);
269
270 break;
271
272 default:
273 break;
274 }
275
276 return key;
277}
278
280{
281 KeyBlock *kb;
282
283 /* Locate the key which is out of position. */
284 for (kb = key->block.first(); kb; kb = kb->next) {
285 if ((kb->next) && (kb->pos > kb->next->pos)) {
286 break;
287 }
288 }
289
290 /* If we find a key, move it. */
291 if (kb) {
292 kb = kb->next; /* next key is the out-of-order one */
293 BLI_remlink(&key->block, kb);
294
295 /* Find the right location and insert before. */
296 for (KeyBlock &kb2 : key->block) {
297 if (kb2.pos > kb->pos) {
298 BLI_insertlinkafter(&key->block, kb2.prev, kb);
299 break;
300 }
301 }
302 }
303
304 /* New rule; first key is refkey, this to match drawing channels... */
305 key->refkey = key->block.first();
306}
307
308/**************** do the key ****************/
309
311{
312 float t2, t3, fc;
313
314 switch (type) {
315 case KEY_LINEAR:
316 data[0] = 0.0f;
317 data[1] = -t + 1.0f;
318 data[2] = t;
319 data[3] = 0.0f;
320 break;
321 case KEY_CARDINAL:
322 t2 = t * t;
323 t3 = t2 * t;
324 fc = 0.71f;
325
326 data[0] = -fc * t3 + 2.0f * fc * t2 - fc * t;
327 data[1] = (2.0f - fc) * t3 + (fc - 3.0f) * t2 + 1.0f;
328 data[2] = (fc - 2.0f) * t3 + (3.0f - 2.0f * fc) * t2 + fc * t;
329 data[3] = fc * t3 - fc * t2;
330 break;
331 case KEY_BSPLINE:
332 t2 = t * t;
333 t3 = t2 * t;
334
335 data[0] = -0.16666666f * t3 + 0.5f * t2 - 0.5f * t + 0.16666666f;
336 data[1] = 0.5f * t3 - t2 + 0.66666666f;
337 data[2] = -0.5f * t3 + 0.5f * t2 + 0.5f * t + 0.16666666f;
338 data[3] = 0.16666666f * t3;
339 break;
340 case KEY_CATMULL_ROM:
341 t2 = t * t;
342 t3 = t2 * t;
343 fc = 0.5f;
344
345 data[0] = -fc * t3 + 2.0f * fc * t2 - fc * t;
346 data[1] = (2.0f - fc) * t3 + (fc - 3.0f) * t2 + 1.0f;
347 data[2] = (fc - 2.0f) * t3 + (3.0f - 2.0f * fc) * t2 + fc * t;
348 data[3] = fc * t3 - fc * t2;
349 break;
350 }
351}
352
354{
355 float t2, fc;
356
357 switch (type) {
358 case KEY_LINEAR:
359 data[0] = 0.0f;
360 data[1] = -1.0f;
361 data[2] = 1.0f;
362 data[3] = 0.0f;
363 break;
364 case KEY_CARDINAL:
365 t2 = t * t;
366 fc = 0.71f;
367
368 data[0] = -3.0f * fc * t2 + 4.0f * fc * t - fc;
369 data[1] = 3.0f * (2.0f - fc) * t2 + 2.0f * (fc - 3.0f) * t;
370 data[2] = 3.0f * (fc - 2.0f) * t2 + 2.0f * (3.0f - 2.0f * fc) * t + fc;
371 data[3] = 3.0f * fc * t2 - 2.0f * fc * t;
372 break;
373 case KEY_BSPLINE:
374 t2 = t * t;
375
376 data[0] = -0.5f * t2 + t - 0.5f;
377 data[1] = 1.5f * t2 - t * 2.0f;
378 data[2] = -1.5f * t2 + t + 0.5f;
379 data[3] = 0.5f * t2;
380 break;
381 case KEY_CATMULL_ROM:
382 t2 = t * t;
383 fc = 0.5f;
384
385 data[0] = -3.0f * fc * t2 + 4.0f * fc * t - fc;
386 data[1] = 3.0f * (2.0f - fc) * t2 + 2.0f * (fc - 3.0f) * t;
387 data[2] = 3.0f * (fc - 2.0f) * t2 + 2.0f * (3.0f - 2.0f * fc) * t + fc;
388 data[3] = 3.0f * fc * t2 - 2.0f * fc * t;
389 break;
390 }
391}
392
393void key_curve_normal_weights(const float t, float data[4], const KeyInterpolationType type)
394{
395 float fc;
396
397 switch (type) {
398 case KEY_LINEAR:
399 data[0] = 0.0f;
400 data[1] = 0.0f;
401 data[2] = 0.0f;
402 data[3] = 0.0f;
403 break;
404 case KEY_CARDINAL:
405 fc = 0.71f;
406
407 data[0] = -6.0f * fc * t + 4.0f * fc;
408 data[1] = 6.0f * (2.0f - fc) * t + 2.0f * (fc - 3.0f);
409 data[2] = 6.0f * (fc - 2.0f) * t + 2.0f * (3.0f - 2.0f * fc);
410 data[3] = 6.0f * fc * t - 2.0f * fc;
411 break;
412 case KEY_BSPLINE:
413 data[0] = -1.0f * t + 1.0f;
414 data[1] = 3.0f * t - 2.0f;
415 data[2] = -3.0f * t + 1.0f;
416 data[3] = 1.0f * t;
417 break;
418 case KEY_CATMULL_ROM:
419 fc = 0.5f;
420
421 data[0] = -6.0f * fc * t + 4.0f * fc;
422 data[1] = 6.0f * (2.0f - fc) * t + 2.0f * (fc - 3.0f);
423 data[2] = 6.0f * (fc - 2.0f) * t + 2.0f * (3.0f - 2.0f * fc);
424 data[3] = 6.0f * fc * t - 2.0f * fc;
425 break;
426 }
427}
428
439static bool get_keys_for_absolute_eval(float eval_time,
440 const ListBaseT<KeyBlock> *keyblocks,
441 KeyBlock *r_target_keys[4],
442 float r_weights[4])
443{
444 KeyBlock *firstkey = keyblocks->first();
445 KeyBlock *lastkey = keyblocks->last();
446 eval_time = clamp_f(eval_time, firstkey->pos, lastkey->pos);
447
448 r_target_keys[0] = r_target_keys[1] = r_target_keys[2] = r_target_keys[3] = firstkey;
449 r_weights[0] = r_weights[1] = r_weights[2] = r_weights[3] = firstkey->pos;
450
451 if (firstkey->next == nullptr) {
452 /* There is only a single shapekey, we cannot do interpolation in this case. */
453 return true;
454 }
455
456 r_target_keys[2] = firstkey->next;
457 r_weights[2] = r_target_keys[2]->pos;
458 r_target_keys[3] = r_target_keys[2]->next;
459 if (r_target_keys[3] == nullptr) {
460 r_target_keys[3] = r_target_keys[2];
461 }
462 r_weights[3] = r_target_keys[3]->pos;
463 KeyBlock *key_iter = r_target_keys[3];
464
465 /* Find the correct shapekeys. */
466 while (r_weights[2] < eval_time) {
467 if (key_iter->next == nullptr) {
468 /* This triggers when `key_iter->next` has been a nullptr for one loop. */
469 if (r_weights[2] == r_weights[3]) {
470 break;
471 }
472 }
473 else {
474 key_iter = key_iter->next;
475 }
476
477 r_weights[0] = r_weights[1];
478 r_target_keys[0] = r_target_keys[1];
479 r_weights[1] = r_weights[2];
480 r_target_keys[1] = r_target_keys[2];
481 r_weights[2] = r_weights[3];
482 r_target_keys[2] = r_target_keys[3];
483 r_weights[3] = key_iter->pos;
484 r_target_keys[3] = key_iter;
485 }
486
487 bool bsplinetype = false;
488 if (r_target_keys[1]->type == KEY_BSPLINE || r_target_keys[2]->type == KEY_BSPLINE) {
489 bsplinetype = true;
490 }
491
492 if (bsplinetype == false) { /* B spline doesn't go through the control points. */
493 if (eval_time <= r_weights[1]) {
494 /* This can happen if there are only 2 shapekeys. */
495 r_weights[2] = r_weights[1];
496 r_target_keys[2] = r_target_keys[1];
497 return true;
498 }
499 if (eval_time >= r_weights[2]) { /* `eval_time` after 2nd key. */
500 return true;
501 }
502 }
503 else if (eval_time > r_weights[2]) { /* Last key. */
504 eval_time = r_weights[2];
505 r_target_keys[3] = r_target_keys[2];
506 r_weights[3] = r_weights[2];
507 }
508
509 float delta = r_weights[2] - r_weights[1];
510 if (delta == 0.0f) {
511 if (bsplinetype == false) {
512 return true; /* Both keys equal. */
513 }
514 }
515 else {
516 delta = (eval_time - r_weights[1]) / delta;
517 }
518
519 /* Interpolation. */
520 key_curve_position_weights(delta, r_weights, KeyInterpolationType(r_target_keys[1]->type));
521
522 if (r_target_keys[1]->type != r_target_keys[2]->type) {
523 float t_other[4];
524 key_curve_position_weights(delta, t_other, KeyInterpolationType(r_target_keys[2]->type));
525 interp_v4_v4v4(r_weights, r_weights, t_other, delta);
526 }
527
528 return false;
529}
530
531static char *key_block_get_data(Key *key, KeyBlock *actkb, KeyBlock *kb, char **freedata)
532{
533 if (kb == actkb) {
534 /* This hack makes it possible to edit shape keys in
535 * edit mode with shape keys blending applied. */
536 if (GS(key->from->name) == ID_ME) {
537
538 Mesh *mesh = id_cast<Mesh *>(key->from);
540
541 if (mesh->runtime->edit_mesh && bm->totvert == kb->totelem) {
542 int a = 0;
543 float (*co)[3];
544 co = MEM_new_array_uninitialized<float[3]>(size_t(bm->totvert), "key_block_get_data");
545
546 BMVert *eve;
547 BMIter iter;
548 BM_ITER_MESH (eve, &iter, const_cast<BMesh *>(bm), BM_VERTS_OF_MESH) {
549 copy_v3_v3(co[a], eve->co);
550 a++;
551 }
552
553 *freedata = reinterpret_cast<char *>(co);
554 return reinterpret_cast<char *>(co);
555 }
556 }
557 }
558
559 *freedata = nullptr;
560 return static_cast<char *>(kb->data);
561}
562
570 const int start_index, const float weight, const float *a, const float *b, float *r_target)
571{
572 r_target[start_index + 0] += weight * (b[start_index + 0] - a[start_index + 0]);
573 r_target[start_index + 1] += weight * (b[start_index + 1] - a[start_index + 1]);
574 r_target[start_index + 2] += weight * (b[start_index + 2] - a[start_index + 2]);
575}
576
580static void flerp(const float f0[3],
581 const float f1[3],
582 const float f2[3],
583 const float f3[3],
584 const float weights[4],
585 float r_out[3])
586{
587 r_out[0] = weights[0] * f0[0] + weights[1] * f1[0] + weights[2] * f2[0] + weights[3] * f3[0];
588 r_out[1] = weights[0] * f0[1] + weights[1] * f1[1] + weights[2] * f2[1] + weights[3] * f3[1];
589 r_out[2] = weights[0] * f0[2] + weights[1] * f1[2] + weights[2] * f2[2] + weights[3] * f3[2];
590}
591
595static void copy_key_float3(
596 const int vertex_count, Key *key, KeyBlock *active_keyblock, KeyBlock *source, float *r_target)
597{
598 if (vertex_count == 0 || source->totelem == 0) {
599 return;
600 }
601 char *free_keyblock_data;
602 float *keyblock_data = reinterpret_cast<float *>(
603 key_block_get_data(key, active_keyblock, source, &free_keyblock_data));
604
605 if (vertex_count == source->totelem) {
606 memcpy(r_target, keyblock_data, vertex_count * 3 * sizeof(float));
607 }
608 else {
609 /* In case of KeyBlocks that have a different number of elements than the original data.
610 * Maintained for backwards compatibility even though this state should not be reachable
611 * through normal interactions with Blender. */
612 const float step_rate = source->totelem / float(vertex_count);
613 for (int i = 0; i < vertex_count; i++) {
614 /* Rounding down to avoid exceeding the bounds. */
615 const int source_index = int(step_rate * i);
616 memcpy(&r_target[i * 3], &keyblock_data[source_index * 3], 3 * sizeof(float));
617 }
618 }
619
620 if (free_keyblock_data) {
621 MEM_delete(free_keyblock_data);
622 }
623}
624
633static void copy_key_float3_weighted(const int vertex_count,
634 Key *key,
635 KeyBlock *active_keyblock,
636 KeyBlock *source,
637 const float *weights,
638 float *r_target)
639{
640 if (!weights) {
641 copy_key_float3(vertex_count, key, active_keyblock, source, r_target);
642 return;
643 }
644
645 if (vertex_count != source->totelem) {
646 /* There was a system in place before that worked with keys that only have partial data. I
647 * (christoph) removed that since there is no known case for that. */
649 return;
650 }
651
652 char *free_source_data, *free_refkey_data;
653 const float *source_data = reinterpret_cast<float *>(
654 key_block_get_data(key, active_keyblock, source, &free_source_data));
655 const float *refkey_data = reinterpret_cast<float *>(
656 key_block_get_data(key, active_keyblock, key->refkey, &free_refkey_data));
657
658 for (int i = 0; i < vertex_count; i++) {
659 const int vector_index = i * 3;
660 memcpy(&r_target[vector_index], &refkey_data[vector_index], 3 * sizeof(float));
661 if (weights[i] != 0.0f) {
662 add_weighted_vector(vector_index, weights[i], refkey_data, source_data, r_target);
663 }
664 }
665
666 if (free_source_data) {
667 MEM_delete(free_source_data);
668 }
669 if (free_refkey_data) {
670 MEM_delete(free_refkey_data);
671 }
672}
673
682 KeyBlock *active_keyblock,
683 const std::optional<Span<bool>> keys_to_process,
684 const int vertex_count,
685 float **per_keyblock_weights,
686 float *target_data)
687{
688 /* Creates the basis values of the reference key in target_data. */
689 copy_key_float3(vertex_count, key, active_keyblock, key->refkey, target_data);
690
691 /* Cannot use auto [keyblock_index, kb] here because that would throw a warning at the
692 * parallel_for. */
693 for (std::pair<int, KeyBlock &> enumerator : key->block.enumerate()) {
694 KeyBlock &kb = enumerator.second;
695 if (&kb == key->refkey) {
696 continue;
697 }
698 if (keys_to_process && !(*keys_to_process)[enumerator.first]) {
699 continue;
700 }
701 /* No difference in vertex count allowed. */
702 if (kb.flag & KEYBLOCK_MUTE || kb.totelem != vertex_count) {
703 continue;
704 }
705 const float kb_influence = kb.curval;
706 if (kb_influence == 0.0f) {
707 continue;
708 }
709 /* Reference can be any block. */
710 KeyBlock *reference_kb = static_cast<KeyBlock *>(BLI_findlink(&key->block, kb.relative));
711 if (reference_kb == nullptr) {
712 continue;
713 }
714
715 const float *weights = per_keyblock_weights ? per_keyblock_weights[enumerator.first] : nullptr;
716
717 char *freefrom = nullptr;
718 const float *from = reinterpret_cast<float *>(
719 key_block_get_data(key, active_keyblock, &kb, &freefrom));
720
721 /* For meshes, use the original values instead of the bmesh values to
722 * maintain a constant offset. */
723 const float *reffrom = static_cast<float *>(reference_kb->data);
724 threading::parallel_for(IndexRange(vertex_count), 1024, [&](const IndexRange range) {
725 for (const int i : range) {
726 const float weight = weights ? (weights[i] * kb.curval) : kb.curval;
727 /* Each vertex has 3 floats. */
728 const int vector_index = i * 3;
729 add_weighted_vector(vector_index, weight, reffrom, from, target_data);
730 }
731 });
732
733 if (freefrom) {
734 MEM_delete(freefrom);
735 }
736 }
737}
738
742static void key_evaluate_absolute(const int vertex_count,
743 Key *key,
744 KeyBlock *active_keyblock,
745 KeyBlock *shapekeys[4],
746 const float weights[4],
747 float *r_target)
748{
749 char *freek1, *freek2, *freek3, *freek4;
750 float *k1 = reinterpret_cast<float *>(
751 key_block_get_data(key, active_keyblock, shapekeys[0], &freek1));
752 float *k2 = reinterpret_cast<float *>(
753 key_block_get_data(key, active_keyblock, shapekeys[1], &freek2));
754 float *k3 = reinterpret_cast<float *>(
755 key_block_get_data(key, active_keyblock, shapekeys[2], &freek3));
756 float *k4 = reinterpret_cast<float *>(
757 key_block_get_data(key, active_keyblock, shapekeys[3], &freek4));
758
759 /* The step rate will be 1 in normal cases. We have to account for shapekeys with different
760 * element counts though since that case used to be supported. */
761 const float step_k1 = shapekeys[0]->totelem / float(vertex_count);
762 const float step_k2 = shapekeys[1]->totelem / float(vertex_count);
763 const float step_k3 = shapekeys[2]->totelem / float(vertex_count);
764 const float step_k4 = shapekeys[3]->totelem / float(vertex_count);
765
766 threading::parallel_for(IndexRange(vertex_count), 1024, [&](const IndexRange range) {
767 for (const int i : range) {
768 flerp(&k1[int(i * step_k1) * 3],
769 &k2[int(i * step_k2) * 3],
770 &k3[int(i * step_k3) * 3],
771 &k4[int(i * step_k4) * 3],
772 weights,
773 &r_target[i * 3]);
774 }
775 });
776
777 if (freek1) {
778 MEM_delete(freek1);
779 }
780 if (freek2) {
781 MEM_delete(freek2);
782 }
783 if (freek3) {
784 MEM_delete(freek3);
785 }
786 if (freek4) {
787 MEM_delete(freek4);
788 }
789}
790
791static float *get_weights_array(Object *ob, const char *vgroup, WeightsArrayCache *cache)
792{
793 /* No vgroup string set? */
794 if (vgroup[0] == 0) {
795 return nullptr;
796 }
797
798 const MDeformVert *dvert = nullptr;
799 int totvert = 0;
800
801 /* Gather dvert and totvert. */
802 const BMesh *bm = nullptr;
803 if (ob->type == OB_MESH) {
804 Mesh *mesh = id_cast<Mesh *>(ob->data);
805 dvert = mesh->deform_verts().data();
806 totvert = mesh->verts_num;
807
808 const BMesh *bm_test = BKE_editmesh_bmesh_get(mesh);
809 if (bm_test && bm_test->totvert == totvert) {
810 bm = bm_test;
811 }
812 }
813 else if (ob->type == OB_LATTICE) {
814 Lattice *lt = id_cast<Lattice *>(ob->data);
815 dvert = lt->dvert;
816 totvert = lt->pntsu * lt->pntsv * lt->pntsw;
817 }
818
819 if (dvert == nullptr) {
820 return nullptr;
821 }
822
823 /* Find the group (weak loop-in-loop). */
824 const int defgrp_index = BKE_object_defgroup_name_index(ob, vgroup);
825 if (defgrp_index != -1) {
826 float *weights;
827
828 if (cache) {
829 if (cache->defgroup_weights == nullptr) {
830 int num_defgroup = BKE_object_defgroup_count(ob);
832 "cached defgroup weights");
833 cache->num_defgroup_weights = num_defgroup;
834 }
835
836 if (cache->defgroup_weights[defgrp_index]) {
837 return cache->defgroup_weights[defgrp_index];
838 }
839 }
840
841 weights = MEM_new_array_uninitialized<float>(size_t(totvert), "weights");
842
843 if (bm) {
844 int i;
845 const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
846 BMIter iter;
847 BMVert *eve;
848 BM_ITER_MESH_INDEX (eve, &iter, const_cast<BMesh *>(bm), BM_VERTS_OF_MESH, i) {
849 dvert = static_cast<const MDeformVert *>(BM_ELEM_CD_GET_VOID_P(eve, cd_dvert_offset));
850 weights[i] = BKE_defvert_find_weight(dvert, defgrp_index);
851 }
852 }
853 else {
854 for (int i = 0; i < totvert; i++, dvert++) {
855 weights[i] = BKE_defvert_find_weight(dvert, defgrp_index);
856 }
857 }
858
859 if (cache) {
860 cache->defgroup_weights[defgrp_index] = weights;
861 }
862
863 return weights;
864 }
865 return nullptr;
866}
867
869{
870 float **per_keyblock_weights = MEM_new_array_uninitialized<float *>(size_t(key->totkey),
871 "per keyblock weights");
872
873 for (const auto [keyblock_index, keyblock] : key->block.enumerate()) {
874 per_keyblock_weights[keyblock_index] = get_weights_array(ob, keyblock.vgroup, cache);
875 }
876
877 return per_keyblock_weights;
878}
879
881 float **per_keyblock_weights,
882 WeightsArrayCache *cache)
883{
884 if (cache) {
885 if (cache->num_defgroup_weights) {
886 for (int a = 0; a < cache->num_defgroup_weights; a++) {
887 if (cache->defgroup_weights[a]) {
888 MEM_delete(cache->defgroup_weights[a]);
889 }
890 }
891 MEM_delete(cache->defgroup_weights);
892 }
893 cache->defgroup_weights = nullptr;
894 }
895 else {
896 for (int a = 0; a < key->totkey; a++) {
897 if (per_keyblock_weights[a]) {
898 MEM_delete(per_keyblock_weights[a]);
899 }
900 }
901 }
902
903 MEM_delete(per_keyblock_weights);
904}
905
906static void do_mesh_key(Object *ob,
907 Key *key,
908 const std::optional<Span<bool>> keys_to_process,
909 char *out,
910 const int tot)
911{
913
914 if (key->type == KEY_RELATIVE) {
915 WeightsArrayCache cache = {0, nullptr};
916 float **per_keyblock_weights;
917 per_keyblock_weights = keyblock_get_per_block_weights(ob, key, &cache);
919 key, actkb, keys_to_process, tot, per_keyblock_weights, reinterpret_cast<float *>(out));
920 keyblock_free_per_block_weights(key, per_keyblock_weights, &cache);
921 }
922 else {
923 const float ctime_scaled = key->ctime / 100.0f;
924 KeyBlock *shapekeys[4];
925 float weights[4];
926 const bool simple_copy = get_keys_for_absolute_eval(
927 ctime_scaled, &key->block, shapekeys, weights);
928
929 if (simple_copy == false) {
930 key_evaluate_absolute(tot, key, actkb, shapekeys, weights, reinterpret_cast<float *>(out));
931 }
932 else {
933 copy_key_float3(tot, key, actkb, shapekeys[2], reinterpret_cast<float *>(out));
934 }
935 }
936}
937
938static void do_curve_key(Object *ob,
939 Key *key,
940 const std::optional<Span<bool>> keys_to_process,
941 char *out,
942 const int tot)
943{
945
946 if (key->type == KEY_RELATIVE) {
948 key, actkb, keys_to_process, tot, nullptr, reinterpret_cast<float *>(out));
949 }
950 else {
951 const float ctime_scaled = key->ctime / 100.0f;
952 KeyBlock *shapekeys[4];
953 float weights[4];
954 const bool simple_copy = get_keys_for_absolute_eval(
955 ctime_scaled, &key->block, shapekeys, weights);
956
957 if (simple_copy == false) {
958 key_evaluate_absolute(tot, key, actkb, shapekeys, weights, reinterpret_cast<float *>(out));
959 }
960 else {
961 copy_key_float3(tot, key, actkb, shapekeys[2], reinterpret_cast<float *>(out));
962 }
963 }
964}
965
966static void do_latt_key(Object *ob,
967 Key *key,
968 const std::optional<Span<bool>> keys_to_process,
969 char *out,
970 const int tot)
971{
972 Lattice *lt = id_cast<Lattice *>(ob->data);
974
975 if (key->type == KEY_RELATIVE) {
976 float **per_keyblock_weights;
977 per_keyblock_weights = keyblock_get_per_block_weights(ob, key, nullptr);
979 key, actkb, keys_to_process, tot, per_keyblock_weights, reinterpret_cast<float *>(out));
980 keyblock_free_per_block_weights(key, per_keyblock_weights, nullptr);
981 }
982 else {
983 const float ctime_scaled = key->ctime / 100.0f;
984 KeyBlock *shapekeys[4];
985 float weights[4];
986 const bool simple_copy = get_keys_for_absolute_eval(
987 ctime_scaled, &key->block, shapekeys, weights);
988
989 if (simple_copy == false) {
990 key_evaluate_absolute(tot, key, actkb, shapekeys, weights, reinterpret_cast<float *>(out));
991 }
992 else {
993 copy_key_float3(tot, key, actkb, shapekeys[2], reinterpret_cast<float *>(out));
994 }
995 }
996
997 if (lt->flag & LT_OUTSIDE) {
998 outside_lattice(lt);
999 }
1000}
1001
1002static void keyblock_data_convert_to_lattice(const float (*fp)[3],
1003 BPoint *bpoint,
1004 const int totpoint);
1005static void keyblock_data_convert_to_curve(const float *fp,
1006 ListBaseT<Nurb> *nurb,
1007 const int totpoint);
1008
1010 int *r_totelem,
1011 float *arr,
1012 size_t arr_size,
1013 const std::optional<Span<bool>> keys_to_process,
1014 ID *obdata)
1015{
1016 Key *key = BKE_key_from_object(ob);
1018
1019 if (key == nullptr || key->block.is_empty()) {
1020 return nullptr;
1021 }
1022
1023 /* Compute size of output array. */
1024 int tot = 0, size = 0;
1025 if (ob->type == OB_MESH) {
1026 Mesh *mesh = id_cast<Mesh *>(ob->data);
1027
1028 tot = mesh->verts_num;
1029 size = tot * sizeof(float[KEYELEM_FLOAT_LEN_COORD]);
1030 }
1031 else if (ob->type == OB_LATTICE) {
1032 Lattice *lt = id_cast<Lattice *>(ob->data);
1033
1034 tot = lt->pntsu * lt->pntsv * lt->pntsw;
1035 size = tot * sizeof(float[KEYELEM_FLOAT_LEN_COORD]);
1036 }
1037 else if (ELEM(ob->type, OB_CURVES_LEGACY, OB_SURF)) {
1038 Curve *cu = id_cast<Curve *>(ob->data);
1039
1041 size = tot * sizeof(float[KEYELEM_ELEM_SIZE_CURVE]);
1042 }
1043
1044 /* If nothing to interpolate, cancel. */
1045 if (tot == 0 || size == 0) {
1046 return nullptr;
1047 }
1048
1049 /* Allocate array. */
1050 char *out;
1051 if (arr == nullptr) {
1052 out = MEM_new_array_zeroed<char>(size, "BKE_key_evaluate_object out");
1053 }
1054 else {
1055 if (arr_size != size) {
1056 return nullptr;
1057 }
1058
1059 out = reinterpret_cast<char *>(arr);
1060 }
1061
1062 if (ob->shapeflag & OB_SHAPE_LOCK) {
1063 /* Shape locked, copy the locked shape instead of blending. */
1064 KeyBlock *kb = static_cast<KeyBlock *>(BLI_findlink(&key->block, ob->shapenr - 1));
1065
1066 if (kb && (kb->flag & KEYBLOCK_MUTE)) {
1067 kb = key->refkey;
1068 }
1069
1070 if (kb == nullptr) {
1071 kb = key->block.first();
1072 ob->shapenr = 1;
1073 }
1074
1075 if (OB_TYPE_SUPPORT_VGROUP(ob->type)) {
1076 const float *weights = get_weights_array(ob, kb->vgroup, nullptr);
1077 copy_key_float3_weighted(tot, key, actkb, kb, weights, reinterpret_cast<float *>(out));
1078
1079 if (weights) {
1080 MEM_delete(weights);
1081 }
1082 }
1083 else if (ELEM(ob->type, OB_CURVES_LEGACY, OB_SURF)) {
1084 copy_key_float3(tot, key, actkb, kb, reinterpret_cast<float *>(out));
1085 }
1086 }
1087 else {
1088 if (ob->type == OB_MESH) {
1089 do_mesh_key(ob, key, keys_to_process, out, tot);
1090 }
1091 else if (ob->type == OB_LATTICE) {
1092 do_latt_key(ob, key, keys_to_process, out, tot);
1093 }
1094 else if (ob->type == OB_CURVES_LEGACY) {
1095 do_curve_key(ob, key, keys_to_process, out, tot);
1096 }
1097 else if (ob->type == OB_SURF) {
1098 do_curve_key(ob, key, keys_to_process, out, tot);
1099 }
1100 }
1101
1102 if (obdata != nullptr) {
1103 switch (GS(obdata->name)) {
1104 case ID_ME: {
1105 Mesh *mesh = id_cast<Mesh *>(obdata);
1106 const int totvert = min_ii(tot, mesh->verts_num);
1107 mesh->vert_positions_for_write().take_front(totvert).copy_from(
1108 {reinterpret_cast<const float3 *>(out), totvert});
1109 mesh->tag_positions_changed();
1110 break;
1111 }
1112 case ID_LT: {
1113 Lattice *lattice = id_cast<Lattice *>(obdata);
1114 const int totpoint = min_ii(tot, lattice->pntsu * lattice->pntsv * lattice->pntsw);
1116 reinterpret_cast<const float (*)[3]>(out), lattice->def, totpoint);
1117 break;
1118 }
1119 case ID_CU_LEGACY: {
1120 Curve *curve = id_cast<Curve *>(obdata);
1121 const int totpoint = min_ii(tot, BKE_keyblock_curve_element_count(&curve->nurb));
1123 reinterpret_cast<const float *>(out), &curve->nurb, totpoint);
1124 break;
1125 }
1126 default:
1128 }
1129 }
1130
1131 if (r_totelem) {
1132 *r_totelem = tot;
1133 }
1134 return reinterpret_cast<float *>(out);
1135}
1136
1137float *BKE_key_evaluate_object(Object *ob, int *r_totelem)
1138{
1139 return BKE_key_evaluate_object_ex(ob, r_totelem, nullptr, 0, std::nullopt, nullptr);
1140}
1141
1142int BKE_keyblock_element_count_from_shape(const Key *key, const int shape_index)
1143{
1144 int result = 0;
1145
1146 for (const auto [index, kb] : key->block.enumerate()) {
1147 if (ELEM(shape_index, -1, index)) {
1148 result += kb.totelem;
1149 }
1150 }
1151 return result;
1152}
1153
1155{
1157}
1158
1159size_t BKE_keyblock_element_calc_size_from_shape(const Key *key, const int shape_index)
1160{
1161 return size_t(BKE_keyblock_element_count_from_shape(key, shape_index)) * key->elemsize;
1162}
1163
1165{
1167}
1168
1169/* -------------------------------------------------------------------- */
1175
1178 const int shape_index)
1179{
1180 uint8_t *elements = reinterpret_cast<uint8_t *>(arr.data());
1181
1182 for (const auto [index, kb] : key->block.enumerate()) {
1183 if (ELEM(shape_index, -1, index)) {
1184 const int block_elem_len = kb.totelem * key->elemsize;
1185 memcpy(elements, kb.data, block_elem_len);
1186 elements += block_elem_len;
1187 }
1188 }
1189}
1190
1192{
1194}
1195
1197 const int shape_index,
1198 const Span<float3> coords,
1199 const float4x4 &transform)
1200{
1201 if (key->elemsize != sizeof(float[3])) {
1202 BLI_assert_msg(0, "Invalid elemsize");
1203 return;
1204 }
1205
1206 const float3 *elements = coords.data();
1207
1208 for (const auto [index, kb] : key->block.enumerate()) {
1209 if (ELEM(shape_index, -1, index)) {
1210 const int block_elem_len = kb.totelem;
1211 float (*block_data)[3] = static_cast<float (*)[3]>(kb.data);
1212 for (int data_offset = 0; data_offset < block_elem_len; ++data_offset) {
1213 const float *src_data = reinterpret_cast<const float *>(elements + data_offset);
1214 float *dst_data = reinterpret_cast<float *>(block_data + data_offset);
1215 mul_v3_m4v3(dst_data, transform.ptr(), src_data);
1216 }
1217 elements += block_elem_len;
1218 }
1219 }
1220}
1221
1223 const ListBaseT<Nurb> *nurb,
1224 const int shape_index,
1225 const void *data,
1226 const float4x4 &transform)
1227{
1228 const uint8_t *elements = static_cast<const uint8_t *>(data);
1229
1230 for (const auto [index, kb] : key->block.enumerate()) {
1231 if (ELEM(shape_index, -1, index)) {
1232 const int block_elem_size = kb.totelem * key->elemsize;
1233 BKE_keyblock_curve_data_transform(nurb, transform.ptr(), elements, kb.data);
1234 elements += block_elem_size;
1235 }
1236 }
1237}
1238
1239void BKE_keyblock_data_set(Key *key, const int shape_index, const void *data)
1240{
1241 const uint8_t *elements = static_cast<const uint8_t *>(data);
1242
1243 for (const auto [index, kb] : key->block.enumerate()) {
1244 if (ELEM(shape_index, -1, index)) {
1245 const int block_elem_size = kb.totelem * key->elemsize;
1246 memcpy(kb.data, elements, block_elem_size);
1247 elements += block_elem_size;
1248 }
1249 }
1250}
1251
1253
1254bool BKE_key_idtype_support(const short id_type)
1255{
1256 switch (id_type) {
1257 case ID_ME:
1258 case ID_CU_LEGACY:
1259 case ID_LT:
1260 return true;
1261 default:
1262 return false;
1263 }
1264}
1265
1267{
1268 switch (GS(id->name)) {
1269 case ID_ME: {
1270 Mesh *mesh = id_cast<Mesh *>(id);
1271 return &mesh->key;
1272 }
1273 case ID_CU_LEGACY: {
1274 Curve *cu = id_cast<Curve *>(id);
1275 if (cu->ob_type != OB_FONT) {
1276 return &cu->key;
1277 }
1278 break;
1279 }
1280 case ID_LT: {
1281 Lattice *lt = id_cast<Lattice *>(id);
1282 return &lt->key;
1283 }
1284 default:
1285 break;
1286 }
1287
1288 return nullptr;
1289}
1290
1292{
1293 Key **key_p;
1294 key_p = BKE_key_from_id_p(id);
1295 if (key_p) {
1296 return *key_p;
1297 }
1298
1299 return nullptr;
1300}
1301
1303{
1304 if (ob == nullptr || ob->data == nullptr) {
1305 return nullptr;
1306 }
1307
1308 return BKE_key_from_id_p(ob->data);
1309}
1310
1312{
1313 Key **key_p;
1314 key_p = BKE_key_from_object_p(ob);
1315 if (key_p) {
1316 return *key_p;
1317 }
1318
1319 return nullptr;
1320}
1321
1322KeyBlock *BKE_keyblock_add(Key *key, const char *name)
1323{
1324 float curpos = -0.1;
1325
1326 KeyBlock *kb = key->block.last();
1327 if (kb) {
1328 curpos = kb->pos;
1329 }
1330
1331 kb = MEM_new<KeyBlock>("Keyblock");
1332 BLI_addtail(&key->block, kb);
1333 kb->type = KEY_LINEAR;
1334
1335 const int tot = key->block.count();
1336 if (name) {
1337 STRNCPY_UTF8(kb->name, name);
1338 }
1339 else {
1340 if (tot == 1) {
1341 STRNCPY_UTF8(kb->name, DATA_("Basis"));
1342 }
1343 else {
1344 SNPRINTF_UTF8(kb->name, DATA_("Key %d"), tot - 1);
1345 }
1346 }
1347
1348 BLI_uniquename(&key->block, kb, DATA_("Key"), '.', offsetof(KeyBlock, name), sizeof(kb->name));
1349
1350 kb->uid = key->uidgen++;
1351
1352 key->totkey++;
1353 if (key->totkey == 1) {
1354 key->refkey = kb;
1355 }
1356
1357 kb->slidermin = 0.0f;
1358 kb->slidermax = 1.0f;
1359
1360 /* \note The caller may want to set this to current time, but don't do it here since we need to
1361 * sort which could cause problems in some cases, see #BKE_keyblock_add_ctime. */
1362 kb->pos = curpos + 0.1f; /* Only used for absolute shape keys. */
1363
1364 return kb;
1365}
1366
1368{
1369 BLI_assert(BLI_findindex(&key->block, kb_src) != -1);
1370 KeyBlock *kb_dst = BKE_keyblock_add(key, kb_src->name);
1371 kb_dst->totelem = kb_src->totelem;
1372 kb_dst->data = MEM_dupalloc_void(kb_src->data);
1373 BLI_remlink(&key->block, kb_dst);
1374 BLI_insertlinkafter(&key->block, kb_src, kb_dst);
1375 BKE_keyblock_copy_settings(kb_dst, kb_src);
1376 kb_dst->flag = kb_src->flag;
1377 return kb_dst;
1378}
1379
1380KeyBlock *BKE_keyblock_add_ctime(Key *key, const char *name, const bool do_force)
1381{
1382 KeyBlock *kb = BKE_keyblock_add(key, name);
1383 const float cpos = key->ctime / 100.0f;
1384
1385 /* In case of absolute keys, there is no point in adding more than one key with the same pos.
1386 * Hence only set new key-block pos to current time if none previous one already use it.
1387 * Now at least people just adding absolute keys without touching to ctime
1388 * won't have to systematically use retiming func (and have ordering issues, too). See #39897.
1389 */
1390 if (!do_force && (key->type != KEY_RELATIVE)) {
1391 for (KeyBlock &it_kb : key->block) {
1392 /* Use epsilon to avoid floating point precision issues.
1393 * 1e-3 because the position is stored as frame * 1e-2. */
1394 if (compare_ff(it_kb.pos, cpos, 1e-3f)) {
1395 return kb;
1396 }
1397 }
1398 }
1399 if (do_force || (key->type != KEY_RELATIVE)) {
1400 kb->pos = cpos;
1401 BKE_key_sort(key);
1402 }
1403
1404 return kb;
1405}
1406
1408{
1409 Key *key = BKE_key_from_object(ob);
1410
1411 return BKE_keyblock_find_by_index(key, ob->shapenr - 1);
1412}
1413
1415{
1416 Key *key = BKE_key_from_object(ob);
1417
1418 if (key) {
1419 return key->refkey;
1420 }
1421
1422 return nullptr;
1423}
1424
1426{
1427 if (!key) {
1428 return nullptr;
1429 }
1430
1431 return static_cast<KeyBlock *>(BLI_findlink(&key->block, index));
1432}
1433
1434KeyBlock *BKE_keyblock_find_name(Key *key, const char name[])
1435{
1436 return static_cast<KeyBlock *>(BLI_findstring(&key->block, name, offsetof(KeyBlock, name)));
1437}
1438
1440{
1441 for (KeyBlock &kb : key->block) {
1442 if (kb.uid == uid) {
1443 return &kb;
1444 }
1445 }
1446 return nullptr;
1447}
1448
1450{
1451 kb_dst->pos = kb_src->pos;
1452 kb_dst->curval = kb_src->curval;
1453 kb_dst->type = kb_src->type;
1454 kb_dst->relative = kb_src->relative;
1455 STRNCPY(kb_dst->vgroup, kb_src->vgroup);
1456 kb_dst->slidermin = kb_src->slidermin;
1457 kb_dst->slidermax = kb_src->slidermax;
1458}
1459
1460std::optional<std::string> BKE_keyblock_curval_rnapath_get(const Key *key, const KeyBlock *kb)
1461{
1462 if (ELEM(nullptr, key, kb)) {
1463 return std::nullopt;
1464 }
1466 const_cast<ID *>(&key->id), RNA_ShapeKey, (KeyBlock *)kb);
1467 PropertyRNA *prop = RNA_struct_find_property(&ptr, "value");
1468 return RNA_path_from_ID_to_property(&ptr, prop);
1469}
1470
1471/* conversion functions */
1472
1473/************************* Lattice ************************/
1474
1476{
1477
1478 BLI_assert(kb->totelem == lt->pntsu * lt->pntsv * lt->pntsw);
1479
1480 const int tot = kb->totelem;
1481 if (tot == 0) {
1482 return;
1483 }
1484
1485 BPoint *bp = lt->def;
1486 float (*fp)[3];
1487 fp = static_cast<float (*)[3]>(kb->data);
1488 for (int a = 0; a < kb->totelem; a++, fp++, bp++) {
1489 copy_v3_v3(*fp, bp->vec);
1490 }
1491}
1492
1494{
1495 const int tot = lt->pntsu * lt->pntsv * lt->pntsw;
1496 if (tot == 0) {
1497 return;
1498 }
1499
1500 MEM_SAFE_DELETE_VOID(kb->data);
1501
1502 kb->data = MEM_new_array_uninitialized(size_t(tot), size_t(lt->key->elemsize), __func__);
1503 kb->totelem = tot;
1504
1506}
1507
1508static void keyblock_data_convert_to_lattice(const float (*fp)[3],
1509 BPoint *bpoint,
1510 const int totpoint)
1511{
1512 for (int i = 0; i < totpoint; i++, fp++, bpoint++) {
1513 copy_v3_v3(bpoint->vec, *fp);
1514 }
1515}
1516
1518{
1519 BPoint *bp = lt->def;
1520 const float (*fp)[3] = static_cast<const float (*)[3]>(kb->data);
1521 const int tot = min_ii(kb->totelem, lt->pntsu * lt->pntsv * lt->pntsw);
1522
1524}
1525
1526/************************* Curve ************************/
1527
1528int BKE_keyblock_curve_element_count(const ListBaseT<Nurb> *nurb)
1529{
1530 const Nurb *nu;
1531 int tot = 0;
1532
1533 nu = nurb->first();
1534 while (nu) {
1535 if (nu->bezt) {
1536 tot += KEYELEM_ELEM_LEN_BEZTRIPLE * nu->pntsu;
1537 }
1538 else if (nu->bp) {
1539 tot += KEYELEM_ELEM_LEN_BPOINT * nu->pntsu * nu->pntsv;
1540 }
1541
1542 nu = nu->next;
1543 }
1544 return tot;
1545}
1546
1548 KeyBlock *kb,
1549 const ListBaseT<Nurb> *nurb)
1550{
1552
1553 const int tot = kb->totelem;
1554 if (tot == 0) {
1555 return;
1556 }
1557
1558 BezTriple *bezt;
1559 BPoint *bp;
1560 int a;
1561 float *fp = static_cast<float *>(kb->data);
1562 for (Nurb &nu : *nurb) {
1563 if (nu.bezt) {
1564 for (a = nu.pntsu, bezt = nu.bezt; a; a--, bezt++) {
1565 for (int i = 0; i < 3; i++) {
1566 copy_v3_v3(&fp[i * 3], bezt->vec[i]);
1567 }
1568 fp[9] = bezt->tilt;
1569 fp[10] = bezt->radius;
1571 }
1572 }
1573 else {
1574 for (a = nu.pntsu * nu.pntsv, bp = nu.bp; a; a--, bp++) {
1575 copy_v3_v3(fp, bp->vec);
1576 fp[3] = bp->tilt;
1577 fp[4] = bp->radius;
1579 }
1580 }
1581 }
1582}
1583
1584void BKE_keyblock_curve_data_transform(const ListBaseT<Nurb> *nurb,
1585 const float mat[4][4],
1586 const void *src_data,
1587 void *dst_data)
1588{
1589 const float *src = static_cast<const float *>(src_data);
1590 float *dst = static_cast<float *>(dst_data);
1591 for (Nurb &nu : *nurb) {
1592 if (nu.bezt) {
1593 for (int a = nu.pntsu; a; a--) {
1594 for (int i = 0; i < 3; i++) {
1595 mul_v3_m4v3(&dst[i * 3], mat, &src[i * 3]);
1596 }
1597 dst[9] = src[9];
1598 dst[10] = src[10];
1601 }
1602 }
1603 else {
1604 for (int a = nu.pntsu * nu.pntsv; a; a--) {
1605 mul_v3_m4v3(dst, mat, src);
1606 dst[3] = src[3];
1607 dst[4] = src[4];
1610 }
1611 }
1612 }
1613}
1614
1615void BKE_keyblock_convert_from_curve(const Curve *cu, KeyBlock *kb, const ListBaseT<Nurb> *nurb)
1616{
1617 const int tot = BKE_keyblock_curve_element_count(nurb);
1618 if (tot == 0) {
1619 return;
1620 }
1621
1622 MEM_SAFE_DELETE_VOID(kb->data);
1623
1624 kb->data = MEM_new_array_uninitialized(size_t(tot), size_t(cu->key->elemsize), __func__);
1625 kb->totelem = tot;
1626
1627 BKE_keyblock_update_from_curve(cu, kb, nurb);
1628}
1629
1630static void keyblock_data_convert_to_curve(const float *fp, ListBaseT<Nurb> *nurb, int totpoint)
1631{
1632 for (Nurb *nu = nurb->first(); nu && totpoint > 0; nu = nu->next) {
1633 if (nu->bezt != nullptr) {
1634 BezTriple *bezt = nu->bezt;
1635 for (int i = nu->pntsu; i && (totpoint -= KEYELEM_ELEM_LEN_BEZTRIPLE) >= 0;
1636 i--, bezt++, fp += KEYELEM_FLOAT_LEN_BEZTRIPLE)
1637 {
1638 for (int j = 0; j < 3; j++) {
1639 copy_v3_v3(bezt->vec[j], &fp[j * 3]);
1640 }
1641 bezt->tilt = fp[9];
1642 bezt->radius = fp[10];
1643 }
1644 }
1645 else {
1646 BPoint *bp = nu->bp;
1647 for (int i = nu->pntsu * nu->pntsv; i && (totpoint -= KEYELEM_ELEM_LEN_BPOINT) >= 0;
1648 i--, bp++, fp += KEYELEM_FLOAT_LEN_BPOINT)
1649 {
1650 copy_v3_v3(bp->vec, fp);
1651 bp->tilt = fp[3];
1652 bp->radius = fp[4];
1653 }
1654 }
1655 }
1656}
1657
1658void BKE_keyblock_convert_to_curve(KeyBlock *kb, Curve * /*cu*/, ListBaseT<Nurb> *nurb)
1659{
1660 const float *fp = static_cast<const float *>(kb->data);
1661 const int tot = min_ii(kb->totelem, BKE_keyblock_curve_element_count(nurb));
1662
1663 keyblock_data_convert_to_curve(fp, nurb, tot);
1664}
1665
1666/************************* Mesh ************************/
1667
1669{
1670 BLI_assert(mesh->verts_num == kb->totelem);
1671
1672 const int tot = mesh->verts_num;
1673 if (tot == 0) {
1674 return;
1675 }
1676
1677 const Span<float3> positions = mesh->vert_positions();
1678 memcpy(kb->data, positions.data(), sizeof(float[3]) * tot);
1679}
1680
1682{
1683 const int len = mesh->verts_num;
1684
1685 if (mesh->verts_num == 0) {
1686 return;
1687 }
1688
1689 MEM_SAFE_DELETE_VOID(kb->data);
1690
1691 kb->data = MEM_new_array_uninitialized(size_t(len), size_t(key->elemsize), __func__);
1692 kb->totelem = len;
1693
1695}
1696
1698{
1699 vert_positions.take_front(kb->totelem).copy_from({static_cast<float3 *>(kb->data), kb->totelem});
1700}
1701
1703 Mesh *mesh,
1704 float (*r_vert_normals)[3],
1705 float (*r_face_normals)[3],
1706 float (*r_loop_normals)[3])
1707{
1708 using namespace blender::bke;
1709 if (r_vert_normals == nullptr && r_face_normals == nullptr && r_loop_normals == nullptr) {
1710 return;
1711 }
1712
1713 Array<float3> positions(mesh->vert_positions());
1714 BKE_keyblock_convert_to_mesh(kb, positions);
1715 const OffsetIndices faces = mesh->faces();
1716 const Span<int> corner_verts = mesh->corner_verts();
1717 const Span<int> corner_edges = mesh->corner_edges();
1718
1719 const bool loop_normals_needed = r_loop_normals != nullptr;
1720 const bool vert_normals_needed = r_vert_normals != nullptr;
1721 const bool face_normals_needed = r_face_normals != nullptr || vert_normals_needed ||
1722 loop_normals_needed;
1723
1724 float (*vert_normals)[3] = r_vert_normals;
1725 float (*face_normals)[3] = r_face_normals;
1726 bool free_vert_normals = false;
1727 bool free_face_normals = false;
1728 if (vert_normals_needed && r_vert_normals == nullptr) {
1729 vert_normals = MEM_new_array_uninitialized<float[3]>(size_t(mesh->verts_num), __func__);
1730 free_vert_normals = true;
1731 }
1732 if (face_normals_needed && r_face_normals == nullptr) {
1733 face_normals = MEM_new_array_uninitialized<float[3]>(size_t(mesh->faces_num), __func__);
1734 free_face_normals = true;
1735 }
1736
1737 if (face_normals_needed) {
1739 positions, faces, corner_verts, {reinterpret_cast<float3 *>(face_normals), faces.size()});
1740 }
1741 if (vert_normals_needed) {
1742 mesh::normals_calc_verts(positions,
1743 faces,
1744 corner_verts,
1745 mesh->vert_to_face_map(),
1746 {reinterpret_cast<const float3 *>(face_normals), faces.size()},
1747 {reinterpret_cast<float3 *>(vert_normals), mesh->verts_num});
1748 }
1749 if (loop_normals_needed) {
1750 const AttributeAccessor attributes = mesh->attributes();
1751 const VArraySpan sharp_edges = *attributes.lookup<bool>("sharp_edge", AttrDomain::Edge);
1752 const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", AttrDomain::Face);
1753 const VArraySpan custom_normals = *attributes.lookup<short2>("custom_normal",
1756 faces,
1757 corner_verts,
1758 corner_edges,
1759 mesh->vert_to_face_map(),
1760 {reinterpret_cast<float3 *>(face_normals), faces.size()},
1761 sharp_edges,
1762 sharp_faces,
1763 custom_normals,
1764 nullptr,
1765 {reinterpret_cast<float3 *>(r_loop_normals), corner_verts.size()});
1766 }
1767
1768 if (free_vert_normals) {
1769 MEM_delete(vert_normals);
1770 }
1771 if (free_face_normals) {
1772 MEM_delete(face_normals);
1773 }
1774}
1775
1776/************************* raw coords ************************/
1777
1778bool BKE_keyblock_move(Object *ob, int org_index, int new_index)
1779{
1780 Key *key = BKE_key_from_object(ob);
1781 KeyBlock *kb;
1782 const int act_index = ob->shapenr - 1;
1783 const int totkey = key->totkey;
1784 int i;
1785 bool rev, in_range = false;
1786
1787 if (org_index < 0) {
1788 org_index = act_index;
1789 }
1790
1791 CLAMP(new_index, 0, key->totkey - 1);
1792 CLAMP(org_index, 0, key->totkey - 1);
1793
1794 if (new_index == org_index) {
1795 return false;
1796 }
1797
1798 rev = ((new_index - org_index) < 0) ? true : false;
1799
1800 /* We swap 'org' element with its previous/next neighbor (depending on direction of the move)
1801 * repeatedly, until we reach final position.
1802 * This allows us to only loop on the list once! */
1803 for (kb = static_cast<KeyBlock *>(rev ? key->block.last() : key->block.first()),
1804 i = (rev ? totkey - 1 : 0);
1805 kb;
1806 kb = (rev ? kb->prev : kb->next), rev ? i-- : i++)
1807 {
1808 if (i == org_index) {
1809 in_range = true; /* Start list items swapping... */
1810 }
1811 else if (i == new_index) {
1812 in_range = false; /* End list items swapping. */
1813 }
1814
1815 if (in_range) {
1816 KeyBlock *other_kb = rev ? kb->prev : kb->next;
1817
1818 /* Swap with previous/next list item. */
1819 BLI_listbase_swaplinks(&key->block, kb, other_kb);
1820
1821 /* Swap absolute positions. */
1822 std::swap(kb->pos, other_kb->pos);
1823
1824 kb = other_kb;
1825 }
1826
1827 /* Adjust relative indices, this has to be done on the whole list! */
1828 if (kb->relative == org_index) {
1829 kb->relative = new_index;
1830 }
1831 else if (kb->relative < org_index && kb->relative >= new_index) {
1832 /* Remove after, insert before this index. */
1833 kb->relative++;
1834 }
1835 else if (kb->relative > org_index && kb->relative <= new_index) {
1836 /* Remove before, insert after this index. */
1837 kb->relative--;
1838 }
1839 }
1840
1841 /* Need to update active shape number if it's affected,
1842 * same principle as for relative indices above. */
1843 if (org_index == act_index) {
1844 ob->shapenr = new_index + 1;
1845 }
1846 else if (act_index < org_index && act_index >= new_index) {
1847 ob->shapenr++;
1848 }
1849 else if (act_index > org_index && act_index <= new_index) {
1850 ob->shapenr--;
1851 }
1852
1853 /* First key is always refkey, matches interface and BKE_key_sort. */
1854 key->refkey = key->block.first();
1855
1856 return true;
1857}
1858
1859bool BKE_keyblock_is_basis(const Key *key, const int index)
1860{
1861 const KeyBlock *kb;
1862 int i;
1863
1864 if (key->type == KEY_RELATIVE) {
1865 for (i = 0, kb = key->block.first(); kb; i++, kb = kb->next) {
1866 if ((i != index) && (kb->relative == index)) {
1867 return true;
1868 }
1869 }
1870 }
1871
1872 return false;
1873}
1874
1875std::optional<Array<bool>> BKE_keyblock_get_dependent_keys(const Key *key, const int index)
1876{
1877 if (key->type != KEY_RELATIVE) {
1878 return std::nullopt;
1879 }
1880
1881 const int count = key->block.count();
1882
1883 if (index < 0 || index >= count) {
1884 return std::nullopt;
1885 }
1886
1887 /* Seed the table with the specified key. */
1888 Array<bool> marked(count, false);
1889
1890 marked[index] = true;
1891
1892 /* Iterative breadth-first search through the key list. This method minimizes
1893 * the number of scans through the list and is fail-safe vs reference cycles. */
1894 bool updated, found = false;
1895
1896 do {
1897 updated = false;
1898
1899 for (const auto [i, kb] : key->block.enumerate()) {
1900 if (!marked[i] && kb.relative >= 0 && kb.relative < count && marked[kb.relative]) {
1901 marked[i] = true;
1902 updated = found = true;
1903 }
1904 }
1905 } while (updated);
1906
1907 if (!found) {
1908 return std::nullopt;
1909 }
1910
1911 /* After the search is complete, exclude the original key. */
1912 marked[index] = false;
1913 return marked;
1914}
1915
1916void BKE_keyblock_rename(Main &bmain, Key *key, KeyBlock *kb, const char *newname)
1917{
1918 char oldname[sizeof(kb->name)];
1919
1920 /* Make a copy of the old name first. */
1921 STRNCPY(oldname, kb->name);
1922 /* Copy the new name into the name slot. */
1923 STRNCPY_UTF8(kb->name, newname);
1924
1925 /* Make sure the name is truly unique. */
1926 BLI_uniquename(&key->block,
1927 kb,
1929 '.',
1930 offsetof(KeyBlock, name),
1931 sizeof(kb->name));
1932
1933 /* Fix all the animation data which may link to this. */
1935 "key_blocks",
1936 RNA_path_name_to_infix(oldname),
1938 /*verify_paths=*/true,
1939 bmain);
1940}
1941} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
support for deformation groups and hooks.
#define BKE_LIB_FOREACHID_PROCESS_ID(data_, id_, cb_flag_)
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_SHAPEKEY
#define CTX_DATA_(context, msgid)
#define DATA_(msgid)
ID and Library types, which are fundamental for SDNA.
#define ID_IS_PACKED(_id)
Definition DNA_ID.h:703
#define FILTER_ID_CU_LEGACY
Definition DNA_ID.h:1203
#define FILTER_ID_LT
Definition DNA_ID.h:1209
#define FILTER_ID_ME
Definition DNA_ID.h:1213
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define FILTER_ID_KE
Definition DNA_ID.h:1234
#define KEYELEM_FLOAT_LEN_COORD
#define KEYELEM_ELEM_LEN_BPOINT
#define KEYELEM_FLOAT_LEN_BEZTRIPLE
#define KEYELEM_ELEM_SIZE_CURVE
#define KEYELEM_FLOAT_LEN_BPOINT
#define KEYELEM_ELEM_LEN_BEZTRIPLE
Object is a sort of wrapper for general info.
#define OB_TYPE_SUPPORT_VGROUP(_type)
Read Blender memory allocation and freeing.
#define CLAMP(a, b, c)
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
constexpr IndexRange()=default
constexpr T * data() const
Definition BLI_span.hh:545
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
constexpr const T * data() const
Definition BLI_span.hh:219
GAttributeReader lookup(const StringRef name) const
constexpr void copy_from(Span< T > values) const
#define offsetof(t, d)
#define IPO_FLOAT
Definition key.cc:144
#define IPO_BPOINT
Definition key.cc:146
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void normals_calc_corners(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, GroupedSpan< int > vert_to_face_map, Span< float3 > face_normals, Span< bool > sharp_edges, Span< bool > sharp_faces, Span< short2 > custom_normals, CornerNormalSpaceArray *r_fan_spaces, MutableSpan< float3 > r_corner_normals)
void normals_calc_verts(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face_map, Span< float3 > face_normals, MutableSpan< float3 > vert_normals)
void normals_calc_faces(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< float3 > face_normals)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_keyblock_data_set_with_mat4(Key *key, int shape_index, Span< float3 > coords, const float4x4 &transform)
Definition key.cc:1196
void BKE_keyblock_update_from_lattice(const Lattice *lt, KeyBlock *kb)
Definition key.cc:1475
static float ** keyblock_get_per_block_weights(Object *ob, Key *key, WeightsArrayCache *cache)
Definition key.cc:868
static void flerp(const float f0[3], const float f1[3], const float f2[3], const float f3[3], const float weights[4], float r_out[3])
Definition key.cc:580
void BKE_keyblock_data_get_from_shape(const Key *key, MutableSpan< float3 > arr, int shape_index)
Definition key.cc:1176
KeyBlock * BKE_keyblock_duplicate(Key *key, KeyBlock *kb_src)
Definition key.cc:1367
char * STRNCPY(char(&dst)[N], const char *src)
static void keyblock_data_convert_to_curve(const float *fp, ListBaseT< Nurb > *nurb, const int totpoint)
Definition key.cc:1630
Key ** BKE_key_from_object_p(Object *ob)
Definition key.cc:1302
bool BKE_key_idtype_support(short id_type)
Definition key.cc:1254
static void do_curve_key(Object *ob, Key *key, const std::optional< Span< bool > > keys_to_process, char *out, const int tot)
Definition key.cc:938
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
int BKE_object_defgroup_count(const Object *ob)
Definition deform.cc:608
bool BKE_keyblock_move(Object *ob, int org_index, int new_index)
Definition key.cc:1778
std::optional< std::string > RNA_path_from_ID_to_property(const PointerRNA *ptr, PropertyRNA *prop)
Definition rna_path.cc:1237
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
void BKE_keyblock_curve_data_transform(const ListBaseT< Nurb > *nurb, const float mat[4][4], const void *src, void *dst)
Definition key.cc:1584
static float * get_weights_array(Object *ob, const char *vgroup, WeightsArrayCache *cache)
Definition key.cc:791
void interp_v4_v4v4(float r[4], const float a[4], const float b[4], float t)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
bool BKE_keyblock_is_basis(const Key *key, int index)
Definition key.cc:1859
void BKE_keyblock_convert_to_mesh(const KeyBlock *kb, MutableSpan< float3 > vert_positions)
Definition key.cc:1697
int BKE_keyblock_element_count_from_shape(const Key *key, int shape_index)
Definition key.cc:1142
void BKE_keyblock_update_from_mesh(const Mesh *mesh, KeyBlock *kb)
Definition key.cc:1668
void BKE_key_sort(Key *key)
Definition key.cc:279
void BKE_keyblock_curve_data_set_with_mat4(Key *key, const ListBaseT< Nurb > *nurb, int shape_index, const void *data, const float4x4 &transform)
Definition key.cc:1222
KeyBlock * BKE_keyblock_from_object(Object *ob)
Definition key.cc:1407
std::string RNA_path_name_to_infix(const StringRefNull string)
Definition rna_path.cc:1419
void BKE_keyblock_convert_to_curve(KeyBlock *kb, Curve *cu, ListBaseT< Nurb > *nurb)
Definition key.cc:1658
static void key_evaluate_relative_float3(Key *key, KeyBlock *active_keyblock, const std::optional< Span< bool > > keys_to_process, const int vertex_count, float **per_keyblock_weights, float *target_data)
Definition key.cc:681
void * BLI_findstring(const ListBase *listbase, const char *id, int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:615
static void shapekey_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition key.cc:116
float * BKE_key_evaluate_object_ex(Object *ob, int *r_totelem, float *arr, size_t arr_size, std::optional< Span< bool > > keys_to_process, ID *obdata)
Definition key.cc:1009
static void shapekey_foreach_id(ID *id, LibraryForeachIDData *data)
Definition key.cc:98
KeyBlock * BKE_keyblock_from_object_reference(Object *ob)
Definition key.cc:1414
KeyBlock * BKE_keyblock_find_name(Key *key, const char name[])
Definition key.cc:1434
static ID ** shapekey_owner_pointer_get(ID *id, const bool debug_relationship_assert)
Definition key.cc:104
MatBase< float, 4, 4 > float4x4
void key_curve_normal_weights(float t, float data[4], KeyInterpolationType type)
Definition key.cc:393
void BKE_keyblock_rename(Main &bmain, Key *key, KeyBlock *kb, const char *newname)
Definition key.cc:1916
static char * key_block_get_data(Key *key, KeyBlock *actkb, KeyBlock *kb, char **freedata)
Definition key.cc:531
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
void MEM_delete_void(void *vmemh)
std::optional< Array< bool > > BKE_keyblock_get_dependent_keys(const Key *key, int index)
Definition key.cc:1875
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
void BKE_animdata_fix_paths(ID &id, StringRef prefix, StringRef old_infix, StringRef new_infix, bool verify_paths, const DriverMap &driver_map)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
KeyBlock * BKE_keyblock_find_by_index(Key *key, int index)
Definition key.cc:1425
void BLI_uniquename(const ListBase *list, void *vlink, const char *defname, char delim, int name_offset, size_t name_maxncpy) ATTR_NONNULL(1
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
Key * BKE_key_from_id(ID *id)
Definition key.cc:1291
KeyBlock * BKE_keyblock_find_uid(Key *key, int uid)
Definition key.cc:1439
void * MEM_dupalloc_void(const void *vmemh) ATTR_WARN_UNUSED_RESULT
void BKE_keyblock_mesh_calc_normals(const KeyBlock *kb, Mesh *mesh, float(*r_vert_normals)[3], float(*r_face_normals)[3], float(*r_loop_normals)[3])
Definition key.cc:1702
Key * BKE_key_add(Main *bmain, ID *id)
Definition key.cc:228
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
BMesh const char void * data
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
void outside_lattice(Lattice *lt)
Definition lattice.cc:415
Key ** BKE_key_from_id_p(ID *id)
Definition key.cc:1266
std::optional< std::string > BKE_keyblock_curval_rnapath_get(const Key *key, const KeyBlock *kb)
Definition key.cc:1460
unsigned char uint8_t
Definition BKE_text.h:79
static void do_latt_key(Object *ob, Key *key, const std::optional< Span< bool > > keys_to_process, char *out, const int tot)
Definition key.cc:966
void BLI_listbase_swaplinks(ListBase *listbase, void *vlinka, void *vlinkb) ATTR_NONNULL(1
Definition listbase.cc:171
static bool get_keys_for_absolute_eval(float eval_time, const ListBaseT< KeyBlock > *keyblocks, KeyBlock *r_target_keys[4], float r_weights[4])
Definition key.cc:439
int BKE_object_defgroup_name_index(const Object *ob, StringRef name)
Definition deform.cc:597
static void keyblock_data_convert_to_lattice(const float(*fp)[3], BPoint *bpoint, const int totpoint)
Definition key.cc:1508
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
size_t BKE_keyblock_element_calc_size(const Key *key)
Definition key.cc:1164
@ KEY_CATMULL_ROM
void BLO_read_array_and_validate_size(BlendDataReader *reader, T **ptr_p, SizeT *array_size, const int elems=1)
void BKE_keyblock_data_get(const Key *key, MutableSpan< float3 > arr)
Definition key.cc:1191
static void keyblock_free_per_block_weights(Key *key, float **per_keyblock_weights, WeightsArrayCache *cache)
Definition key.cc:880
void BKE_keyblock_convert_from_lattice(const Lattice *lt, KeyBlock *kb)
Definition key.cc:1493
static void copy_key_float3(const int vertex_count, Key *key, KeyBlock *active_keyblock, KeyBlock *source, float *r_target)
Definition key.cc:595
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_keyblock_data_set(Key *key, int shape_index, const void *data)
Definition key.cc:1239
@ INDEX_ID_KE
Definition DNA_ID.h:1323
int BKE_keyblock_curve_element_count(const ListBaseT< Nurb > *nurb)
Definition key.cc:1528
VecBase< int16_t, 2 > short2
static void shapekey_blend_read_after_liblink(BlendLibReader *, ID *id)
Definition key.cc:165
KeyBlock * BKE_keyblock_add(Key *key, const char *name)
Definition key.cc:1322
float * BKE_key_evaluate_object(Object *ob, int *r_totelem)
Definition key.cc:1137
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static void copy_key_float3_weighted(const int vertex_count, Key *key, KeyBlock *active_keyblock, KeyBlock *source, const float *weights, float *r_target)
Definition key.cc:633
static void shapekey_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int)
Definition key.cc:64
@ ID_TAG_EXTERN
Definition DNA_ID.h:845
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
void BKE_keyblock_copy_settings(KeyBlock *kb_dst, const KeyBlock *kb_src)
copy shape-key attributes, but not key data or name/UID.
Definition key.cc:1449
static void shapekey_free_data(ID *id)
Definition key.cc:87
static void add_weighted_vector(const int start_index, const float weight, const float *a, const float *b, float *r_target)
Definition key.cc:569
int BKE_keyblock_element_count(const Key *key)
Definition key.cc:1154
KeyBlock * BKE_keyblock_add_ctime(Key *key, const char *name, bool do_force)
Definition key.cc:1380
void key_curve_position_weights(float t, float data[4], KeyInterpolationType type)
Definition key.cc:310
void BKE_key_free_nolib(Key *key)
Definition key.cc:218
size_t BKE_keyblock_element_calc_size_from_shape(const Key *key, int shape_index)
Definition key.cc:1159
@ IDWALK_CB_LOOPBACK
VecBase< float, 3 > float3
Key * BKE_key_from_object(Object *ob)
Definition key.cc:1311
void BKE_keyblock_convert_to_lattice(const KeyBlock *kb, Lattice *lt)
Definition key.cc:1517
static void do_mesh_key(Object *ob, Key *key, const std::optional< Span< bool > > keys_to_process, char *out, const int tot)
Definition key.cc:906
void BKE_keyblock_convert_from_mesh(const Mesh *mesh, const Key *key, KeyBlock *kb)
Definition key.cc:1681
MINLINE int min_ii(int a, int b)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
void BKE_keyblock_update_from_curve(const Curve *cu, KeyBlock *kb, const ListBaseT< Nurb > *nurb)
Definition key.cc:1547
MINLINE int compare_ff(float a, float b, float max_diff)
void BKE_keyblock_convert_from_curve(const Curve *cu, KeyBlock *kb, const ListBaseT< Nurb > *nurb)
Definition key.cc:1615
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE float clamp_f(float value, float min, float max)
static void key_evaluate_absolute(const int vertex_count, Key *key, KeyBlock *active_keyblock, KeyBlock *shapekeys[4], const float weights[4], float *r_target)
Definition key.cc:742
static void shapekey_blend_read_data(BlendDataReader *reader, ID *id)
Definition key.cc:148
IDTypeInfo IDType_ID_KE
Definition key.cc:173
@ IDTYPE_FLAGS_NO_LIBLINKING
Definition BKE_idtype.hh:38
void key_curve_tangent_weights(float t, float data[4], KeyInterpolationType type)
Definition key.cc:353
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
bool is_undo() const
void write_raw(size_t size_in_bytes, const void *data)
ListBaseT< Nurb > nurb
struct Key * key
char name[258]
Definition DNA_ID.h:433
struct KeyBlock * prev
KeyInterpolationType type
struct KeyBlock * next
KeyBlockFlag flag
KeyBlock * refkey
char elemstr[32]
ListBaseT< KeyBlock > block
ShapekeyContainerType type
struct MDeformVert * dvert
struct BPoint * def
BezTriple * bezt
struct Nurb * next
eObject_ShapeFlag shapeflag
float ** defgroup_weights
Definition key.cc:215
#define N_(msgid)