Blender V5.3
transform_convert.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 "DNA_anim_types.h"
11#include "DNA_mesh_types.h"
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_array_utils.hh"
16#include "BLI_function_ref.hh"
17#include "BLI_kdtree.hh"
18#include "BLI_linklist_stack.hh"
19#include "BLI_listbase.hh"
20#include "BLI_math_matrix_c.hh"
21#include "BLI_math_vector_c.hh"
22
23#include "BKE_action.hh"
24#include "BKE_anim_data.hh"
25#include "BKE_constraint.h"
26#include "BKE_context.hh"
27#include "BKE_global.hh"
28#include "BKE_layer.hh"
29#include "BKE_lib_id.hh"
30#include "BKE_modifier.hh"
31#include "BKE_nla.hh"
32#include "BKE_object_types.hh"
33#include "BKE_scene.hh"
34
35#include "ED_particle.hh"
36#include "ED_screen.hh"
37#include "ED_screen_types.hh"
38
39#include "ANIM_keyframing.hh"
40#include "ANIM_nla.hh"
41
42#include "SEQ_retiming.hh"
43
44#include "UI_view2d.hh"
45
46#include "WM_types.hh"
47
49
50#include "transform.hh"
51
52/* Own include. */
53#include "transform_convert.hh"
54
55namespace blender::ed::transform {
56
61
63{
64 if (data_len_all != 1) {
65 return;
66 }
68 return;
69 }
71 return;
72 }
73 if (t->flag & T_OVERRIDE_CENTER) {
74 return;
75 }
76
78}
79
84
85/* -------------------------------------------------------------------- */
88
93static void make_sorted_index_map(TransDataContainer *tc, FunctionRef<bool(int, int)> compare)
94{
95 BLI_assert(tc->sorted_index_map == nullptr);
97
98 const MutableSpan sorted_index_span(tc->sorted_index_map, tc->data_len);
99 array_utils::fill_index_range(sorted_index_span);
100 std::ranges::sort(sorted_index_span, compare);
101}
102
109{
110 const bool use_dist = (t->flag & T_PROP_CONNECTED);
111 const auto compare = [&](const int a, const int b) {
112 /* If both selected, then they are equivalent. To keep memory access sequential (and thus more
113 * predictable for pre-caching) when iterating the arrays, keep them sorted by array index. */
114 const bool is_selected_a = tc->data[a].flag & TD_SELECTED;
115 const bool is_selected_b = tc->data[b].flag & TD_SELECTED;
116 if (is_selected_a && is_selected_b) {
117 return a < b;
118 }
119
120 /* Selected comes before unselected. */
121 if (is_selected_a) {
122 return true;
123 }
124 if (is_selected_b) {
125 return false;
126 }
127
128 /* If both are unselected, only then the distance matters. */
129 if (use_dist) {
130 return tc->data[a].dist < tc->data[b].dist;
131 }
132 return tc->data[a].rdist < tc->data[b].rdist;
133 };
134
135 /* The "sort by distance" is often preceded by "calculate distance", which is
136 * often preceded by "sort selected first". */
137 MEM_SAFE_DELETE(tc->sorted_index_map);
138
139 make_sorted_index_map(tc, compare);
140}
147
153{
154 BLI_assert_msg(tc->sorted_index_map == nullptr,
155 "Expected sorting by selection state to only happen once");
156
157 const auto compare = [&](const int a, const int b) {
158 /* If the selection state is the same, they are equivalent. To keep memory
159 * access sequential (and thus more predictable for pre-caching) when
160 * iterating the arrays, keep them sorted by array index. */
161 const bool is_selected_a = tc->data[a].flag & TD_SELECTED;
162 const bool is_selected_b = tc->data[b].flag & TD_SELECTED;
163 if (is_selected_a == is_selected_b) {
164 return a < b;
165 }
166
167 /* If A is selected, a comes before b, so return true.
168 * If B is selected, a comes after b, so return false. */
169 return is_selected_a;
170 };
171 make_sorted_index_map(tc, compare);
172}
179
181 const TransData *td,
182 const bool use_island,
183 const float proj_vec[3])
184{
185 float3 vec;
186
187 if (use_island) {
188 if (tc->use_local_mat) {
189 mul_v3_m4v3(vec, tc->mat, td->iloc);
190 }
191 else {
192 mul_v3_m3v3(vec, td->mtx, td->iloc);
193 }
194 }
195 else {
196 if (tc->use_local_mat) {
197 mul_v3_m4v3(vec, tc->mat, td->center);
198 }
199 else {
200 mul_v3_m3v3(vec, td->mtx, td->center);
201 }
202 }
203
204 if (proj_vec) {
205 float vec_p[3];
206 project_v3_v3v3(vec_p, vec, proj_vec);
207 sub_v3_v3(vec, vec_p);
208 }
209
210 return vec;
211}
212
219static void set_prop_dist(TransInfo *t, const bool with_dist)
220{
221 float _proj_vec[3];
222 const float *proj_vec = nullptr;
223
224 /* Support for face-islands. */
225 const bool use_island = transdata_check_local_islands(t, t->around);
226
227 if (t->flag & T_PROP_PROJECTED) {
228 if (t->spacetype == SPACE_VIEW3D && t->region && t->region->regiontype == RGN_TYPE_WINDOW) {
229 RegionView3D *rv3d = static_cast<RegionView3D *>(t->region->regiondata);
230 normalize_v3_v3(_proj_vec, rv3d->viewinv[2]);
231 proj_vec = _proj_vec;
232 }
233 }
234
235 /* Count number of selected. */
236 int td_table_len = 0;
238 tc->foreach_index_selected([&](const int /*i*/) { td_table_len++; });
239 }
240
241 /* Pointers to selected's #TransData.
242 * Used to find #TransData from the index returned by #blender::kdtree_find_nearest. */
243 TransData **td_table = MEM_new_array_uninitialized<TransData *>(td_table_len, __func__);
244
245 /* Create and fill KD-tree of selected's positions - in global or proj_vec space. */
246 KDTree<float3> *td_tree = kdtree_new<float3>(td_table_len);
247
248 int td_table_index = 0;
250 tc->foreach_index_selected([&](const int i) {
251 TransData *td = &tc->data[i];
252 /* Initialize, it was malloced. */
253 td->rdist = 0.0f;
254
255 const float3 vec = prop_dist_loc_get(tc, td, use_island, proj_vec);
256
257 kdtree_insert<float3>(td_tree, td_table_index, vec);
258 td_table[td_table_index++] = td;
259 });
260 }
261 BLI_assert(td_table_index == td_table_len);
262
263 kdtree_balance<float3>(td_tree);
264
265 /* For each non-selected vertex, find distance to the nearest selected vertex. */
267 tc->foreach_index([&](const int i) {
268 TransData *td = &tc->data[i];
269 if (td->flag & TD_SELECTED) {
270 return true;
271 }
272
273 const float3 vec = prop_dist_loc_get(tc, td, use_island, proj_vec);
274
275 KDTreeNearest<float3> nearest;
276 const int td_index = kdtree_find_nearest<float3>(td_tree, vec, &nearest);
277
278 td->rdist = -1.0f;
279 if (td_index != -1) {
280 td->rdist = nearest.dist;
281 if (use_island) {
282 /* Use center and axismtx of closest point found. */
283 copy_v3_v3(td->center, td_table[td_index]->center);
284 copy_m3_m3(td->axismtx, td_table[td_index]->axismtx);
285 }
286 }
287
288 if (with_dist) {
289 td->dist = td->rdist;
290 }
291 return true;
292 });
293 }
294
295 kdtree_free<float3>(td_tree);
296 MEM_delete(td_table);
297}
298
300
301/* -------------------------------------------------------------------- */
304
306static bool pchan_autoik_adjust(bPoseChannel *pchan, short chainlen)
307{
308 bool changed = false;
309
310 /* Don't bother to search if no valid constraints. */
311 if ((pchan->constflag & (PCHAN_HAS_IK | PCHAN_HAS_NO_TARGET)) == 0) {
312 return changed;
313 }
314
315 /* Check if pchan has ik-constraint. */
316 for (bConstraint &con : pchan->constraints) {
317 if (con.type != CONSTRAINT_TYPE_KINEMATIC) {
318 continue;
319 }
320 if (!BKE_constraint_has_influence(&con)) {
321 continue;
322 }
323
324 bKinematicConstraint *data = static_cast<bKinematicConstraint *>(con.data);
325
326 /* Only accept if a temporary one (for auto-IK). */
327 if (data->flag & CONSTRAINT_IK_TEMP) {
328 /* `chainlen` is new `chainlen`, but is limited by maximum `chainlen`. */
329 const int old_rootbone = data->rootbone;
330 if ((chainlen == 0) || (chainlen > data->max_rootbone)) {
331 data->rootbone = data->max_rootbone;
332 }
333 else {
334 data->rootbone = chainlen;
335 }
336 changed |= (data->rootbone != old_rootbone);
337 }
338 }
339
340 return changed;
341}
342
344{
345 Main *bmain = CTX_data_main(t->context);
346
347 short *chainlen = &t->settings->autoik_chainlen;
348
349 /* `mode` determines what change to apply to `chainlen`. */
350 if (mode == 1) {
351 /* `mode==1` is from WHEELMOUSEDOWN: increases len. */
352 (*chainlen)++;
353 }
354 else if (mode == -1) {
355 /* `mode==-1` is from WHEELMOUSEUP: decreases len. */
356 if (*chainlen > 0) {
357 (*chainlen)--;
358 }
359 else {
360 /* IK length did not change, skip updates. */
361 return;
362 }
363 }
364
365 /* Apply to all pose-channels. */
366 bool changed = false;
367
369
370 /* Sanity checks (don't assume `t->poseobj` is set, or that it is an armature). */
371 if (ELEM(nullptr, tc->poseobj, tc->poseobj->pose)) {
372 continue;
373 }
374
375 for (bPoseChannel &pchan : tc->poseobj->pose->chanbase) {
376 changed |= pchan_autoik_adjust(&pchan, *chainlen);
377 }
378 }
379
380 if (changed) {
381 /* TODO(sergey): Consider doing partial update only. */
383 }
384}
385
387
388/* -------------------------------------------------------------------- */
391
392void calc_distanceCurveVerts(TransData *head, TransData *tail, bool cyclic)
393{
394 TransData *td;
396 BLI_LINKSTACK_INIT(queue);
397 for (td = head; td <= tail; td++) {
398 if (td->flag & TD_SELECTED) {
399 td->dist = 0.0f;
400 BLI_LINKSTACK_PUSH(queue, td);
401 }
402 else {
403 td->dist = FLT_MAX;
404 }
405 }
406
407 while ((td = BLI_LINKSTACK_POP(queue))) {
408 float dist;
409 float vec[3];
410
411 TransData *next_td = nullptr;
412
413 if (td + 1 <= tail) {
414 next_td = td + 1;
415 }
416 else if (cyclic) {
417 next_td = head;
418 }
419
420 if (next_td != nullptr) {
421 sub_v3_v3v3(vec, next_td->center, td->center);
422 mul_m3_v3(head->mtx, vec);
423 dist = len_v3(vec) + td->dist;
424
425 if (dist < next_td->dist) {
426 next_td->dist = dist;
427 BLI_LINKSTACK_PUSH(queue, next_td);
428 }
429 }
430
431 next_td = nullptr;
432
433 if (td - 1 >= head) {
434 next_td = td - 1;
435 }
436 else if (cyclic) {
437 next_td = tail;
438 }
439
440 if (next_td != nullptr) {
441 sub_v3_v3v3(vec, next_td->center, td->center);
442 mul_m3_v3(head->mtx, vec);
443 dist = len_v3(vec) + td->dist;
444
445 if (dist < next_td->dist) {
446 next_td->dist = dist;
447 BLI_LINKSTACK_PUSH(queue, next_td);
448 }
449 }
450 }
451 BLI_LINKSTACK_FREE(queue);
452}
453
455{
457 td->flag |= TD_BEZTRIPLE;
458 hdata = td->hdata = MEM_new_uninitialized<TransDataCurveHandleFlags>("CuHandle Data");
459 hdata->ih1 = bezt->h1;
460 hdata->h1 = &bezt->h1;
461 hdata->ih2 = bezt->h2; /* In case the second is not selected. */
462 hdata->h2 = &bezt->h2;
463 return hdata;
464}
465
467
468/* -------------------------------------------------------------------- */
471
473{
474 /* NOTE(@ideasman42): Often used to clip UV's after proportional editing:
475 * In this case the radius of the proportional region can end outside the clipping area,
476 * while not ideal an elegant solution here would likely be computationally expensive
477 * as it would need to calculate the transform value that would meet the UV bounds.
478 * While it would be technically correct to handle this properly,
479 * there isn't a strong use case for it. */
480
482 TransData *td = tc->data;
483 for (int a = 0; a < tc->data_len; a++, td++) {
484 if ((td->flag & TD_SKIP) || (!td->loc)) {
485 continue;
486 }
487
488 td->loc[0] = min_ff(max_ff(0.0f, td->loc[0]), t->aspect[0]);
489 td->loc[1] = min_ff(max_ff(0.0f, td->loc[1]), t->aspect[1]);
490 }
491 }
492}
493
495
496/* -------------------------------------------------------------------- */
499
501{
502 char dir;
503 float center[2];
504 if (t->flag & T_MODAL) {
505 ui::view2d_region_to_view(static_cast<View2D *>(t->view),
506 t->mouse.imval[0],
507 t->mouse.imval[1],
508 &center[0],
509 &center[1]);
510 dir = (center[0] > cframe) ? 'R' : 'L';
511 {
512 /* XXX: This saves the direction in the "mirror" property to be used for redo! */
513 if (dir == 'R') {
514 t->flag |= T_NO_MIRROR;
515 }
516 }
517 }
518 else {
519 dir = (t->flag & T_NO_MIRROR) ? 'R' : 'L';
520 }
521
522 return dir;
523}
524
525bool FrameOnMouseSide(char side, float frame, float cframe)
526{
527 /* Both sides, so it doesn't matter. */
528 if (side == 'B') {
529 return true;
530 }
531
532 /* Only on the named side. */
533 if (side == 'R') {
534 return (frame >= cframe);
535 }
536 return (frame <= cframe);
537}
538
540
541/* -------------------------------------------------------------------- */
544
545bool constraints_list_needinv(TransInfo *t, ListBaseT<bConstraint> *list)
546{
547 /* Loop through constraints, checking if there's one of the mentioned
548 * constraints needing special crazy-space corrections. */
549 if (list) {
550 for (bConstraint &con : *list) {
551 /* Only consider constraint if it is enabled, and has influence on result. */
552 if (!BKE_constraint_has_influence(&con)) {
553 continue;
554 }
555
556 /* Affirmative: returns for specific constraints here. */
557 /* Constraints that require this regardless. */
558 if (ELEM(con.type,
564 {
565 return true;
566 }
567
568 /* Constraints that require this only under special conditions. */
569 if (con.type == CONSTRAINT_TYPE_CHILDOF) {
570 /* ChildOf constraint only works when using all location components, see #42256. */
571 bChildOfConstraint *data = static_cast<bChildOfConstraint *>(con.data);
572
573 if ((data->flag & CHILDOF_LOCX) && (data->flag & CHILDOF_LOCY) &&
574 (data->flag & CHILDOF_LOCZ))
575 {
576 return true;
577 }
578 }
579 else if (con.type == CONSTRAINT_TYPE_ROTLIKE) {
580 /* CopyRot constraint only does this when rotating, and offset is on. */
581 bRotateLikeConstraint *data = static_cast<bRotateLikeConstraint *>(con.data);
582
585 {
586 return true;
587 }
588 }
589 else if (con.type == CONSTRAINT_TYPE_TRANSLIKE) {
590 /* Copy Transforms constraint only does this in the Before mode. */
591 bTransLikeConstraint *data = static_cast<bTransLikeConstraint *>(con.data);
592
595 {
596 return true;
597 }
598 if (ELEM(data->mix_mode, TRANSLIKE_MIX_BEFORE_SPLIT) && ELEM(t->mode, TFM_ROTATION)) {
599 return true;
600 }
601 }
602 else if (con.type == CONSTRAINT_TYPE_ACTION) {
603 /* The Action constraint only does this in the Before mode. */
604 bActionConstraint *data = static_cast<bActionConstraint *>(con.data);
605
608 {
609 return true;
610 }
611 if (ELEM(data->mix_mode, ACTCON_MIX_BEFORE_SPLIT) && ELEM(t->mode, TFM_ROTATION)) {
612 return true;
613 }
614 }
615 else if (con.type == CONSTRAINT_TYPE_TRANSFORM) {
616 /* Transform constraint needs it for rotation at least (r.57309),
617 * but doing so when translating may also mess things up, see: #36203. */
618 bTransformConstraint *data = static_cast<bTransformConstraint *>(con.data);
619
620 if (data->to == TRANS_ROTATION) {
621 if (t->mode == TFM_ROTATION && data->mix_mode_rot == TRANS_MIXROT_BEFORE) {
622 return true;
623 }
624 }
625 }
626 }
627 }
628
629 /* No appropriate candidates found. */
630 return false;
631}
632
634
635/* -------------------------------------------------------------------- */
638
640{
641 /* NOTE: Sequencer freeing has its own function now because of a conflict
642 * with transform's order of freeing (campbell).
643 * Order changed, the sequencer stuff should go back in here. */
644
645 /* Early out when nothing happened. */
646 if (t->data_len_all == 0 || t->mode == TFM_DUMMY) {
647 return;
648 }
649
651 return;
652 }
653
656}
657
659{
660 if (t->options & CTX_CURSOR) {
661 return G_TRANSFORM_CURSOR;
662 }
663 if (t->spacetype == SPACE_SEQ) {
664 return G_TRANSFORM_SEQ;
665 }
666 if (t->spacetype == SPACE_GRAPH) {
667 return G_TRANSFORM_FCURVES;
668 }
669 if ((t->flag & T_EDIT) || (t->options & CTX_POSE_BONE)) {
670 return G_TRANSFORM_EDIT;
671 }
672 if (t->options & (CTX_OBJECT | CTX_TEXTURE_SPACE)) {
673 return G_TRANSFORM_OBJ;
674 }
675
676 return 0;
677}
678
680
681/* -------------------------------------------------------------------- */
684
686{
687 BLI_assert(ELEM(t->data_len_all, 0, -1));
688 t->data_len_all = 0;
689 int data_container_len_orig = t->data_container_len;
690 for (TransDataContainer *th_end = t->data_container - 1,
691 *tc = &t->data_container[t->data_container_len - 1];
692 tc != th_end;
693 tc--)
694 {
695 if (tc->data_len == 0) {
696 uint index = tc - t->data_container;
697 if (index + 1 != t->data_container_len) {
698 std::swap(t->data_container[index], t->data_container[t->data_container_len - 1]);
699 }
700 t->data_container_len -= 1;
701 }
702 else {
703 t->data_len_all += tc->data_len;
704 }
705 }
706 if (data_container_len_orig != t->data_container_len) {
708 t->data_container, sizeof(*t->data_container) * t->data_container_len));
709 }
710 return t->data_len_all;
711}
712
714{
715 /* NOTE: Proportional editing is not usable in pose mode yet #32444. */
716 /* NOTE: This `ELEM` uses more than 16 elements and so has been split. */
717 if (!(ELEM(t->data_type,
735 {
736 /* Disable proportional editing. */
737 t->options |= CTX_NO_PET;
739 return;
740 }
741
742 if (t->data_len_all && (t->flag & T_PROP_EDIT)) {
744 /* Selected objects are already first, no need to presort. */
745 }
746 else {
748 }
749
751 /* Distance has already been set. */
752 }
753 else if (ELEM(t->data_type,
757 {
758 if (t->flag & T_PROP_CONNECTED) {
759 /* Already calculated by #transform_convert_mesh_connectivity_distance. */
760 }
761 else {
762 set_prop_dist(t, false);
763 }
764 }
765 else if (t->data_type == &TransConvertType_MeshUV && t->flag & T_PROP_CONNECTED) {
766 /* Already calculated by #uv_set_connectivity_distance. */
767 }
768 else if (t->data_type == &TransConvertType_Curve) {
770 if (t->flag & T_PROP_CONNECTED) {
771 /* Already calculated by #calc_distanceCurveVerts. */
772 }
773 else {
774 set_prop_dist(t, false);
775 }
776 }
777 else if (ELEM(t->data_type,
780 {
782 if (t->flag & T_PROP_CONNECTED) {
783 /* Already calculated by #curve_connected_point_distances /
784 * #cyclic_curve_connected_point_distances. */
785 }
786 else {
787 set_prop_dist(t, false);
788 }
789 }
790 else if (t->data_type == &TransConvertType_Mask) {
791 if (t->flag & T_PROP_CONNECTED) {
792 /* Already calculated by #calc_distanceCurveVerts in #createTransMaskingData. */
793 }
794 else {
795 set_prop_dist(t, false);
796 }
797 }
798 else {
799 set_prop_dist(t, true);
800 }
801
803 }
804 else if (ELEM(t->obedit_type, OB_CURVES_LEGACY)) {
805 /* Needed because bezier handles can be partially selected
806 * and are still added into transform data. */
808 }
809}
810
811/* For multi object editing. */
813{
814 if (!ELEM(t->data_type,
828 {
829 /* Does not support Multi object editing. */
830 return;
831 }
832
833 const eObjectMode object_mode = obact ? obact->mode : OB_MODE_OBJECT;
834 const short object_type = obact ? obact->type : -1;
835
836 if ((object_mode & OB_MODE_EDIT) ||
838 ((object_mode & OB_MODE_POSE) && (object_type == OB_ARMATURE)))
839 {
840 if (t->data_container) {
841 MEM_delete(t->data_container);
842 }
843
844 Vector<Object *> local_objects;
845 if (objects.is_empty()) {
846 ObjectsInModeParams params = {0};
847 params.object_mode = object_mode;
848 /* Pose transform operates on `ob->pose` so don't skip duplicate object-data. */
849 params.no_dup_data = (object_mode & OB_MODE_POSE) == 0;
851 *t->bmain,
852 t->scene,
853 t->view_layer,
854 static_cast<const View3D *>((t->spacetype == SPACE_VIEW3D) ? t->view : nullptr),
855 &params);
856 objects = local_objects;
857 }
858
860 t->data_container_len = objects.size();
861
862 for (int i = 0; i < objects.size(); i++) {
864 if (!(t->flag & T_NO_MIRROR) && (objects[i]->type == OB_MESH)) {
865 tc->use_mirror_axis_x = ((id_cast<Mesh *>(objects[i]->data))->symmetry & ME_SYMMETRY_X) !=
866 0;
867 tc->use_mirror_axis_y = ((id_cast<Mesh *>(objects[i]->data))->symmetry & ME_SYMMETRY_Y) !=
868 0;
869 tc->use_mirror_axis_z = ((id_cast<Mesh *>(objects[i]->data))->symmetry & ME_SYMMETRY_Z) !=
870 0;
871 }
872
873 if (object_mode & OB_MODE_EDIT || object_mode & OB_MODE_PAINT_GREASE_PENCIL) {
874 tc->obedit = objects[i];
875 /* Check needed for UVs. */
876 if ((t->flag & T_2D_EDIT) == 0) {
877 tc->use_local_mat = true;
878 }
879 }
880 else if (object_mode & OB_MODE_POSE) {
881 tc->poseobj = objects[i];
882 tc->use_local_mat = true;
883 }
885 tc->use_local_mat = true;
886 }
887
888 if (tc->use_local_mat) {
889 BLI_assert((t->flag & T_2D_EDIT) == 0);
890 copy_m4_m4(tc->mat, objects[i]->object_to_world().ptr());
891 copy_m3_m4(tc->mat3, tc->mat);
892 /* For non-invertible scale matrices, #invert_m4_m4_fallback()
893 * can still provide a valid pivot. */
895 invert_m3_m3(tc->imat3, tc->mat3);
897 }
898 /* Otherwise leave as zero. */
899 }
900 }
901}
902
903static TransConvertTypeInfo *convert_type_get(const TransInfo *t, Object **r_obj_armature)
904{
905 ViewLayer *view_layer = t->view_layer;
908
909 /* If tests must match recalc_data for correct updates. */
910 if (t->options & CTX_CURSOR) {
911 if (t->spacetype == SPACE_IMAGE) {
913 }
914
915 if (t->spacetype == SPACE_SEQ) {
917 }
918
920 }
921 if (!(t->options & CTX_PAINT_CURVE) && (t->spacetype == SPACE_VIEW3D) && ob &&
922 (ob->mode == OB_MODE_SCULPT) && ob->runtime->sculpt_session)
923 {
925 }
926 if (t->options & CTX_TEXTURE_SPACE) {
928 }
929 if (t->options & CTX_EDGE_DATA) {
931 }
932 if (t->options & CTX_GPENCIL_STROKES) {
933 if (t->obedit_type == OB_GREASE_PENCIL) {
935 }
936 return nullptr;
937 }
938 if (t->spacetype == SPACE_IMAGE) {
939 if (t->options & CTX_MASK) {
940 return &TransConvertType_Mask;
941 }
942 if (t->options & CTX_PAINT_CURVE) {
943 if (!ELEM(t->mode, TFM_SHEAR, TFM_SHRINKFATTEN)) {
945 }
946 }
947 else if (t->obedit_type == OB_MESH) {
949 }
950 return nullptr;
951 }
952 if (t->spacetype == SPACE_ACTION) {
954 }
955 if (t->spacetype == SPACE_NLA) {
956 return &TransConvertType_NLA;
957 }
958 if (t->spacetype == SPACE_SEQ) {
959 if (t->options & CTX_SEQUENCER_IMAGE) {
961 }
964 }
966 }
967 if (t->spacetype == SPACE_GRAPH) {
969 }
970 if (t->spacetype == SPACE_NODE) {
971 return &TransConvertType_Node;
972 }
973 if (t->spacetype == SPACE_CLIP) {
974 if (t->options & CTX_MOVIECLIP) {
975 if (t->region && (t->region->regiontype == RGN_TYPE_PREVIEW)) {
977 }
979 }
980 if (t->options & CTX_MASK) {
981 return &TransConvertType_Mask;
982 }
983 return nullptr;
984 }
985 if (t->obedit_type != -1) {
986 if (t->obedit_type == OB_MESH) {
987 if (t->mode == TFM_SKIN_RESIZE) {
989 }
992 }
993 return &TransConvertType_Mesh;
994 }
997 }
998 if (t->obedit_type == OB_LATTICE) {
1000 }
1001 if (t->obedit_type == OB_MBALL) {
1002 return &TransConvertType_MBall;
1003 }
1004 if (t->obedit_type == OB_ARMATURE) {
1006 }
1007 if (t->obedit_type == OB_CURVES) {
1009 }
1010 if (t->obedit_type == OB_POINTCLOUD) {
1012 }
1013 return nullptr;
1014 }
1015 if (ob && (ob->mode & OB_MODE_POSE)) {
1016 return &TransConvertType_Pose;
1017 }
1018 if (ob && (ob->mode & OB_MODE_ALL_WEIGHT_PAINT) && !(t->options & CTX_PAINT_CURVE)) {
1020 if (ob_armature) {
1021 *r_obj_armature = ob_armature;
1022 return &TransConvertType_Pose;
1023 }
1024 return nullptr;
1025 }
1026 if (ob && (ob->mode & OB_MODE_PARTICLE_EDIT) &&
1028 {
1030 }
1031 if (ob && ((ob->mode & OB_MODE_ALL_PAINT_MESH) || (ob->mode & OB_MODE_SCULPT_CURVES))) {
1034 }
1035 return nullptr;
1036 }
1037 if (ob && ELEM(ob->mode,
1041 {
1042 /* Draw and Edit mode handle transformations via #TransConverType_GreasePencil, avoid
1043 * transformations otherwise. */
1044 return nullptr;
1045 }
1047}
1048
1050{
1051 t->data_len_all = -1;
1052
1053 Object *ob_armature = nullptr;
1054 t->data_type = convert_type_get(t, &ob_armature);
1055 if (t->data_type == nullptr) {
1056 printf("edit type not implemented!\n");
1057 BLI_assert(t->data_len_all == -1);
1058 t->data_len_all = 0;
1059 return;
1060 }
1061
1062 t->flag |= eTFlag(t->data_type->flags);
1063
1064 if (ob_armature) {
1065 init_TransDataContainers(t, ob_armature, {ob_armature});
1066 }
1067 else {
1070 init_TransDataContainers(t, ob, {});
1071 }
1072
1074 t->options |= CTX_OBJECT;
1075
1076 /* Needed for correct Object.obmat after duplication, see: #62135. */
1078
1081 }
1084 }
1086 /* Check if we're transforming the camera from the camera. */
1087 if ((t->spacetype == SPACE_VIEW3D) && (t->region->regiontype == RGN_TYPE_WINDOW)) {
1088 View3D *v3d = static_cast<View3D *>(t->view);
1089 RegionView3D *rv3d = static_cast<RegionView3D *>(t->region->regiondata);
1090 if ((rv3d->persp == RV3D_CAMOB) && v3d->camera) {
1091 /* We could have a flag to easily check an object is being transformed. */
1092 if (v3d->camera->id.tag & ID_TAG_DOIT) {
1093 t->options |= CTX_CAMERA;
1094 }
1095 }
1096 else if (v3d->ob_center && v3d->ob_center->id.tag & ID_TAG_DOIT) {
1097 t->options |= CTX_CAMERA;
1098 }
1099 }
1100 }
1101 else {
1102 if (t->data_type == &TransConvertType_Pose) {
1103 t->options |= CTX_POSE_BONE;
1104 }
1105 else if (t->data_type == &TransConvertType_Sequencer) {
1106 /* Sequencer has no use for floating point transform. */
1107 t->num.flag |= NUM_NO_FRACTION;
1108 }
1110 t->obedit_type = -1;
1111 }
1112 else if (t->data_type == &TransConvertType_NLA) {
1113 t->obedit_type = -1;
1114 }
1116 }
1117
1119
1121}
1122
1124
1125/* -------------------------------------------------------------------- */
1128
1130{
1131 Object *ob = tc->obedit;
1132 ModifierData *md = ob->modifiers.first();
1133 tc->has_mirror_clipping = false;
1134
1135 for (; md; md = md->next) {
1136 if ((md->type == eModifierType_Mirror) && (md->mode & eModifierMode_Realtime)) {
1137 MirrorModifierData *mmd = reinterpret_cast<MirrorModifierData *>(md);
1138
1139 if ((mmd->flag & MOD_MIR_CLIPPING) == 0) {
1140 continue;
1141 }
1142
1143 if ((mmd->flag & (MOD_MIR_AXIS_X | MOD_MIR_AXIS_Y | MOD_MIR_AXIS_Z)) == 0) {
1144 continue;
1145 }
1146
1147 float mtx[4][4], imtx[4][4];
1148
1149 if (mmd->mirror_ob) {
1150 float obinv[4][4];
1151
1152 invert_m4_m4(obinv, mmd->mirror_ob->object_to_world().ptr());
1153 mul_m4_m4m4(mtx, obinv, ob->object_to_world().ptr());
1154 invert_m4_m4(imtx, mtx);
1155 }
1156
1157 TransData *td = tc->data;
1158 for (int i = 0; i < tc->data_len; i++, td++) {
1159 float loc[3], iloc[3];
1160
1161 if (td->loc == nullptr) {
1162 break;
1163 }
1164
1165 if (td->flag & TD_SKIP) {
1166 continue;
1167 }
1168
1169 copy_v3_v3(loc, td->loc);
1170 copy_v3_v3(iloc, td->iloc);
1171
1172 if (mmd->mirror_ob) {
1173 mul_m4_v3(mtx, loc);
1174 mul_m4_v3(mtx, iloc);
1175 }
1176
1177 bool is_clipping = false;
1178 if (mmd->flag & MOD_MIR_AXIS_X) {
1179 if (fabsf(iloc[0]) <= mmd->tolerance || loc[0] * iloc[0] < 0.0f) {
1180 loc[0] = 0.0f;
1181 is_clipping = true;
1182 }
1183 }
1184
1185 if (mmd->flag & MOD_MIR_AXIS_Y) {
1186 if (fabsf(iloc[1]) <= mmd->tolerance || loc[1] * iloc[1] < 0.0f) {
1187 loc[1] = 0.0f;
1188 is_clipping = true;
1189 }
1190 }
1191 if (mmd->flag & MOD_MIR_AXIS_Z) {
1192 if (fabsf(iloc[2]) <= mmd->tolerance || loc[2] * iloc[2] < 0.0f) {
1193 loc[2] = 0.0f;
1194 is_clipping = true;
1195 }
1196 }
1197
1198 if (is_clipping) {
1199 if (mmd->mirror_ob) {
1200 mul_m4_v3(imtx, loc);
1201 }
1202 copy_v3_v3(td->loc, loc);
1203 tc->has_mirror_clipping = true;
1204 }
1205 }
1206 }
1207 }
1208}
1209
1211{
1212 Scene *scene = t->scene;
1213 wmTimer *animtimer = t->animtimer;
1214 ScreenAnimData *sad = static_cast<ScreenAnimData *>((animtimer) ? animtimer->customdata :
1215 nullptr);
1216
1217 /* Sanity checks. */
1218 if (ELEM(nullptr, scene, id, sad)) {
1219 return;
1220 }
1221
1222 /* Check if we need a new strip if:
1223 * - If `animtimer` is running.
1224 * - We're not only keying for available channels.
1225 * - The option to add new actions for each round is not enabled.
1226 */
1229 {
1230 /* If playback has just looped around,
1231 * we need to add a new NLA track+strip to allow a clean pass to occur. */
1232 if ((sad) && (sad->flag & ANIMPLAY_FLAG_JUMPED)) {
1233 AnimData *adt = BKE_animdata_from_id(id);
1234 const bool is_first = (adt) && (adt->nla_tracks.first_ == nullptr);
1235
1236 /* Perform push-down manually with some differences
1237 * NOTE: #BKE_nla_action_pushdown() sync warning. */
1238 if ((adt->action) && !(adt->flag & ADT_NLA_EDIT_ON)) {
1239 /* Only push down if action is more than 1-2 frames long. */
1240 const float2 frame_range = adt->action->wrap().get_frame_range_of_keys(true);
1241 if (frame_range[1] > frame_range[0] + 2.0f) {
1242 /* TODO: call #BKE_nla_action_pushdown() instead? */
1243
1244 /* Add a new NLA strip to the track, which references the active action + slot. */
1246 BLI_assert(strip);
1248
1249 /* Clear reference to action now that we've pushed it onto the stack. */
1250 const bool unassign_ok = animrig::unassign_action(*id);
1252 unassign_ok,
1253 "Expecting un-assigning an action to always work when pushing down an NLA strip");
1254 UNUSED_VARS_NDEBUG(unassign_ok);
1255
1256 /* Adjust blending + extend so that they will behave correctly. */
1260
1261 /* Copy current "action blending" settings from adt to the strip,
1262 * as it was keyframed with these settings, so omitting them will
1263 * change the effect, see: #54766. */
1264 if (is_first == false) {
1265 strip->blendmode = adt->act_blendmode;
1266 strip->influence = adt->act_influence;
1267
1268 if (adt->act_influence < 1.0f) {
1269 /* Enable "user-controlled" influence (which will insert a default keyframe)
1270 * so that the influence doesn't get lost on the new update.
1271 *
1272 * NOTE: An alternative way would have been to instead hack the influence
1273 * to not get always get reset to full strength if NLASTRIP_FLAG_USR_INFLUENCE
1274 * is disabled but auto-blending isn't being used. However, that approach
1275 * is a bit hacky/hard to discover, and may cause backwards compatibility issues,
1276 * so it's better to just do it this way. */
1279 }
1280 }
1281
1282 /* Also, adjust the AnimData's action extend mode to be on
1283 * 'nothing' so that previous result still play. */
1285 }
1286 }
1287 }
1288 }
1289}
1290
1291void transform_convert_flush_handle2D(TransData *td, TransData2D *td2d, const float y_fac)
1292{
1293 float delta_x = td->loc[0] - td->iloc[0];
1294 float delta_y = (td->loc[1] - td->iloc[1]) * y_fac;
1295
1296 /* If the handles are to be moved too
1297 * (as side-effect of keyframes moving, to keep the general effect)
1298 * offset them by the same amount so that the general angles are maintained
1299 * (i.e. won't change while handles are free-to-roam and keyframes are snap-locked).
1300 */
1301 if ((td->flag & TD_MOVEHANDLE1) && td2d->h1) {
1302 td2d->h1[0] = td2d->ih1[0] + delta_x;
1303 td2d->h1[1] = td2d->ih1[1] + delta_y;
1304 }
1305 if ((td->flag & TD_MOVEHANDLE2) && td2d->h2) {
1306 td2d->h2[0] = td2d->ih2[0] + delta_x;
1307 td2d->h2[1] = td2d->ih2[1] + delta_y;
1308 }
1309}
1310
1312{
1313 if (!t->data_type || !t->data_type->recalc_data) {
1314 return;
1315 }
1316 t->data_type->recalc_data(t);
1317}
1318
1320
1321} // namespace blender::ed::transform
Functions to insert, delete or modify keyframes.
Blender kernel action and pose functionality.
A KD-tree for nearest neighbor search.
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define OB_MODE_ALL_PAINT_MESH
#define OB_MODE_ALL_WEIGHT_PAINT
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define MEM_realloc_uninitialized(vmemh, len)
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr bool is_empty() const
Definition BLI_span.hh:264
#define FLT_MAX
ActionSlotAssignmentResult assign_action_slot_handle(NlaStrip &strip, slot_handle_t slot_handle, ID &animated_id)
bool unassign_action(ID &animated_id)
bool is_keying_flag(const Scene *scene, eKeying_Flag flag)
void fill_index_range(MutableSpan< T > span, const T start=0)
TransConvertTypeInfo TransConvertType_Pose
TransConvertTypeInfo TransConvertType_Mesh
void animrecord_check_state(TransInfo *t, ID *id)
TransConvertTypeInfo TransConvertType_Mask
TransConvertTypeInfo TransConvertType_Sequencer
void special_aftertrans_update(bContext *C, TransInfo *t)
void recalc_data(TransInfo *t)
static void sort_trans_data_selected_first(TransInfo *t)
void clipUVData(TransInfo *t)
static void sort_trans_data_dist_container(const TransInfo *t, TransDataContainer *tc)
TransConvertTypeInfo TransConvertType_Sculpt
TransConvertTypeInfo TransConvertType_Node
bool transform_mode_use_local_origins(const TransInfo *t)
static void set_prop_dist(TransInfo *t, const bool with_dist)
TransConvertTypeInfo TransConvertType_Graph
static bool pchan_autoik_adjust(bPoseChannel *pchan, short chainlen)
char transform_convert_frame_side_dir_get(TransInfo *t, float cframe)
void transform_around_single_fallback(TransInfo *t)
TransConvertTypeInfo TransConvertType_Curve
TransConvertTypeInfo TransConvertType_CursorImage
TransConvertTypeInfo TransConvertType_MeshEdge
bool constraints_list_needinv(TransInfo *t, ListBaseT< bConstraint > *list)
static void init_TransDataContainers(TransInfo *t, Object *obact, Span< Object * > objects)
static void sort_trans_data_selected_first_container(TransDataContainer *tc)
TransConvertTypeInfo TransConvertType_Lattice
static void make_sorted_index_map(TransDataContainer *tc, FunctionRef< bool(int, int)> compare)
Object * transform_object_deform_pose_armature_get(const TransInfo *t, Object *ob)
TransConvertTypeInfo TransConvertType_Particle
TransDataCurveHandleFlags * initTransDataCurveHandles(TransData *td, BezTriple *bezt)
TransConvertTypeInfo TransConvertType_MBall
void transform_convert_clip_mirror_modifier_apply(TransDataContainer *tc)
static float3 prop_dist_loc_get(const TransDataContainer *tc, const TransData *td, const bool use_island, const float proj_vec[3])
TransConvertTypeInfo TransConvertType_Action
bool FrameOnMouseSide(char side, float frame, float cframe)
void transform_autoik_update(TransInfo *t, short mode)
TransConvertTypeInfo TransConvertType_NLA
TransConvertTypeInfo TransConvertType_MeshSkin
void sort_trans_data_dist(TransInfo *t)
static void init_proportional_edit(TransInfo *t)
static TransConvertTypeInfo * convert_type_get(const TransInfo *t, Object **r_obj_armature)
void transform_around_single_fallback_ex(TransInfo *t, int data_len_all)
void transform_convert_flush_handle2D(TransData *td, TransData2D *td2d, const float y_fac)
TransConvertTypeInfo TransConvertType_MeshUV
TransConvertTypeInfo TransConvertType_Tracking
TransConvertTypeInfo TransConvertType_Object
TransConvertTypeInfo TransConvertType_CursorSequencer
static int countAndCleanTransDataContainer(TransInfo *t)
TransConvertTypeInfo TransConvertType_PaintCurve
int special_transform_moving(TransInfo *t)
void calc_distanceCurveVerts(TransData *head, TransData *tail, bool cyclic)
TransConvertTypeInfo TransConvertType_EditArmature
void create_trans_data(bContext *C, TransInfo *t)
bool transdata_check_local_islands(TransInfo *t, short around)
TransConvertTypeInfo TransConvertType_Cursor3D
bool retiming_keys_are_selected(const Scene *scene)
void view2d_region_to_view(const View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
Definition view2d.cc:1651
MINLINE float min_ff(float a, float b)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void mul_m3_v3(const float M[3][3], float r[3])
void kdtree_balance(KDTree< CoordT > *tree)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void copy_m3_m4(float m1[3][3], const float m2[4][4])
@ SCE_XFORM_SKIP_CHILDREN
void copy_m3_m3(float m1[3][3], const float m2[3][3])
bool invert_m4_m4_fallback(float inverse[4][4], const float mat[4][4])
@ NLASTRIP_FLAG_USR_INFLUENCE
@ NLASTRIP_FLAG_SELECT
@ NLASTRIP_FLAG_ACTIVE
@ NLASTRIP_FLAG_AUTO_BLENDS
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
@ NLASTRIP_EXTEND_NOTHING
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
@ V3D_AROUND_CENTER_BOUNDS
@ V3D_AROUND_LOCAL_ORIGINS
@ V3D_AROUND_CENTER_MEDIAN
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void BKE_scene_graph_evaluated_ensure(Depsgraph *depsgraph, Main *bmain)
Definition scene.cc:2872
void BKE_nlastrip_validate_fcurves(NlaStrip *strip)
void project_v3_v3v3(float out[3], const float p[3], const float v_proj[3])
MINLINE void sub_v3_v3(float r[3], const float a[3])
KDTree< CoordT > * kdtree_new(uint nodes_len_capacity)
Definition BLI_kdtree.hh:65
void mul_m4_v3(const float M[4][4], float r[3])
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
VecBase< float, 2 > float2
PTCacheEdit * PE_get_current(Depsgraph *depsgraph, Scene *scene, Object *ob)
bool BKE_constraint_has_influence(const struct bConstraint *con)
@ OB_MODE_VERTEX_GREASE_PENCIL
@ OB_MODE_SCULPT_GREASE_PENCIL
@ OB_MODE_SCULPT_CURVES
@ OB_MODE_PARTICLE_EDIT
@ OB_MODE_WEIGHT_GREASE_PENCIL
@ OB_MODE_PAINT_GREASE_PENCIL
BMesh const char void * data
Vector< Object * > BKE_view_layer_array_from_objects_in_mode_params(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d, const ObjectsInModeParams *params)
void kdtree_insert(KDTree< CoordT > *tree, int index, const CoordT &co)
Definition BLI_kdtree.hh:96
int kdtree_find_nearest(const KDTree< CoordT > *tree, const CoordT &co, KDTreeNearest< CoordT > *r_nearest)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
void normalize_m3_m3(float R[3][3], const float M[3][3]) ATTR_NONNULL()
MINLINE float normalize_v3_v3(float r[3], const float a[3])
void kdtree_free(KDTree< CoordT > *tree)
Definition BLI_kdtree.hh:84
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
Main * CTX_data_main(const bContext *C)
MINLINE float max_ff(float a, float b)
@ ID_TAG_DOIT
Definition DNA_ID.h:1029
@ G_TRANSFORM_SEQ
@ G_TRANSFORM_OBJ
@ G_TRANSFORM_FCURVES
@ G_TRANSFORM_CURSOR
@ G_TRANSFORM_EDIT
void DEG_relations_tag_update(Main *bmain)
NlaStrip * BKE_nlastack_add_strip(OwnedAnimData owned_adt, const bool is_liboverride)
@ AUTOKEY_FLAG_INSERTAVAILABLE
@ AUTOKEY_FLAG_LAYERED_RECORD
void copy_m4_m4(float m1[4][4], const float m2[4][4])
int PE_start_edit(PTCacheEdit *edit)
VecBase< float, 3 > float3
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
@ NUM_NO_FRACTION
#define fabsf
eRegion_Type regiontype
eNlaStrip_Blend_Mode act_blendmode
eNlaStrip_Extrapolate_Mode act_extendmode
eAnimData_Flag flag
ListBaseT< NlaTrack > nla_tracks
eBezTriple_Handle h2
eBezTriple_Handle h1
struct ModifierData * next
eNlaStrip_Blend_Mode blendmode
eNlaStrip_Extrapolate_Mode extendmode
eNlaStrip_Flag flag
ListBaseT< ModifierData > modifiers
bke::ObjectRuntime * runtime
struct bPose * pose
eRegionView3D_Persp persp
struct ToolSettings * toolsettings
struct Object * ob_center
struct Object * camera
ListBaseT< struct bConstraint > constraints
ePchan_ConstFlag constflag
ListBaseT< bPoseChannel > chanbase
void(*) special_aftertrans_update(bContext *C, TransInfo *t)
void(*) create_trans_data(bContext *C, TransInfo *t)
bool foreach_index(FunctionRef< bool(int)> fn) const
Definition transform.hh:784
void foreach_index_selected(FunctionRef< void(int)> fn) const
Definition transform.hh:811
TransDataCurveHandleFlags * hdata
Definition transform.hh:539
TransConvertTypeInfo * data_type
Definition transform.hh:836
TransDataContainer * data_container
Definition transform.hh:828
#define T_PROP_EDIT_ALL
Definition transform.hh:34
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition transform.hh:48
conversion and adaptation of different datablocks to a common struct.