Blender V5.3
uvedit_ops.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 <cmath>
10#include <cstdlib>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "DNA_image_types.h"
16#include "DNA_material_types.h"
17#include "DNA_mesh_types.h"
18#include "DNA_node_types.h"
19#include "DNA_object_types.h"
20#include "DNA_scene_types.h"
21#include "DNA_space_types.h"
22
23#include "BLI_bounds.hh"
24#include "BLI_enum_flags.hh"
25#include "BLI_kdtree.hh"
26#include "BLI_math_base.hh"
27#include "BLI_math_geom_c.hh"
28#include "BLI_math_vector.hh"
29#include "BLI_math_vector_c.hh"
30
31#include "BLT_translation.hh"
32
33#include "BKE_context.hh"
34#include "BKE_customdata.hh"
35#include "BKE_editmesh.hh"
36#include "BKE_layer.hh"
38#include "BKE_material.hh"
39#include "BKE_mesh_mapping.hh"
40#include "BKE_mesh_types.hh"
41#include "BKE_node.hh"
43
44#include "DEG_depsgraph.hh"
46
47#include "ED_image.hh"
48#include "ED_mesh.hh"
49#include "ED_node.hh"
50#include "ED_screen.hh"
51#include "ED_uvedit.hh"
52
53#include "RNA_access.hh"
54#include "RNA_define.hh"
55#include "RNA_enum_types.hh"
56
57#include "WM_api.hh"
58#include "WM_message.hh"
59#include "WM_types.hh"
60
61#include "UI_interface.hh"
63#include "UI_resources.hh"
64#include "UI_view2d.hh"
65
66#include "uvedit_intern.hh"
67
68namespace blender {
69
70/* -------------------------------------------------------------------- */
73
75{
76 if (!obedit) {
77 return false;
78 }
79
80 if (obedit->type != OB_MESH) {
81 return false;
82 }
83
84 return EDBM_uv_check(id_cast<const Mesh *>(obedit->data));
85}
86
88
89/* -------------------------------------------------------------------- */
92
94{
96}
97
99 int mat_nr,
100 Image **r_ima,
101 ImageUser **r_iuser,
102 const bNode **r_node,
103 const bNodeTree **r_ntree)
104{
106 BKE_object_material_get(ob, mat_nr);
107 bNodeTree *ntree = ma ? ma->nodetree : nullptr;
108 bNode *node = (ntree) ? bke::node_get_active_texture(*ntree) : nullptr;
109
111 if (r_ima) {
112 *r_ima = id_cast<Image *>(node->id);
113 }
114 if (r_iuser) {
115 if (node->type_legacy == SH_NODE_TEX_IMAGE) {
116 *r_iuser = &(static_cast<NodeTexImage *>(node->storage))->iuser;
117 }
118 else if (node->type_legacy == SH_NODE_TEX_ENVIRONMENT) {
119 *r_iuser = &(static_cast<NodeTexEnvironment *>(node->storage))->iuser;
120 }
121 else {
122 *r_iuser = nullptr;
123 }
124 }
125 if (r_node) {
126 *r_node = node;
127 }
128 if (r_ntree) {
129 *r_ntree = ntree;
130 }
131 return true;
132 }
133
134 if (r_ima) {
135 *r_ima = nullptr;
136 }
137 if (r_iuser) {
138 *r_iuser = nullptr;
139 }
140 if (r_node) {
141 *r_node = node;
142 }
143 if (r_ntree) {
144 *r_ntree = ntree;
145 }
146
147 return false;
148}
149
150void ED_object_assign_active_image(Main *bmain, Object *ob, int mat_nr, Image *ima)
151{
152 Material *ma = BKE_object_material_get(ob, mat_nr);
153 bNode *node = ma ? bke::node_get_active_texture(*ma->nodetree) : nullptr;
154
156 node->id = &ima->id;
157 BKE_main_ensure_invariants(*bmain, ma->nodetree->id);
158 }
159}
160
162
163/* -------------------------------------------------------------------- */
166
168{
169 if (sima && (sima->flag & SI_LIVE_UNWRAP)) {
170 ED_uvedit_live_unwrap_begin(scene, obedit, nullptr);
173 }
174}
175
177
178/* -------------------------------------------------------------------- */
181
182void ED_uvedit_foreach_uv(const Scene *scene,
183 BMesh *bm,
184 const bool skip_invisible,
185 const bool selected,
186 FunctionRef<void(float[2])> user_fn)
187{
188 /* Check selection for quick return. */
189 const bool synced_selection = (scene->toolsettings->uv_flag & UV_FLAG_SELECT_SYNC) != 0;
190 if (synced_selection && bm->totvertsel == (selected ? 0 : bm->totvert)) {
191 return;
192 }
193
194 BMFace *efa;
195 BMLoop *l;
196 BMIter iter, liter;
197
198 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
199
200 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
201 if (skip_invisible && !uvedit_face_visible_test(scene, efa)) {
202 continue;
203 }
204
205 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
206 if (uvedit_uv_select_test(scene, bm, l, offsets) == selected) {
207 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
208 user_fn(luv);
209 }
210 }
211 }
212}
213
215 const Span<Object *> objects_edit,
216 const bool skip_invisible,
217 const bool skip_nonselected,
218 FunctionRef<void(float[2])> user_fn)
219{
220 for (Object *obedit : objects_edit) {
222 ED_uvedit_foreach_uv(scene, bm, skip_invisible, skip_nonselected, user_fn);
223 }
224}
225
227 const Span<Object *> objects_edit,
228 float r_min[2],
229 float r_max[2])
230{
231 bool changed = false;
232 INIT_MINMAX2(r_min, r_max);
233 ED_uvedit_foreach_uv_multi(scene, objects_edit, true, true, [&](float luv[2]) {
234 minmax_v2v2_v2(r_min, r_max, luv);
235 changed = true;
236 });
237 return changed;
238}
239
241{
242 BMFace *efa;
243 BMLoop *l;
244 BMIter iter, liter;
245
246 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
247 uvedit_face_select_set_no_sync(ts, bm, efa, true);
248 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
251 }
252 }
253}
254
255static bool uvedit_median_multi(const Scene *scene, const Span<Object *> objects_edit, float co[2])
256{
257 uint sel = 0;
258 zero_v2(co);
259
260 ED_uvedit_foreach_uv_multi(scene, objects_edit, true, true, [&](float luv[2]) {
261 add_v2_v2(co, luv);
262 sel++;
263 });
264
265 mul_v2_fl(co, 1.0f / float(sel));
266
267 return (sel != 0);
268}
269
271 Span<Object *> objects_edit,
272 float cent[2],
273 char mode)
274{
275 bool changed = false;
276
277 if (mode == V3D_AROUND_CENTER_BOUNDS) { /* bounding box */
278 float min[2], max[2];
279 if (ED_uvedit_minmax_multi(scene, objects_edit, min, max)) {
280 mid_v2_v2v2(cent, min, max);
281 changed = true;
282 }
283 }
284 else {
285 if (uvedit_median_multi(scene, objects_edit, cent)) {
286 changed = true;
287 }
288 }
289
290 return changed;
291}
292
294 const SpaceImage *sima,
295 Scene *scene,
296 ViewLayer *view_layer,
297 float r_center[2],
298 char mode,
299 bool *r_has_select)
300{
301 bool changed = false;
302 switch (mode) {
303 case V3D_AROUND_CURSOR: {
304 copy_v2_v2(r_center, sima->cursor);
305 changed = true;
306 if (r_has_select != nullptr) {
307 Vector<Object *> objects =
309 bmain, scene, view_layer, nullptr);
310 *r_has_select = uvedit_select_is_any_selected_multi(scene, objects);
311 }
312 break;
313 }
314 default: {
315 Vector<Object *> objects =
317 bmain, scene, view_layer, nullptr);
318 changed = ED_uvedit_center_multi(scene, objects, r_center, mode);
319 if (r_has_select != nullptr) {
320 *r_has_select = changed;
321 }
322 break;
323 }
324 }
325 return changed;
326}
327
328enum class UVMoveType {
330 Pixel = 1,
331 Udim = 2,
332};
333enum class UVMoveDirection {
334 X = 0,
335 Y = 1,
336};
337
339
340{
341 const Main *bmain = CTX_data_main(C);
342 Scene *scene = CTX_data_scene(C);
343 ViewLayer *view_layer = CTX_data_view_layer(C);
346 *bmain, scene, view_layer, nullptr);
347 UVMoveType type = UVMoveType(RNA_enum_get(op->ptr, "type"));
348 UVMoveDirection axis = UVMoveDirection(RNA_enum_get(op->ptr, "axis"));
349 int distance = RNA_int_get(op->ptr, "distance");
350
351 int size[2];
352 ED_space_image_get_size(sima, &size[0], &size[1]);
353 float distance_final;
354 if (type == UVMoveType::Dynamic) {
355 distance_final = float(distance) / sima->custom_grid_subdiv[int(axis)];
356 }
357 else if (type == UVMoveType::Pixel) {
358 distance_final = float(distance) / size[int(axis)];
359 }
360 else {
361 distance_final = distance;
362 }
363 for (Object *obedit : objects) {
365 bool changed = false;
366 if (bm->totvertsel == 0) {
367 continue;
368 }
369
370 ED_uvedit_foreach_uv(scene, bm, true, true, [&axis, &distance_final, &changed](float luv[2]) {
371 luv[int(axis)] += distance_final;
372 changed = true;
373 });
374
375 if (changed) {
376 uvedit_live_unwrap_update(sima, scene, obedit);
377 DEG_id_tag_update(obedit->data, 0);
378 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
379 }
380 }
381 return OPERATOR_FINISHED;
382}
383
385{
386 static const EnumPropertyItem shift_items[] = {
387 {int(UVMoveType::Dynamic), "DYNAMIC", 0, "Dynamic", "Move by dynamic grid"},
388 {int(UVMoveType::Pixel), "PIXEL", 0, "Pixel", "Move by pixel"},
389 {int(UVMoveType::Udim), "UDIM", 0, "UDIM", "Move by UDIM"},
390 {0, nullptr, 0, nullptr, nullptr},
391 };
392
393 static const EnumPropertyItem axis_items[] = {
394 {int(UVMoveDirection::X), "X", 0, "X axis", "Move vertices on the X axis"},
395 {int(UVMoveDirection::Y), "Y", 0, "Y axis", "Move vertices on the Y axis"},
396 {0, nullptr, 0, nullptr, nullptr},
397 };
398
399 /* identifiers */
400 ot->name = "Move on Axis";
401 ot->description = "Move UVs on an axis";
402 ot->idname = "UV_OT_move_on_axis";
404
405 /* API callbacks. */
408
409 /* properties */
410 RNA_def_enum(ot->srna, "type", shift_items, int(UVMoveType::Udim), "Type", "Move Type");
412 ot->srna, "axis", axis_items, int(UVMoveDirection::X), "Axis", "Axis to move UVs on");
413 RNA_def_int(ot->srna,
414 "distance",
415 1,
416 INT_MIN,
417 INT_MAX,
418 "Distance",
419 "Distance to move UVs",
420 INT_MIN,
421 INT_MAX);
422}
423
424
425/* -------------------------------------------------------------------- */
428
439 Mean = 0,
440 Min = 1,
441 Max = 2,
442};
443
444static bool uvedit_uv_align_weld(Scene *scene,
445 BMesh *bm,
446 const eUVWeldAlign tool,
447 const float cent[2])
448{
449 bool changed = false;
450
451 ED_uvedit_foreach_uv(scene, bm, true, true, [&](float luv[2]) {
452 if (ELEM(tool, UV_ALIGN_X, UV_WELD)) {
453 if (luv[0] != cent[0]) {
454 luv[0] = cent[0];
455 changed = true;
456 }
457 }
458 if (ELEM(tool, UV_ALIGN_Y, UV_WELD)) {
459 if (luv[1] != cent[1]) {
460 luv[1] = cent[1];
461 changed = true;
462 }
463 }
464 });
465
466 return changed;
467}
468
472 UVEP_SELECTED = (1 << 0),
473 UVEP_PINNED = (1 << 1), /* i.e. Pinned verts are preferred to selected. */
474};
476
478{
480 if (pinned) {
481 precedence |= UVEP_PINNED;
482 }
483 return precedence;
484}
485
490static bool uvedit_line_update_endpoint(const float *luv,
491 const bool pinned,
492 float uv_a[2],
493 eUVEndPointPrecedence *prec_a,
494 float uv_b[2],
495 eUVEndPointPrecedence *prec_b)
496{
498
499 float len_sq_a = len_squared_v2v2(uv_a, luv);
500 float len_sq_b = len_squared_v2v2(uv_b, luv);
501
502 /* Caching the value of `len_sq_ab` is unlikely to be faster than recalculating.
503 * Profile before optimizing. */
504 float len_sq_ab = len_squared_v2v2(uv_a, uv_b);
505
506 if ((*prec_a < flags && 0.0f < len_sq_b) || (*prec_a == flags && len_sq_ab < len_sq_b)) {
507 *prec_a = flags;
508 copy_v2_v2(uv_a, luv);
509 return true;
510 }
511
512 if ((*prec_b < flags && 0.0f < len_sq_a) || (*prec_b == flags && len_sq_ab < len_sq_a)) {
513 *prec_b = flags;
514 copy_v2_v2(uv_b, luv);
515 return true;
516 }
517
518 return false;
519}
520
526 const int len,
527 const BMUVOffsets &offsets,
528 const eUVWeldAlign tool)
529{
530 float uv_start[2];
531 float uv_end[2];
534
535 /* Find start and end of line. */
536 for (int i = 0; i < 10; i++) { /* Heuristic to prevent infinite loop. */
537 bool update = false;
538 for (int j = 0; j < len; j++) {
539 float *luv = BM_ELEM_CD_GET_FLOAT_P(element[j].l, offsets.uv);
540 bool pinned = BM_ELEM_CD_GET_BOOL(element[j].l, offsets.pin);
541 update |= uvedit_line_update_endpoint(luv, pinned, uv_start, &prec_start, uv_end, &prec_end);
542 }
543 if (!update) {
544 break;
545 }
546 }
547
548 if (prec_start == UVEP_INVALID || prec_end == UVEP_INVALID) {
549 return false; /* Unable to find two endpoints. */
550 }
551
552 float a = 0.0f; /* Similar to "slope". */
553 eUVWeldAlign tool_local = tool;
554
555 if (tool_local == UV_STRAIGHTEN_X) {
556 if (uv_start[1] == uv_end[1]) {
557 /* Caution, different behavior outside line segment. */
558 tool_local = UV_STRAIGHTEN;
559 }
560 else {
561 a = (uv_end[0] - uv_start[0]) / (uv_end[1] - uv_start[1]);
562 }
563 }
564 else if (tool_local == UV_STRAIGHTEN_Y) {
565 if (uv_start[0] == uv_end[0]) {
566 /* Caution, different behavior outside line segment. */
567 tool_local = UV_STRAIGHTEN;
568 }
569 else {
570 a = (uv_end[1] - uv_start[1]) / (uv_end[0] - uv_start[0]);
571 }
572 }
573
574 bool changed = false;
575 for (int j = 0; j < len; j++) {
576 float *luv = BM_ELEM_CD_GET_FLOAT_P(element[j].l, offsets.uv);
577 /* Projection of point (x, y) over line (x1, y1, x2, y2) along X axis:
578 * new_y = (y2 - y1) / (x2 - x1) * (x - x1) + y1
579 * Maybe this should be a BLI func? Or is it already existing?
580 * Could use interp_v2_v2v2, but not sure it's worth it here. */
581 if (tool_local == UV_STRAIGHTEN_X) {
582 luv[0] = a * (luv[1] - uv_start[1]) + uv_start[0];
583 }
584 else if (tool_local == UV_STRAIGHTEN_Y) {
585 luv[1] = a * (luv[0] - uv_start[0]) + uv_start[1];
586 }
587 else {
588 closest_to_line_segment_v2(luv, luv, uv_start, uv_end);
589 }
590 changed = true; /* TODO: Did the UV actually move? */
591 }
592 return changed;
593}
594
598static bool uvedit_uv_straighten(Scene *scene, BMesh *bm, eUVWeldAlign tool)
599{
600 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
601 if (offsets.uv == -1) {
602 return false;
603 }
604
605 UvElementMap *element_map = BM_uv_element_map_create(bm, scene, true, false, true, true);
606 if (element_map == nullptr) {
607 return false;
608 }
609
610 bool changed = false;
611 for (int i = 0; i < element_map->total_islands; i++) {
612 changed |= uvedit_uv_straighten_elements(element_map->storage + element_map->island_indices[i],
613 element_map->island_total_uvs[i],
614 offsets,
615 tool);
616 }
617
618 BM_uv_element_map_free(element_map);
619 return changed;
620}
628 X = 0,
629 Y = 1,
630};
632 Max = 0,
633 Min = 1,
634 Center = 2,
635 None = 3,
636};
642
647
652static bool uvedit_uv_islands_arrange(const Scene *scene,
653 BMesh *bm,
654 const UVAlignIslandAxis axis,
655 const UVAlignIslandMode align,
656 const UVAlignIslandOrder order,
657 const float margin,
659{
660 bool changed = false;
661 UvElementMap *element_map = BM_uv_element_map_create(bm, scene, true, false, true, true);
662 if (element_map == nullptr) {
663 return changed;
664 }
665
666 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
667 const uint other_axis = (uint(axis) + 1) % 2;
668 Array<UVAlignIslandBounds> island_bounds_all(element_map->total_islands);
669 for (int i = 0; i < element_map->total_islands; i++) {
670 UvElement *element = element_map->storage + element_map->island_indices[i];
671 UVAlignIslandBounds &island_bounds = island_bounds_all[i];
672 INIT_MINMAX2(island_bounds.bounds.min, island_bounds.bounds.max);
673 for (int j = 0; j < element_map->island_total_uvs[i]; j++) {
674 float *luv = BM_ELEM_CD_GET_FLOAT_P(element[j].l, offsets.uv);
675 minmax_v2v2_v2(island_bounds.bounds.min, island_bounds.bounds.max, luv);
676 }
677 island_bounds.index = i;
678 }
679 std::stable_sort(island_bounds_all.begin(),
680 island_bounds_all.end(),
681 [&order, &axis](const UVAlignIslandBounds &a, const UVAlignIslandBounds &b) {
682 if (order == UVAlignIslandOrder::Fixed) {
683 return a.bounds.min[int(axis)] < b.bounds.min[int(axis)];
684 }
685 const float area_a = (a.bounds.size()[0] * a.bounds.size()[1]);
686 const float area_b = (b.bounds.size()[0] * b.bounds.size()[1]);
687 return (order == UVAlignIslandOrder::LargeToSmall) ? (area_a > area_b) :
688 (area_a < area_b);
689 });
690
691 for (const UVAlignIslandBounds &island_bounds : island_bounds_all) {
692 UvElement *element = element_map->storage + element_map->island_indices[island_bounds.index];
693 for (int j = 0; j < element_map->island_total_uvs[island_bounds.index]; j++) {
694 float *luv = BM_ELEM_CD_GET_FLOAT_P(element[j].l, offsets.uv);
695 if (align == UVAlignIslandMode::Min) {
696 luv[other_axis] += position[other_axis] - island_bounds.bounds.min[other_axis];
697 }
698 else if (align == UVAlignIslandMode::Center) {
699 luv[other_axis] += position[other_axis] - island_bounds.bounds.center()[other_axis];
700 }
701 else if (align == UVAlignIslandMode::Max) {
702 luv[other_axis] += position[other_axis] - island_bounds.bounds.max[other_axis];
703 }
704 luv[int(axis)] += position[int(axis)] - island_bounds.bounds.min[int(axis)];
705 }
706 position[int(axis)] += island_bounds.bounds.size()[int(axis)] + margin;
707 changed = true;
708 }
709 BM_uv_element_map_free(element_map);
710 return changed;
711}
712
714{
715 const Main *bmain = CTX_data_main(C);
716 Scene *scene = CTX_data_scene(C);
717 ViewLayer *view_layer = CTX_data_view_layer(C);
719
721 *bmain, scene, view_layer, nullptr);
722
723 const UVAlignInitialPosition initial_position = UVAlignInitialPosition(
724 RNA_enum_get(op->ptr, "initial_position"));
725 const UVAlignIslandAxis axis = UVAlignIslandAxis(RNA_enum_get(op->ptr, "axis"));
726 const UVAlignIslandMode align = UVAlignIslandMode(RNA_enum_get(op->ptr, "align"));
727 const UVAlignIslandOrder order = UVAlignIslandOrder(RNA_enum_get(op->ptr, "order"));
728 const float margin = RNA_float_get(op->ptr, "margin");
729 const uint other_axis = (uint(axis) + 1) % 2;
730
731 float2 position = {0.0f, 0.0f};
732 Bounds<float2> bounds = {{0.0f, 0.0f}, {1.0f, 1.0f}};
733 if (initial_position == UVAlignInitialPosition::BoundingBox) {
734 INIT_MINMAX2(bounds.min, bounds.max);
735 for (Object *obedit : objects) {
737 ED_uvedit_foreach_uv(scene, bm, true, true, [&](float luv[2]) {
738 minmax_v2v2_v2(bounds.min, bounds.max, luv);
739 });
740 }
741 }
742 else if (initial_position == UVAlignInitialPosition::ActiveUDIM) {
743 if (sima && sima->image && (sima->image->source == IMA_SRC_TILED)) {
744 const int tile_x = sima->image->active_tile_index % 10;
745 const int tile_y = sima->image->active_tile_index / 10;
746 bounds.min[0] = tile_x;
747 bounds.min[1] = tile_y;
748 bounds.max[0] = tile_x + 1.0f;
749 bounds.max[1] = tile_y + 1.0f;
750 }
751 }
752 else if (initial_position == UVAlignInitialPosition::UVTileGrid) {
753 /* Leave the minimum at zero. */
754 if (sima) {
755 bounds.max[0] = sima->tile_grid_shape[0];
756 bounds.max[1] = sima->tile_grid_shape[1];
757 }
758 }
759 else {
760 if (sima) {
761 position = sima->cursor;
762 }
763 }
764 if (ELEM(initial_position,
768 {
769 if (align == UVAlignIslandMode::Min) {
770 position[other_axis] = bounds.min[other_axis];
771 }
772 else if (align == UVAlignIslandMode::Center) {
773 position[other_axis] = bounds.center()[other_axis];
774 }
775 else {
776 position[other_axis] = bounds.max[other_axis];
777 }
778 position[int(axis)] = bounds.min[int(axis)];
779 }
780 for (Object *obedit : objects) {
782 if (bm->totvertsel == 0) {
783 continue;
784 }
785 if (uvedit_uv_islands_arrange(scene, bm, axis, align, order, margin, position)) {
786 uvedit_live_unwrap_update(sima, scene, obedit);
787 DEG_id_tag_update(obedit->data, 0);
788 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
789 }
790 }
791 return OPERATOR_FINISHED;
792}
793
795{
796 static const EnumPropertyItem initial_position_items[] = {
798 "BOUNDING_BOX",
799 0,
800 "Bounding Box",
801 "Initial alignment based on the islands bounding box"},
803 "UV_GRID",
804 0,
805 "UV Grid",
806 "Initial alignment based on UV Tile Grid"},
808 "ACTIVE_UDIM",
809 0,
810 "Active UDIM",
811 "Initial alignment based on Active UDIM"},
813 "CURSOR",
814 0,
815 "2D Cursor",
816 "Initial alignment based on 2D cursor"},
817 {0, nullptr, 0, nullptr, nullptr},
818
819 };
820 static const EnumPropertyItem axis_items[] = {
821 {int(UVAlignIslandAxis::X), "X", 0, "X", "Align UV islands along the X axis"},
822 {int(UVAlignIslandAxis::Y), "Y", 0, "Y", "Align UV islands along the Y axis"},
823 {0, nullptr, 0, nullptr, nullptr},
824 };
825 static const EnumPropertyItem align_items[] = {
826 {int(UVAlignIslandMode::Min), "MIN", 0, "Min", "Align the islands to the min of the island"},
828 "MAX",
829 0,
830 "Max",
831 "Align the islands to the max side of the island"},
833 "CENTER",
834 0,
835 "Center",
836 "Align the islands to the center of the largest island"},
837 {int(UVAlignIslandMode::None), "NONE", 0, "None", "Preserve island alignment"},
838 {0, nullptr, 0, nullptr, nullptr},
839 };
840
841 static const EnumPropertyItem sort_items[] = {
843 "LARGE_TO_SMALL",
844 0,
845 "Largest to Smallest",
846 "Sort islands from largest to smallest"},
848 "SMALL_TO_LARGE",
849 0,
850 "Smallest to Largest",
851 "Sort islands from smallest to largest"},
852 {int(UVAlignIslandOrder::Fixed), "Fixed", 0, "Fixed", "Preserve island order"},
853 {0, nullptr, 0, nullptr, nullptr},
854 };
855 /* identifiers */
856 ot->name = "Arrange/Align Islands";
857 ot->description = "Arrange selected UV islands on a line";
858 ot->idname = "UV_OT_arrange_islands";
860
861 /* API callbacks. */
864
865 /* properties */
866 RNA_def_enum(ot->srna,
867 "initial_position",
868 initial_position_items,
870 "Initial Position",
871 "Initial position to arrange islands from");
872 RNA_def_enum(ot->srna,
873 "axis",
874 axis_items,
876 "Axis",
877 "Axis to arrange UV islands on");
878 RNA_def_enum(ot->srna,
879 "align",
880 align_items,
882 "Align",
883 "Location to align islands on");
884 RNA_def_enum(ot->srna,
885 "order",
886 sort_items,
888 "Order",
889 "Order of islands");
890
892 ot->srna, "margin", 0.05f, 0.0f, 1.0f, "Margin", "Space between islands", 0.0f, 1.0f);
893}
894
895static void uv_weld(bContext *C)
896{
897 const Main *bmain = CTX_data_main(C);
898 Scene *scene = CTX_data_scene(C);
899 ViewLayer *view_layer = CTX_data_view_layer(C);
901 float cent[2];
902
904 *bmain, scene, view_layer, nullptr);
905
906 ED_uvedit_center_multi(scene, objects, cent, 0);
907
908 for (Object *obedit : objects) {
910 bool changed = false;
911
912 if (bm->totvertsel == 0) {
913 continue;
914 }
915
916 changed |= uvedit_uv_align_weld(scene, bm, UV_WELD, cent);
917
918 if (changed) {
919 uvedit_live_unwrap_update(sima, scene, obedit);
920 DEG_id_tag_update(obedit->data, 0);
921 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
922 }
923 }
924}
925
926static void uv_align(bContext *C, eUVWeldAlign tool, UVAlignPositionMode position_mode)
927{
928 const Main *bmain = CTX_data_main(C);
929 Scene *scene = CTX_data_scene(C);
930 ViewLayer *view_layer = CTX_data_view_layer(C);
932 float pos[2], min[2], max[2];
933 const bool align_auto = (tool == UV_ALIGN_AUTO);
935
937 *bmain, scene, view_layer, nullptr);
938
939 if (tool == UV_ALIGN_AUTO) {
941 scene, objects, true, true, [&](float luv[2]) { minmax_v2v2_v2(min, max, luv); });
942 tool = (max[0] - min[0] >= max[1] - min[1]) ? UV_ALIGN_Y : UV_ALIGN_X;
943 }
944
945 if (!align_auto && ELEM(tool, UV_ALIGN_X, UV_ALIGN_Y) &&
947 {
948 ED_uvedit_minmax_multi(scene, objects, min, max);
949 if (position_mode == UVAlignPositionMode::Min) {
950 pos[0] = min[0];
951 pos[1] = min[1];
952 }
953 else {
954 pos[0] = max[0];
955 pos[1] = max[1];
956 }
957 }
958 else {
960 }
961
962 for (Object *obedit : objects) {
964 bool changed = false;
965
966 if (bm->totvertsel == 0) {
967 continue;
968 }
969
971 changed |= uvedit_uv_align_weld(scene, bm, tool, pos);
972 }
973
975 changed |= uvedit_uv_straighten(scene, bm, tool);
976 }
977
978 if (changed) {
979 uvedit_live_unwrap_update(sima, scene, obedit);
980 DEG_id_tag_update(obedit->data, 0);
981 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
982 }
983 }
984}
986{
987 uv_align(C,
988 eUVWeldAlign(RNA_enum_get(op->ptr, "axis")),
989 UVAlignPositionMode(RNA_enum_get(op->ptr, "position_mode")));
990
991 return OPERATOR_FINISHED;
992}
993
994static bool uv_align_poll_property(const bContext * /*C*/, wmOperator *op, const PropertyRNA *prop)
995{
996 const char *prop_id = RNA_property_identifier(prop);
997
998 if (STREQ(prop_id, "position_mode")) {
999 int axis = RNA_enum_get(op->ptr, "axis");
1000 if (!ELEM(axis, UV_ALIGN_X, UV_ALIGN_Y)) {
1001 return false;
1002 }
1003 }
1004 return true;
1005}
1007{
1008 static const EnumPropertyItem axis_items[] = {
1010 "ALIGN_S",
1011 0,
1012 "Straighten",
1013 "Align UV vertices along the line defined by the endpoints"},
1015 "ALIGN_T",
1016 0,
1017 "Straighten X",
1018 "Align UV vertices, moving them horizontally to the line defined by the endpoints"},
1020 "ALIGN_U",
1021 0,
1022 "Straighten Y",
1023 "Align UV vertices, moving them vertically to the line defined by the endpoints"},
1025 "ALIGN_AUTO",
1026 0,
1027 "Align Auto",
1028 "Automatically choose the direction on which there is most alignment already"},
1029 {UV_ALIGN_X, "ALIGN_X", 0, "Align Vertically", "Align UV vertices on a vertical line"},
1030 {UV_ALIGN_Y, "ALIGN_Y", 0, "Align Horizontally", "Align UV vertices on a horizontal line"},
1031 {0, nullptr, 0, nullptr, nullptr},
1032 };
1033
1034 static const EnumPropertyItem position_mode_items[] = {
1035 {int(UVAlignPositionMode::Mean), "MEAN", 0, "Mean", "Align UVs along the mean position"},
1036 {int(UVAlignPositionMode::Min), "MIN", 0, "Minimum", "Align UVs along the minimum position"},
1037 {int(UVAlignPositionMode::Max), "MAX", 0, "Maximum", "Align UVs along the maximum position"},
1038 {0, nullptr, 0, nullptr, nullptr},
1039 };
1040
1041 /* identifiers */
1042 ot->name = "Align";
1043
1044 ot->description = "Aligns selected UV vertices on a line";
1045 ot->idname = "UV_OT_align";
1047
1048 /* API callbacks. */
1049 ot->exec = uv_align_exec;
1051
1053
1054 /* properties */
1056 ot->srna, "axis", axis_items, UV_ALIGN_AUTO, "Axis", "Axis to align UV locations on");
1057 RNA_def_enum(ot->srna,
1058 "position_mode",
1059 position_mode_items,
1061 "Position Mode",
1062 "Method of calculating the alignment position");
1063}
1064
1066
1067/* -------------------------------------------------------------------- */
1070
1072{
1073 const Main *bmain = CTX_data_main(C);
1074 Scene *scene = CTX_data_scene(C);
1075 ViewLayer *view_layer = CTX_data_view_layer(C);
1077
1078 const float threshold = RNA_float_get(op->ptr, "threshold");
1079
1081 *bmain, scene, view_layer, nullptr);
1082
1083 bool *changed = MEM_new_array_zeroed<bool>(objects.size(), __func__);
1084
1085 /* Maximum index of an objects[i]'s UVs in UV_arr.
1086 * It helps find which UV in *uv_map_arr belongs to which object. */
1087 uint *ob_uv_map_max_idx = MEM_new_array_zeroed<uint>(objects.size(), __func__);
1088
1089 /* Calculate max possible number of kdtree nodes. */
1090 int uv_maxlen = 0;
1091 for (Object *obedit : objects) {
1093 if (bm->totvertsel == 0) {
1094 continue;
1095 }
1096
1097 uv_maxlen += bm->totloop;
1098 }
1099
1100 KDTree<float2> *tree = kdtree_new<float2>(uv_maxlen);
1101
1102 Vector<int> duplicates;
1103 Vector<float *> uv_map_arr;
1104
1105 int uv_map_count = 0; /* Also used for *duplicates count. */
1106
1107 for (const int ob_index : objects.index_range()) {
1108 Object *obedit = objects[ob_index];
1110 ED_uvedit_foreach_uv(scene, bm, true, true, [&](float luv[2]) {
1111 kdtree_insert<float2>(tree, uv_map_count, luv);
1112 duplicates.append(-1);
1113 uv_map_arr.append(luv);
1114 uv_map_count++;
1115 });
1116
1117 ob_uv_map_max_idx[ob_index] = uv_map_count - 1;
1118 }
1119
1121 int found_duplicates = kdtree_calc_duplicates_fast<float2>(
1122 tree, threshold, false, duplicates.data());
1123
1124 if (found_duplicates > 0) {
1125 /* Calculate average uv for duplicates. */
1126 int *uv_duplicate_count = MEM_new_array_zeroed<int>(uv_map_count, __func__);
1127 for (int i = 0; i < uv_map_count; i++) {
1128 if (duplicates[i] == -1) { /* If doesn't reference another */
1129 uv_duplicate_count[i]++; /* self */
1130 continue;
1131 }
1132
1133 if (duplicates[i] != i) {
1134 /* If not self then accumulate uv for averaging.
1135 * Self uv is already present in accumulator */
1136 add_v2_v2(uv_map_arr[duplicates[i]], uv_map_arr[i]);
1137 }
1138 uv_duplicate_count[duplicates[i]]++;
1139 }
1140
1141 for (int i = 0; i < uv_map_count; i++) {
1142 if (uv_duplicate_count[i] < 2) {
1143 continue;
1144 }
1145
1146 mul_v2_fl(uv_map_arr[i], 1.0f / float(uv_duplicate_count[i]));
1147 }
1148 MEM_delete(uv_duplicate_count);
1149
1150 /* Update duplicated uvs. */
1151 uint ob_index = 0;
1152 for (int i = 0; i < uv_map_count; i++) {
1153 /* Make sure we know which object owns the uv_map at this index.
1154 * Remember that in some cases the object will have no loop uv,
1155 * thus we need the while loop, and not simply an if check. */
1156 while (ob_uv_map_max_idx[ob_index] < i) {
1157 ob_index++;
1158 }
1159
1160 if (duplicates[i] == -1) {
1161 continue;
1162 }
1163
1164 copy_v2_v2(uv_map_arr[i], uv_map_arr[duplicates[i]]);
1165 changed[ob_index] = true;
1166 }
1167
1168 for (ob_index = 0; ob_index < objects.size(); ob_index++) {
1169 if (changed[ob_index]) {
1170 Object *obedit = objects[ob_index];
1171 uvedit_live_unwrap_update(sima, scene, obedit);
1172 DEG_id_tag_update(obedit->data, 0);
1174 }
1175 }
1176 }
1177
1178 kdtree_free<float2>(tree);
1179 MEM_delete(changed);
1180 MEM_delete(ob_uv_map_max_idx);
1181
1182 return OPERATOR_FINISHED;
1183}
1184
1186{
1187 const Main *bmain = CTX_data_main(C);
1188 Scene *scene = CTX_data_scene(C);
1189 ViewLayer *view_layer = CTX_data_view_layer(C);
1191 const float threshold = RNA_float_get(op->ptr, "threshold");
1192
1194 *bmain, scene, view_layer, nullptr);
1195
1196 /* Calculate max possible number of kdtree nodes. */
1197 int uv_maxlen = 0;
1198 for (Object *obedit : objects) {
1199 const BMesh *bm = BKE_editmesh_bmesh_get(obedit);
1200 uv_maxlen += bm->totloop;
1201 }
1202
1203 KDTree<float2> *tree = kdtree_new<float2>(uv_maxlen);
1204
1205 Vector<float *> uv_map_arr;
1206
1207 int uv_map_count = 0;
1208
1209 /* Add visible non-selected uvs to tree */
1210 ED_uvedit_foreach_uv_multi(scene, objects, true, false, [&](float luv[2]) {
1211 kdtree_insert<float2>(tree, uv_map_count, luv);
1212 uv_map_arr.append(luv);
1213 uv_map_count++;
1214 });
1215
1217
1218 /* For each selected uv, find duplicate non selected uv. */
1219 for (Object *obedit : objects) {
1220 bool changed = false;
1222 ED_uvedit_foreach_uv(scene, bm, true, true, [&](float luv[2]) {
1223 KDTreeNearest<float2> nearest;
1224 const int i = kdtree_find_nearest<float2>(tree, luv, &nearest);
1225
1226 if (i != -1 && nearest.dist < threshold) {
1227 copy_v2_v2(luv, uv_map_arr[i]);
1228 changed = true;
1229 }
1230 });
1231
1232 if (changed) {
1233 uvedit_live_unwrap_update(sima, scene, obedit);
1234 DEG_id_tag_update(obedit->data, 0);
1235 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
1236 }
1237 }
1238
1239 kdtree_free<float2>(tree);
1240
1241 return OPERATOR_FINISHED;
1242}
1243
1245{
1246 /* NOTE: The calculation for the center-point of loops belonging to a vertex will be skewed
1247 * if one UV coordinate holds more loops than the others. */
1248
1249 const Main *bmain = CTX_data_main(C);
1250 Scene *scene = CTX_data_scene(C);
1252 ViewLayer *view_layer = CTX_data_view_layer(C);
1254 *bmain, scene, view_layer, nullptr);
1255
1256 /* Only use the squared distance, to avoid a square-root. */
1257 const float threshold_sq = math::square(RNA_float_get(op->ptr, "threshold"));
1258
1259 for (Object *obedit : objects) {
1261 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
1262 BMVert *v;
1263 BMLoop *l;
1264 BMIter viter, liter;
1265
1266 /* The `changed` variable keeps track if any loops from the current object are merged. */
1267 Vector<float *> uvs;
1268 uvs.reserve(32);
1269 bool changed = false;
1270
1271 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
1272
1273 BLI_assert(uvs.size() == 0);
1274 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1275 if (uvedit_uv_select_test(scene, bm, l, offsets)) {
1276 uvs.append(BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv));
1277 }
1278 }
1279 if (uvs.size() <= 1) {
1280 uvs.clear();
1281 continue;
1282 }
1283
1284 while (uvs.size() > 1) {
1285 const int uvs_num = uvs.size();
1286 float2 uv_average = {0.0f, 0.0f};
1287 for (const float *luv : uvs) {
1288 uv_average += float2(luv);
1289 }
1290 uv_average /= uvs_num;
1291
1292 /* Find the loop closest to the uv_average. This loop will be the base that all
1293 * other loop's distances are calculated from. */
1294
1295 float dist_best_sq = math::distance_squared(uv_average, float2(uvs[0]));
1296 float *uv_ref = uvs[0];
1297 int uv_ref_index = 0;
1298 for (int i = 1; i < uvs_num; i++) {
1299 const float dist_test_sq = math::distance_squared(uv_average, float2(uvs[i]));
1300 if (dist_test_sq < dist_best_sq) {
1301 dist_best_sq = dist_test_sq;
1302 uv_ref = uvs[i];
1303 uv_ref_index = i;
1304 }
1305 }
1306
1307 const int uvs_end = uvs_num - 1;
1308 std::swap(uvs[uv_ref_index], uvs[uvs_end]);
1309
1310 /* Move all the UVs within threshold to the end of the array. Sum of all UV coordinates
1311 * within threshold is initialized with `uv_ref` coordinate data since while loop
1312 * ends once it hits `uv_ref` UV. */
1313 float2 uv_merged_average = {uv_ref[0], uv_ref[1]};
1314 int i = 0;
1315 int uvs_num_merged = 1;
1316 while (uvs[i] != uv_ref && i < uvs_num - uvs_num_merged) {
1317 const float dist_test_sq = len_squared_v2v2(uv_ref, uvs[i]);
1318 if (dist_test_sq < threshold_sq) {
1319 uv_merged_average += float2(uvs[i]);
1320 std::swap(uvs[i], uvs[uvs_end - uvs_num_merged]);
1321 uvs_num_merged++;
1322 if (dist_test_sq != 0.0f) {
1323 changed = true;
1324 }
1325 }
1326 else {
1327 i++;
1328 }
1329 }
1330
1331 /* Recalculate `uv_average` so it only considers UV's that are being included in merge
1332 * operation. Then Shift all loops to that position. */
1333 if (uvs_num_merged > 1) {
1334 uv_merged_average /= uvs_num_merged;
1335
1336 for (int j = uvs_num - uvs_num_merged; j < uvs_num; j++) {
1337 copy_v2_v2(uvs[j], uv_merged_average);
1338 }
1339 }
1340
1341 uvs.resize(uvs_num - uvs_num_merged);
1342 }
1343 uvs.clear();
1344 }
1345 if (changed) {
1346 uvedit_live_unwrap_update(sima, scene, obedit);
1347 DEG_id_tag_update(obedit->data, 0);
1348 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
1349 }
1350 }
1351
1352 return OPERATOR_FINISHED;
1353}
1354
1356{
1357 if (RNA_boolean_get(op->ptr, "use_unselected")) {
1359 }
1360 if (RNA_boolean_get(op->ptr, "use_shared_vertex")) {
1362 }
1363 return uv_remove_doubles_to_selected(C, op);
1364}
1365
1367{
1368 /* identifiers */
1369 ot->name = "Merge UVs by Distance";
1370 ot->description =
1371 "Selected UV vertices that are within a radius of each other are welded together";
1372 ot->idname = "UV_OT_remove_doubles";
1374
1375 /* API callbacks. */
1378
1379 RNA_def_float(ot->srna,
1380 "threshold",
1381 0.02f,
1382 0.0f,
1383 10.0f,
1384 "Merge Distance",
1385 "Maximum distance between welded vertices",
1386 0.0f,
1387 1.0f);
1389 "use_unselected",
1390 false,
1391 "Unselected",
1392 "Merge selected to other unselected vertices");
1394 ot->srna, "use_shared_vertex", false, "Shared Vertex", "Weld UVs based on shared vertices");
1395}
1396
1398
1399/* -------------------------------------------------------------------- */
1402
1404{
1405 uv_weld(C);
1406
1407 return OPERATOR_FINISHED;
1408}
1409
1411{
1412 /* identifiers */
1413 ot->name = "Weld";
1414 ot->description = "Weld selected UV vertices together";
1415 ot->idname = "UV_OT_weld";
1417
1418 /* API callbacks. */
1419 ot->exec = uv_weld_exec;
1421}
1422
1424
1425/* -------------------------------------------------------------------- */
1428
1429static void uv_snap_to_pixel(float uvco[2], float w, float h)
1430{
1431 uvco[0] = roundf(uvco[0] * w) / w;
1432 uvco[1] = roundf(uvco[1] * h) / h;
1433}
1434
1436{
1437 int width = 0, height = 0;
1438
1439 ED_space_image_get_size(sima, &width, &height);
1440 uv_snap_to_pixel(sima->cursor, width, height);
1441}
1442
1444 Span<Object *> objects_edit,
1445 SpaceImage *sima)
1446{
1447 return ED_uvedit_center_multi(scene, objects_edit, sima->cursor, sima->around);
1448}
1449
1450static void uv_snap_cursor_to_origin(float uvco[2])
1451{
1452 uvco[0] = 0;
1453 uvco[1] = 0;
1454}
1455
1457{
1459
1460 bool changed = false;
1461
1462 switch (RNA_enum_get(op->ptr, "target")) {
1463 case 0:
1465 changed = true;
1466 break;
1467 case 1: {
1468 const Main *bmain = CTX_data_main(C);
1469 Scene *scene = CTX_data_scene(C);
1470 ViewLayer *view_layer = CTX_data_view_layer(C);
1471
1472 Vector<Object *> objects =
1474 *bmain, scene, view_layer, nullptr);
1475 changed = uv_snap_cursor_to_selection(scene, objects, sima);
1476 break;
1477 }
1478 case 2:
1480 changed = true;
1481 break;
1482 }
1483
1484 if (!changed) {
1485 return OPERATOR_CANCELLED;
1486 }
1487
1489
1490 return OPERATOR_FINISHED;
1491}
1492
1494{
1495 static const EnumPropertyItem target_items[] = {
1496 {0, "PIXELS", 0, "Pixels", ""},
1497 {1, "SELECTED", 0, "Selected", ""},
1498 {2, "ORIGIN", 0, "Origin", ""},
1499 {0, nullptr, 0, nullptr, nullptr},
1500 };
1501
1502 /* identifiers */
1503 ot->name = "Snap Cursor";
1504 ot->description = "Snap cursor to target type";
1505 ot->idname = "UV_OT_snap_cursor";
1507
1508 /* API callbacks. */
1510 ot->poll = ED_operator_uvedit_space_image; /* requires space image */
1511
1512 /* properties */
1514 ot->srna, "target", target_items, 0, "Target", "Target to snap the selected UVs to");
1515}
1516
1518
1519/* -------------------------------------------------------------------- */
1522
1523static bool uv_snap_uvs_to_cursor(Scene *scene, Object *obedit, const float cursor[2])
1524{
1526 bool changed = false;
1527
1528 ED_uvedit_foreach_uv(scene, bm, true, true, [&](float luv[2]) {
1529 copy_v2_v2(luv, cursor);
1530 changed = true;
1531 });
1532
1533 return changed;
1534}
1535
1536static bool uv_snap_uvs_offset(Scene *scene, Object *obedit, const float offset[2])
1537{
1539 bool changed = false;
1540
1541 ED_uvedit_foreach_uv(scene, bm, true, true, [&](float luv[2]) {
1542 add_v2_v2(luv, offset);
1543 changed = true;
1544 });
1545
1546 return changed;
1547}
1548
1550{
1552 BMFace *f;
1553 BMLoop *l, *lsub;
1554 BMIter iter, liter, lsubiter;
1555 float *luv;
1556 bool changed = false;
1557 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
1558
1559 /* Index every vert that has a selected UV using it, but only once so as to
1560 * get unique indices and to count how much to `malloc`. */
1561 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
1562 if (uvedit_face_visible_test(scene, f)) {
1564 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1566 }
1567 }
1568 else {
1570 }
1571 }
1572
1573 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
1574 if (BM_elem_flag_test(f, BM_ELEM_TAG)) { /* Face: visible. */
1575 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1576 if (BM_elem_flag_test(l, BM_ELEM_TAG)) { /* Loop: selected. */
1577 float uv[2] = {0.0f, 0.0f};
1578 int uv_tot = 0;
1579
1580 BM_ITER_ELEM (lsub, &lsubiter, l->v, BM_LOOPS_OF_VERT) {
1581 if (BM_elem_flag_test(lsub->f, BM_ELEM_TAG) && /* Face: visible. */
1582 !BM_elem_flag_test(lsub, BM_ELEM_TAG)) /* Loop: unselected. */
1583 {
1584 luv = BM_ELEM_CD_GET_FLOAT_P(lsub, offsets.uv);
1585 add_v2_v2(uv, luv);
1586 uv_tot++;
1587 }
1588 }
1589
1590 if (uv_tot) {
1591 luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
1592 mul_v2_v2fl(luv, uv, 1.0f / float(uv_tot));
1593 changed = true;
1594 }
1595 }
1596 }
1597 }
1598 }
1599
1600 return changed;
1601}
1602
1603static bool uv_snap_uvs_to_pixels(SpaceImage *sima, Scene *scene, Object *obedit)
1604{
1606 int width = 0, height = 0;
1607 float w, h;
1608 bool changed = false;
1609
1610 ED_space_image_get_size(sima, &width, &height);
1611 w = float(width);
1612 h = float(height);
1613
1614 ED_uvedit_foreach_uv(scene, bm, true, true, [&](float luv[2]) {
1615 uv_snap_to_pixel(luv, w, h);
1616 changed = true;
1617 });
1618
1619 return changed;
1620}
1621
1623{
1624 const Main *bmain = CTX_data_main(C);
1625 Scene *scene = CTX_data_scene(C);
1626 ViewLayer *view_layer = CTX_data_view_layer(C);
1628 const int target = RNA_enum_get(op->ptr, "target");
1629 float offset[2] = {0};
1630
1632 *bmain, scene, view_layer, nullptr);
1633
1634 if (target == 2) {
1635 float center[2];
1636 if (!ED_uvedit_center_multi(scene, objects, center, sima->around)) {
1637 return OPERATOR_CANCELLED;
1638 }
1639 sub_v2_v2v2(offset, sima->cursor, center);
1640 }
1641
1642 bool changed_multi = false;
1643 for (Object *obedit : objects) {
1645 if (bm->totvertsel == 0) {
1646 continue;
1647 }
1648
1649 bool changed = false;
1650 switch (target) {
1651 case 0:
1652 changed = uv_snap_uvs_to_pixels(sima, scene, obedit);
1653 break;
1654 case 1:
1655 changed = uv_snap_uvs_to_cursor(scene, obedit, sima->cursor);
1656 break;
1657 case 2:
1658 changed = uv_snap_uvs_offset(scene, obedit, offset);
1659 break;
1660 case 3:
1661 changed = uv_snap_uvs_to_adjacent_unselected(scene, obedit);
1662 break;
1663 }
1664
1665 if (changed) {
1666 changed_multi = true;
1667 uvedit_live_unwrap_update(sima, scene, obedit);
1668 DEG_id_tag_update(obedit->data, 0);
1669 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
1670 }
1671 }
1672
1673 return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1674}
1675
1677{
1678 static const EnumPropertyItem target_items[] = {
1679 {0, "PIXELS", 0, "Pixels", ""},
1680 {1, "CURSOR", 0, "Cursor", ""},
1681 {2, "CURSOR_OFFSET", 0, "Cursor (Offset)", ""},
1682 {3, "ADJACENT_UNSELECTED", 0, "Adjacent Unselected", ""},
1683 {0, nullptr, 0, nullptr, nullptr},
1684 };
1685
1686 /* identifiers */
1687 ot->name = "Snap Selection";
1688 ot->description = "Snap selected UV vertices to target type";
1689 ot->idname = "UV_OT_snap_selected";
1691
1692 /* API callbacks. */
1695
1696 /* properties */
1698 ot->srna, "target", target_items, 0, "Target", "Target to snap the selected UVs to");
1699}
1700
1702
1703/* -------------------------------------------------------------------- */
1706
1708{
1709 const Main *bmain = CTX_data_main(C);
1710 Scene *scene = CTX_data_scene(C);
1711 ViewLayer *view_layer = CTX_data_view_layer(C);
1712 BMFace *efa;
1713 BMLoop *l;
1714 BMIter iter, liter;
1715 const bool clear = RNA_boolean_get(op->ptr, "clear");
1716 const bool invert = RNA_boolean_get(op->ptr, "invert");
1717
1719 *bmain, scene, view_layer, nullptr);
1720
1721 for (Object *obedit : objects) {
1722 Mesh &mesh = *id_cast<Mesh *>(obedit->data);
1724
1725 bool changed = false;
1726
1727 const StringRef active_uv_name = mesh.active_uv_map_name();
1728 if (bm->totvertsel == 0) {
1729 continue;
1730 }
1731
1732 if (clear && !BM_uv_map_attr_pin_exists(bm, active_uv_name)) {
1733 continue;
1734 }
1735
1736 BM_uv_map_attr_pin_ensure_named(bm, active_uv_name);
1737 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
1738
1739 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
1740 if (!uvedit_face_visible_test(scene, efa)) {
1741 continue;
1742 }
1743
1744 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
1745
1746 if (uvedit_uv_select_test(scene, bm, l, offsets)) {
1747 changed = true;
1748 if (invert) {
1749 BM_ELEM_CD_SET_BOOL(l, offsets.pin, !BM_ELEM_CD_GET_BOOL(l, offsets.pin));
1750 }
1751 else {
1752 BM_ELEM_CD_SET_BOOL(l, offsets.pin, !clear);
1753 }
1754 }
1755 }
1756 }
1757
1758 if (changed) {
1759 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
1761 }
1762 }
1763
1764 return OPERATOR_FINISHED;
1765}
1766
1768{
1769 PropertyRNA *prop;
1770
1771 /* identifiers */
1772 ot->name = "Pin";
1773 ot->description =
1774 "Set/clear selected UV vertices as anchored between multiple unwrap operations";
1775 ot->idname = "UV_OT_pin";
1777
1778 /* API callbacks. */
1779 ot->exec = uv_pin_exec;
1781
1782 /* properties */
1783 prop = RNA_def_boolean(
1784 ot->srna, "clear", false, "Clear", "Clear pinning for the selection instead of setting it");
1786 prop = RNA_def_boolean(ot->srna,
1787 "invert",
1788 false,
1789 "Invert",
1790 "Invert pinning for the selection instead of setting it");
1792}
1793
1795
1796/* -------------------------------------------------------------------- */
1799
1800/* check if we are selected or unselected based on 'bool_test' arg,
1801 * needed for select swap support */
1802#define UV_VERT_SEL_TEST(ts, bm, l, bool_test) \
1803 (uvedit_vert_select_get_no_sync(ts, bm, l) == bool_test)
1804
1805#define UV_EDGE_SEL_TEST(ts, bm, l, bool_test) \
1806 (uvedit_edge_select_get_no_sync(ts, bm, l) == bool_test)
1807
1808static bool bm_face_is_any_sel(const ToolSettings *ts,
1809 const BMesh *bm,
1810 BMFace *f,
1811 bool select_test)
1812{
1813 BMIter liter;
1814 BMLoop *l;
1815 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1816 if (UV_VERT_SEL_TEST(ts, bm, l, select_test) || UV_EDGE_SEL_TEST(ts, bm, l, select_test)) {
1817 return true;
1818 }
1819 }
1820 return false;
1821}
1822
1824 const BMesh *bm,
1825 BMFace *f,
1826 bool select_test)
1827{
1828 BMIter liter;
1829 BMLoop *l;
1830 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1831 if (UV_VERT_SEL_TEST(ts, bm, l, select_test)) {
1832 return true;
1833 }
1834 }
1835 return false;
1836}
1837
1839 const BMesh *bm,
1840 BMFace *f,
1841 bool select_test)
1842{
1843 BMIter liter;
1844 BMLoop *l;
1845 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1846 if (UV_EDGE_SEL_TEST(ts, bm, l, select_test)) {
1847 return true;
1848 }
1849 }
1850 return false;
1851}
1852
1853/* is every UV vert selected or unselected depending on bool_test */
1855 const BMesh *bm,
1856 BMFace *f,
1857 bool select_test)
1858{
1859 BMLoop *l_iter;
1860 BMLoop *l_first;
1861
1862 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
1863 do {
1864 if (!UV_EDGE_SEL_TEST(ts, bm, l_iter, select_test)) {
1865 return false;
1866 }
1867 } while ((l_iter = l_iter->next) != l_first);
1868
1869 return true;
1870}
1871
1872static bool uv_mesh_hide_select_sync(const ToolSettings *ts, Object *ob, BMEditMesh *em, bool swap)
1873{
1874 const bool select_to_hide = !swap;
1876 bool changed = false;
1877
1879 /* Simple case, no need to synchronize UV's, forward to mesh hide. */
1880 changed = EDBM_mesh_hide(bm, em->selectmode, swap);
1881 }
1882 else {
1883 /* For vertices & edges hiding faces immediately causes a feedback loop,
1884 * where hiding doesn't work predictably as values are being both read and written to.
1885 * Perform two passes, use tagging. */
1886
1887 /* Vertex and edge modes use almost the same logic. */
1889 BMIter iter;
1891
1892 if (em->selectmode & SCE_SELECT_VERTEX) {
1893 BMFace *f;
1894 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
1896 continue;
1897 }
1898 BMLoop *l_iter, *l_first;
1899 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
1900 do {
1901 if ((BM_elem_flag_test_bool(l_iter->v, BM_ELEM_SELECT) == select_to_hide) &&
1902 (BM_elem_flag_test_bool(l_iter, BM_ELEM_SELECT_UV) == select_to_hide))
1903 {
1905 changed = true;
1906 break;
1907 }
1908 } while ((l_iter = l_iter->next) != l_first);
1909 }
1910 }
1911 else {
1913 BMFace *f;
1914 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
1916 continue;
1917 }
1918 BMLoop *l_iter, *l_first;
1919 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
1920 do {
1921 if ((BM_elem_flag_test_bool(l_iter->e, BM_ELEM_SELECT) == select_to_hide) &&
1922 (BM_elem_flag_test_bool(l_iter, BM_ELEM_SELECT_UV_EDGE) == select_to_hide))
1923 {
1925 changed = true;
1926 break;
1927 }
1928 } while ((l_iter = l_iter->next) != l_first);
1929 }
1930 }
1931
1932 if (changed) {
1933 BMFace *f;
1934 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
1936 BM_elem_hide_set(bm, f, true);
1937 }
1938 }
1939 if (swap) {
1940 /* Without re-selecting, the faces vertices are de-selected when hiding adjacent faces.
1941 *
1942 * TODO(@ideasman42): consider a more elegant solution of ensuring
1943 * faces at the boundaries don't get their vertices de-selected.
1944 * This is low-priority as it's no a bottleneck. */
1945 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
1946 if (!BM_elem_flag_test(f, BM_ELEM_TAG)) {
1947 BM_face_select_set(bm, f, true);
1948 }
1949 }
1950 }
1951 }
1952 }
1953 else {
1955 BMIter iter;
1956 BMFace *f;
1957 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
1959 continue;
1960 }
1961 if (BM_elem_flag_test_bool(f, BM_ELEM_SELECT_UV) == select_to_hide) {
1962 BM_elem_hide_set(bm, f, true);
1963 changed = true;
1964 }
1965 }
1966 }
1967
1968 if (changed) {
1969 if (swap) {
1971 }
1972 else {
1974 }
1975 /* Clearing is OK even when hiding unselected
1976 * as the remaining geometry is entirely selected. */
1978 }
1979 }
1980
1981 if (changed) {
1982 Mesh *mesh = id_cast<Mesh *>(ob->data);
1983 EDBMUpdate_Params params = {0};
1984 params.calc_looptris = true;
1985 params.calc_normals = false;
1986 params.is_destructive = false;
1987 EDBM_update(mesh, &params);
1988 }
1989
1990 return changed;
1991}
1992
1996static bool uv_mesh_hide_no_sync(const Scene *scene, Object *ob, BMEditMesh *em, bool swap)
1997{
1998 const ToolSettings *ts = scene->toolsettings;
2001
2002 BMFace *efa;
2003 BMLoop *l;
2004 const bool use_face_center = (ts->uv_selectmode == UV_SELECT_FACE);
2005
2006 /* Track mesh selection changes. */
2007 bool changed = false;
2008 bool changed_uv = false;
2009
2010 const auto bm_vert_deselect_for_pass_fn = [&scene, &bm, &swap](BMVert *v, int pass) -> bool {
2011 if (pass == 0) {
2012 BMIter liter;
2013 BMLoop *l_iter;
2014 BM_ITER_ELEM (l_iter, &liter, v, BM_LOOPS_OF_VERT) {
2015 if (uvedit_face_visible_test(scene, l_iter->f)) {
2016 if (!UV_VERT_SEL_TEST(scene->toolsettings, bm, l_iter, !swap)) {
2017 /* Part of a mixed selection, bail out. */
2018 return false;
2019 }
2020 }
2021 }
2022 }
2023 BM_vert_select_set(bm, v, false);
2024 return true;
2025 };
2026
2027 const auto bm_edge_deselect_for_pass_fn = [&scene, &bm, &swap](BMEdge *e, int pass) -> bool {
2028 if (pass == 0) {
2029 BMLoop *l_iter, *l_first;
2030 l_iter = l_first = e->l;
2031 do {
2032 if (uvedit_face_visible_test(scene, l_iter->f)) {
2033 if (!UV_EDGE_SEL_TEST(scene->toolsettings, bm, l_iter, !swap)) {
2034 /* Part of a mixed selection, bail out. */
2035 return false;
2036 }
2037 }
2038 } while ((l_iter = l_iter->radial_next) != l_first);
2039 }
2040 BM_edge_select_set(bm, e, false);
2041 return true;
2042 };
2043
2044 /* Hiding in Vertex/Edge Mode
2045 * ==========================
2046 *
2047 * Hiding in the UV editor works by de-selecting faces,
2048 * which can only be reliably done in face selection mode.
2049 *
2050 * The crux of the problem: To achieve the desired result of "hide"
2051 * we need to de-select individual *faces* in vertex/edge selection mode.
2052 * This isn't always possible because vertex/edge selection will flush up to faces.
2053 * *But* an approximation can work, sometimes achieving matching results... read on.
2054 *
2055 * Multi-Pass Hide
2056 * ===============
2057 *
2058 * Multi-pass hiding solves the following problem: in vertex/edge mesh selection modes,
2059 * too many UVs are hidden (hiding in this case de-selects).
2060 * It's not needed for face-only selection.
2061 *
2062 * The most common example is when the user hides a selected island,
2063 * and faces on the boundaries of neighboring islands are hidden too. See #153464.
2064 *
2065 * This makes some sense; in vertex selections mode we can't guarantee a single face
2066 * can be de-selected, so the simple "hide" implementation just de-selects vertices
2067 * (in vertex selection mode), and we have to accept this operation can't represent
2068 * the face selection state we would like. ... or do we?
2069 *
2070 * De-selecting vertices that aren't at the island boundaries will often
2071 * de-select the faces we would need to hide the island *without* touching the other islands.
2072 * While it's not a guarantee for all topologies, it often works (quad-grids work for e.g.).
2073 *
2074 * Take the following example:
2075 *
2076 * \code{.unparsed}
2077 * A B
2078 * +---+---+ +---+---+
2079 * | | | | x | |
2080 * +---+---+ +---+---+
2081 * | | | | x | |
2082 * +---+---+ +---+---+
2083 * L1 L2
2084 * \endcode
2085 *
2086 * Consider:
2087 * - "A" is a selected island to be hidden.
2088 * - "B" is an unselected island.
2089 * - Loops L1 and L2 share underlying vertices (the mesh is a grid of 8 connected quads).
2090 *
2091 * De-selecting all "A" vertices would hide faces on "B" (marked with an "x").
2092 *
2093 * In vertex selection mode it is only necessary to de-select the center vertex of island A,
2094 * which de-selects all faces that make the "A" UV island,
2095 * causing it to be hidden and "B" to be left untouched.
2096 *
2097 * The multi-pass hide does this:
2098 * - First it only de-selects verts where every visible connected face is selected (i.e. verts
2099 * interior to the region being hidden, not on a boundary with another visible island).
2100 * - The second pass uses the simple hide-all-connected-vertices.
2101 *
2102 * This works because the second pass only de-selects vertices still connected to visible faces,
2103 * which (if the topology allows), will have become hidden in the first pass.
2104 *
2105 * Note that this explanation uses vertices, the same kind of problem applies to edge-selection.
2106 * Where the solution for hiding "A" is to de-select the edges connected to the center vertex.
2107 */
2108
2109 /* Use multi-pass for vertex & edge selection only. */
2110 BMIter iter, liter;
2111 const int pass_end = (em->selectmode == SCE_SELECT_FACE) ? 0 : 1;
2112 for (int pass = 0; pass <= pass_end; pass++) {
2113 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2114 if (!uvedit_face_visible_test(scene, efa) || !bm_face_is_any_sel(ts, bm, efa, !swap)) {
2115 continue;
2116 }
2117
2118 if (em->selectmode & SCE_SELECT_VERTEX) {
2119 if (use_face_center) {
2120 if (bm_face_is_all_uv_sel(ts, bm, efa, true) == !swap) {
2121 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2122 /* For both edge & vertex selection rely on edge selection tests,
2123 * since all vert selection tests are invalid in case of sticky selections. */
2124 if (UV_EDGE_SEL_TEST(ts, bm, l, !swap)) {
2125 changed |= bm_vert_deselect_for_pass_fn(l->v, pass);
2126 }
2127 }
2128 }
2129 }
2130 else {
2131 if (ts->uv_selectmode == UV_SELECT_EDGE) {
2132 bool e_select_prev = UV_EDGE_SEL_TEST(ts, bm, BM_FACE_FIRST_LOOP(efa)->prev, !swap);
2133 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2134 const bool e_select_curr = UV_EDGE_SEL_TEST(ts, bm, l, !swap);
2135 if (e_select_prev || e_select_curr) {
2136 changed |= bm_vert_deselect_for_pass_fn(l->v, pass);
2137 }
2138 e_select_prev = e_select_curr;
2139 }
2140 }
2141 else {
2142 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2143 if (UV_VERT_SEL_TEST(ts, bm, l, !swap)) {
2144 changed |= bm_vert_deselect_for_pass_fn(l->v, pass);
2145 }
2146 }
2147 }
2148 }
2149 if (pass == pass_end) {
2150 if (!swap) {
2151 uvedit_face_select_disable(scene, bm, efa);
2152 changed_uv = true;
2153 }
2154 }
2155 }
2156 else if (em->selectmode & SCE_SELECT_EDGE) {
2157 if (use_face_center) {
2158 if (bm_face_is_all_uv_sel(ts, bm, efa, true) == !swap) {
2159 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2160 if (UV_EDGE_SEL_TEST(ts, bm, l, !swap)) {
2161 changed |= bm_edge_deselect_for_pass_fn(l->e, pass);
2162 }
2163 }
2164 }
2165 }
2166 else {
2167 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2168 if (ts->uv_selectmode == UV_SELECT_EDGE) {
2169 if (UV_EDGE_SEL_TEST(ts, bm, l, !swap)) {
2170 changed |= bm_edge_deselect_for_pass_fn(l->e, pass);
2171 }
2172 }
2173 else {
2174 if (UV_VERT_SEL_TEST(ts, bm, l, !swap)) {
2175 changed |= bm_edge_deselect_for_pass_fn(l->e, pass);
2176 }
2177 }
2178 }
2179 }
2180 if (pass == pass_end) {
2181 if (!swap) {
2182 uvedit_face_select_disable(scene, bm, efa);
2183 changed_uv = true;
2184 }
2185 }
2186 }
2187 else { /* `em->selectmode == SCE_SELECT_FACE` */
2188 if (use_face_center) {
2189 /* Deselect BMesh face if UV face is (de)selected depending on #swap. */
2190 if (bm_face_is_all_uv_sel(ts, bm, efa, !swap)) {
2191 BM_face_select_set(bm, efa, false);
2192 changed = true;
2193 }
2194 }
2195 else {
2196 /* Deselect BMesh face depending on the type of UV select-mode
2197 * and the type of UV element being considered. */
2198 const bool has_select = (ts->uv_selectmode == UV_SELECT_EDGE) ?
2199 bm_face_is_any_edge_sel(ts, bm, efa, !swap) :
2200 bm_face_is_any_vert_sel(ts, bm, efa, !swap);
2201 if (has_select) {
2202 BM_face_select_set(bm, efa, false);
2203 changed = true;
2204 }
2205 }
2206 uvedit_face_select_disable(scene, bm, efa);
2207 changed_uv = true;
2208 }
2209 }
2210 /* End face loop. */
2211
2212 if (changed) {
2213 /* Flush edit-mesh selections to ensure valid selection states.
2214 * In face-select mode, rely on #ID_RECALC_SELECT to flush. */
2216 /* NOTE: Make sure correct flags are used.
2217 * Previously this was done by passing `SCE_SELECT_VERTEX | SCE_SELECT_EDGE`,
2218 * which doesn't work now that we support proper UV edge selection. */
2220 }
2221 }
2222 }
2223
2224 if (changed) {
2226 }
2227
2228 /* The UV selection may change without changing the mesh selection, tag for either. */
2229 if (changed || changed_uv) {
2232 }
2233
2234 return changed || changed_uv;
2235}
2236
2238{
2239 const Main *bmain = CTX_data_main(C);
2240 ViewLayer *view_layer = CTX_data_view_layer(C);
2241 Scene *scene = CTX_data_scene(C);
2242 const ToolSettings *ts = scene->toolsettings;
2243 const bool swap = RNA_boolean_get(op->ptr, "unselected");
2244
2246 *bmain, scene, view_layer, nullptr);
2247
2248 for (Object *ob : objects) {
2250
2251 if (ts->uv_flag & UV_FLAG_SELECT_SYNC) {
2252 uv_mesh_hide_select_sync(ts, ob, em, swap);
2253 }
2254 else {
2255 uv_mesh_hide_no_sync(scene, ob, em, swap);
2256 }
2257 }
2258
2259 return OPERATOR_FINISHED;
2260}
2261
2262#undef UV_VERT_SEL_TEST
2263#undef UV_EDGE_SEL_TEST
2264
2266{
2267 /* identifiers */
2268 ot->name = "Hide Selected";
2269 ot->description = "Hide (un)selected UV vertices";
2270 ot->idname = "UV_OT_hide";
2272
2273 /* API callbacks. */
2274 ot->exec = uv_hide_exec;
2276
2277 /* props */
2279 ot->srna, "unselected", false, "Unselected", "Hide unselected rather than selected");
2280}
2281
2283
2284/* -------------------------------------------------------------------- */
2287
2289{
2290 const Main *bmain = CTX_data_main(C);
2291 ViewLayer *view_layer = CTX_data_view_layer(C);
2292 Scene *scene = CTX_data_scene(C);
2293 const ToolSettings *ts = scene->toolsettings;
2294
2295 const bool use_face_center = (ts->uv_selectmode == UV_SELECT_FACE);
2296 const bool select = RNA_boolean_get(op->ptr, "select");
2297
2299 *bmain, scene, view_layer, nullptr);
2300
2301 for (Object *ob : objects) {
2304 BMFace *efa;
2305 BMLoop *l;
2306 BMIter iter, liter;
2307
2308 /* NOTE: Selecting faces is delayed so that it doesn't select verts/edges and confuse certain
2309 * UV selection checks.
2310 * This creates a temporary state which breaks certain UV selection functions that do face
2311 * visibility checks internally. Current implementation handles each case separately. */
2312
2313 /* call the mesh function if we are in mesh sync sel */
2314 if (ts->uv_flag & UV_FLAG_SELECT_SYNC) {
2315 if (EDBM_mesh_reveal(bm, em->selectmode, select)) {
2316 Mesh *mesh = id_cast<Mesh *>(ob->data);
2317 EDBMUpdate_Params params = {0};
2318 params.calc_looptris = true;
2319 params.calc_normals = false;
2320 params.is_destructive = false;
2321 EDBM_update(mesh, &params);
2322 }
2323 continue;
2324 }
2325
2326 /* NOTE(@sidd017): Supporting selections in all cases is quite difficult considering there are
2327 * at least 12 cases to look into (3 mesh select-modes + 4 uv select-modes + sticky modes).
2328 * For now we select all UV faces as sticky disabled to ensure proper UV selection states (vert
2329 * + edge flags) */
2330 if (use_face_center) {
2331 if (em->selectmode == SCE_SELECT_FACE) {
2332 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2335 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2338 }
2340 // BM_face_select_set(bm, efa, true);
2342 }
2343 }
2344 }
2345 else {
2346 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2349 int totsel = 0;
2350 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2351 if (em->selectmode == SCE_SELECT_VERTEX) {
2352 totsel += BM_elem_flag_test(l->v, BM_ELEM_SELECT);
2353 }
2354 else if (em->selectmode == SCE_SELECT_EDGE) {
2355 totsel += BM_elem_flag_test(l->e, BM_ELEM_SELECT);
2356 }
2357 }
2358
2359 if (!totsel) {
2360 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2361
2364 }
2365 }
2367 // BM_face_select_set(bm, efa, true);
2369 }
2370 }
2371 }
2372 }
2373 else if (em->selectmode == SCE_SELECT_FACE) {
2374 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2377 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2380 }
2382 // BM_face_select_set(bm, efa, true);
2384 }
2385 }
2386 }
2387 else {
2388 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2391 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2394 }
2396 // BM_face_select_set(bm, efa, true);
2398 }
2399 }
2400 }
2401
2402 /* re-select tagged faces */
2404
2407 }
2408
2409 return OPERATOR_FINISHED;
2410}
2411
2413{
2414 /* identifiers */
2415 ot->name = "Reveal Hidden";
2416 ot->description = "Reveal all hidden UV vertices";
2417 ot->idname = "UV_OT_reveal";
2419
2420 /* API callbacks. */
2421 ot->exec = uv_reveal_exec;
2423
2424 RNA_def_boolean(ot->srna, "select", true, "Select", "");
2425}
2426
2428
2429/* -------------------------------------------------------------------- */
2432
2434{
2436
2437 if (!sima) {
2438 return OPERATOR_CANCELLED;
2439 }
2440
2441 RNA_float_get_array(op->ptr, "location", sima->cursor);
2442
2443 {
2444 wmMsgBus *mbus = CTX_wm_message_bus(C);
2445 bScreen *screen = CTX_wm_screen(C);
2446 WM_msg_publish_rna_prop(mbus, &screen->id, sima, SpaceImageEditor, cursor_location);
2447 }
2448
2450
2451 /* Use pass-through to allow click-drag to transform the cursor. */
2453}
2454
2456{
2457 ARegion *region = CTX_wm_region(C);
2458 float location[2];
2459
2460 if (region->regiontype == RGN_TYPE_WINDOW) {
2462 if (sima && ED_space_image_show_cache_and_mval_over(sima, region, event->mval)) {
2463 return OPERATOR_PASS_THROUGH;
2464 }
2465 }
2466
2468 &region->v2d, event->mval[0], event->mval[1], &location[0], &location[1]);
2469 RNA_float_set_array(op->ptr, "location", location);
2470
2471 return uv_set_2d_cursor_exec(C, op);
2472}
2473
2475{
2476 /* identifiers */
2477 ot->name = "Set 2D Cursor";
2478 ot->description = "Set 2D cursor location";
2479 ot->idname = "UV_OT_cursor_set";
2480
2481 /* API callbacks. */
2485
2486 /* properties */
2488 "location",
2489 2,
2490 nullptr,
2491 -FLT_MAX,
2492 FLT_MAX,
2493 "Location",
2494 "Cursor location in normalized (0.0 to 1.0) coordinates",
2495 -10.0f,
2496 10.0f);
2497}
2498
2500
2501/* -------------------------------------------------------------------- */
2504
2506{
2507 const Main *bmain = CTX_data_main(C);
2508 Scene *scene = CTX_data_scene(C);
2509 ViewLayer *view_layer = CTX_data_view_layer(C);
2510 const bool mark_seams = RNA_boolean_get(op->ptr, "mark_seams");
2511 const bool mark_sharp = RNA_boolean_get(op->ptr, "mark_sharp");
2512 bool changed_multi = false;
2513
2515 *bmain, scene, view_layer, nullptr);
2516
2517 for (Object *ob : objects) {
2518 Mesh *mesh = id_cast<Mesh *>(ob->data);
2520 BMIter iter;
2521
2522 if (!EDBM_uv_check(mesh)) {
2523 continue;
2524 }
2525
2526 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
2527 bool changed = false;
2528
2529 BMFace *f;
2530 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
2531 if (!uvedit_face_visible_test(scene, f)) {
2532 continue;
2533 }
2534
2535 BMLoop *l_iter;
2536 BMLoop *l_first;
2537
2538 l_iter = l_first = BM_FACE_FIRST_LOOP(f);
2539 do {
2540 if (l_iter == l_iter->radial_next) {
2541 continue;
2542 }
2543 if (!uvedit_edge_select_test(scene, bm, l_iter, offsets)) {
2544 continue;
2545 }
2546
2547 bool mark = false;
2548 BMLoop *l_other = l_iter->radial_next;
2549 do {
2550 if (!BM_loop_uv_share_edge_check(l_iter, l_other, offsets.uv)) {
2551 mark = true;
2552 break;
2553 }
2554 } while ((l_other = l_other->radial_next) != l_iter);
2555
2556 if (mark) {
2557 if (mark_seams) {
2559 }
2560 if (mark_sharp) {
2562 }
2563 changed = true;
2564 }
2565 } while ((l_iter = l_iter->next) != l_first);
2566 }
2567
2568 if (changed) {
2569 changed_multi = true;
2570 DEG_id_tag_update(&mesh->id, 0);
2572 }
2573 }
2574
2575 return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
2576}
2577
2579{
2580 /* identifiers */
2581 ot->name = "Seams from Islands";
2582 ot->description = "Set mesh seams according to island setup in the UV editor";
2583 ot->idname = "UV_OT_seams_from_islands";
2584
2585 /* flags */
2587
2588 /* API callbacks. */
2591
2592 RNA_def_boolean(ot->srna, "mark_seams", true, "Mark Seams", "Mark boundary edges as seams");
2593 RNA_def_boolean(ot->srna, "mark_sharp", false, "Mark Sharp", "Mark boundary edges as sharp");
2594}
2595
2597
2598/* -------------------------------------------------------------------- */
2601
2603{
2604 const Main *bmain = CTX_data_main(C);
2605 Scene *scene = CTX_data_scene(C);
2606 ViewLayer *view_layer = CTX_data_view_layer(C);
2607 const ToolSettings *ts = scene->toolsettings;
2608
2609 BMFace *efa;
2610 BMLoop *loop;
2611 BMIter iter, liter;
2612
2613 const bool flag_set = !RNA_boolean_get(op->ptr, "clear");
2614 const bool synced_selection = (ts->uv_flag & UV_FLAG_SELECT_SYNC) != 0;
2615
2617 *bmain, scene, view_layer, nullptr);
2618
2619 bool changed = false;
2620
2621 for (Object *ob : objects) {
2622 Mesh *mesh = id_cast<Mesh *>(ob->data);
2624
2625 if (synced_selection && (bm->totedgesel == 0)) {
2626 continue;
2627 }
2628
2629 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
2630
2631 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2632 if (uvedit_face_visible_test(scene, efa)) {
2633 BM_ITER_ELEM (loop, &liter, efa, BM_LOOPS_OF_FACE) {
2634 if (uvedit_edge_select_test(scene, bm, loop, offsets)) {
2635 BM_elem_flag_set(loop->e, BM_ELEM_SEAM, flag_set);
2636 changed = true;
2637 }
2638 }
2639 }
2640 }
2641
2642 if (changed) {
2643 DEG_id_tag_update(&mesh->id, 0);
2645 }
2646 }
2647
2648 if (changed) {
2649 ED_uvedit_live_unwrap(scene, objects);
2650 }
2651
2652 return OPERATOR_FINISHED;
2653}
2654
2656{
2657 if (RNA_struct_property_is_set(op->ptr, "clear")) {
2658 return uv_mark_seam_exec(C, op);
2659 }
2660
2661 ui::PopupMenu *pup = ui::popup_menu_begin(C, IFACE_("Edges"), ICON_NONE);
2662 ui::Layout &layout = *popup_menu_layout(pup);
2663
2665 PointerRNA op_ptr = layout.op(
2666 op->type->idname, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Mark Seam"), ICON_NONE);
2667 RNA_boolean_set(&op_ptr, "clear", false);
2668 op_ptr = layout.op(
2669 op->type->idname, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Clear Seam"), ICON_NONE);
2670 RNA_boolean_set(&op_ptr, "clear", true);
2671
2672 popup_menu_end(C, pup);
2673
2674 return OPERATOR_INTERFACE;
2675}
2676
2678{
2679 /* identifiers */
2680 ot->name = "Mark Seam";
2681 ot->description = "Mark selected UV edges as seams";
2682 ot->idname = "UV_OT_mark_seam";
2683
2684 /* flags */
2686
2687 /* API callbacks. */
2688 ot->exec = uv_mark_seam_exec;
2691
2692 RNA_def_boolean(ot->srna, "clear", false, "Clear Seams", "Clear instead of marking seams");
2693}
2694
2695static bool uv_copy_mirrored_faces(const Scene *scene,
2696 BMesh *bm,
2697 const int mesh_axis,
2698 const int uv_axis,
2699 const int precision,
2700 int *r_double_warn)
2701{
2702 *r_double_warn = 0;
2703 const float precision_scale = powf(10.0f, precision);
2704 /* TODO: replace mirror look-ups with #EditMeshSymmetryHelper. */
2705 Map<float3, BMVert *> mirror_gt, mirror_lt;
2707
2708 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
2709 BLI_assert(offsets.uv != -1);
2710 UNUSED_VARS_NDEBUG(offsets);
2711 BMVert *v;
2712 BMIter iter;
2713
2714 const int axis = mesh_axis % 3;
2715 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
2716 float3 pos = math::round(float3(v->co) * precision_scale);
2717 if (pos[axis] >= 0.0f) {
2718 if (!mirror_gt.add_overwrite(pos, v)) {
2719 (*r_double_warn)++;
2720 }
2721 }
2722 if (pos[axis] <= 0.0f) {
2723 if (!mirror_lt.add_overwrite(pos, v)) {
2724 (*r_double_warn)++;
2725 }
2726 }
2727 }
2728
2729 for (const auto &[pos, v] : mirror_gt.items()) {
2730 float3 mirror_pos = pos;
2731 mirror_pos[axis] = -mirror_pos[axis];
2732 BMVert *v_mirror = mirror_lt.lookup_default(mirror_pos, nullptr);
2733 if (v_mirror) {
2734 vmap.add(v, v_mirror);
2735 }
2736 }
2737 for (const auto &[pos, v] : mirror_lt.items()) {
2738 float3 mirror_pos = pos;
2739 mirror_pos[axis] = -mirror_pos[axis];
2740 BMVert *v_mirror = mirror_gt.lookup_default(mirror_pos, nullptr);
2741 if (v_mirror) {
2742 vmap.add(v, v_mirror);
2743 }
2744 }
2745
2746 Map<Array<BMVert *>, BMFace *> sorted_verts_to_face;
2747 /* Maps faces to their corresponding mirrored face. */
2748 Map<BMFace *, BMFace *> face_map;
2749
2750 BMFace *f;
2751 BMIter iter_face;
2752 BM_ITER_MESH (f, &iter_face, bm, BM_FACES_OF_MESH) {
2753 Array<BMVert *> sorted_verts(f->len);
2754 bool valid = true;
2755 int loop_index = 0;
2756 BMLoop *l;
2757 BMIter liter;
2758 BM_ITER_ELEM_INDEX (l, &liter, f, BM_LOOPS_OF_FACE, loop_index) {
2759 if (!vmap.contains(l->v)) {
2760 valid = false;
2761 break;
2762 }
2763 sorted_verts[loop_index] = l->v;
2764 }
2765 if (valid) {
2766 std::ranges::sort(sorted_verts);
2767 sorted_verts_to_face.add(std::move(sorted_verts), f);
2768 }
2769 }
2770
2771 for (const auto &[sorted_verts, f_dst] : sorted_verts_to_face.items()) {
2772 Array<BMVert *> mirror_verts(sorted_verts.size());
2773 for (int index = 0; index < sorted_verts.size(); index++) {
2774 mirror_verts[index] = vmap.lookup_default(sorted_verts[index], nullptr);
2775 }
2776 std::ranges::sort(mirror_verts);
2777 BMFace *f_src = sorted_verts_to_face.lookup_default(mirror_verts, nullptr);
2778 if (f_src) {
2779 if (f_src != f_dst) {
2780 face_map.add(f_dst, f_src);
2781 }
2782 }
2783 }
2784
2785 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_PROP_FLOAT2);
2786
2787 bool changed = false;
2788 const int other_uv_axis = 1 - uv_axis;
2789 const bool is_negative = mesh_axis >= OB_NEGX;
2790 for (const auto &[f_dst, f_src] : face_map.items()) {
2791
2792 /* Skip unless both faces have all their UVs selected. */
2793 if (!uvedit_face_visible_test(scene, f_dst) || !uvedit_face_select_test(scene, bm, f_dst)) {
2794 continue;
2795 }
2796 if (!uvedit_face_visible_test(scene, f_src) || !uvedit_face_select_test(scene, bm, f_src)) {
2797 continue;
2798 }
2799
2800 {
2801 float f_dst_center[3];
2802 BM_face_calc_center_median(f_dst, f_dst_center);
2803 if (is_negative ? (f_dst_center[axis] > 0.0f) : (f_dst_center[axis] < 0.0f)) {
2804 continue;
2805 }
2806 }
2807
2808 BMIter liter;
2809 BMLoop *l_dst;
2810
2811 BM_ITER_ELEM (l_dst, &liter, f_dst, BM_LOOPS_OF_FACE) {
2812 BMVert *v_src = vmap.lookup_default(l_dst->v, nullptr);
2813 if (!v_src) {
2814 continue;
2815 }
2816
2817 BMLoop *l_src = BM_face_vert_share_loop(f_src, v_src);
2818 if (!l_src) {
2819 continue;
2820 }
2821 const float *uv_src = BM_ELEM_CD_GET_FLOAT_P(l_src, cd_loop_uv_offset);
2822 float *uv_dst = BM_ELEM_CD_GET_FLOAT_P(l_dst, cd_loop_uv_offset);
2823
2824 uv_dst[uv_axis] = -(uv_src[uv_axis] - 0.5f) + 0.5f;
2825 uv_dst[other_uv_axis] = uv_src[other_uv_axis];
2826 changed = true;
2827 }
2828 }
2829
2830 return changed;
2831}
2832
2834{
2835 const Main *bmain = CTX_data_main(C);
2836 Scene *scene = CTX_data_scene(C);
2837 ViewLayer *view_layer = CTX_data_view_layer(C);
2839 *bmain, scene, view_layer, nullptr);
2840 const int precision = RNA_int_get(op->ptr, "precision");
2841
2842 const int mesh_axis = RNA_enum_get(op->ptr, "mesh_axis");
2843 const int uv_axis = RNA_enum_get(op->ptr, "uv_axis");
2844
2845 int total_duplicates = 0;
2846 int meshes_with_duplicates = 0;
2847
2848 for (Object *obedit : objects) {
2850
2851 int double_warn = 0;
2852
2853 bool changed = uv_copy_mirrored_faces(scene, bm, mesh_axis, uv_axis, precision, &double_warn);
2854
2855 if (double_warn) {
2856 total_duplicates += double_warn;
2857 meshes_with_duplicates++;
2858 }
2859
2860 if (changed) {
2861 DEG_id_tag_update(obedit->data, 0);
2862 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
2863 }
2864 }
2865
2866 if (total_duplicates) {
2867 BKE_reportf(op->reports,
2869 "%d duplicates found in %d mesh(es), mirror may be incomplete",
2870 total_duplicates,
2871 meshes_with_duplicates);
2872 }
2873
2874 return OPERATOR_FINISHED;
2875}
2877{
2878 ot->name = "Copy Mirrored UV Coords";
2879 ot->description = "Copy mirror UV coordinates based on a mirrored mesh";
2880 ot->idname = "UV_OT_copy_mirrored_faces";
2881
2884
2886
2887 RNA_def_enum(ot->srna,
2888 "mesh_axis",
2890 OB_POSX,
2891 "Mesh Axis",
2892 "Mirror vertices based on mesh axis");
2894 ot->srna, "uv_axis", rna_enum_axis_xy_items, 0, "UV Axis", "Axis to mirror UV coordinates");
2895 RNA_def_int(ot->srna,
2896 "precision",
2897 3,
2898 1,
2899 16,
2900 "Precision",
2901 "Tolerance for finding vertex duplicates",
2902 1,
2903 16);
2904}
2905
2907
2908/* -------------------------------------------------------------------- */
2911
2913{
2914 /* `uvedit_select.cc` */
2933
2935
2938
2941
2946
2952
2963
2968
2972}
2973
2975{
2976 wmOperatorType *ot;
2977 wmOperatorTypeMacro *otmacro;
2978
2979 ot = WM_operatortype_append_macro("UV_OT_rip_move",
2980 "Rip Move UVs",
2981 "Unstitch UVs and move the result",
2984 WM_operatortype_macro_define(ot, "UV_OT_rip");
2985 otmacro = WM_operatortype_macro_define(ot, "TRANSFORM_OT_translate");
2986 RNA_boolean_set(otmacro->ptr, "use_proportional_edit", false);
2987 RNA_boolean_set(otmacro->ptr, "mirror", false);
2988}
2989
2991{
2992 wmKeyMap *keymap;
2993
2994 keymap = WM_keymap_ensure(keyconf, "UV Editor", SPACE_EMPTY, RGN_TYPE_WINDOW);
2995 keymap->poll = ED_operator_uvedit;
2996}
2997
2999
3000} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
General operations, lookup, etc. for materials.
#define SH_NODE_TEX_IMAGE
#define SH_NODE_TEX_ENVIRONMENT
A KD-tree for nearest neighbor search.
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define Y
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define ND_SPACE_IMAGE
Definition WM_types.hh:565
#define ND_SELECT
Definition WM_types.hh:551
#define NC_SPACE
Definition WM_types.hh:405
#define INIT_MINMAX2(min, max)
#define UNUSED_VARS_NDEBUG(...)
#define ELEM(...)
#define STREQ(a, b)
#define BM_ELEM_CD_GET_BOOL(ele, offset)
#define BM_ELEM_SELECT_UV_EDGE
#define BM_FACE_FIRST_LOOP(p)
#define BM_ELEM_CD_GET_FLOAT_P(ele, offset)
#define BM_ELEM_CD_SET_BOOL(ele, offset, f)
#define BM_elem_flag_disable(ele, hflag)
#define BM_elem_flag_set(ele, hflag, val)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_test_bool(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_ELEM_INDEX(ele, iter, data, itype, indexvar)
#define BM_elem_hide_set(bm, ele, hide)
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
const T * end() const
Definition BLI_array.hh:336
const T * begin() const
Definition BLI_array.hh:332
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:325
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
bool contains(const Key &key) const
Definition BLI_map.hh:353
ItemIterator items() const &
Definition BLI_map.hh:902
int64_t size() const
void append(const T &value)
IndexRange index_range() const
void reserve(const int64_t min_capacity)
#define powf(x, y)
#define roundf(x)
#define FLT_MAX
#define INT_MAX
#define position
float distance(float, float) RET
float2x2 invert(float2x2 mat)
bool is_negative(float3x3 mat)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ENUM_OPERATORS(Layer::Flags)
bNode * node_get_active_texture(bNodeTree &ntree)
T square(const T &a)
T distance_squared(const VecBase< T, Size > &a, const VecBase< T, Size > &b)
T round(const T &a)
PopupMenu * popup_menu_begin(bContext *C, const char *title, int icon) ATTR_NONNULL()
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
BMesh Iterators.
void UV_OT_paste(wmOperatorType *ot)
static wmOperatorStatus uv_move_on_axis_exec(bContext *C, wmOperator *op)
void WM_main_add_notifier(uint type, void *reference)
void EDBM_flag_disable_all(BMesh *bm, char hflag)
void BM_face_calc_center_median(const BMFace *f, float r_cent[3])
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
void RNA_float_set_array(PointerRNA *ptr, const char *name, const float *values)
void uvedit_live_unwrap_update(SpaceImage *sima, Scene *scene, Object *obedit)
void kdtree_balance(KDTree< CoordT > *tree)
bool BM_uv_map_attr_pin_exists(const BMesh *bm, const StringRef uv_map_name)
static void UV_OT_cursor_set(wmOperatorType *ot)
bool ED_operator_uvedit(bContext *C)
void UV_OT_minimize_stretch(wmOperatorType *ot)
static wmOperatorStatus uv_set_2d_cursor_invoke(bContext *C, wmOperator *op, const wmEvent *event)
UVAlignPositionMode
void UV_OT_custom_region_set(wmOperatorType *ot)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void UV_OT_project_from_view(wmOperatorType *ot)
bool uvedit_edge_select_test(const Scene *scene, const BMesh *bm, const BMLoop *l, const BMUVOffsets &offsets)
@ RPT_WARNING
Definition BKE_report.hh:40
static wmOperatorStatus uv_arrange_islands_exec(bContext *C, wmOperator *op)
static void uv_weld(bContext *C)
void EDBM_selectmode_flush(BMesh *bm, short selectmode)
void UV_OT_copy(wmOperatorType *ot)
bool ED_space_image_show_cache_and_mval_over(const SpaceImage *sima, ARegion *region, const int mval[2])
static bool uv_snap_uvs_to_adjacent_unselected(Scene *scene, Object *obedit)
const EnumPropertyItem rna_enum_object_axis_flip_items[]
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void UV_OT_pack_islands(wmOperatorType *ot)
void UV_OT_select_linked(wmOperatorType *ot)
unsigned int uint
Definition BKE_text.h:55
void ED_uvedit_foreach_uv(const Scene *scene, BMesh *bm, const bool skip_invisible, const bool selected, FunctionRef< void(float[2])> user_fn)
void UV_OT_shortest_path_select(wmOperatorType *ot)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
bool uvedit_face_visible_test(const Scene *scene, const BMFace *efa)
static wmOperatorStatus uv_seams_from_islands_exec(bContext *C, wmOperator *op)
MINLINE void mul_v2_fl(float r[2], float f)
static bool uv_snap_cursor_to_selection(Scene *scene, Span< Object * > objects_edit, SpaceImage *sima)
static wmOperatorStatus uv_remove_doubles_exec(bContext *C, wmOperator *op)
static wmOperatorStatus uv_set_2d_cursor_exec(bContext *C, wmOperator *op)
const char * RNA_property_identifier(const PropertyRNA *prop)
MINLINE void copy_v2_v2(float r[2], const float a[2])
Material * BKE_object_material_get(Object *ob, short act)
static bool bm_face_is_any_edge_sel(const ToolSettings *ts, const BMesh *bm, BMFace *f, bool select_test)
void EDBM_update(Mesh *mesh, const EDBMUpdate_Params *params)
void uvedit_face_select_disable(const Scene *scene, BMesh *bm, BMFace *efa)
void uvedit_vert_select_set_no_sync(const ToolSettings *ts, const BMesh *bm, BMLoop *l, bool select)
const EnumPropertyItem rna_enum_axis_xy_items[]
bool uvedit_select_is_any_selected_multi(const Scene *scene, Span< Object * > objects)
static wmOperatorStatus uv_snap_cursor_exec(bContext *C, wmOperator *op)
void ED_space_image_get_size(SpaceImage *sima, int *r_width, int *r_height)
static void uv_snap_to_pixel(float uvco[2], float w, float h)
@ V3D_AROUND_CENTER_BOUNDS
@ V3D_AROUND_CENTER_MEDIAN
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
static bool uv_snap_uvs_to_pixels(SpaceImage *sima, Scene *scene, Object *obedit)
void ED_uvedit_foreach_uv_multi(const Scene *scene, Span< Object * > objects_edit, const bool skip_invisible, const bool skip_nonselected, FunctionRef< void(float[2])> user_fn)
void UV_OT_select_similar(wmOperatorType *ot)
void UV_OT_select_linked_pick(wmOperatorType *ot)
wmOperatorTypeMacro * WM_operatortype_macro_define(wmOperatorType *ot, const char *idname)
static void UV_OT_snap_cursor(wmOperatorType *ot)
int RNA_int_get(PointerRNA *ptr, const char *name)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
bool ED_uvedit_minmax_multi(const Scene *scene, Span< Object * > objects_edit, float r_min[2], float r_max[2])
static void uv_align(bContext *C, eUVWeldAlign tool, UVAlignPositionMode position_mode)
ATTR_WARN_UNUSED_RESULT const BMVert * v
static bool uv_mesh_hide_no_sync(const Scene *scene, Object *ob, BMEditMesh *em, bool swap)
static wmOperatorStatus uv_hide_exec(bContext *C, wmOperator *op)
void UV_OT_rip(wmOperatorType *ot)
static bool uvedit_uv_straighten(Scene *scene, BMesh *bm, eUVWeldAlign tool)
void BM_uv_element_map_free(UvElementMap *element_map)
void UV_OT_select_by_winding(wmOperatorType *ot)
void ED_uvedit_live_unwrap_re_solve()
void BM_mesh_select_mode_flush(BMesh *bm)
bool EDBM_uv_check(const Mesh *mesh)
ViewLayer * CTX_data_view_layer(const bContext *C)
static wmOperatorStatus uv_mark_seam_invoke(bContext *C, wmOperator *op, const wmEvent *)
PropertyRNA * RNA_def_float_vector(StructOrFunctionRNA *cont_, const char *identifier, const int len, const float *default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
static void UV_OT_arrange_islands(wmOperatorType *ot)
bool uvedit_uv_select_test(const Scene *scene, const BMesh *bm, const BMLoop *l, const BMUVOffsets &offsets)
static wmOperatorStatus uv_mark_seam_exec(bContext *C, wmOperator *op)
KDTree< CoordT > * kdtree_new(uint nodes_len_capacity)
Definition BLI_kdtree.hh:65
void UV_OT_sphere_project(wmOperatorType *ot)
void ED_uvedit_live_unwrap(const Scene *scene, Span< Object * > objects)
bool ED_operator_editmesh(bContext *C)
static bool uvedit_uv_align_weld(Scene *scene, BMesh *bm, const eUVWeldAlign tool, const float cent[2])
void UV_OT_select_circle(wmOperatorType *ot)
void uvedit_edge_select_set_no_sync(const ToolSettings *ts, const BMesh *bm, BMLoop *l, bool select)
VecBase< float, 2 > float2
MINLINE void add_v2_v2(float r[2], const float a[2])
bool EDBM_mesh_hide(BMesh *bm, const short selectmode, bool swap)
void UV_OT_select_mode(wmOperatorType *ot)
void UV_OT_select_loop(wmOperatorType *ot)
void minmax_v2v2_v2(float min[2], float max[2], const float vec[2])
void UV_OT_select(wmOperatorType *ot)
void DEG_id_tag_update(ID *id, unsigned int flags)
static bool uvedit_line_update_endpoint(const float *luv, const bool pinned, float uv_a[2], eUVEndPointPrecedence *prec_a, float uv_b[2], eUVEndPointPrecedence *prec_b)
void BM_mesh_elem_hflag_enable_test(BMesh *bm, const char htype, const char hflag, const bool respecthide, const bool overwrite, const char hflag_test)
void UV_OT_shortest_path_pick(wmOperatorType *ot)
bool EDBM_mesh_reveal(BMesh *bm, const short selectmode, bool select)
static wmOperatorStatus uv_reveal_exec(bContext *C, wmOperator *op)
void UV_OT_unwrap(wmOperatorType *ot)
static wmOperatorStatus uv_remove_doubles_to_selected(bContext *C, wmOperator *op)
static void UV_OT_pin(wmOperatorType *ot)
void BKE_main_ensure_invariants(Main &bmain, std::optional< Span< ID * > > modified_ids=std::nullopt)
void kdtree_insert(KDTree< CoordT > *tree, int index, const CoordT &co)
Definition BLI_kdtree.hh:96
void BM_face_select_set(BMesh *bm, BMFace *f, const bool select)
Select Face.
static bool uv_snap_uvs_to_cursor(Scene *scene, Object *obedit, const float cursor[2])
void UV_OT_select_box(wmOperatorType *ot)
bool ED_uvedit_center_from_pivot_ex(const Main &bmain, const SpaceImage *sima, Scene *scene, ViewLayer *view_layer, float r_center[2], char mode, bool *r_has_select)
bool uvedit_face_select_test(const Scene *scene, const BMesh *bm, const BMFace *efa)
static bool uv_mesh_hide_select_sync(const ToolSettings *ts, Object *ob, BMEditMesh *em, bool swap)
static wmOperatorStatus uv_align_exec(bContext *C, wmOperator *op)
int kdtree_find_nearest(const KDTree< CoordT > *tree, const CoordT &co, KDTreeNearest< CoordT > *r_nearest)
static bool uv_copy_mirrored_faces(const Scene *scene, BMesh *bm, const int mesh_axis, const int uv_axis, const int precision, int *r_double_warn)
wmKeyMap * WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:935
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
void uvedit_face_select_set_no_sync(const ToolSettings *ts, const BMesh *bm, BMFace *f, bool select)
void ED_operatortypes_uvedit()
static bool bm_face_is_any_vert_sel(const ToolSettings *ts, const BMesh *bm, BMFace *f, bool select_test)
static void UV_OT_align(wmOperatorType *ot)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
MINLINE void mul_v2_v2fl(float r[2], const float a[2], float f)
void UV_OT_select_overlap(wmOperatorType *ot)
bool ED_uvedit_sync_uvselect_ignore(const ToolSettings *ts)
static eUVEndPointPrecedence uvedit_line_update_get_precedence(const bool pinned)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool ED_operator_uvedit_space_image(bContext *C)
wmMsgBus * CTX_wm_message_bus(const bContext *C)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
static void UV_OT_move_on_axis(wmOperatorType *ot)
void BM_vert_select_set(BMesh *bm, BMVert *v, const bool select)
Select Vert.
void ED_uvedit_select_all(const ToolSettings *ts, BMesh *bm)
void ED_uvedit_live_unwrap_end(bool cancel)
void UV_OT_select_edge_ring(wmOperatorType *ot)
void BM_uv_map_attr_pin_ensure_named(BMesh *bm, const StringRef uv_map_name)
static wmOperatorStatus uv_pin_exec(bContext *C, wmOperator *op)
static bool uvedit_uv_islands_arrange(const Scene *scene, BMesh *bm, const UVAlignIslandAxis axis, const UVAlignIslandMode align, const UVAlignIslandOrder order, const float margin, float2 &position)
void BM_select_history_validate(BMesh *bm)
void UV_OT_select_pinned(wmOperatorType *ot)
eUVEndPointPrecedence
@ UVEP_SELECTED
static wmOperatorStatus uv_weld_exec(bContext *C, wmOperator *)
void UV_OT_select_tile(wmOperatorType *ot)
BMEditMesh * BKE_editmesh_from_object(Object *ob)
Return the BMEditMesh for a given object.
Definition editmesh.cc:59
void UV_OT_cube_project(wmOperatorType *ot)
void ED_keymap_uvedit(wmKeyConfig *keyconf)
static bool uv_snap_uvs_offset(Scene *scene, Object *obedit, const float offset[2])
SpaceImage * CTX_wm_space_image(const bContext *C)
void UV_OT_average_islands_scale(wmOperatorType *ot)
void UV_OT_select_more(wmOperatorType *ot)
void kdtree_free(KDTree< CoordT > *tree)
Definition BLI_kdtree.hh:84
void UV_OT_cylinder_project(wmOperatorType *ot)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
static bool uv_align_poll_property(const bContext *, wmOperator *op, const PropertyRNA *prop)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
wmOperatorType * WM_operatortype_append_macro(const char *idname, const char *name, const char *description, int flag)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
static bool bm_face_is_any_sel(const ToolSettings *ts, const BMesh *bm, BMFace *f, bool select_test)
@ WM_CURSOR_CROSS
Definition wm_cursors.hh:28
void UV_OT_select_all(wmOperatorType *ot)
void ED_operatormacros_uvedit()
Main * CTX_data_main(const bContext *C)
static void UV_OT_snap_selected(wmOperatorType *ot)
UvElementMap * BM_uv_element_map_create(BMesh *bm, const Scene *scene, bool uv_selected, bool use_winding, bool use_seams, bool do_islands)
static void UV_OT_remove_doubles(wmOperatorType *ot)
Material * BKE_object_material_get_eval(Object *ob, short act)
static void UV_OT_seams_from_islands(wmOperatorType *ot)
void mid_v2_v2v2(float r[2], const float a[2], const float b[2])
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
void ED_uvedit_live_unwrap_begin(Scene *scene, Object *obedit, struct wmWindow *win_modal)
UVAlignInitialPosition
int kdtree_calc_duplicates_fast(const KDTree< CoordT > *tree, const typename KDTree< CoordT >::ValueType range, const bool use_index_order, int *duplicates)
void UV_OT_reset(wmOperatorType *ot)
const BMesh * BKE_editmesh_bmesh_get(const Object *ob)
Definition editmesh.cc:65
bool BM_loop_uv_share_edge_check(const BMLoop *l_a, const BMLoop *l_b, const int cd_loop_uv_offset)
bool EDBM_uvselect_clear(BMesh *bm)
BMLoop * BM_face_vert_share_loop(BMFace *f, BMVert *v)
Return the Loop Shared by Face and Vertex.
bool ED_uvedit_test(Object *obedit)
Definition uvedit_ops.cc:74
Scene * CTX_data_scene(const bContext *C)
bScreen * CTX_wm_screen(const bContext *C)
static void UV_OT_mark_seam(wmOperatorType *ot)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
bool ED_uvedit_center_multi(const Scene *scene, Span< Object * > objects_edit, float r_cent[2], char mode)
static void uv_snap_cursor_to_origin(float uvco[2])
bool ED_space_image_cursor_poll(bContext *C)
static void UV_OT_reveal(wmOperatorType *ot)
BMUVOffsets BM_uv_map_offsets_get(const BMesh *bm)
VecBase< float, 3 > float3
static wmOperatorStatus uv_copy_mirrored_faces_exec(bContext *C, wmOperator *op)
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
ATTR_WARN_UNUSED_RESULT const void * element
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void UV_OT_copy_mirrored_faces(wmOperatorType *ot)
static wmOperatorStatus uv_remove_doubles_to_selected_shared_vertex(bContext *C, wmOperator *op)
static void UV_OT_hide(wmOperatorType *ot)
bool DEG_is_evaluated(const T *id)
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
static bool uvedit_uv_straighten_elements(const UvElement *element, const int len, const BMUVOffsets &offsets, const eUVWeldAlign tool)
static bool is_image_texture_node(bNode *node)
Definition uvedit_ops.cc:93
float closest_to_line_segment_v2(float r_close[2], const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:381
void UV_OT_select_lasso(wmOperatorType *ot)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
void ED_object_assign_active_image(Main *bmain, Object *ob, int mat_nr, Image *ima)
@ BM_ELEM_SELECT_UV
MINLINE void zero_v2(float r[2])
ARegion * CTX_wm_region(const bContext *C)
static wmOperatorStatus uv_remove_doubles_to_unselected(bContext *C, wmOperator *op)
void UV_OT_select_split(wmOperatorType *ot)
static wmOperatorStatus uv_snap_selection_exec(bContext *C, wmOperator *op)
float RNA_float_get(PointerRNA *ptr, const char *name)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void UV_OT_smart_project(wmOperatorType *ot)
void RNA_float_get_array(PointerRNA *ptr, const char *name, float *values)
static bool bm_face_is_all_uv_sel(const ToolSettings *ts, const BMesh *bm, BMFace *f, bool select_test)
@ UV_STRAIGHTEN_Y
@ UV_STRAIGHTEN
@ UV_ALIGN_AUTO
@ UV_STRAIGHTEN_X
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static bool uvedit_median_multi(const Scene *scene, const Span< Object * > objects_edit, float co[2])
void UV_OT_stitch(wmOperatorType *ot)
static void UV_OT_weld(wmOperatorType *ot)
void UV_OT_select_less(wmOperatorType *ot)
bool ED_object_get_active_image(Object *ob, int mat_nr, Image **r_ima, ImageUser **r_iuser, const bNode **r_node, const bNodeTree **r_ntree)
Definition uvedit_ops.cc:98
static void uv_snap_cursor_to_pixels(SpaceImage *sima)
void BM_edge_select_set(BMesh *bm, BMEdge *e, const bool select)
Select Edge.
#define X(TYPE)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define swap(a, b)
Definition sort.cc:61
#define min(a, b)
Definition sort.cc:38
eRegion_Type regiontype
struct BMLoop * next
struct BMEdge * e
struct BMVert * v
struct BMLoop * radial_next
struct BMFace * f
CustomData ldata
bool uv_select_sync_valid
struct ToolSettings * toolsettings
eSpaceImage_Flag flag
eTool_UvSelectMode uv_selectmode
void operator_context_set(wm::OpCallContext opcontext)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
bool(*) poll(struct bContext *)
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
bool(*) poll_property(const bContext *C, wmOperator *op, const PropertyRNA *prop) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1171
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports
struct wmOperatorType * type
#define UV_VERT_SEL_TEST(ts, bm, l, bool_test)
#define UV_EDGE_SEL_TEST(ts, bm, l, bool_test)
#define WM_msg_publish_rna_prop(mbus, id_, data_, type_, prop_)