Blender V5.3
uvedit_unwrap_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_meshdata_types.h"
16#include "DNA_modifier_types.h"
17#include "DNA_object_types.h"
18#include "DNA_scene_types.h"
19
20#include "BKE_global.hh"
21
22#include "BLI_array.hh"
23#include "BLI_linklist.hh"
24#include "BLI_listbase.hh"
25#include "BLI_math_geom_c.hh"
26#include "BLI_math_matrix_c.hh"
28#include "BLI_math_vector_c.hh"
29#include "BLI_memarena.hh"
30#include "BLI_rect.hh"
31#include "BLI_string_utf8.hh"
32#include "BLI_time.hh"
33#include "BLI_utildefines.hh"
34#include "BLI_vector.hh"
35
36#include "BLT_translation.hh"
37
38#include "BKE_context.hh"
39#include "BKE_customdata.hh"
40#include "BKE_deform.hh"
41#include "BKE_editmesh.hh"
42#include "BKE_image.hh"
43#include "BKE_layer.hh"
44#include "BKE_lib_id.hh"
45#include "BKE_main.hh"
46#include "BKE_mesh.hh"
47#include "BKE_object_types.hh"
48#include "BKE_report.hh"
49#include "BKE_screen.hh"
50#include "BKE_subdiv.hh"
51#include "BKE_subdiv_mesh.hh"
53#include "BKE_uvproject.h"
54
55#include "DEG_depsgraph.hh"
56
57#include "GEO_uv_pack.hh"
59
61#include "UI_resources.hh"
62
63#include "ED_image.hh"
64#include "ED_mesh.hh"
65#include "ED_screen.hh"
66#include "ED_undo.hh"
67#include "ED_uvedit.hh"
68#include "ED_view3d.hh"
69
71
72#include "RNA_access.hh"
73#include "RNA_define.hh"
74
75#include "WM_api.hh"
76#include "WM_types.hh"
77
78#include "uvedit_intern.hh"
79
80namespace blender {
81
85
86/* -------------------------------------------------------------------- */
89
90static bool uvedit_ensure_uvs(Object *obedit)
91{
92 if (ED_uvedit_test(obedit)) {
93 return true;
94 }
96 BMFace *efa;
97 BMIter iter;
98
100 ED_mesh_uv_add(id_cast<Mesh *>(obedit->data), nullptr, true, true, nullptr);
101 }
102
103 /* Happens when there are no faces. */
104 if (!ED_uvedit_test(obedit)) {
105 return false;
106 }
107
108 /* select new UVs (ignore UV_FLAG_SELECT_SYNC in this case) */
109 bm->uv_select_sync_valid = false;
110 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
111 BMIter liter;
112 BMLoop *l;
113
115 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
118 }
119 }
120
121 return true;
122}
123
125
126/* -------------------------------------------------------------------- */
129
131{
133 ot->srna,
134 "correct_aspect",
135 true,
136 "Correct Aspect",
137 "Map UVs taking aspect ratio of the image associated with the material into account");
138}
139
141
142/* -------------------------------------------------------------------- */
145
147{
148 if (!sima) {
149 return; /* Nothing to do. */
150 }
151
152 /* NOTE: Presently, when UDIM grid and tiled image are present together, only active tile for
153 * the tiled image is considered. */
154 const Image *image = sima->image;
155 if (image && image->source == IMA_SRC_TILED) {
156 ImageTile *active_tile = static_cast<ImageTile *>(
157 BLI_findlink(&image->tiles, image->active_tile_index));
158 if (active_tile) {
159 udim_base_offset[0] = (active_tile->tile_number - 1001) % 10;
160 udim_base_offset[1] = (active_tile->tile_number - 1001) / 10;
161 }
162 return;
163 }
164
165 /* TODO: Support storing an active UDIM when there are no tiles present.
166 * Until then, use 2D cursor to find the active tile index for the UDIM grid. */
167 if (uv_coords_isect_udim(sima->image, sima->tile_grid_shape, sima->cursor)) {
168 udim_base_offset[0] = floorf(sima->cursor[0]);
169 udim_base_offset[1] = floorf(sima->cursor[1]);
170 }
171}
172
173
175{
176 if (stop) {
177 return *stop;
178 }
179 return false;
180}
181
182/* -------------------------------------------------------------------- */
185
216
218{
219 only_selected_uvs = options.only_selected_uvs;
220 only_selected_faces = options.only_selected_faces;
221 use_seams = !options.topology_from_uvs || options.topology_from_uvs_use_seams;
222 correct_aspect = options.correct_aspect;
223 pin_unselected = options.pin_unselected;
224}
225
226static void modifier_unwrap_state(Object *obedit,
227 const UnwrapOptions *options,
228 bool *r_use_subsurf)
229{
230 ModifierData *md;
231 bool subsurf = options->use_subsurf;
232
233 md = obedit->modifiers.first();
234
235 /* Subdivision-surface will take the modifier settings
236 * only if modifier is first or right after mirror. */
237 if (subsurf) {
238 if (md && md->type == eModifierType_Subsurf) {
239 const SubsurfModifierData &smd = *reinterpret_cast<const SubsurfModifierData *>(md);
240 if (smd.levels > 0) {
241 /* Skip all calculation for zero subdivision levels, similar to the way the modifier is
242 * disabled in that case. */
243 subsurf = true;
244 }
245 else {
246 subsurf = false;
247 }
248 }
249 else {
250 subsurf = false;
251 }
252 }
253
254 *r_use_subsurf = subsurf;
255}
256
258{
260
261 /* To be set by the upper layer */
262 options.topology_from_uvs = false;
263 options.topology_from_uvs_use_seams = false;
264 options.only_selected_faces = false;
265 options.only_selected_uvs = false;
266 options.pin_unselected = false;
267
268 options.slim.skip_init = false;
269 options.use_original_bounds = false;
270
271 if (ts) {
272 options.method = ts->unwrapper;
273 options.correct_aspect = (ts->uvcalc_flag & UVCALC_NO_ASPECT_CORRECT) == 0;
274 options.fill_holes = (ts->uvcalc_flag & UVCALC_FILLHOLES) != 0;
275 options.use_subsurf = (ts->uvcalc_flag & UVCALC_USESUBSURF) != 0;
276
278 STRNCPY_UTF8(options.weight_group, ts->uvcalc_weight_group);
279 options.slim.weight_influence = ts->uvcalc_weight_factor;
280
281 options.slim.iterations = ts->uvcalc_iterations;
282 options.slim.no_flip = ts->uvcalc_flag & UVCALC_UNWRAP_NO_FLIP;
283 }
284 else {
285 options.method = RNA_enum_get(op->ptr, "method");
286 options.correct_aspect = RNA_boolean_get(op->ptr, "correct_aspect");
287 options.fill_holes = RNA_boolean_get(op->ptr, "fill_holes");
288 options.use_subsurf = RNA_boolean_get(op->ptr, "use_subsurf_data");
289 options.use_original_bounds = RNA_boolean_get(op->ptr, "use_original_bounds");
290
291 options.use_weights = RNA_boolean_get(op->ptr, "use_weights");
292 RNA_string_get(op->ptr, "weight_group", options.weight_group);
293 options.slim.weight_influence = RNA_float_get(op->ptr, "weight_factor");
294
295 options.slim.iterations = RNA_int_get(op->ptr, "iterations");
296 options.slim.no_flip = RNA_boolean_get(op->ptr, "no_flip");
297 }
298
299#ifndef WITH_UV_SLIM
302 if (op) {
303 BKE_report(op->reports, RPT_WARNING, "Built without SLIM, falling back to conformal method");
304 }
305 }
306#endif /* !WITH_UV_SLIM */
307
308 if (options.weight_group[0] == '\0' || options.use_weights == false) {
309 options.slim.weight_influence = 0.0f;
310 }
311
312 options.use_abf = options.method == UVCALC_UNWRAP_METHOD_ANGLE;
314
315 /* SLIM requires hole filling */
316 if (options.use_slim) {
317 options.fill_holes = true;
318 }
319
320 if (ob) {
321 bool use_subsurf_final;
322 modifier_unwrap_state(ob, &options, &use_subsurf_final);
323 options.use_subsurf = use_subsurf_final;
324 }
325
326 return options;
327}
328
329/* Generic sync functions
330 *
331 * NOTE: these could be moved to a generic API.
332 */
333
334template<typename T>
336 PointerRNA *ptr, const char *prop_name, T flag, bool flipped, T *value_p)
337{
338 if (PropertyRNA *prop = RNA_struct_find_property(ptr, prop_name)) {
339 if (RNA_property_is_set(ptr, prop)) {
340 if (RNA_property_boolean_get(ptr, prop) ^ flipped) {
341 *value_p |= flag;
342 }
343 else {
344 *value_p &= ~flag;
345 }
346 return true;
347 }
348 RNA_property_boolean_set(ptr, prop, ((*value_p & flag) > T{}) ^ flipped);
349 return false;
350 }
352 return false;
353}
354
355static bool rna_property_sync_enum(PointerRNA *ptr, const char *prop_name, int *value_p)
356{
357 if (PropertyRNA *prop = RNA_struct_find_property(ptr, prop_name)) {
358 if (RNA_property_is_set(ptr, prop)) {
359 *value_p = RNA_property_enum_get(ptr, prop);
360 return true;
361 }
362 RNA_property_enum_set(ptr, prop, *value_p);
363 return false;
364 }
366 return false;
367}
368
369template<typename T>
370static bool rna_property_sync_enum_char(PointerRNA *ptr, const char *prop_name, T *value_p)
371{
372 int value_i = int(*value_p);
373 if (rna_property_sync_enum(ptr, prop_name, &value_i)) {
374 *value_p = T(value_i);
375 return true;
376 }
377 return false;
378}
379
380static bool rna_property_sync_int(PointerRNA *ptr, const char *prop_name, int *value_p)
381{
382 if (PropertyRNA *prop = RNA_struct_find_property(ptr, prop_name)) {
383 if (RNA_property_is_set(ptr, prop)) {
384 *value_p = RNA_property_int_get(ptr, prop);
385 return true;
386 }
387 RNA_property_int_set(ptr, prop, *value_p);
388 return false;
389 }
391 return false;
392}
393
394static bool rna_property_sync_float(PointerRNA *ptr, const char *prop_name, float *value_p)
395{
396 if (PropertyRNA *prop = RNA_struct_find_property(ptr, prop_name)) {
397 if (RNA_property_is_set(ptr, prop)) {
398 *value_p = RNA_property_float_get(ptr, prop);
399 return true;
400 }
401 RNA_property_float_set(ptr, prop, *value_p);
402 return false;
403 }
405 return false;
406}
407
408static bool rna_property_sync_string(PointerRNA *ptr, const char *prop_name, char value_p[])
409{
410 if (PropertyRNA *prop = RNA_struct_find_property(ptr, prop_name)) {
411 if (RNA_property_is_set(ptr, prop)) {
412 RNA_property_string_get(ptr, prop, value_p);
413 return true;
414 }
415 RNA_property_string_set(ptr, prop, value_p);
416 return false;
417 }
419 return false;
420}
421
423{
424 /* Remember last method for live unwrap. */
425 rna_property_sync_enum_char(op->ptr, "method", &ts->unwrapper);
426
427 /* Remember packing margin. */
428 rna_property_sync_float(op->ptr, "margin", &ts->uvcalc_margin);
429
430 rna_property_sync_int(op->ptr, "iterations", &ts->uvcalc_iterations);
431
432 rna_property_sync_float(op->ptr, "weight_factor", &ts->uvcalc_weight_factor);
433
434 rna_property_sync_string(op->ptr, "weight_group", ts->uvcalc_weight_group);
435
436 rna_property_sync_flag(op->ptr, "fill_holes", UVCALC_FILLHOLES, false, &ts->uvcalc_flag);
438 op->ptr, "correct_aspect", UVCALC_NO_ASPECT_CORRECT, true, &ts->uvcalc_flag);
439 rna_property_sync_flag(op->ptr, "use_subsurf_data", UVCALC_USESUBSURF, false, &ts->uvcalc_flag);
440 rna_property_sync_flag(op->ptr, "no_flip", UVCALC_UNWRAP_NO_FLIP, false, &ts->uvcalc_flag);
441
443 op->ptr, "use_weights", UVCALC_UNWRAP_USE_WEIGHTS, false, &ts->uvcalc_flag);
444}
445
446static bool uvedit_have_selection(const Scene *scene, BMesh *bm, const UnwrapOptions *options)
447{
448 BMFace *efa;
449 BMLoop *l;
450 BMIter iter, liter;
451 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
452
453 if (offsets.uv == -1) {
454 return (bm->totfacesel != 0);
455 }
456
457 /* verify if we have any selected uv's before unwrapping,
458 * so we can cancel the operator early */
459 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
462 continue;
463 }
464 }
465 else if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
466 continue;
467 }
468
469 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
470 if (uvedit_uv_select_test(scene, bm, l, offsets)) {
471 break;
472 }
473 }
474
475 if (options->only_selected_uvs && !l) {
476 continue;
477 }
478
479 return true;
480 }
481
482 return false;
483}
484
485static bool uvedit_have_selection_multi(const Scene *scene,
486 const Span<Object *> objects,
487 const UnwrapOptions *options)
488{
489 bool have_select = false;
490 for (Object *obedit : objects) {
492 if (uvedit_have_selection(scene, bm, options)) {
493 have_select = true;
494 break;
495 }
496 }
497 return have_select;
498}
499
501 const int material_index,
502 float *r_aspx,
503 float *r_aspy)
504{
505 if (material_index < 0 || material_index >= ob->totcol) [[unlikely]] {
506 *r_aspx = 1.0f;
507 *r_aspy = 1.0f;
508 return;
509 }
510 Image *ima;
511 ED_object_get_active_image(ob, material_index + 1, &ima, nullptr, nullptr, nullptr);
512 ED_image_get_uv_aspect(ima, nullptr, r_aspx, r_aspy);
513}
514
515void ED_uvedit_get_aspect(Object *ob, float *r_aspx, float *r_aspy)
516{
518 BLI_assert(bm != nullptr);
519 bool sloppy = true;
520 bool selected = false;
521 BMFace *efa = BM_mesh_active_face_get(bm, sloppy, selected);
522 if (!efa) {
523 *r_aspx = 1.0f;
524 *r_aspy = 1.0f;
525 return;
526 }
527
528 ED_uvedit_get_aspect_from_material(ob, efa->mat_nr, r_aspx, r_aspy);
529}
530
532{
533 float aspect[2];
534 ED_uvedit_get_aspect(ob, &aspect[0], &aspect[1]);
535 return aspect[0] / aspect[1];
536}
537
538static bool uvedit_is_face_affected(const Scene *scene,
539 const BMesh *bm,
540 BMFace *efa,
541 const UnwrapOptions *options,
542 const BMUVOffsets &offsets)
543{
545 return false;
546 }
547
548 if (options->only_selected_faces && !BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
549 return false;
550 }
551
552 if (options->only_selected_uvs) {
553 BMLoop *l;
554 BMIter iter;
555 BM_ITER_ELEM (l, &iter, efa, BM_LOOPS_OF_FACE) {
556 if (uvedit_uv_select_test(scene, bm, l, offsets)) {
557 return true;
558 }
559 }
560 return false;
561 }
562
563 return true;
564}
565
566/* Prepare unique indices for each unique pinned UV, even if it shares a BMVert.
567 */
569 const Scene *scene,
570 const BMesh *bm,
571 BMFace *efa,
572 const UnwrapOptions *options,
573 const BMUVOffsets &offsets)
574{
575 BMIter liter;
576 BMLoop *l;
577 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
578 bool pin = BM_ELEM_CD_GET_BOOL(l, offsets.pin);
579 if (options->pin_unselected && !pin) {
580 pin = !uvedit_uv_select_test(scene, bm, l, offsets);
581 }
582 if (pin) {
583 int bmvertindex = BM_elem_index_get(l->v);
584 const float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
585 geometry::uv_prepare_pin_index(handle, bmvertindex, luv);
586 }
587 }
588}
589
591 const Scene *scene,
592 const BMesh *bm,
593 BMFace *efa,
594 geometry::ParamKey face_index,
595 const UnwrapOptions *options,
596 const BMUVOffsets &offsets,
597 const int cd_weight_offset,
598 const int cd_weight_index)
599{
606
607 int i;
608
609 BMIter liter;
610 BMLoop *l;
611
612 /* let parametrizer split the ngon, it can make better decisions
613 * about which split is best for unwrapping than poly-fill. */
614 BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) {
615 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
616
617 vkeys[i] = geometry::uv_find_pin_index(handle, BM_elem_index_get(l->v), luv);
618 co[i] = l->v->co;
619 uv[i] = luv;
620 pin[i] = BM_ELEM_CD_GET_BOOL(l, offsets.pin);
621 select[i] = uvedit_uv_select_test(scene, bm, l, offsets);
622 if (options->pin_unselected && !select[i]) {
623 pin[i] = true;
624 }
625
626 /* Optional vertex group weighting. */
627 if (cd_weight_offset >= 0 && cd_weight_index >= 0) {
628 MDeformVert *dv = static_cast<MDeformVert *> BM_ELEM_CD_GET_VOID_P(l->v, cd_weight_offset);
629 weight[i] = BKE_defvert_find_weight(dv, cd_weight_index);
630 }
631 else {
632 weight[i] = 1.0f;
633 }
634 }
635
637 face_index,
638 i,
639 vkeys.data(),
640 co.data(),
641 uv.data(),
642 weight.data(),
643 pin.data(),
644 select.data());
645}
646
647/* Set seams on UV Parametrizer based on options. */
649 BMesh *bm,
650 const UnwrapOptions *options)
651{
652 if (options->topology_from_uvs && !options->topology_from_uvs_use_seams) {
653 return; /* Seams are not required with these options. */
654 }
655
656 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
657 if (offsets.uv == -1) {
658 return; /* UVs aren't present on BMesh. Nothing to do. */
659 }
660
661 BMEdge *edge;
662 BMIter iter;
663 BM_ITER_MESH (edge, &iter, bm, BM_EDGES_OF_MESH) {
664 if (!BM_elem_flag_test(edge, BM_ELEM_SEAM)) {
665 continue; /* No seam on this edge, nothing to do. */
666 }
667
668 /* Pinned vertices might have more than one ParamKey per BMVert.
669 * Check all the BM_LOOPS_OF_EDGE to find all the ParamKeys.
670 */
671 BMLoop *l;
672 BMIter liter;
673 BM_ITER_ELEM (l, &liter, edge, BM_LOOPS_OF_EDGE) {
674 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
675 float *luv_next = BM_ELEM_CD_GET_FLOAT_P(l->next, offsets.uv);
676 ParamKey vkeys[2];
677 vkeys[0] = geometry::uv_find_pin_index(handle, BM_elem_index_get(l->v), luv);
678 vkeys[1] = geometry::uv_find_pin_index(handle, BM_elem_index_get(l->next->v), luv_next);
679
680 /* Set the seam. */
682 }
683 }
684}
685
686/*
687 * Version of #construct_param_handle_multi with a separate BMesh parameter.
688 */
690 Object *ob,
691 const UnwrapOptions *options,
692 int *r_count_failed = nullptr)
693{
694 BMFace *efa;
695 BMIter iter;
696 int i;
698
699 ParamHandle *handle = new geometry::ParamHandle();
700
701 if (options->correct_aspect) {
703 }
704
705 /* we need the vert indices */
707
708 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
709 const int cd_weight_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
710 const int cd_weight_index = BKE_object_defgroup_name_index(ob, options->weight_group);
711
712 BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
713 if (uvedit_is_face_affected(scene, bm, efa, options, offsets)) {
714 uvedit_prepare_pinned_indices(handle, scene, bm, efa, options, offsets);
715 }
716 }
717
718 BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
719 if (uvedit_is_face_affected(scene, bm, efa, options, offsets)) {
721 handle, scene, bm, efa, i, options, offsets, cd_weight_offset, cd_weight_index);
722 }
723 }
724
726
728 handle, options->fill_holes, options->topology_from_uvs, r_count_failed);
729
730 return handle;
731}
732
737 const Span<Object *> objects,
738 const UnwrapOptions *options)
739{
740 BMFace *efa;
741 BMIter iter;
742 int i;
743
744 ParamHandle *handle = new geometry::ParamHandle();
745
746 if (options->correct_aspect) {
747 Object *ob = objects[0];
749 }
750
751 /* we need the vert indices */
753
754 int offset = 0;
755
756 for (Object *obedit : objects) {
758 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
759 if (offsets.uv == -1) {
760 continue;
761 }
762
763 const int cd_weight_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
764 const int cd_weight_index = BKE_object_defgroup_name_index(obedit, options->weight_group);
765
766 BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
767 if (uvedit_is_face_affected(scene, bm, efa, options, offsets)) {
768 uvedit_prepare_pinned_indices(handle, scene, bm, efa, options, offsets);
769 }
770 }
771
772 BM_ITER_MESH_INDEX (efa, &iter, bm, BM_FACES_OF_MESH, i) {
773 if (uvedit_is_face_affected(scene, bm, efa, options, offsets)) {
775 scene,
776 bm,
777 efa,
778 i + offset,
779 options,
780 offsets,
781 cd_weight_offset,
782 cd_weight_index);
783 }
784 }
785
787
788 offset += bm->totface;
789 }
790
792 handle, options->fill_holes, options->topology_from_uvs, nullptr);
793
794 return handle;
795}
796
797static void texface_from_original_index(const Scene *scene,
798 const BMesh *bm,
799 const BMUVOffsets &offsets,
800 BMFace *efa,
801 int index,
802 float **r_uv,
803 bool *r_pin,
804 bool *r_select)
805{
806 BMLoop *l;
807 BMIter liter;
808
809 *r_uv = nullptr;
810 *r_pin = false;
811 *r_select = true;
812
813 if (index == ORIGINDEX_NONE) {
814 return;
815 }
816
817 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
818 if (BM_elem_index_get(l->v) == index) {
819 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
820 *r_uv = luv;
821 *r_pin = BM_ELEM_CD_GET_BOOL(l, offsets.pin);
822 *r_select = uvedit_uv_select_test(scene, bm, l, offsets);
823 break;
824 }
825 }
826}
827
828static Mesh *subdivide_edit_mesh(const Object *object, BMesh *bm, const SubsurfModifierData *smd)
829{
831 bm, nullptr, id_cast<const Mesh *>(object->data));
833
835 /* A zero level must be prevented by #modifier_unwrap_state
836 * since necessary data won't be available, see: #128958. */
837 BLI_assert(settings.level > 0);
838
839 /* Level 1 causes disconnected triangles, force level 2 to prevent this, see: #129503. */
840 if (settings.level == 1) {
841 settings.level = 2;
842 }
843
844 bke::subdiv::ToMeshSettings mesh_settings;
845 mesh_settings.resolution = (1 << smd->levels) + 1;
847
848 bke::subdiv::Subdiv *subdiv = bke::subdiv::new_from_mesh(&settings, me_from_em);
849 if (!subdiv) {
850 return nullptr;
851 }
852 Mesh *result = bke::subdiv::subdiv_to_mesh(subdiv, &mesh_settings, me_from_em);
853 BKE_id_free(nullptr, me_from_em);
855 return result;
856}
857
864 Object *ob,
865 const UnwrapOptions *options,
866 int *r_count_failed = nullptr)
867{
868 /* pointers to modifier data for unwrap control */
869 SubsurfModifierData *smd_real;
870 /* Modifier initialization data, will control what type of subdivision will happen. */
871 SubsurfModifierData smd = {{nullptr}};
872
873 /* Holds a map to edit-faces for every subdivision-surface face.
874 * These will be used to get hidden/ selected flags etc. */
875 BMFace **faceMap;
876 /* Similar to the above, we need a way to map edges to their original ones. */
877 BMEdge **edgeMap;
878
880 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
881 const int cd_weight_index = BKE_object_defgroup_name_index(ob, options->weight_group);
882
883 ParamHandle *handle = new geometry::ParamHandle();
884
885 if (options->correct_aspect) {
887 }
888
889 /* number of subdivisions to perform */
890 ModifierData *md = ob->modifiers.first();
891 smd_real = reinterpret_cast<SubsurfModifierData *>(md);
892
893 smd.levels = smd_real->levels;
894 smd.subdivType = smd_real->subdivType;
895 smd.flags = smd_real->flags;
896 smd.quality = smd_real->quality;
897
898 Mesh *subdiv_mesh = subdivide_edit_mesh(ob, bm, &smd);
899
900 const Span<float3> subsurf_positions = subdiv_mesh->vert_positions();
901 const Span<int2> subsurf_edges = subdiv_mesh->edges();
902 const OffsetIndices subsurf_facess = subdiv_mesh->faces();
903 const Span<int> subsurf_corner_verts = subdiv_mesh->corner_verts();
904 const Span<MDeformVert> subsurf_deform_verts = subdiv_mesh->deform_verts();
905
906 const int *origVertIndices = static_cast<const int *>(
908 const int *origEdgeIndices = static_cast<const int *>(
910 const int *origPolyIndices = static_cast<const int *>(
912
913 faceMap = MEM_new_array_uninitialized<BMFace *>(subdiv_mesh->faces_num, "unwrap_edit_face_map");
914
917
918 /* map subsurfed faces to original editFaces */
919 for (int i = 0; i < subdiv_mesh->faces_num; i++) {
920 faceMap[i] = BM_face_at_index(bm, origPolyIndices[i]);
921 }
922
923 edgeMap = MEM_new_array_uninitialized<BMEdge *>(subdiv_mesh->edges_num, "unwrap_edit_edge_map");
924
925 /* map subsurfed edges to original editEdges */
926 for (int i = 0; i < subdiv_mesh->edges_num; i++) {
927 /* not all edges correspond to an old edge */
928 edgeMap[i] = (origEdgeIndices[i] != ORIGINDEX_NONE) ?
929 BM_edge_at_index(bm, origEdgeIndices[i]) :
930 nullptr;
931 }
932
933 /* Prepare and feed faces to the solver. */
934 for (const int i : subsurf_facess.index_range()) {
935 ParamKey key, vkeys[4];
936 bool pin[4], select[4];
937 const float *co[4];
938 float *uv[4];
939 float weight[4];
940 BMFace *origFace = faceMap[i];
941
943 if (BM_elem_flag_test(origFace, BM_ELEM_HIDDEN)) {
944 continue;
945 }
946 }
947 else {
948 if (BM_elem_flag_test(origFace, BM_ELEM_HIDDEN) ||
949 (options->only_selected_faces && !BM_elem_flag_test(origFace, BM_ELEM_SELECT)))
950 {
951 continue;
952 }
953 }
954
955 const Span<int> poly_corner_verts = subsurf_corner_verts.slice(subsurf_facess[i]);
956
957 /* We will not check for v4 here. Sub-surface faces always have 4 vertices. */
958 BLI_assert(poly_corner_verts.size() == 4);
959 key = ParamKey(i);
960 vkeys[0] = ParamKey(poly_corner_verts[0]);
961 vkeys[1] = ParamKey(poly_corner_verts[1]);
962 vkeys[2] = ParamKey(poly_corner_verts[2]);
963 vkeys[3] = ParamKey(poly_corner_verts[3]);
964
965 co[0] = subsurf_positions[poly_corner_verts[0]];
966 co[1] = subsurf_positions[poly_corner_verts[1]];
967 co[2] = subsurf_positions[poly_corner_verts[2]];
968 co[3] = subsurf_positions[poly_corner_verts[3]];
969
970 /* Optional vertex group weights. */
971 if (cd_weight_index >= 0) {
972 weight[0] = BKE_defvert_find_weight(&subsurf_deform_verts[poly_corner_verts[0]],
973 cd_weight_index);
974 weight[1] = BKE_defvert_find_weight(&subsurf_deform_verts[poly_corner_verts[1]],
975 cd_weight_index);
976 weight[2] = BKE_defvert_find_weight(&subsurf_deform_verts[poly_corner_verts[2]],
977 cd_weight_index);
978 weight[3] = BKE_defvert_find_weight(&subsurf_deform_verts[poly_corner_verts[3]],
979 cd_weight_index);
980 }
981 else {
982 weight[0] = 1.0f;
983 weight[1] = 1.0f;
984 weight[2] = 1.0f;
985 weight[3] = 1.0f;
986 }
987
988 /* This is where all the magic is done.
989 * If the vertex exists in the, we pass the original uv pointer to the solver, thus
990 * flushing the solution to the edit mesh. */
992 bm,
993 offsets,
994 origFace,
995 origVertIndices[poly_corner_verts[0]],
996 &uv[0],
997 &pin[0],
998 &select[0]);
1000 bm,
1001 offsets,
1002 origFace,
1003 origVertIndices[poly_corner_verts[1]],
1004 &uv[1],
1005 &pin[1],
1006 &select[1]);
1008 bm,
1009 offsets,
1010 origFace,
1011 origVertIndices[poly_corner_verts[2]],
1012 &uv[2],
1013 &pin[2],
1014 &select[2]);
1016 bm,
1017 offsets,
1018 origFace,
1019 origVertIndices[poly_corner_verts[3]],
1020 &uv[3],
1021 &pin[3],
1022 &select[3]);
1023
1024 geometry::uv_parametrizer_face_add(handle, key, 4, vkeys, co, uv, weight, pin, select);
1025 }
1026
1027 /* These are calculated from original mesh too. */
1028 for (const int64_t i : subsurf_edges.index_range()) {
1029 if ((edgeMap[i] != nullptr) && BM_elem_flag_test(edgeMap[i], BM_ELEM_SEAM)) {
1030 const int2 &edge = subsurf_edges[i];
1031 ParamKey vkeys[2];
1032 vkeys[0] = ParamKey(edge[0]);
1033 vkeys[1] = ParamKey(edge[1]);
1035 }
1036 }
1037
1039 handle, options->fill_holes, options->topology_from_uvs, r_count_failed);
1040
1041 /* cleanup */
1042 MEM_delete(faceMap);
1043 MEM_delete(edgeMap);
1044 BKE_id_free(nullptr, subdiv_mesh);
1045
1046 return handle;
1047}
1048
1050
1051/* -------------------------------------------------------------------- */
1054
1064
1066{
1067 const Main *bmain = CTX_data_main(C);
1068 const Scene *scene = CTX_data_scene(C);
1069 ViewLayer *view_layer = CTX_data_view_layer(C);
1070
1072 options.topology_from_uvs = true;
1073 options.fill_holes = RNA_boolean_get(op->ptr, "fill_holes");
1074 options.only_selected_faces = true;
1075 options.only_selected_uvs = true;
1076 options.correct_aspect = true;
1077
1079 *bmain, scene, view_layer, CTX_wm_view3d(C));
1080
1081 if (!uvedit_have_selection_multi(scene, objects, &options)) {
1082 return false;
1083 }
1084
1085 MinStretch *ms = MEM_new<MinStretch>(__func__);
1086 ms->scene = scene;
1087 ms->objects_edit = objects;
1088 ms->blend = RNA_float_get(op->ptr, "blend");
1089 ms->iterations = RNA_int_get(op->ptr, "iterations");
1090 ms->i = 0;
1091 ms->handle = construct_param_handle_multi(scene, objects, &options);
1093
1095 if (ms->blend != 0.0f) {
1097 }
1098
1099 op->customdata = ms;
1100
1101 return true;
1102}
1103
1104static void minimize_stretch_iteration(bContext *C, wmOperator *op, bool interactive)
1105{
1106 MinStretch *ms = static_cast<MinStretch *>(op->customdata);
1107 ScrArea *area = CTX_wm_area(C);
1108 const Scene *scene = CTX_data_scene(C);
1109 ToolSettings *ts = scene->toolsettings;
1110 const bool synced_selection = (ts->uv_flag & UV_FLAG_SELECT_SYNC) != 0;
1111
1114
1115 ms->i++;
1116 RNA_int_set(op->ptr, "iterations", ms->i);
1117
1118 if (interactive && (BLI_time_now_seconds() - ms->lasttime > 0.5)) {
1119 char str[UI_MAX_DRAW_STR];
1120
1122
1123 if (area) {
1124 SNPRINTF_UTF8(str, IFACE_("Minimize Stretch. Blend %.2f"), ms->blend);
1125 ED_area_status_text(area, str);
1126 ED_workspace_status_text(C, IFACE_("Press + and -, or scroll wheel to set blending"));
1127 }
1128
1130
1131 for (Object *obedit : ms->objects_edit) {
1133 if (synced_selection && (bm->totfacesel == 0)) {
1134 continue;
1135 }
1136
1139 }
1140 }
1141}
1142
1143static void minimize_stretch_exit(bContext *C, wmOperator *op, bool cancel)
1144{
1145 MinStretch *ms = static_cast<MinStretch *>(op->customdata);
1146 ScrArea *area = CTX_wm_area(C);
1147 const Scene *scene = CTX_data_scene(C);
1148 ToolSettings *ts = scene->toolsettings;
1149 const bool synced_selection = (ts->uv_flag & UV_FLAG_SELECT_SYNC) != 0;
1150
1151 ED_area_status_text(area, nullptr);
1152 ED_workspace_status_text(C, nullptr);
1153
1154 if (ms->timer) {
1156 }
1157
1158 if (cancel) {
1160 }
1161 else {
1163 }
1164
1166 delete (ms->handle);
1167
1168 for (Object *obedit : ms->objects_edit) {
1170 if (synced_selection && (bm->totfacesel == 0)) {
1171 continue;
1172 }
1173
1176 }
1177
1178 MEM_delete(ms);
1179 op->customdata = nullptr;
1180}
1181
1183{
1184 int i, iterations;
1185
1186 if (!minimize_stretch_init(C, op)) {
1187 return OPERATOR_CANCELLED;
1188 }
1189
1190 iterations = RNA_int_get(op->ptr, "iterations");
1191 for (i = 0; i < iterations; i++) {
1192 minimize_stretch_iteration(C, op, false);
1193 }
1194 minimize_stretch_exit(C, op, false);
1195
1196 return OPERATOR_FINISHED;
1197}
1198
1200 wmOperator *op,
1201 const wmEvent * /*event*/)
1202{
1203 if (!minimize_stretch_init(C, op)) {
1204 return OPERATOR_CANCELLED;
1205 }
1206
1207 minimize_stretch_iteration(C, op, true);
1208
1209 MinStretch *ms = static_cast<MinStretch *>(op->customdata);
1212
1214}
1215
1217{
1218 MinStretch *ms = static_cast<MinStretch *>(op->customdata);
1219
1220 switch (event->type) {
1221 case EVT_ESCKEY:
1222 case RIGHTMOUSE:
1223 minimize_stretch_exit(C, op, true);
1224 return OPERATOR_CANCELLED;
1225 case EVT_RETKEY:
1226 case EVT_PADENTER:
1227 case LEFTMOUSE:
1228 minimize_stretch_exit(C, op, false);
1229 return OPERATOR_FINISHED;
1230 case EVT_PADPLUSKEY:
1231 case WHEELUPMOUSE:
1232 if (event->val == KM_PRESS) {
1233 if (ms->blend < 0.95f) {
1234 ms->blend += 0.1f;
1235 ms->lasttime = 0.0f;
1236 RNA_float_set(op->ptr, "blend", ms->blend);
1237 minimize_stretch_iteration(C, op, true);
1238 }
1239 }
1240 break;
1241 case EVT_PADMINUS:
1242 case WHEELDOWNMOUSE:
1243 if (event->val == KM_PRESS) {
1244 if (ms->blend > 0.05f) {
1245 ms->blend -= 0.1f;
1246 ms->lasttime = 0.0f;
1247 RNA_float_set(op->ptr, "blend", ms->blend);
1248 minimize_stretch_iteration(C, op, true);
1249 }
1250 }
1251 break;
1252 case TIMER:
1253 if (ms->timer == event->customdata) {
1254 double start = BLI_time_now_seconds();
1255
1256 do {
1257 minimize_stretch_iteration(C, op, true);
1258 } while (BLI_time_now_seconds() - start < 0.01);
1259 }
1260 break;
1261 default: {
1262 break;
1263 }
1264 }
1265
1266 if (ms->iterations && ms->i >= ms->iterations) {
1267 minimize_stretch_exit(C, op, false);
1268 return OPERATOR_FINISHED;
1269 }
1270
1272}
1273
1275{
1276 minimize_stretch_exit(C, op, true);
1277}
1278
1280{
1281 /* identifiers */
1282 ot->name = "Minimize Stretch";
1283 ot->idname = "UV_OT_minimize_stretch";
1285 ot->description = "Reduce UV stretching by relaxing angles";
1286
1287 /* API callbacks. */
1293
1294 /* properties */
1296 "fill_holes",
1297 true,
1298 "Fill Holes",
1299 "Virtually fill holes in mesh before unwrapping, to better avoid overlaps and "
1300 "preserve symmetry");
1302 "blend",
1303 0.0f,
1304 0.0f,
1305 1.0f,
1306 "Blend",
1307 "Blend factor between stretch minimized and original",
1308 0.0f,
1309 1.0f);
1310 RNA_def_int(ot->srna,
1311 "iterations",
1312 0,
1313 0,
1314 INT_MAX,
1315 "Iterations",
1316 "Number of iterations to run, 0 is unlimited when run interactively",
1317 0,
1318 100);
1319}
1320
1322
1324 const float matrix[2][2], /* Scale and rotation. */
1325 const float pre_translate[2] /* (pre) Translation. */
1326)
1327{
1328 /* Use a pre-transform to compute `A * (x+b)`
1329 *
1330 * \note Ordinarily, we'd use a post_transform like `A * x + b`
1331 * In general, post-transforms are easier to work with when using homogeneous coordinates.
1332 *
1333 * When UV mapping into the unit square, post-transforms can lose precision on small islands.
1334 * Instead we're using a pre-transform to maintain precision.
1335 *
1336 * To convert post-transform to pre-transform, use `A * x + b == A * (x + c), c = A^-1 * b`
1337 */
1338
1339 const int cd_loop_uv_offset = island->offsets.uv;
1340 const int faces_len = island->faces_len;
1341 for (int i = 0; i < faces_len; i++) {
1342 BMFace *f = island->faces[i];
1343 BMLoop *l;
1344 BMIter iter;
1345 BM_ITER_ELEM (l, &iter, f, BM_LOOPS_OF_FACE) {
1346 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
1347 geometry::mul_v2_m2_add_v2v2(luv, matrix, luv, pre_translate);
1348 }
1349 }
1350}
1351
1356 const float coords[2],
1357 float nearest_tile_co[2])
1358{
1359 BKE_image_find_nearest_tile_with_offset(image, coords, nearest_tile_co);
1360
1361 /* Add 0.5 to get tile center coordinates. */
1362 float nearest_tile_center_co[2] = {nearest_tile_co[0], nearest_tile_co[1]};
1363 add_v2_fl(nearest_tile_center_co, 0.5f);
1364
1365 return len_squared_v2v2(coords, nearest_tile_center_co);
1366}
1367
1371static float uv_nearest_grid_tile_distance(const int udim_grid[2],
1372 const float coords[2],
1373 float nearest_tile_co[2])
1374{
1375 const float coords_floor[2] = {floorf(coords[0]), floorf(coords[1])};
1376
1377 if (coords[0] > udim_grid[0]) {
1378 nearest_tile_co[0] = udim_grid[0] - 1;
1379 }
1380 else if (coords[0] < 0) {
1381 nearest_tile_co[0] = 0;
1382 }
1383 else {
1384 nearest_tile_co[0] = coords_floor[0];
1385 }
1386
1387 if (coords[1] > udim_grid[1]) {
1388 nearest_tile_co[1] = udim_grid[1] - 1;
1389 }
1390 else if (coords[1] < 0) {
1391 nearest_tile_co[1] = 0;
1392 }
1393 else {
1394 nearest_tile_co[1] = coords_floor[1];
1395 }
1396
1397 /* Add 0.5 to get tile center coordinates. */
1398 float nearest_tile_center_co[2] = {nearest_tile_co[0], nearest_tile_co[1]};
1399 add_v2_fl(nearest_tile_center_co, 0.5f);
1400
1401 return len_squared_v2v2(coords, nearest_tile_center_co);
1402}
1403
1404static bool island_has_pins(const Scene *scene,
1405 const BMesh *bm,
1406 FaceIsland *island,
1407 const geometry::UVPackIsland_Params *params)
1408{
1409 const bool pin_unselected = params->pin_unselected;
1410 const bool only_selected_faces = params->only_selected_faces;
1411 BMLoop *l;
1412 BMIter iter;
1413 const int pin_offset = island->offsets.pin;
1414 for (int i = 0; i < island->faces_len; i++) {
1415 BMFace *efa = island->faces[i];
1416 if (pin_unselected && only_selected_faces && !BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
1417 return true;
1418 }
1419 BM_ITER_ELEM (l, &iter, island->faces[i], BM_LOOPS_OF_FACE) {
1420 if (BM_ELEM_CD_GET_BOOL(l, pin_offset)) {
1421 return true;
1422 }
1423 if (pin_unselected && !uvedit_uv_select_test(scene, bm, l, island->offsets)) {
1424 return true;
1425 }
1426 }
1427 }
1428 return false;
1429}
1430
1444static void uvedit_pack_islands_multi(const Scene *scene,
1445 const Span<Object *> objects,
1446 BMesh **bmesh_override,
1447 const SpaceImage *udim_source_closest,
1448 const bool original_selection,
1449 const bool notify_wm,
1450 const rctf *custom_region,
1452{
1453 Vector<FaceIsland *> island_vector;
1454 Vector<bool> pinned_vector;
1455
1456 for (const int ob_index : objects.index_range()) {
1457 Object *obedit = objects[ob_index];
1458 BMesh *bm = nullptr;
1459 if (bmesh_override) {
1460 /* NOTE: obedit is still required for aspect ratio and ID_RECALC_GEOMETRY. */
1461 bm = bmesh_override[ob_index];
1462 }
1463 else {
1465 }
1466 BLI_assert(bm);
1467
1468 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
1469 if (offsets.uv == -1) {
1470 continue;
1471 }
1472
1473 const float aspect_y = params->correct_aspect ? ED_uvedit_get_aspect_y(obedit) : 1.0f;
1474
1475 bool only_selected_faces = params->only_selected_faces;
1476 bool only_selected_uvs = params->only_selected_uvs;
1477 const bool ignore_pinned = params->pin_method == ED_UVPACK_PIN_IGNORE;
1478 if (ignore_pinned && params->pin_unselected) {
1479 only_selected_faces = false;
1480 only_selected_uvs = false;
1481 }
1482 ListBaseT<FaceIsland> island_list = {nullptr};
1484 bm,
1485 &island_list,
1486 only_selected_faces,
1487 only_selected_uvs,
1488 params->use_seams,
1489 aspect_y,
1490 offsets);
1491
1492 /* Remove from linked list and append to Vector. */
1493 for (FaceIsland &island : island_list.items_mutable()) {
1494 BLI_remlink(&island_list, &island);
1495 const bool pinned = island_has_pins(scene, bm, &island, params);
1496 if (ignore_pinned && pinned) {
1497 MEM_delete(island.faces);
1498 MEM_delete(&island);
1499 continue;
1500 }
1501 island_vector.append(&island);
1502 pinned_vector.append(pinned);
1503 }
1504 }
1505
1506 if (island_vector.is_empty()) {
1507 return;
1508 }
1509
1510 /* Coordinates of bounding box containing all selected UVs. */
1511 float selection_min_co[2], selection_max_co[2];
1512 INIT_MINMAX2(selection_min_co, selection_max_co);
1513
1514 for (int index = 0; index < island_vector.size(); index++) {
1515 FaceIsland *island = island_vector[index];
1516
1517 for (int i = 0; i < island->faces_len; i++) {
1518 BMFace *f = island->faces[i];
1519 BM_face_uv_minmax(f, selection_min_co, selection_max_co, island->offsets.uv);
1520 }
1521 }
1522
1523 /* Center of bounding box containing all selected UVs. */
1524 float selection_center[2];
1525 mid_v2_v2v2(selection_center, selection_min_co, selection_max_co);
1526
1527 if (original_selection) {
1528 /* Protect against degenerate source AABB. */
1529 if ((selection_max_co[0] - selection_min_co[0]) * (selection_max_co[1] - selection_min_co[1]) >
1530 1e-40f)
1531 {
1532 copy_v2_v2(params->udim_base_offset, selection_min_co);
1533 params->target_extent = selection_max_co[1] - selection_min_co[1];
1534 params->target_aspect_y = (selection_max_co[0] - selection_min_co[0]) /
1535 (selection_max_co[1] - selection_min_co[1]);
1536 }
1537 }
1538 else if (custom_region) {
1539 if (!BLI_rctf_is_empty(custom_region)) {
1540 const float2 custom_region_size = {
1541 BLI_rctf_size_x(custom_region),
1542 BLI_rctf_size_y(custom_region),
1543 };
1544 ARRAY_SET_ITEMS(params->udim_base_offset, custom_region->xmin, custom_region->ymin);
1545 params->target_extent = custom_region_size.y;
1546 params->target_aspect_y = custom_region_size.x / custom_region_size.y;
1547 }
1548 }
1549
1551 Heap *heap = BLI_heap_new();
1552
1553 Vector<geometry::PackIsland *> pack_island_vector;
1554 for (int i = 0; i < island_vector.size(); i++) {
1555 FaceIsland *face_island = island_vector[i];
1556 geometry::PackIsland *pack_island = new geometry::PackIsland();
1557 pack_island->caller_index = i;
1558 pack_island->aspect_y = face_island->aspect_y;
1559 pack_island->pinned = pinned_vector[i];
1560 pack_island_vector.append(pack_island);
1561
1562 for (int i = 0; i < face_island->faces_len; i++) {
1563 BMFace *f = face_island->faces[i];
1564
1565 /* Storage. */
1566 Array<float2> uvs(f->len);
1567
1568 /* Obtain UVs of face. */
1569 BMLoop *l;
1570 BMIter iter;
1571 int j;
1572 BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, j) {
1573 copy_v2_v2(uvs[j], BM_ELEM_CD_GET_FLOAT_P(l, face_island->offsets.uv));
1574 }
1575
1576 pack_island->add_polygon(uvs, arena, heap);
1577
1578 BLI_memarena_clear(arena);
1579 }
1580 }
1581 BLI_heap_free(heap, nullptr);
1582 BLI_memarena_free(arena);
1583
1584 const float scale = pack_islands(pack_island_vector, *params);
1585 const bool is_cancelled = params->isCancelled();
1586
1587 float base_offset[2] = {0.0f, 0.0f};
1588 copy_v2_v2(base_offset, params->udim_base_offset);
1589
1590 if (udim_source_closest) {
1591 const Image *image = udim_source_closest->image;
1592 const int *udim_grid = udim_source_closest->tile_grid_shape;
1593 /* Check if selection lies on a valid UDIM grid tile. */
1594 bool is_valid_udim = uv_coords_isect_udim(image, udim_grid, selection_center);
1595 if (is_valid_udim) {
1596 base_offset[0] = floorf(selection_center[0]);
1597 base_offset[1] = floorf(selection_center[1]);
1598 }
1599 /* If selection doesn't lie on any UDIM then find the closest UDIM grid or image tile. */
1600 else {
1601 float nearest_image_tile_co[2] = {FLT_MAX, FLT_MAX};
1602 float nearest_image_tile_dist = FLT_MAX, nearest_grid_tile_dist = FLT_MAX;
1603 if (image) {
1604 nearest_image_tile_dist = uv_nearest_image_tile_distance(
1605 image, selection_center, nearest_image_tile_co);
1606 }
1607
1608 float nearest_grid_tile_co[2] = {0.0f, 0.0f};
1609 nearest_grid_tile_dist = uv_nearest_grid_tile_distance(
1610 udim_grid, selection_center, nearest_grid_tile_co);
1611
1612 base_offset[0] = (nearest_image_tile_dist < nearest_grid_tile_dist) ?
1613 nearest_image_tile_co[0] :
1614 nearest_grid_tile_co[0];
1615 base_offset[1] = (nearest_image_tile_dist < nearest_grid_tile_dist) ?
1616 nearest_image_tile_co[1] :
1617 nearest_grid_tile_co[1];
1618 }
1619 }
1620
1621 float matrix[2][2];
1622 float matrix_inverse[2][2];
1623 float pre_translate[2];
1624 for (const int64_t i : pack_island_vector.index_range()) {
1625 if (is_cancelled) {
1626 continue;
1627 }
1628 geometry::PackIsland *pack_island = pack_island_vector[i];
1629 FaceIsland *island = island_vector[pack_island->caller_index];
1630 const float island_scale = pack_island->can_scale_(*params) ? scale : 1.0f;
1631 pack_island->build_transformation(island_scale, pack_island->angle, matrix);
1632 invert_m2_m2(matrix_inverse, matrix);
1633
1634 /* Add base_offset, post transform. */
1635 if (!pinned_vector[i] || params->pin_method != ED_UVPACK_PIN_LOCK_ALL) {
1636 mul_v2_m2v2(pre_translate, matrix_inverse, base_offset);
1637
1638 /* Add pre-translation from #pack_islands. */
1639 pre_translate[0] += pack_island->pre_translate.x;
1640 pre_translate[1] += pack_island->pre_translate.y;
1641
1642 /* Perform the transformation. */
1643 island_uv_transform(island, matrix, pre_translate);
1644 }
1645 }
1646
1647 for (const int64_t i : pack_island_vector.index_range()) {
1648 geometry::PackIsland *pack_island = pack_island_vector[i];
1649 /* Cleanup memory. */
1650 pack_island_vector[i] = nullptr;
1651 delete pack_island;
1652 }
1653
1654 if (notify_wm && !is_cancelled) {
1655 for (Object *obedit : objects) {
1657 WM_main_add_notifier(NC_GEOM | ND_DATA, obedit->data);
1658 }
1659 }
1660
1661 for (FaceIsland *island : island_vector) {
1662 MEM_delete(island->faces);
1663 MEM_delete(island);
1664 }
1665}
1666
1667/* -------------------------------------------------------------------- */
1670
1671/* TODO: support this, interaction with the job-system needs to be handled carefully. */
1672// #define USE_INTERACTIVE_PACK
1673
1674/* Packing targets. */
1675enum {
1680};
1681
1698
1699static void pack_islands_startjob(void *pidv, wmJobWorkerStatus *worker_status)
1700{
1701 worker_status->progress = 0.02f;
1702
1703 UVPackIslandsData *pid = static_cast<UVPackIslandsData *>(pidv);
1704
1705 pid->pack_island_params.stop = &worker_status->stop;
1706 pid->pack_island_params.do_update = &worker_status->do_update;
1707 pid->pack_island_params.progress = &worker_status->progress;
1708
1710 pid->objects,
1711 nullptr,
1712 (pid->udim_source == PACK_UDIM_SRC_CLOSEST) ? pid->sima : nullptr,
1714 !pid->use_job,
1715 (pid->udim_source == PACK_CUSTOM_REGION) ? &pid->custom_region :
1716 nullptr,
1717 &pid->pack_island_params);
1718
1719 worker_status->progress = 0.99f;
1720 worker_status->do_update = true;
1721}
1722
1723static void pack_islands_endjob(void *pidv)
1724{
1725 UVPackIslandsData *pid = static_cast<UVPackIslandsData *>(pidv);
1726 for (Object *obedit : pid->objects) {
1729 }
1731
1732 if (pid->undo_str) {
1734 }
1735}
1736
1737static void pack_islands_freejob(void *pidv)
1738{
1739 WM_cursor_wait(false);
1740 UVPackIslandsData *pid = static_cast<UVPackIslandsData *>(pidv);
1741 WM_locked_interface_set(pid->wm, false);
1742 MEM_delete(pid);
1743}
1744
1746{
1747 ViewLayer *view_layer = CTX_data_view_layer(C);
1748 const Main *bmain = CTX_data_main(C);
1749 const Scene *scene = CTX_data_scene(C);
1750 const SpaceImage *sima = CTX_wm_space_image(C);
1751 const ToolSettings *ts = scene->toolsettings;
1752
1754 options.topology_from_uvs = true;
1755 options.only_selected_faces = true;
1756 options.only_selected_uvs = true;
1757 options.fill_holes = false;
1758 options.correct_aspect = true;
1759
1761 *bmain, scene, view_layer, CTX_wm_view3d(C));
1762
1763 /* Early exit in case no UVs are selected. */
1764 if (!uvedit_have_selection_multi(scene, objects, &options)) {
1765 return OPERATOR_CANCELLED;
1766 }
1767
1768 /* RNA props */
1769 const int udim_source = RNA_enum_get(op->ptr, "udim_source");
1770 if (RNA_struct_property_is_set(op->ptr, "margin")) {
1771 scene->toolsettings->uvcalc_margin = RNA_float_get(op->ptr, "margin");
1772 }
1773 else {
1774 RNA_float_set(op->ptr, "margin", scene->toolsettings->uvcalc_margin);
1775 }
1776
1777 UVPackIslandsData *pid = MEM_new<UVPackIslandsData>(__func__);
1778 pid->use_job = op->flag & OP_IS_INVOKE;
1779 pid->scene = scene;
1780 pid->objects = std::move(objects);
1781 pid->sima = sima;
1782 pid->udim_source = udim_source;
1783 pid->wm = CTX_wm_manager(C);
1784
1785 if (udim_source == PACK_CUSTOM_REGION) {
1786 if (ts->uv_flag & UV_FLAG_CUSTOM_REGION) {
1788 }
1789 else {
1790 pid->custom_region.xmin = pid->custom_region.ymin = 0.0f;
1791 pid->custom_region.xmax = pid->custom_region.ymax = 1.0f;
1792 }
1793 }
1794 else {
1795 pid->custom_region = {0.0f};
1796 }
1797
1798 geometry::UVPackIsland_Params &pack_island_params = pid->pack_island_params;
1799 {
1800 /* Call default constructor and copy the defaults. */
1801 geometry::UVPackIsland_Params default_params;
1802 pack_island_params = default_params;
1803 }
1804
1805 pack_island_params.setFromUnwrapOptions(options);
1806 if (RNA_boolean_get(op->ptr, "rotate")) {
1807 pack_island_params.rotate_method = eUVPackIsland_RotationMethod(
1808 RNA_enum_get(op->ptr, "rotate_method"));
1809 }
1810 else {
1811 pack_island_params.rotate_method = ED_UVPACK_ROTATION_NONE;
1812 }
1813 pack_island_params.scale_to_fit = RNA_boolean_get(op->ptr, "scale");
1814 pack_island_params.merge_overlap = RNA_boolean_get(op->ptr, "merge_overlap");
1815
1816 if (RNA_boolean_get(op->ptr, "pin")) {
1817 pack_island_params.pin_method = eUVPackIsland_PinMethod(RNA_enum_get(op->ptr, "pin_method"));
1818 }
1819 else {
1820 pack_island_params.pin_method = ED_UVPACK_PIN_NONE;
1821 }
1822
1823 pack_island_params.margin_method = eUVPackIsland_MarginMethod(
1824 RNA_enum_get(op->ptr, "margin_method"));
1825 pack_island_params.margin = RNA_float_get(op->ptr, "margin");
1826 pack_island_params.shape_method = eUVPackIsland_ShapeMethod(
1827 RNA_enum_get(op->ptr, "shape_method"));
1828
1829 if (udim_source == PACK_UDIM_SRC_ACTIVE) {
1830 pack_island_params.setUDIMOffsetFromSpaceImage(sima);
1831 }
1832
1833 /* Needed even when jobs aren't used, see: #146195. */
1834 G.is_break = false;
1835
1836 if (pid->use_job) {
1837 /* Setup job. */
1838 if (pid->wm->op_undo_depth == 0) {
1839 /* The job must do its own undo push. */
1840 pid->undo_context = C;
1841 pid->undo_str = op->type->name;
1842 }
1843
1844 wmJob *wm_job = WM_jobs_get(
1845 pid->wm, CTX_wm_window(C), scene, "Packing UVs...", WM_JOB_PROGRESS, WM_JOB_TYPE_UV_PACK);
1847 WM_jobs_timer(wm_job, 0.1, 0, 0);
1850
1851 WM_cursor_wait(true);
1852 WM_jobs_start(CTX_wm_manager(C), wm_job);
1853 return OPERATOR_FINISHED;
1854 }
1855
1856 wmJobWorkerStatus worker_status = {};
1857 pack_islands_startjob(pid, &worker_status);
1860
1861 return OPERATOR_FINISHED;
1862}
1863
1866 "SCALED",
1867 0,
1868 "Scaled",
1869 "Use scale of existing UVs to multiply margin"},
1870 {ED_UVPACK_MARGIN_ADD, "ADD", 0, "Add", "Just add the margin, ignoring any UV scale"},
1872 "FRACTION",
1873 0,
1874 "Fraction",
1875 "Specify a precise fraction of final UV output"},
1876 {0, nullptr, 0, nullptr, nullptr},
1877};
1878
1880 {ED_UVPACK_ROTATION_ANY, "ANY", 0, "Any", "Any angle is allowed for rotation"},
1882 "CARDINAL",
1883 0,
1884 "Cardinal",
1885 "Only 90 degree rotations are allowed"},
1887
1888#define PACK_ROTATE_METHOD_AXIS_ALIGNED_OFFSET 3
1890 "AXIS_ALIGNED",
1891 0,
1892 "Axis-aligned",
1893 "Rotated to a minimal rectangle, either vertical or horizontal"},
1895 "AXIS_ALIGNED_X",
1896 0,
1897 "Axis-aligned (Horizontal)",
1898 "Rotate islands to be aligned horizontally"},
1900 "AXIS_ALIGNED_Y",
1901 0,
1902 "Axis-aligned (Vertical)",
1903 "Rotate islands to be aligned vertically"},
1904 {0, nullptr, 0, nullptr, nullptr},
1905};
1906
1908 {ED_UVPACK_SHAPE_CONCAVE, "CONCAVE", 0, "Exact Shape (Concave)", "Uses exact geometry"},
1909 {ED_UVPACK_SHAPE_CONVEX, "CONVEX", 0, "Boundary Shape (Convex)", "Uses convex hull"},
1911 {ED_UVPACK_SHAPE_AABB, "AABB", 0, "Bounding Box", "Uses bounding boxes"},
1912 {0, nullptr, 0, nullptr, nullptr},
1913};
1914
1922 {ED_UVPACK_PIN_LOCK_SCALE, "SCALE", 0, "Scale", "Pinned islands won't rescale"},
1923 {ED_UVPACK_PIN_LOCK_ROTATION, "ROTATION", 0, "Rotation", "Pinned islands won't rotate"},
1925 "ROTATION_SCALE",
1926 0,
1927 "Rotation and Scale",
1928 "Pinned islands will translate only"},
1929 {ED_UVPACK_PIN_LOCK_ALL, "LOCKED", 0, "All", "Pinned islands are locked in place"},
1930 {0, nullptr, 0, nullptr, nullptr},
1931};
1932
1933static void uv_pack_islands_ui(bContext * /*C*/, wmOperator *op)
1934{
1935 ui::Layout &layout = *op->layout;
1936 layout.use_property_split_set(true);
1937 layout.use_property_decorate_set(false);
1938 layout.prop(op->ptr, "shape_method", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1939 layout.prop(op->ptr, "scale", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1940 {
1941 layout.prop(op->ptr, "rotate", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1942 ui::Layout &row = layout.row(true);
1943 row.active_set(RNA_boolean_get(op->ptr, "rotate"));
1944 row.prop(op->ptr, "rotate_method", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1945 layout.separator();
1946 }
1947 layout.prop(op->ptr, "margin_method", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1948 layout.prop(op->ptr, "margin", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1949 layout.separator();
1950 {
1951 layout.prop(op->ptr, "pin", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1952 ui::Layout &row = layout.row(true);
1953 row.active_set(RNA_boolean_get(op->ptr, "pin"));
1954 row.prop(op->ptr, "pin_method", UI_ITEM_NONE, IFACE_("Lock Method"), ICON_NONE);
1955 layout.separator();
1956 }
1957 layout.prop(op->ptr, "merge_overlap", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1958 layout.prop(op->ptr, "udim_source", UI_ITEM_NONE, std::nullopt, ICON_NONE);
1959 layout.separator();
1960}
1961
1963{
1964 return WM_operator_props_popup_confirm_ex(C, op, event, IFACE_("Pack Islands"), IFACE_("Pack"));
1965}
1966
1968{
1969 static const EnumPropertyItem pack_target[] = {
1970 {PACK_UDIM_SRC_CLOSEST, "CLOSEST_UDIM", 0, "Closest UDIM", "Pack islands to closest UDIM"},
1972 "ACTIVE_UDIM",
1973 0,
1974 "Active UDIM",
1975 "Pack islands to active UDIM image tile or UDIM grid tile where 2D cursor is located"},
1977 "ORIGINAL_AABB",
1978 0,
1979 "Original bounding box",
1980 "Pack to starting bounding box of islands"},
1981 {PACK_CUSTOM_REGION, "CUSTOM_REGION", 0, "Custom Region", "Pack islands to custom region"},
1982 {0, nullptr, 0, nullptr, nullptr},
1983 };
1984 /* identifiers */
1985 ot->name = "Pack Islands";
1986 ot->idname = "UV_OT_pack_islands";
1987 ot->description =
1988 "Transform all islands so that they fill up the UV/UDIM space as much as possible";
1989
1990#ifdef USE_INTERACTIVE_PACK
1992#else
1993 /* The operator will handle undo, so the job system can push() it after the job completes. */
1994 ot->flag = OPTYPE_REGISTER;
1995#endif
1996
1997 /* API callbacks. */
1998 ot->exec = pack_islands_exec;
1999
2000#ifdef USE_INTERACTIVE_PACK
2002#else
2004#endif
2005 ot->ui = uv_pack_islands_ui;
2007
2008 /* properties */
2009 RNA_def_enum(ot->srna, "udim_source", pack_target, PACK_UDIM_SRC_CLOSEST, "Pack to", "");
2010 RNA_def_boolean(ot->srna, "rotate", true, "Rotate", "Rotate islands to improve layout");
2011 RNA_def_enum(ot->srna,
2012 "rotate_method",
2015 "Rotation Method",
2016 "");
2017 RNA_def_boolean(ot->srna, "scale", true, "Scale", "Scale islands to fill unit square");
2019 ot->srna, "merge_overlap", false, "Merge Overlapping", "Overlapping islands stick together");
2020 RNA_def_enum(ot->srna,
2021 "margin_method",
2024 "Margin Method",
2025 "");
2027 ot->srna, "margin", 0.001f, 0.0f, 1.0f, "Margin", "Space between islands", 0.0f, 1.0f);
2029 "pin",
2030 false,
2031 "Lock Pinned Islands",
2032 "Constrain islands containing any pinned UV's");
2033 RNA_def_enum(ot->srna,
2034 "pin_method",
2037 "Pin Method",
2038 "");
2039 RNA_def_enum(ot->srna,
2040 "shape_method",
2043 "Shape Method",
2044 "");
2045}
2046
2048
2049/* -------------------------------------------------------------------- */
2052
2054{
2055 const Main *bmain = CTX_data_main(C);
2056 const Scene *scene = CTX_data_scene(C);
2057 ViewLayer *view_layer = CTX_data_view_layer(C);
2058 ToolSettings *ts = scene->toolsettings;
2059 const bool synced_selection = (ts->uv_flag & UV_FLAG_SELECT_SYNC) != 0;
2060
2061 UnwrapOptions options = unwrap_options_get(nullptr, nullptr, ts);
2062 options.topology_from_uvs = true;
2063 options.only_selected_faces = true;
2064 options.only_selected_uvs = true;
2065 options.fill_holes = false;
2066 options.correct_aspect = true;
2067
2069 *bmain, scene, view_layer, CTX_wm_view3d(C));
2070
2071 if (!uvedit_have_selection_multi(scene, objects, &options)) {
2072 return OPERATOR_CANCELLED;
2073 }
2074
2075 /* RNA props */
2076 const bool scale_uv = RNA_boolean_get(op->ptr, "scale_uv");
2077 const bool shear = RNA_boolean_get(op->ptr, "shear");
2078
2079 ParamHandle *handle = construct_param_handle_multi(scene, objects, &options);
2080 geometry::uv_parametrizer_average(handle, false, scale_uv, shear);
2082 delete (handle);
2083
2084 for (Object *obedit : objects) {
2086 if (synced_selection && (bm->totvertsel == 0)) {
2087 continue;
2088 }
2089
2091 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
2092 }
2093 return OPERATOR_FINISHED;
2094}
2095
2097{
2098 /* identifiers */
2099 ot->name = "Average Islands Scale";
2100 ot->idname = "UV_OT_average_islands_scale";
2101 ot->description = "Average the size of separate UV islands, based on their area in 3D space";
2102
2104
2105 /* API callbacks. */
2108
2109 /* properties */
2110 RNA_def_boolean(ot->srna, "scale_uv", false, "Non-Uniform", "Scale U and V independently");
2111 RNA_def_boolean(ot->srna, "shear", false, "Shear", "Reduce shear within islands");
2112}
2113
2115
2116/* -------------------------------------------------------------------- */
2119
2120static struct {
2123 wmTimer *timer;
2124} g_live_unwrap = {nullptr};
2125
2127{
2128 /* NOTE: don't validate the timer, assume the timer passed in is valid. */
2129 return g_live_unwrap.timer == timer;
2130}
2131
2139{
2140 if (g_live_unwrap.timer == nullptr) {
2141 return false;
2142 }
2143 if (BLI_findindex(&wm->runtime->timers, g_live_unwrap.timer) != -1) {
2144 return false;
2145 }
2146 g_live_unwrap.timer = nullptr;
2147 return true;
2148}
2149
2150void ED_uvedit_live_unwrap_begin(Scene *scene, Object *obedit, wmWindow *win_modal)
2151{
2152 ParamHandle *handle = nullptr;
2153
2154 if (!ED_uvedit_test(obedit)) {
2155 return;
2156 }
2157
2158 UnwrapOptions options = unwrap_options_get(nullptr, obedit, scene->toolsettings);
2159 options.topology_from_uvs = false;
2160 options.only_selected_faces = false;
2161 options.only_selected_uvs = false;
2162
2163 if (options.use_subsurf) {
2164 handle = construct_param_handle_subsurfed(scene, obedit, &options, nullptr);
2165 }
2166 else {
2167 handle = construct_param_handle(scene, obedit, &options, nullptr);
2168 }
2169
2170 if (options.use_slim) {
2171 options.slim.no_flip = false;
2172 options.slim.skip_init = true;
2174
2175 if (win_modal) {
2176 wmWindowManager *wm = G_MAIN->wm.first();
2177 /* Clear in the unlikely event this is still set. */
2179 BLI_assert(!g_live_unwrap.timer);
2180 g_live_unwrap.timer = WM_event_timer_add(wm, win_modal, TIMER, 0.01f);
2181 }
2182 }
2183 else {
2185 handle, true, options.use_abf, options.use_original_bounds);
2186 }
2187
2188 /* Create or increase size of g_live_unwrap.handles array */
2189 if (g_live_unwrap.handles == nullptr) {
2190 g_live_unwrap.len_alloc = 32;
2192 g_live_unwrap.len_alloc, "uvedit_live_unwrap_liveHandles");
2193 g_live_unwrap.len = 0;
2194 }
2195 if (g_live_unwrap.len >= g_live_unwrap.len_alloc) {
2196 g_live_unwrap.len_alloc *= 2;
2197 g_live_unwrap.handles = static_cast<ParamHandle **>(MEM_realloc_uninitialized(
2198 g_live_unwrap.handles, sizeof(ParamHandle *) * g_live_unwrap.len_alloc));
2199 }
2200 g_live_unwrap.handles[g_live_unwrap.len] = handle;
2201 g_live_unwrap.len++;
2202}
2203
2205{
2206 if (g_live_unwrap.handles) {
2207 for (int i = 0; i < g_live_unwrap.len; i++) {
2208 if (uv_parametrizer_is_slim(g_live_unwrap.handles[i])) {
2210 }
2211 else {
2212 geometry::uv_parametrizer_lscm_solve(g_live_unwrap.handles[i], nullptr, nullptr);
2213 }
2214
2216 }
2217 }
2218}
2219
2220void ED_uvedit_live_unwrap_end(const bool cancel)
2221{
2222 if (g_live_unwrap.timer) {
2223 wmWindowManager *wm = G_MAIN->wm.first();
2225 if (g_live_unwrap.timer) {
2226 wmWindow *win = g_live_unwrap.timer->win;
2228 g_live_unwrap.timer = nullptr;
2229 }
2230 }
2231
2232 if (g_live_unwrap.handles) {
2233 for (int i = 0; i < g_live_unwrap.len; i++) {
2234 if (uv_parametrizer_is_slim(g_live_unwrap.handles[i])) {
2236 }
2237 else {
2239 }
2240
2241 if (cancel) {
2243 }
2244 delete (g_live_unwrap.handles[i]);
2245 }
2246 MEM_delete(g_live_unwrap.handles);
2247 g_live_unwrap.handles = nullptr;
2248 g_live_unwrap.len = 0;
2249 g_live_unwrap.len_alloc = 0;
2250 }
2251}
2252
2254
2255/* -------------------------------------------------------------------- */
2258
2259#define VIEW_ON_EQUATOR 0
2260#define VIEW_ON_POLES 1
2261#define ALIGN_TO_OBJECT 2
2262
2263#define POLAR_ZX 0
2264#define POLAR_ZY 1
2265
2266enum {
2268 FAN = 1,
2269};
2270
2271static void uv_map_transform_calc_bounds(BMesh *bm, float r_min[3], float r_max[3])
2272{
2273 BMFace *efa;
2274 BMIter iter;
2275 INIT_MINMAX(r_min, r_max);
2276 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2278 BM_face_calc_bounds_expand(efa, r_min, r_max);
2279 }
2280 }
2281}
2282
2283static void uv_map_transform_calc_center_median(BMesh *bm, float r_center[3])
2284{
2285 BMFace *efa;
2286 BMIter iter;
2287 uint center_accum_num = 0;
2288 zero_v3(r_center);
2289 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2291 float center[3];
2292 BM_face_calc_center_median(efa, center);
2293 add_v3_v3(r_center, center);
2294 center_accum_num += 1;
2295 }
2296 }
2297 mul_v3_fl(r_center, 1.0f / float(center_accum_num));
2298}
2299
2300static void uv_map_transform_center(const Scene *scene,
2301 View3D *v3d,
2302 Object *ob,
2303 BMesh *bm,
2304 float r_center[3],
2305 float r_bounds[2][3])
2306{
2307 /* only operates on the edit object - this is all that's needed now */
2308 const int around = (v3d) ? scene->toolsettings->transform_pivot_point :
2310
2311 float bounds[2][3];
2312 INIT_MINMAX(bounds[0], bounds[1]);
2313 bool is_minmax_set = false;
2314
2315 switch (around) {
2316 case V3D_AROUND_CENTER_BOUNDS: /* bounding box center */
2317 {
2319 is_minmax_set = true;
2320 mid_v3_v3v3(r_center, bounds[0], bounds[1]);
2321 break;
2322 }
2325 break;
2326 }
2327 case V3D_AROUND_CURSOR: /* cursor center */
2328 {
2329 invert_m4_m4(ob->runtime->world_to_object.ptr(), ob->object_to_world().ptr());
2330 mul_v3_m4v3(r_center, ob->world_to_object().ptr(), scene->cursor.location);
2331 break;
2332 }
2333 case V3D_AROUND_ACTIVE: {
2334 BMEditSelection ese;
2335 if (BM_select_history_active_get(bm, &ese)) {
2336 BM_editselection_center(&ese, r_center);
2337 break;
2338 }
2340 }
2341 case V3D_AROUND_LOCAL_ORIGINS: /* object center */
2342 default:
2343 zero_v3(r_center);
2344 break;
2345 }
2346
2347 /* if this is passed, always set! */
2348 if (r_bounds) {
2349 if (!is_minmax_set) {
2351 }
2352 copy_v3_v3(r_bounds[0], bounds[0]);
2353 copy_v3_v3(r_bounds[1], bounds[1]);
2354 }
2355}
2356
2357static void uv_map_rotation_matrix_ex(float result[4][4],
2358 RegionView3D *rv3d,
2359 Object *ob,
2360 float upangledeg,
2361 float sideangledeg,
2362 float radius,
2363 const float offset[4])
2364{
2365 float rotup[4][4], rotside[4][4], viewmatrix[4][4], rotobj[4][4];
2366 float sideangle = 0.0f, upangle = 0.0f;
2367
2368 /* get rotation of the current view matrix */
2369 if (rv3d) {
2370 copy_m4_m4(viewmatrix, rv3d->viewmat);
2371 }
2372 else {
2373 unit_m4(viewmatrix);
2374 }
2375
2376 /* but shifting */
2377 zero_v3(viewmatrix[3]);
2378
2379 /* get rotation of the current object matrix */
2380 copy_m4_m4(rotobj, ob->object_to_world().ptr());
2381 zero_v3(rotobj[3]);
2382
2383 /* but shifting */
2384 add_v4_v4(rotobj[3], offset);
2385 rotobj[3][3] = 0.0f;
2386
2387 zero_m4(rotup);
2388 zero_m4(rotside);
2389
2390 /* Compensate front/side.. against opengl x,y,z world definition.
2391 * This is "a sledgehammer to crack a nut" (overkill), a few plus minus 1 will do here.
2392 * I wanted to keep the reason here, so we're rotating. */
2393 sideangle = float(M_PI) * (sideangledeg + 180.0f) / 180.0f;
2394 rotside[0][0] = cosf(sideangle);
2395 rotside[0][1] = -sinf(sideangle);
2396 rotside[1][0] = sinf(sideangle);
2397 rotside[1][1] = cosf(sideangle);
2398 rotside[2][2] = 1.0f;
2399
2400 upangle = float(M_PI) * upangledeg / 180.0f;
2401 rotup[1][1] = cosf(upangle) / radius;
2402 rotup[1][2] = -sinf(upangle) / radius;
2403 rotup[2][1] = sinf(upangle) / radius;
2404 rotup[2][2] = cosf(upangle) / radius;
2405 rotup[0][0] = 1.0f / radius;
2406
2407 /* Calculate transforms. */
2408 mul_m4_series(result, rotup, rotside, viewmatrix, rotobj);
2409}
2410
2411static void uv_map_transform(bContext *C, wmOperator *op, float rotmat[3][3])
2412{
2413 Object *obedit = CTX_data_edit_object(C);
2415
2416 const int align = RNA_enum_get(op->ptr, "align");
2417 const int direction = RNA_enum_get(op->ptr, "direction");
2418 const float radius = RNA_struct_find_property(op->ptr, "radius") ?
2419 RNA_float_get(op->ptr, "radius") :
2420 1.0f;
2421
2422 /* Be compatible to the "old" sphere/cylinder mode. */
2423 if (direction == ALIGN_TO_OBJECT) {
2424 unit_m3(rotmat);
2425
2426 if (align == POLAR_ZY) {
2427 rotmat[0][0] = 0.0f;
2428 rotmat[0][1] = 1.0f;
2429 rotmat[1][0] = -1.0f;
2430 rotmat[1][1] = 0.0f;
2431 }
2432 return;
2433 }
2434
2435 const float up_angle_deg = (direction == VIEW_ON_EQUATOR) ? 90.0f : 0.0f;
2436 const float side_angle_deg = (align == POLAR_ZY) == (direction == VIEW_ON_EQUATOR) ? 90.0f :
2437 0.0f;
2438 const float offset[4] = {0};
2439 float rotmat4[4][4];
2440 uv_map_rotation_matrix_ex(rotmat4, rv3d, obedit, up_angle_deg, side_angle_deg, radius, offset);
2441 copy_m3_m4(rotmat, rotmat4);
2442}
2443
2444static void uv_transform_properties(wmOperatorType *ot, int radius)
2445{
2446 static const EnumPropertyItem direction_items[] = {
2447 {VIEW_ON_EQUATOR, "VIEW_ON_EQUATOR", 0, "View on Equator", "3D view is on the equator"},
2448 {VIEW_ON_POLES, "VIEW_ON_POLES", 0, "View on Poles", "3D view is on the poles"},
2450 "ALIGN_TO_OBJECT",
2451 0,
2452 "Align to Object",
2453 "Align according to object transform"},
2454 {0, nullptr, 0, nullptr, nullptr},
2455 };
2456 static const EnumPropertyItem align_items[] = {
2457 {POLAR_ZX, "POLAR_ZX", 0, "Polar ZX", "Polar 0 is X"},
2458 {POLAR_ZY, "POLAR_ZY", 0, "Polar ZY", "Polar 0 is Y"},
2459 {0, nullptr, 0, nullptr, nullptr},
2460 };
2461
2462 static const EnumPropertyItem pole_items[] = {
2463 {PINCH, "PINCH", 0, "Pinch", "UVs are pinched at the poles"},
2464 {FAN, "FAN", 0, "Fan", "UVs are fanned at the poles"},
2465 {0, nullptr, 0, nullptr, nullptr},
2466 };
2467
2468 RNA_def_enum(ot->srna,
2469 "direction",
2470 direction_items,
2472 "Direction",
2473 "Direction of the sphere or cylinder");
2474 RNA_def_enum(ot->srna,
2475 "align",
2476 align_items,
2477 POLAR_ZX,
2478 "Align",
2479 "How to determine rotation around the pole");
2480 RNA_def_enum(ot->srna, "pole", pole_items, PINCH, "Pole", "How to handle faces at the poles");
2482 "seam",
2483 false,
2484 "Preserve Seams",
2485 "Separate projections by islands isolated by seams");
2486
2487 if (radius) {
2488 RNA_def_float(ot->srna,
2489 "radius",
2490 1.0f,
2491 0.0f,
2492 FLT_MAX,
2493 "Radius",
2494 "Radius of the sphere or cylinder",
2495 0.0001f,
2496 100.0f);
2497 }
2498}
2499
2501 const int cd_loop_uv_offset,
2502 const float aspect_y)
2503{
2504 BLI_assert(aspect_y != 1.0f); /* Nothing to do, should be handled by caller. */
2505 BLI_assert(aspect_y > 0.0f); /* Negative aspect ratios are not supported. */
2506
2507 BMLoop *l;
2508 BMIter iter;
2509 BM_ITER_ELEM (l, &iter, efa, BM_LOOPS_OF_FACE) {
2510 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
2511 if (aspect_y > 1.0f) {
2512 /* Reduce round-off error, i.e. `u = (u - 0.5) / aspect_y + 0.5`. */
2513 luv[0] = luv[0] / aspect_y + (0.5f - 0.5f / aspect_y);
2514 }
2515 else {
2516 /* Reduce round-off error, i.e. `v = (v - 0.5) * aspect_y + 0.5`. */
2517 luv[1] = luv[1] * aspect_y + (0.5f - 0.5f * aspect_y);
2518 }
2519 }
2520}
2521
2523{
2524 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_PROP_FLOAT2);
2525 const float aspect_y = ED_uvedit_get_aspect_y(ob);
2526 if (aspect_y == 1.0f) {
2527 /* Scaling by 1.0 has no effect. */
2528 return;
2529 }
2530 BMFace *efa;
2531 BMIter iter;
2532 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2534 shrink_loop_uv_by_aspect_ratio(efa, cd_loop_uv_offset, aspect_y);
2535 }
2536 }
2537}
2538
2540{
2541 const int materials_num = ob->totcol;
2542 if (materials_num == 0) {
2543 /* Without any materials, there is no aspect_y information and nothing to do. */
2544 return;
2545 }
2546
2547 Array<float, 16> material_aspect_y(materials_num, -1);
2548 /* Lazily initialize aspect ratio for materials. */
2549
2550 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_PROP_FLOAT2);
2551
2552 BMFace *efa;
2553 BMIter iter;
2554 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2555 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2556 continue;
2557 }
2558
2559 const int material_index = efa->mat_nr;
2560 if (material_index < 0 || material_index >= materials_num) [[unlikely]] {
2561 /* The index might be for a material slot which is not currently setup. */
2562 continue;
2563 }
2564
2565 float aspect_y = material_aspect_y[material_index];
2566 if (aspect_y == -1.0f) {
2567 /* Lazily initialize aspect ratio for materials. */
2568 float aspx, aspy;
2569 ED_uvedit_get_aspect_from_material(ob, material_index, &aspx, &aspy);
2570 aspect_y = aspx / aspy;
2571 material_aspect_y[material_index] = aspect_y;
2572 }
2573
2574 if (aspect_y == 1.0f) {
2575 /* Scaling by 1.0 has no effect. */
2576 continue;
2577 }
2578 shrink_loop_uv_by_aspect_ratio(efa, cd_loop_uv_offset, aspect_y);
2579 }
2580}
2581
2582#undef VIEW_ON_EQUATOR
2583#undef VIEW_ON_POLES
2584#undef ALIGN_TO_OBJECT
2585
2586#undef POLAR_ZX
2587#undef POLAR_ZY
2588
2590
2591/* -------------------------------------------------------------------- */
2594
2595static void uv_map_clip_correct_properties_ex(wmOperatorType *ot, bool clip_to_bounds)
2596{
2598 /* Optional, since not all unwrapping types need to be clipped. */
2599 if (clip_to_bounds) {
2601 "clip_to_bounds",
2602 false,
2603 "Clip to Bounds",
2604 "Clip UV coordinates to bounds after unwrapping");
2605 }
2607 "scale_to_bounds",
2608 false,
2609 "Scale to Bounds",
2610 "Scale UV coordinates to bounds after unwrapping");
2611}
2612
2617
2625static void uv_map_clip_correct(const Scene *scene,
2626 const Span<Object *> objects,
2627 wmOperator *op,
2628 bool per_face_aspect,
2629 bool only_selected_uvs)
2630{
2631 BMFace *efa;
2632 BMLoop *l;
2633 BMIter iter, liter;
2634 float dx, dy, min[2], max[2];
2635 const bool correct_aspect = RNA_boolean_get(op->ptr, "correct_aspect");
2636 const bool clip_to_bounds = (RNA_struct_find_property(op->ptr, "clip_to_bounds") &&
2637 RNA_boolean_get(op->ptr, "clip_to_bounds"));
2638 const bool scale_to_bounds = RNA_boolean_get(op->ptr, "scale_to_bounds");
2639
2641
2642 for (Object *ob : objects) {
2644 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
2645
2646 /* Correct for image aspect ratio. */
2647 if (correct_aspect) {
2648 if (per_face_aspect) {
2650 }
2651 else {
2652 correct_uv_aspect(ob, bm);
2653 }
2654 }
2655
2656 if (scale_to_bounds) {
2657 /* find uv limits */
2658 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2659 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2660 continue;
2661 }
2662
2663 if (only_selected_uvs && !uvedit_face_select_test(scene, bm, efa)) {
2664 continue;
2665 }
2666
2667 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2668 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
2669 minmax_v2v2_v2(min, max, luv);
2670 }
2671 }
2672 }
2673 else if (clip_to_bounds) {
2674 /* clipping and wrapping */
2675 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2676 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2677 continue;
2678 }
2679
2680 if (only_selected_uvs && !uvedit_face_select_test(scene, bm, efa)) {
2681 continue;
2682 }
2683
2684 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2685 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
2686 clamp_v2(luv, 0.0f, 1.0f);
2687 }
2688 }
2689 }
2690 }
2691
2692 if (scale_to_bounds) {
2693 /* rescale UV to be in 1/1 */
2694 dx = (max[0] - min[0]);
2695 dy = (max[1] - min[1]);
2696
2697 if (dx > 0.0f) {
2698 dx = 1.0f / dx;
2699 }
2700 if (dy > 0.0f) {
2701 dy = 1.0f / dy;
2702 }
2703
2704 if (dx == 1.0f && dy == 1.0f && min[0] == 0.0f && min[1] == 0.0f) {
2705 /* Scaling by 1.0, without translating, has no effect. */
2706 return;
2707 }
2708
2709 for (Object *ob : objects) {
2711 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
2712
2713 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
2714 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2715 continue;
2716 }
2717
2718 if (only_selected_uvs && !uvedit_face_select_test(scene, bm, efa)) {
2719 continue;
2720 }
2721
2722 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2723 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
2724
2725 luv[0] = (luv[0] - min[0]) * dx;
2726 luv[1] = (luv[1] - min[1]) * dy;
2727 }
2728 }
2729 }
2730 }
2731}
2732
2734
2735/* -------------------------------------------------------------------- */
2738
2739/* Assumes UV Map exists, doesn't run update functions. */
2740static void uvedit_unwrap(const Scene *scene,
2741 Object *obedit,
2742 const UnwrapOptions *options,
2743 int *r_count_changed,
2744 int *r_count_failed)
2745{
2748 return;
2749 }
2750
2751 bool use_subsurf;
2752 modifier_unwrap_state(obedit, options, &use_subsurf);
2753
2754 ParamHandle *handle;
2755 if (use_subsurf) {
2756 handle = construct_param_handle_subsurfed(scene, obedit, options, r_count_failed);
2757 }
2758 else {
2759 handle = construct_param_handle(scene, obedit, options, r_count_failed);
2760 }
2761
2762 if (options->use_slim) {
2764 handle, &options->slim, options->use_original_bounds, r_count_changed, r_count_failed);
2765 }
2766 else {
2768 handle, false, options->use_abf, options->use_original_bounds);
2769 geometry::uv_parametrizer_lscm_solve(handle, r_count_changed, r_count_failed);
2771 }
2772 if (options->use_original_bounds) {
2774 }
2775 else {
2776 geometry::uv_parametrizer_average(handle, true, false, false);
2777 }
2778
2780
2781 delete (handle);
2782}
2783
2784static void uvedit_unwrap_multi(const Scene *scene,
2785 const Span<Object *> objects,
2786 const UnwrapOptions *options,
2787 int *r_count_changed = nullptr,
2788 int *r_count_failed = nullptr)
2789{
2790 for (Object *obedit : objects) {
2791 uvedit_unwrap(scene, obedit, options, r_count_changed, r_count_failed);
2793 WM_main_add_notifier(NC_GEOM | ND_DATA, obedit->data);
2794 }
2795}
2796
2797void ED_uvedit_live_unwrap(const Scene *scene, const Span<Object *> objects)
2798{
2799 if (scene->toolsettings->edge_mode_live_unwrap) {
2800 UnwrapOptions options = unwrap_options_get(nullptr, nullptr, scene->toolsettings);
2801 options.topology_from_uvs = false;
2802 options.only_selected_faces = false;
2803 options.only_selected_uvs = false;
2804
2805 uvedit_unwrap_multi(scene, objects, &options, nullptr);
2806
2807 geometry::UVPackIsland_Params pack_island_params;
2808 pack_island_params.setFromUnwrapOptions(options);
2809 pack_island_params.rotate_method = ED_UVPACK_ROTATION_ANY;
2810 pack_island_params.pin_method = ED_UVPACK_PIN_IGNORE;
2811 pack_island_params.margin_method = ED_UVPACK_MARGIN_SCALED;
2812 pack_island_params.margin = scene->toolsettings->uvcalc_margin;
2813
2815 scene, objects, nullptr, nullptr, false, true, nullptr, &pack_island_params);
2816 }
2817}
2818
2819enum {
2822};
2823
2825{
2826 const Main *bmain = CTX_data_main(C);
2827 ViewLayer *view_layer = CTX_data_view_layer(C);
2828 const Scene *scene = CTX_data_scene(C);
2829 const bool sync_selection = (scene->toolsettings->uv_flag & UV_FLAG_SELECT_SYNC) != 0;
2830
2831 int reported_errors = 0;
2832
2834 *bmain, scene, view_layer, CTX_wm_view3d(C));
2835
2837
2838 UnwrapOptions options = unwrap_options_get(op, nullptr, nullptr);
2839 options.topology_from_uvs = false;
2840 options.only_selected_faces = true;
2841 options.only_selected_uvs = false;
2842
2843 /* We will report an error unless at least one object
2844 * has the subsurf modifier in the right place. */
2845 bool subsurf_error = options.use_subsurf;
2846
2847 if (CTX_wm_space_image(C)) {
2848 /* Inside the UV Editor, only unwrap selected UVs. */
2849 if (sync_selection) {
2850 /* It's important to include unselected faces so they are taken into account
2851 * when unwrapping adjacent UV's, so it's possible to select part of an island
2852 * without it becoming disconnected from the unselected region, see: #148536. */
2853 options.only_selected_faces = false;
2854 }
2855 options.only_selected_uvs = true;
2856 options.pin_unselected = true;
2857 }
2858
2859 if (!uvedit_have_selection_multi(scene, objects, &options)) {
2860 return OPERATOR_CANCELLED;
2861 }
2862
2863 /* add uvs if they don't exist yet */
2864 for (Object *obedit : objects) {
2865 float obsize[3];
2866 bool use_subsurf_final;
2867
2868 if (!uvedit_ensure_uvs(obedit)) {
2869 continue;
2870 }
2871
2872 if (subsurf_error) {
2873 /* Double up the check here but better keep uvedit_unwrap interface simple and not
2874 * pass operator for warning append. */
2875 modifier_unwrap_state(obedit, &options, &use_subsurf_final);
2876 if (use_subsurf_final) {
2877 subsurf_error = false;
2878 }
2879 }
2880
2881 if (reported_errors & (UNWRAP_ERROR_NONUNIFORM | UNWRAP_ERROR_NEGATIVE)) {
2882 continue;
2883 }
2884
2885 mat4_to_size(obsize, obedit->object_to_world().ptr());
2886 if (!(fabsf(obsize[0] - obsize[1]) < 1e-4f && fabsf(obsize[1] - obsize[2]) < 1e-4f)) {
2887 if ((reported_errors & UNWRAP_ERROR_NONUNIFORM) == 0) {
2888 BKE_report(op->reports,
2889 RPT_INFO,
2890 "Object has non-uniform scale, unwrap will operate on a non-scaled version of "
2891 "the mesh");
2892 reported_errors |= UNWRAP_ERROR_NONUNIFORM;
2893 }
2894 }
2895 else if (is_negative_m4(obedit->object_to_world().ptr())) {
2896 if ((reported_errors & UNWRAP_ERROR_NEGATIVE) == 0) {
2897 BKE_report(
2898 op->reports,
2899 RPT_INFO,
2900 "Object has negative scale, unwrap will operate on a non-flipped version of the mesh");
2901 reported_errors |= UNWRAP_ERROR_NEGATIVE;
2902 }
2903 }
2904 }
2905
2906 if (subsurf_error) {
2907 BKE_report(op->reports,
2908 RPT_INFO,
2909 "Subdivision Surface modifier needs to be first to work with unwrap");
2910 }
2911
2912 /* execute unwrap */
2913 int count_changed = 0;
2914 int count_failed = 0;
2915
2916 if (options.use_original_bounds) {
2918 /* Fall back to a regular unwrap (with packing) instead of aborting. */
2919 BKE_report(op->reports, RPT_WARNING, "Original Bounds could not stitch islands, ignoring");
2920 options.use_original_bounds = false;
2921 }
2922 }
2923 uvedit_unwrap_multi(scene, objects, &options, &count_changed, &count_failed);
2924 geometry::UVPackIsland_Params pack_island_params;
2925 pack_island_params.setFromUnwrapOptions(options);
2926 pack_island_params.rotate_method = ED_UVPACK_ROTATION_ANY;
2927 pack_island_params.pin_method = ED_UVPACK_PIN_IGNORE;
2928 pack_island_params.margin_method = eUVPackIsland_MarginMethod(
2929 RNA_enum_get(op->ptr, "margin_method"));
2930 pack_island_params.margin = RNA_float_get(op->ptr, "margin");
2931
2932 if (!options.use_original_bounds) {
2934 scene, objects, nullptr, nullptr, false, true, nullptr, &pack_island_params);
2935 }
2936
2937 if (count_failed == 0 && count_changed == 0) {
2938 BKE_report(op->reports,
2940 "Unwrap could not solve any island(s), edge seams may need to be added");
2941 }
2942 else if (count_failed) {
2943 BKE_reportf(op->reports,
2945 "Unwrap failed to solve %d of %d island(s), edge seams may need to be added",
2946 count_failed,
2947 count_changed + count_failed);
2948 }
2949
2950 return OPERATOR_FINISHED;
2951}
2952
2953static void unwrap_draw(bContext * /*C*/, wmOperator *op)
2954{
2955 ui::Layout &layout = *op->layout;
2956
2957 layout.use_property_split_set(true);
2958 layout.use_property_decorate_set(false);
2959
2960 /* Main draw call */
2961 PointerRNA ptr = RNA_pointer_create_discrete(nullptr, op->type->srna, op->properties);
2962
2963 ui::Layout *col = &layout.column(true);
2964 col->prop(&ptr, "method", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2965 bool is_slim = RNA_enum_get(op->ptr, "method") == UVCALC_UNWRAP_METHOD_MINIMUM_STRETCH;
2966
2967 if (is_slim) {
2968 col->prop(&ptr, "iterations", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2969 col->prop(&ptr, "no_flip", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2970
2971 col->separator();
2972 col->prop(&ptr, "use_weights", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2973
2974 if (RNA_boolean_get(op->ptr, "use_weights")) {
2975 col = &layout.column(true);
2976 col->prop(&ptr, "weight_group", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2977 col->prop(&ptr, "weight_factor", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2978 }
2979 }
2980 else {
2981 col->prop(&ptr, "fill_holes", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2982 }
2983
2984 col->separator();
2985 col->prop(&ptr, "use_subsurf_data", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2986 col->prop(&ptr, "use_original_bounds", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2987 const bool use_original_bounds = RNA_boolean_get(op->ptr, "use_original_bounds");
2988
2989 col->separator();
2990
2991 blender::ui::Layout &sub = col->column(false);
2992 sub.active_set(!use_original_bounds);
2993
2994 sub.prop(&ptr, "correct_aspect", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2995 sub.separator();
2996 sub.prop(&ptr, "margin_method", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2997 sub.prop(&ptr, "margin", UI_ITEM_NONE, std::nullopt, ICON_NONE);
2998}
2999
3001{
3002 const ToolSettings tool_settings_default = {};
3003 static char
3004 tool_settings_default_uvcalc_weight_group[sizeof(tool_settings_default.uvcalc_weight_group)];
3005 STRNCPY_UTF8(tool_settings_default_uvcalc_weight_group,
3006 tool_settings_default.uvcalc_weight_group);
3007
3008 static const EnumPropertyItem method_items[] = {
3009 {UVCALC_UNWRAP_METHOD_ANGLE, "ANGLE_BASED", 0, "Angle Based", ""},
3010 {UVCALC_UNWRAP_METHOD_CONFORMAL, "CONFORMAL", 0, "Conformal", ""},
3011 {UVCALC_UNWRAP_METHOD_MINIMUM_STRETCH, "MINIMUM_STRETCH", 0, "Minimum Stretch", ""},
3012 {0, nullptr, 0, nullptr, nullptr},
3013 };
3014
3015 /* identifiers */
3016 ot->name = "Unwrap";
3017 ot->description = "Unwrap the mesh of the object being edited";
3018 ot->idname = "UV_OT_unwrap";
3020
3021 /* API callbacks. */
3022 ot->exec = unwrap_exec;
3023 ot->poll = ED_operator_uvmap;
3024
3025 /* Only draw relevant ui elements */
3026 ot->ui = unwrap_draw;
3027
3028 /* properties */
3029 ot->prop = RNA_def_enum(
3030 ot->srna,
3031 "method",
3032 method_items,
3033 tool_settings_default.unwrapper,
3034 "Method",
3035 "Unwrapping method (Angle Based usually gives better results than Conformal, while "
3036 "being somewhat slower)");
3038 "fill_holes",
3039 tool_settings_default.uvcalc_flag & UVCALC_FILLHOLES,
3040 "Fill Holes",
3041 "Virtually fill holes in mesh before unwrapping, to better avoid overlaps and "
3042 "preserve symmetry");
3043
3045
3047 ot->srna,
3048 "use_subsurf_data",
3049 false,
3050 "Use Subdivision Surface",
3051 "Map UVs taking vertex position after Subdivision Surface modifier has been applied");
3053 "use_original_bounds",
3054 false,
3055 "Original Bounds",
3056 "Unwrap islands into their original bounds, instead of re-packing");
3057
3058 RNA_def_enum(ot->srna,
3059 "margin_method",
3062 "Margin Method",
3063 "");
3065 ot->srna, "margin", 0.001f, 0.0f, 1.0f, "Margin", "Space between islands", 0.0f, 1.0f);
3066
3067 /* SLIM only */
3069 "no_flip",
3070 tool_settings_default.uvcalc_flag & UVCALC_UNWRAP_NO_FLIP,
3071 "No Flip",
3072 "Prevent flipping UV's, "
3073 "flipping may lower distortion depending on the position of pins");
3074
3075 RNA_def_int(ot->srna,
3076 "iterations",
3077 tool_settings_default.uvcalc_iterations,
3078 0,
3079 10000,
3080 "Iterations",
3081 "Number of iterations when \"Minimum Stretch\" method is used",
3082 1,
3083 30);
3084
3086 "use_weights",
3087 tool_settings_default.uvcalc_flag & UVCALC_UNWRAP_USE_WEIGHTS,
3088 "Importance Weights",
3089 "Whether to take into account per-vertex importance weights");
3090 RNA_def_string(ot->srna,
3091 "weight_group",
3092 tool_settings_default_uvcalc_weight_group,
3094 "Weight Group",
3095 "Vertex group name for importance weights (modulating the deform)");
3097 ot->srna,
3098 "weight_factor",
3099 tool_settings_default.uvcalc_weight_factor,
3100 -10000.0,
3101 10000.0,
3102 "Weight Factor",
3103 "How much influence the weightmap has for weighted parameterization, 0 being no influence",
3104 -10.0,
3105 10.0);
3106}
3107
3109
3110/* -------------------------------------------------------------------- */
3113
3114/* Ignore all areas below this, as the UVs get zeroed. */
3115static const float smart_uv_project_area_ignore = 1e-12f;
3116
3118 float area;
3120};
3121
3122static int smart_uv_project_thickface_area_cmp_fn(const void *tf_a_p, const void *tf_b_p)
3123{
3124 const ThickFace *tf_a = static_cast<ThickFace *>(const_cast<void *>(tf_a_p));
3125 const ThickFace *tf_b = static_cast<ThickFace *>(const_cast<void *>(tf_b_p));
3126
3127 /* Ignore the area of small faces.
3128 * Also, order checks so `!isfinite(...)` values are counted as zero area. */
3129 if (!((tf_a->area > smart_uv_project_area_ignore) ||
3131 {
3132 return 0;
3133 }
3134
3135 if (tf_a->area < tf_b->area) {
3136 return 1;
3137 }
3138 if (tf_a->area > tf_b->area) {
3139 return -1;
3140 }
3141 return 0;
3142}
3143
3145 const ThickFace *thick_faces,
3146 const uint thick_faces_len,
3147 BMesh *bm,
3148 const float project_angle_limit_half_cos,
3149 const float project_angle_limit_cos,
3150 const float area_weight)
3151{
3152 if (thick_faces_len == 0) [[unlikely]] {
3153 return {};
3154 }
3155
3156 const float *project_normal = thick_faces[0].efa->no;
3157
3158 Vector<const ThickFace *> project_thick_faces;
3159 Vector<float3> project_normal_array;
3160
3162
3163 while (true) {
3164 for (int f_index = thick_faces_len - 1; f_index >= 0; f_index--) {
3165 if (BM_elem_flag_test(thick_faces[f_index].efa, BM_ELEM_TAG)) {
3166 continue;
3167 }
3168
3169 if (dot_v3v3(thick_faces[f_index].efa->no, project_normal) > project_angle_limit_half_cos) {
3170 project_thick_faces.append(&thick_faces[f_index]);
3171 BM_elem_flag_set(thick_faces[f_index].efa, BM_ELEM_TAG, true);
3172 }
3173 }
3174
3175 float average_normal[3] = {0.0f, 0.0f, 0.0f};
3176
3177 if (area_weight <= 0.0f) {
3178 for (int f_proj_index = 0; f_proj_index < project_thick_faces.size(); f_proj_index++) {
3179 const ThickFace *tf = project_thick_faces[f_proj_index];
3180 add_v3_v3(average_normal, tf->efa->no);
3181 }
3182 }
3183 else if (area_weight >= 1.0f) {
3184 for (int f_proj_index = 0; f_proj_index < project_thick_faces.size(); f_proj_index++) {
3185 const ThickFace *tf = project_thick_faces[f_proj_index];
3186 madd_v3_v3fl(average_normal, tf->efa->no, tf->area);
3187 }
3188 }
3189 else {
3190 for (int f_proj_index = 0; f_proj_index < project_thick_faces.size(); f_proj_index++) {
3191 const ThickFace *tf = project_thick_faces[f_proj_index];
3192 const float area_blend = (tf->area * area_weight) + (1.0f - area_weight);
3193 madd_v3_v3fl(average_normal, tf->efa->no, area_blend);
3194 }
3195 }
3196
3197 /* Avoid NAN. */
3198 if (normalize_v3(average_normal) != 0.0f) {
3199 project_normal_array.append(average_normal);
3200 }
3201
3202 /* Find the most unique angle that points away from other normals. */
3203 float anble_best = 1.0f;
3204 uint angle_best_index = 0;
3205
3206 for (int f_index = thick_faces_len - 1; f_index >= 0; f_index--) {
3207 if (BM_elem_flag_test(thick_faces[f_index].efa, BM_ELEM_TAG)) {
3208 continue;
3209 }
3210
3211 float angle_test = -1.0f;
3212 for (int p_index = 0; p_index < project_normal_array.size(); p_index++) {
3213 angle_test = max_ff(angle_test,
3214 dot_v3v3(project_normal_array[p_index], thick_faces[f_index].efa->no));
3215 }
3216
3217 if (angle_test < anble_best) {
3218 anble_best = angle_test;
3219 angle_best_index = f_index;
3220 }
3221 }
3222
3223 if (anble_best < project_angle_limit_cos) {
3224 project_normal = thick_faces[angle_best_index].efa->no;
3225 project_thick_faces.clear();
3226 project_thick_faces.append(&thick_faces[angle_best_index]);
3227 BM_elem_flag_enable(thick_faces[angle_best_index].efa, BM_ELEM_TAG);
3228 }
3229 else {
3230 if (project_normal_array.size() >= 1) {
3231 break;
3232 }
3233 }
3234 }
3235
3237
3238 return project_normal_array;
3239}
3240
3242{
3243 const Main *bmain = CTX_data_main(C);
3244 Scene *scene = CTX_data_scene(C);
3245 ViewLayer *view_layer = CTX_data_view_layer(C);
3246
3247 /* May be nullptr. */
3248 View3D *v3d = CTX_wm_view3d(C);
3249
3250 bool only_selected_uvs = false;
3251 if (CTX_wm_space_image(C)) {
3252 /* Inside the UV Editor, only project selected UVs. */
3253 only_selected_uvs = true;
3254 }
3255
3256 const float project_angle_limit = RNA_float_get(op->ptr, "angle_limit");
3257 const float island_margin = RNA_float_get(op->ptr, "island_margin");
3258 const float area_weight = RNA_float_get(op->ptr, "area_weight");
3259
3260 const float project_angle_limit_cos = cosf(project_angle_limit);
3261 const float project_angle_limit_half_cos = cosf(project_angle_limit / 2);
3262
3263 /* Memory arena for list links (cleared for each object). */
3265
3267 *bmain, scene, view_layer, v3d);
3268
3269 Vector<Object *> objects_changed;
3270
3271 BMFace *efa;
3272 BMIter iter;
3273
3274 for (const int ob_index : objects.index_range()) {
3275 Object *obedit = objects[ob_index];
3277 bool changed = false;
3278
3279 if (!uvedit_ensure_uvs(obedit)) {
3280 continue;
3281 }
3282
3283 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
3284 BLI_assert(offsets.uv >= 0);
3286
3287 uint thick_faces_len = 0;
3288 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3289 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
3290 continue;
3291 }
3292
3293 if (only_selected_uvs) {
3294 if (!uvedit_face_select_test(scene, bm, efa)) {
3295 uvedit_face_select_disable(scene, bm, efa);
3296 continue;
3297 }
3298 }
3299
3300 thick_faces[thick_faces_len].area = BM_face_calc_area(efa);
3301 thick_faces[thick_faces_len].efa = efa;
3302 thick_faces_len++;
3303 }
3304
3305 qsort(thick_faces, thick_faces_len, sizeof(ThickFace), smart_uv_project_thickface_area_cmp_fn);
3306
3307 /* Remove all zero area faces. */
3308 while ((thick_faces_len > 0) &&
3309 !(thick_faces[thick_faces_len - 1].area > smart_uv_project_area_ignore))
3310 {
3311
3312 /* Zero UVs so they don't overlap with other faces being unwrapped. */
3313 BMIter liter;
3314 BMLoop *l;
3315 BM_ITER_ELEM (l, &liter, thick_faces[thick_faces_len - 1].efa, BM_LOOPS_OF_FACE) {
3316 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
3317 zero_v2(luv);
3318 changed = true;
3319 }
3320
3321 thick_faces_len -= 1;
3322 }
3323
3325 thick_faces,
3326 thick_faces_len,
3327 bm,
3328 project_angle_limit_half_cos,
3329 project_angle_limit_cos,
3330 area_weight);
3331
3332 if (project_normal_array.is_empty()) {
3333 MEM_delete(thick_faces);
3334 continue;
3335 }
3336
3337 /* After finding projection vectors, we find the uv positions. */
3338 LinkNode **thickface_project_groups = MEM_new_array_zeroed<LinkNode *>(
3339 project_normal_array.size(), __func__);
3340
3341 BLI_memarena_clear(arena);
3342
3343 for (int f_index = thick_faces_len - 1; f_index >= 0; f_index--) {
3344 const float *f_normal = thick_faces[f_index].efa->no;
3345
3346 float angle_best = dot_v3v3(f_normal, project_normal_array[0]);
3347 uint angle_best_index = 0;
3348
3349 for (int p_index = 1; p_index < project_normal_array.size(); p_index++) {
3350 const float angle_test = dot_v3v3(f_normal, project_normal_array[p_index]);
3351 if (angle_test > angle_best) {
3352 angle_best = angle_test;
3353 angle_best_index = p_index;
3354 }
3355 }
3356
3358 &thickface_project_groups[angle_best_index], &thick_faces[f_index], arena);
3359 }
3360
3361 for (int p_index = 0; p_index < project_normal_array.size(); p_index++) {
3362 if (thickface_project_groups[p_index] == nullptr) {
3363 continue;
3364 }
3365
3366 float axis_mat[3][3];
3367 axis_dominant_v3_to_m3(axis_mat, project_normal_array[p_index]);
3368
3369 for (LinkNode *list = thickface_project_groups[p_index]; list; list = list->next) {
3370 ThickFace *tf = static_cast<ThickFace *>(list->link);
3371 BMIter liter;
3372 BMLoop *l;
3373 BM_ITER_ELEM (l, &liter, tf->efa, BM_LOOPS_OF_FACE) {
3374 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
3375 mul_v2_m3v3(luv, axis_mat, l->v->co);
3376 }
3377 changed = true;
3378 }
3379 }
3380
3381 MEM_delete(thick_faces);
3382
3383 /* No need to free the lists in 'thickface_project_groups' values as the 'arena' is used. */
3384 MEM_delete(thickface_project_groups);
3385
3386 if (changed) {
3387 objects_changed.append(obedit);
3388 }
3389 }
3390
3391 BLI_memarena_free(arena);
3392
3393 /* Pack islands & Stretch to UV bounds */
3394 if (!objects_changed.is_empty()) {
3395
3396 scene->toolsettings->uvcalc_margin = island_margin;
3397
3398 /* Depsgraph refresh functions are called here. */
3399 const bool correct_aspect = RNA_boolean_get(op->ptr, "correct_aspect");
3400
3402 params.rotate_method = eUVPackIsland_RotationMethod(RNA_enum_get(op->ptr, "rotate_method"));
3403 params.only_selected_uvs = only_selected_uvs;
3404 params.only_selected_faces = true;
3405 params.correct_aspect = correct_aspect;
3406 params.use_seams = true;
3407 params.margin_method = eUVPackIsland_MarginMethod(RNA_enum_get(op->ptr, "margin_method"));
3408 params.margin = RNA_float_get(op->ptr, "island_margin");
3409
3411 scene, objects_changed, nullptr, nullptr, false, true, nullptr, &params);
3412
3413 /* #uvedit_pack_islands_multi only supports `per_face_aspect = false`. */
3414 const bool per_face_aspect = false;
3415 uv_map_clip_correct(scene, objects_changed, op, per_face_aspect, only_selected_uvs);
3416 }
3417
3418 return OPERATOR_FINISHED;
3419}
3420
3422{
3424 C, op, event, IFACE_("Smart UV Project"), IFACE_("Unwrap"));
3425}
3426
3428{
3429 PropertyRNA *prop;
3430
3431 /* identifiers */
3432 ot->name = "Smart UV Project";
3433 ot->idname = "UV_OT_smart_project";
3434 ot->description = "Projection unwraps the selected faces of mesh objects";
3435
3437
3438 /* API callbacks. */
3440 ot->poll = ED_operator_uvmap;
3442
3443 /* properties */
3444 prop = RNA_def_float_rotation(ot->srna,
3445 "angle_limit",
3446 0,
3447 nullptr,
3448 DEG2RADF(0.0f),
3449 DEG2RADF(90.0f),
3450 "Angle Limit",
3451 "Lower for more projection groups, higher for less distortion",
3452 DEG2RADF(0.0f),
3453 DEG2RADF(89.0f));
3455
3456 RNA_def_enum(ot->srna,
3457 "margin_method",
3460 "Margin Method",
3461 "");
3462 RNA_def_enum(ot->srna,
3463 "rotate_method",
3464 /* Only show aligned options as the rotation from a projection
3465 * generated from a direction vector isn't meaningful. */
3468 "Rotation Method",
3469 "");
3470 RNA_def_float(ot->srna,
3471 "island_margin",
3472 0.0f,
3473 0.0f,
3474 1.0f,
3475 "Island Margin",
3476 "Margin to reduce bleed from adjacent islands",
3477 0.0f,
3478 1.0f);
3479 RNA_def_float(ot->srna,
3480 "area_weight",
3481 0.0f,
3482 0.0f,
3483 1.0f,
3484 "Area Weight",
3485 "Weight projection's vector by faces with larger areas",
3486 0.0f,
3487 1.0f);
3488
3490}
3491
3493
3494/* -------------------------------------------------------------------- */
3497
3498static wmOperatorStatus uv_from_view_exec(bContext *C, wmOperator *op);
3499
3501{
3502 View3D *v3d = CTX_wm_view3d(C);
3504 const Camera *camera = ED_view3d_camera_data_get(v3d, rv3d);
3505 PropertyRNA *prop;
3506
3507 prop = RNA_struct_find_property(op->ptr, "camera_bounds");
3508 if (!RNA_property_is_set(op->ptr, prop)) {
3509 RNA_property_boolean_set(op->ptr, prop, (camera != nullptr));
3510 }
3511 prop = RNA_struct_find_property(op->ptr, "correct_aspect");
3512 if (!RNA_property_is_set(op->ptr, prop)) {
3513 RNA_property_boolean_set(op->ptr, prop, (camera == nullptr));
3514 }
3515
3516 return uv_from_view_exec(C, op);
3517}
3518
3520{
3521 const Main *bmain = CTX_data_main(C);
3522 ViewLayer *view_layer = CTX_data_view_layer(C);
3523 const Scene *scene = CTX_data_scene(C);
3524 ARegion *region = CTX_wm_region(C);
3525 View3D *v3d = CTX_wm_view3d(C);
3527 const Camera *camera = ED_view3d_camera_data_get(v3d, rv3d);
3528 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
3529 BMFace *efa;
3530 BMLoop *l;
3531 BMIter iter, liter;
3532 float rotmat[4][4];
3533 float objects_pos_offset[4];
3534
3535 const bool use_orthographic = RNA_boolean_get(op->ptr, "orthographic");
3536
3537 /* NOTE: objects that aren't touched are set to nullptr (to skip clipping). */
3539 *bmain, scene, view_layer, v3d);
3540
3541 if (use_orthographic) {
3542 /* Calculate average object position. */
3543 float objects_pos_avg[4] = {0};
3544
3545 for (Object *object : objects) {
3546 add_v4_v4(objects_pos_avg, object->object_to_world().location());
3547 }
3548
3549 mul_v4_fl(objects_pos_avg, 1.0f / objects.size());
3550 negate_v4_v4(objects_pos_offset, objects_pos_avg);
3551 }
3552
3553 Vector<Object *> changed_objects;
3554 Scene *scene_eval = const_cast<Scene *>(DEG_get_evaluated(depsgraph, scene));
3555
3556 for (Object *obedit : objects) {
3558 bool changed = false;
3559
3560 /* add uvs if they don't exist yet */
3561 if (!uvedit_ensure_uvs(obedit)) {
3562 continue;
3563 }
3564
3565 const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_PROP_FLOAT2);
3566
3567 Array<float3> vert_positions_storage;
3568 Object *obedit_eval = DEG_get_evaluated(depsgraph, obedit);
3570 depsgraph, scene_eval, obedit_eval, vert_positions_storage);
3571
3572 if (!vert_positions.is_empty()) {
3574 }
3575
3576 if (use_orthographic) {
3577 uv_map_rotation_matrix_ex(rotmat, rv3d, obedit, 90.0f, 0.0f, 1.0f, objects_pos_offset);
3578
3579 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3580 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
3581 continue;
3582 }
3583
3584 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
3585 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
3586 const float *v_co = vert_positions.is_empty() ?
3587 l->v->co :
3588 &vert_positions[BM_elem_index_get(l->v)].x;
3589 BKE_uvproject_from_view_ortho(luv, v_co, rotmat);
3590 }
3591 changed = true;
3592 }
3593 }
3594 else if (camera) {
3595 const bool camera_bounds = RNA_boolean_get(op->ptr, "camera_bounds");
3597 v3d->camera,
3598 obedit->object_to_world().ptr(),
3599 camera_bounds ? (scene->r.xsch * scene->r.xasp) : 1.0f,
3600 camera_bounds ? (scene->r.ysch * scene->r.yasp) : 1.0f);
3601
3602 if (uci) {
3603 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3604 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
3605 continue;
3606 }
3607
3608 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
3609 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
3610 const float *v_co = vert_positions.is_empty() ?
3611 l->v->co :
3612 &vert_positions[BM_elem_index_get(l->v)].x;
3613 BKE_uvproject_from_camera(luv, v_co, uci);
3614 }
3615 changed = true;
3616 }
3617
3619 }
3620 }
3621 else {
3622 copy_m4_m4(rotmat, obedit->object_to_world().ptr());
3623
3624 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3625 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
3626 continue;
3627 }
3628
3629 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
3630 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
3631 const float *v_co = vert_positions.is_empty() ?
3632 l->v->co :
3633 &vert_positions[BM_elem_index_get(l->v)].x;
3634 BKE_uvproject_from_view(luv, v_co, rv3d->persmat, rotmat, region->winx, region->winy);
3635 }
3636 changed = true;
3637 }
3638 }
3639
3640 if (changed) {
3641 changed_objects.append(obedit);
3643 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
3644 }
3645 }
3646
3647 if (changed_objects.is_empty()) {
3648 return OPERATOR_CANCELLED;
3649 }
3650
3651 const bool per_face_aspect = true;
3652 const bool only_selected_uvs = false;
3653 uv_map_clip_correct(scene, objects, op, per_face_aspect, only_selected_uvs);
3654 return OPERATOR_FINISHED;
3655}
3656
3658{
3660
3661 if (!ED_operator_uvmap(C)) {
3662 return false;
3663 }
3664
3665 return (rv3d != nullptr);
3666}
3667
3669{
3670 /* identifiers */
3671 ot->name = "Project from View";
3672 ot->idname = "UV_OT_project_from_view";
3673 ot->description = "Project the UV vertices of the mesh as seen in current 3D view";
3674
3676
3677 /* API callbacks. */
3679 ot->exec = uv_from_view_exec;
3680 ot->poll = uv_from_view_poll;
3681
3682 /* properties */
3683 RNA_def_boolean(ot->srna, "orthographic", false, "Orthographic", "Use orthographic projection");
3685 "camera_bounds",
3686 true,
3687 "Camera Bounds",
3688 "Map UVs to the camera region taking resolution and aspect into account");
3690}
3691
3693
3694/* -------------------------------------------------------------------- */
3697
3699{
3700 const Main *bmain = CTX_data_main(C);
3701 const Scene *scene = CTX_data_scene(C);
3702 ViewLayer *view_layer = CTX_data_view_layer(C);
3703 View3D *v3d = CTX_wm_view3d(C);
3704
3706 *bmain, scene, view_layer, v3d);
3707 for (Object *obedit : objects) {
3708 Mesh *mesh = id_cast<Mesh *>(obedit->data);
3710 if (bm->totfacesel == 0) {
3711 continue;
3712 }
3713
3714 /* add uvs if they don't exist yet */
3715 if (!uvedit_ensure_uvs(obedit)) {
3716 continue;
3717 }
3718
3720
3722 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
3723 }
3724
3725 return OPERATOR_FINISHED;
3726}
3727
3729{
3730 /* identifiers */
3731 ot->name = "Reset";
3732 ot->idname = "UV_OT_reset";
3733 ot->description = "Reset UV projection";
3734
3736
3737 /* API callbacks. */
3738 ot->exec = reset_exec;
3739 ot->poll = ED_operator_uvmap;
3740}
3741
3743
3744/* -------------------------------------------------------------------- */
3747
3748static void uv_map_mirror(BMFace *efa,
3749 const bool *regular,
3750 const bool fan,
3751 const int cd_loop_uv_offset)
3752{
3753 /* A heuristic to improve alignment of faces near the seam.
3754 * In simple terms, we're looking for faces which span more
3755 * than 0.5 units in the *u* coordinate.
3756 * If we find such a face, we try and improve the unwrapping
3757 * by adding (1.0, 0.0) onto some of the face's UVs.
3758 *
3759 * Note that this is only a heuristic. The property we're
3760 * attempting to maintain is that the winding of the face
3761 * in UV space corresponds with the handedness of the face
3762 * in 3D space w.r.t to the unwrapping. Even for triangles,
3763 * that property is somewhat complicated to evaluate. */
3764
3765 float right_u = -1.0e30f;
3766 BMLoop *l;
3767 BMIter liter;
3769 int j;
3770 BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, j) {
3771 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
3772 uvs[j] = luv;
3773 if (luv[0] >= 1.0f) {
3774 luv[0] -= 1.0f;
3775 }
3776 right_u = max_ff(right_u, luv[0]);
3777 }
3778
3779 float left_u = 1.0e30f;
3780 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
3781 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
3782 if (right_u <= luv[0] + 0.5f) {
3783 left_u = min_ff(left_u, luv[0]);
3784 }
3785 }
3786
3787 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
3788 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
3789 if (luv[0] + 0.5f < right_u) {
3790 if (2 * luv[0] + 1.0f < left_u + right_u) {
3791 luv[0] += 1.0f;
3792 }
3793 }
3794 }
3795 if (!fan) {
3796 return;
3797 }
3798
3799 /* Another heuristic, this time, we attempt to "fan"
3800 * the UVs of faces which pass through one of the poles
3801 * of the unwrapping. */
3802
3803 /* Need to recompute min and max. */
3804 float minmax_u[2] = {1.0e30f, -1.0e30f};
3805 int pole_count = 0;
3806 for (int i = 0; i < efa->len; i++) {
3807 if (regular[i]) {
3808 minmax_u[0] = min_ff(minmax_u[0], uvs[i][0]);
3809 minmax_u[1] = max_ff(minmax_u[1], uvs[i][0]);
3810 }
3811 else {
3812 pole_count++;
3813 }
3814 }
3815 if (ELEM(pole_count, 0, efa->len)) {
3816 return;
3817 }
3818 for (int i = 0; i < efa->len; i++) {
3819 if (regular[i]) {
3820 continue;
3821 }
3822 float u = 0.0f;
3823 float sum = 0.0f;
3824 const int i_plus = (i + 1) % efa->len;
3825 const int i_minus = (i + efa->len - 1) % efa->len;
3826 if (regular[i_plus]) {
3827 u += uvs[i_plus][0];
3828 sum += 1.0f;
3829 }
3830 if (regular[i_minus]) {
3831 u += uvs[i_minus][0];
3832 sum += 1.0f;
3833 }
3834 if (sum == 0) {
3835 u += minmax_u[0] + minmax_u[1];
3836 sum += 2.0f;
3837 }
3838 uvs[i][0] = u / sum;
3839 }
3840}
3841
3857 float branch;
3858};
3859
3870static float uv_sphere_project(const Scene *scene,
3871 BMesh *bm,
3872 BMFace *efa_init,
3873 const float center[3],
3874 const float rotmat[3][3],
3875 const bool fan,
3876 const BMUVOffsets &offsets,
3877 const bool only_selected_uvs,
3878 const bool use_seams,
3879 const float branch_init)
3880{
3881 float max_u = 0.0f;
3882 if (use_seams && BM_elem_flag_test(efa_init, BM_ELEM_TAG)) {
3883 return max_u;
3884 }
3885
3886 /* Similar to #BM_mesh_calc_face_groups with added connectivity information. */
3888 stack.append({efa_init, branch_init});
3889
3890 while (stack.size()) {
3891 UV_FaceBranch face_branch = stack.pop_last();
3892 BMFace *efa = face_branch.efa;
3893
3894 if (use_seams) {
3895 if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
3896 continue; /* Faces might be in the stack more than once. */
3897 }
3898
3899 BM_elem_flag_set(efa, BM_ELEM_TAG, true); /* Visited, don't consider again. */
3900 }
3901
3902 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
3903 continue; /* Unselected face, ignore. */
3904 }
3905
3906 if (only_selected_uvs) {
3907 if (!uvedit_face_select_test(scene, bm, efa)) {
3908 uvedit_face_select_disable(scene, bm, efa);
3909 continue; /* Unselected UV, ignore. */
3910 }
3911 }
3912
3913 /* Remember which UVs are at the pole. */
3915
3916 int i;
3917 BMLoop *l;
3918 BMIter iter;
3919 BM_ITER_ELEM_INDEX (l, &iter, efa, BM_LOOPS_OF_FACE, i) {
3920 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
3921 float pv[3];
3922 sub_v3_v3v3(pv, l->v->co, center);
3923 mul_m3_v3(rotmat, pv);
3924 regular[i] = map_to_sphere(&luv[0], &luv[1], pv[0], pv[1], pv[2]);
3925 if (!use_seams) {
3926 continue; /* Nothing more to do. */
3927 }
3928
3929 /* Move UV to correct branch. */
3930 luv[0] = luv[0] + ceilf(face_branch.branch - 0.5f - luv[0]);
3931 max_u = max_ff(max_u, luv[0]);
3932
3933 BMEdge *edge = l->e;
3934 if (BM_elem_flag_test(edge, BM_ELEM_SEAM)) {
3935 continue; /* Stop flood fill at seams. */
3936 }
3937
3938 /* Extend flood fill by pushing to stack. */
3939 BMFace *efa2;
3940 BMIter iter2;
3941 BM_ITER_ELEM (efa2, &iter2, edge, BM_FACES_OF_EDGE) {
3942 if (!BM_elem_flag_test(efa2, BM_ELEM_TAG)) {
3943 stack.append({efa2, luv[0]});
3944 }
3945 }
3946 }
3947 uv_map_mirror(efa, regular.data(), fan, offsets.uv);
3948 }
3949
3950 return max_u;
3951}
3952
3954{
3955 const Main *bmain = CTX_data_main(C);
3956 const Scene *scene = CTX_data_scene(C);
3957 View3D *v3d = CTX_wm_view3d(C);
3958
3959 bool only_selected_uvs = false;
3960 if (CTX_wm_space_image(C)) {
3961 /* Inside the UV Editor, only project selected UVs. */
3962 only_selected_uvs = true;
3963 }
3964
3965 ViewLayer *view_layer = CTX_data_view_layer(C);
3967 *bmain, scene, view_layer, v3d);
3968 for (Object *obedit : objects) {
3970 BMFace *efa;
3971 BMIter iter;
3972
3973 if (bm->totfacesel == 0) {
3974 continue;
3975 }
3976
3977 /* add uvs if they don't exist yet */
3978 if (!uvedit_ensure_uvs(obedit)) {
3979 continue;
3980 }
3981
3982 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
3983 float center[3], rotmat[3][3];
3984
3985 uv_map_transform(C, op, rotmat);
3986 uv_map_transform_center(scene, v3d, obedit, bm, center, nullptr);
3987
3988 const bool fan = RNA_enum_get(op->ptr, "pole");
3989 const bool use_seams = RNA_boolean_get(op->ptr, "seam");
3990
3991 if (use_seams) {
3993 }
3994
3995 float island_offset = 0.0f;
3996 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
3997 const float max_u = uv_sphere_project(scene,
3998 bm,
3999 efa,
4000 center,
4001 rotmat,
4002 fan,
4003 offsets,
4004 only_selected_uvs,
4005 use_seams,
4006 island_offset + 0.5f);
4007 island_offset = ceilf(max_ff(max_u, island_offset));
4008 }
4009
4010 const bool per_face_aspect = true;
4011 uv_map_clip_correct(scene, {obedit}, op, per_face_aspect, only_selected_uvs);
4012
4014 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
4015 }
4016
4017 return OPERATOR_FINISHED;
4018}
4019
4021{
4022 /* identifiers */
4023 ot->name = "Sphere Projection";
4024 ot->idname = "UV_OT_sphere_project";
4025 ot->description = "Project the UV vertices of the mesh over the curved surface of a sphere";
4026
4028
4029 /* API callbacks. */
4031 ot->poll = ED_operator_uvmap;
4032
4033 /* properties */
4036}
4037
4039
4040/* -------------------------------------------------------------------- */
4043
4044/* See #uv_sphere_project for description of parameters. */
4045static float uv_cylinder_project(const Scene *scene,
4046 BMesh *bm,
4047 BMFace *efa_init,
4048 const float center[3],
4049 const float rotmat[3][3],
4050 const bool fan,
4051 const BMUVOffsets &offsets,
4052 const bool only_selected_uvs,
4053 const bool use_seams,
4054 const float branch_init)
4055{
4056 float max_u = 0.0f;
4057 if (use_seams && BM_elem_flag_test(efa_init, BM_ELEM_TAG)) {
4058 return max_u;
4059 }
4060
4061 /* Similar to BM_mesh_calc_face_groups with added connectivity information. */
4062
4064
4065 stack.append({efa_init, branch_init});
4066
4067 while (stack.size()) {
4068 UV_FaceBranch face_branch = stack.pop_last();
4069 BMFace *efa = face_branch.efa;
4070
4071 if (use_seams) {
4072 if (BM_elem_flag_test(efa, BM_ELEM_TAG)) {
4073 continue; /* Faces might be in the stack more than once. */
4074 }
4075
4076 BM_elem_flag_set(efa, BM_ELEM_TAG, true); /* Visited, don't consider again. */
4077 }
4078
4079 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
4080 continue; /* Unselected face, ignore. */
4081 }
4082
4083 if (only_selected_uvs) {
4084 if (!uvedit_face_select_test(scene, bm, efa)) {
4085 uvedit_face_select_disable(scene, bm, efa);
4086 continue; /* Unselected UV, ignore. */
4087 }
4088 }
4089
4090 /* Remember which UVs are at the pole. */
4092
4093 int i;
4094 BMLoop *l;
4095 BMIter iter;
4096 BM_ITER_ELEM_INDEX (l, &iter, efa, BM_LOOPS_OF_FACE, i) {
4097 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
4098 float pv[3];
4099 sub_v3_v3v3(pv, l->v->co, center);
4100 mul_m3_v3(rotmat, pv);
4101 regular[i] = map_to_tube(&luv[0], &luv[1], pv[0], pv[1], pv[2]);
4102
4103 if (!use_seams) {
4104 continue; /* Nothing more to do. */
4105 }
4106
4107 /* Move UV to correct branch. */
4108 luv[0] = luv[0] + ceilf(face_branch.branch - 0.5f - luv[0]);
4109 max_u = max_ff(max_u, luv[0]);
4110
4111 BMEdge *edge = l->e;
4112 if (BM_elem_flag_test(edge, BM_ELEM_SEAM)) {
4113 continue; /* Stop flood fill at seams. */
4114 }
4115
4116 /* Extend flood fill by pushing to stack. */
4117 BMFace *efa2;
4118 BMIter iter2;
4119 BM_ITER_ELEM (efa2, &iter2, edge, BM_FACES_OF_EDGE) {
4120 if (!BM_elem_flag_test(efa2, BM_ELEM_TAG)) {
4121 stack.append({efa2, luv[0]});
4122 }
4123 }
4124 }
4125
4126 uv_map_mirror(efa, regular.data(), fan, offsets.uv);
4127 }
4128
4129 return max_u;
4130}
4131
4133{
4134 const Main *bmain = CTX_data_main(C);
4135 const Scene *scene = CTX_data_scene(C);
4136 View3D *v3d = CTX_wm_view3d(C);
4137
4138 bool only_selected_uvs = false;
4139 if (CTX_wm_space_image(C)) {
4140 /* Inside the UV Editor, only project selected UVs. */
4141 only_selected_uvs = true;
4142 }
4143
4144 ViewLayer *view_layer = CTX_data_view_layer(C);
4146 *bmain, scene, view_layer, v3d);
4147 for (Object *obedit : objects) {
4149 BMFace *efa;
4150 BMIter iter;
4151
4152 if (bm->totfacesel == 0) {
4153 continue;
4154 }
4155
4156 /* add uvs if they don't exist yet */
4157 if (!uvedit_ensure_uvs(obedit)) {
4158 continue;
4159 }
4160
4161 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
4162 float center[3], rotmat[3][3];
4163
4164 uv_map_transform(C, op, rotmat);
4165 uv_map_transform_center(scene, v3d, obedit, bm, center, nullptr);
4166
4167 const bool fan = RNA_enum_get(op->ptr, "pole");
4168 const bool use_seams = RNA_boolean_get(op->ptr, "seam");
4169
4170 if (use_seams) {
4172 }
4173
4174 float island_offset = 0.0f;
4175
4176 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
4177 if (!BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
4178 continue;
4179 }
4180
4181 if (only_selected_uvs && !uvedit_face_select_test(scene, bm, efa)) {
4182 uvedit_face_select_disable(scene, bm, efa);
4183 continue;
4184 }
4185
4186 const float max_u = uv_cylinder_project(scene,
4187 bm,
4188 efa,
4189 center,
4190 rotmat,
4191 fan,
4192 offsets,
4193 only_selected_uvs,
4194 use_seams,
4195 island_offset + 0.5f);
4196 island_offset = ceilf(max_ff(max_u, island_offset));
4197 }
4198
4199 const bool per_face_aspect = true;
4200 uv_map_clip_correct(scene, {obedit}, op, per_face_aspect, only_selected_uvs);
4201
4203 WM_event_add_notifier(C, NC_GEOM | ND_DATA, obedit->data);
4204 }
4205
4206 return OPERATOR_FINISHED;
4207}
4208
4210{
4211 /* identifiers */
4212 ot->name = "Cylinder Projection";
4213 ot->idname = "UV_OT_cylinder_project";
4214 ot->description = "Project the UV vertices of the mesh over the curved wall of a cylinder";
4215
4217
4218 /* API callbacks. */
4220 ot->poll = ED_operator_uvmap;
4221
4222 /* properties */
4225}
4226
4228
4229/* -------------------------------------------------------------------- */
4232
4233static void uvedit_unwrap_cube_project(const Scene *scene,
4234 BMesh *bm,
4235 float cube_size,
4236 const bool use_select,
4237 const bool only_selected_uvs,
4238 const float center[3])
4239{
4240 BMFace *efa;
4241 BMLoop *l;
4242 BMIter iter, liter;
4243 float loc[3];
4244 int cox, coy;
4245
4246 const BMUVOffsets offsets = BM_uv_map_offsets_get(bm);
4247
4248 if (center) {
4249 copy_v3_v3(loc, center);
4250 }
4251 else {
4252 zero_v3(loc);
4253 }
4254
4255 if (cube_size == 0.0f) [[unlikely]] {
4256 cube_size = 1.0f;
4257 }
4258
4259 /* choose x,y,z axis for projection depending on the largest normal
4260 * component, but clusters all together around the center of map. */
4261
4262 BM_ITER_MESH (efa, &iter, bm, BM_FACES_OF_MESH) {
4263 if (use_select && !BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
4264 continue;
4265 }
4266 if (only_selected_uvs && !uvedit_face_select_test(scene, bm, efa)) {
4267 uvedit_face_select_disable(scene, bm, efa);
4268 continue;
4269 }
4270
4271 axis_dominant_v3(&cox, &coy, efa->no);
4272
4273 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
4274 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, offsets.uv);
4275 luv[0] = 0.5f + ((l->v->co[cox] - loc[cox]) / cube_size);
4276 luv[1] = 0.5f + ((l->v->co[coy] - loc[coy]) / cube_size);
4277 }
4278 }
4279}
4280
4282{
4283 const Main *bmain = CTX_data_main(C);
4284 const Scene *scene = CTX_data_scene(C);
4285 View3D *v3d = CTX_wm_view3d(C);
4286
4287 bool only_selected_uvs = false;
4288 if (CTX_wm_space_image(C)) {
4289 /* Inside the UV Editor, only cube project selected UVs. */
4290 only_selected_uvs = true;
4291 }
4292
4293 PropertyRNA *prop_cube_size = RNA_struct_find_property(op->ptr, "cube_size");
4294 const float cube_size_init = RNA_property_float_get(op->ptr, prop_cube_size);
4295
4296 ViewLayer *view_layer = CTX_data_view_layer(C);
4298 *bmain, scene, view_layer, v3d);
4299 for (const int ob_index : objects.index_range()) {
4300 Object *obedit = objects[ob_index];
4302 if (bm->totfacesel == 0) {
4303 continue;
4304 }
4305
4306 /* add uvs if they don't exist yet */
4307 if (!uvedit_ensure_uvs(obedit)) {
4308 continue;
4309 }
4310
4311 float bounds[2][3];
4312 float (*bounds_buf)[3] = nullptr;
4313
4314 if (!RNA_property_is_set(op->ptr, prop_cube_size)) {
4315 bounds_buf = bounds;
4316 }
4317
4318 float center[3];
4319 uv_map_transform_center(scene, v3d, obedit, bm, center, bounds_buf);
4320
4321 /* calculate based on bounds */
4322 float cube_size = cube_size_init;
4323 if (bounds_buf) {
4324 float dims[3];
4325 sub_v3_v3v3(dims, bounds[1], bounds[0]);
4326 cube_size = std::max({UNPACK3(dims)});
4327 if (ob_index == 0) {
4328 /* This doesn't fit well with, multiple objects. */
4329 RNA_property_float_set(op->ptr, prop_cube_size, cube_size);
4330 }
4331 }
4332
4333 uvedit_unwrap_cube_project(scene, bm, cube_size, true, only_selected_uvs, center);
4334
4335 const bool per_face_aspect = true;
4336 uv_map_clip_correct(scene, {obedit}, op, per_face_aspect, only_selected_uvs);
4337
4340 }
4341
4342 return OPERATOR_FINISHED;
4343}
4344
4346{
4347 /* identifiers */
4348 ot->name = "Cube Projection";
4349 ot->idname = "UV_OT_cube_project";
4350 ot->description = "Project the UV vertices of the mesh over the six faces of a cube";
4351
4353
4354 /* API callbacks. */
4355 ot->exec = cube_project_exec;
4356 ot->poll = ED_operator_uvmap;
4357
4358 /* properties */
4359 RNA_def_float(ot->srna,
4360 "cube_size",
4361 1.0f,
4362 0.0f,
4363 FLT_MAX,
4364 "Cube Size",
4365 "Size of the cube to project on",
4366 0.001f,
4367 100.0f);
4369}
4370
4372
4373/* -------------------------------------------------------------------- */
4376
4377void ED_uvedit_add_simple_uvs(Main *bmain, const Scene *scene, Object *ob)
4378{
4379 Mesh *mesh = id_cast<Mesh *>(ob->data);
4380 bool sync_selection = (scene->toolsettings->uv_flag & UV_FLAG_SELECT_SYNC) != 0;
4381
4382 BMeshCreateParams create_params{};
4383 create_params.use_toolflags = false;
4384 BMesh *bm = BM_mesh_create(&bm_mesh_allocsize_default, &create_params);
4385
4386 /* turn sync selection off,
4387 * since we are not in edit mode we need to ensure only the uv flags are tested */
4389
4390 ED_mesh_uv_ensure(mesh, nullptr);
4391
4392 BMeshFromMeshParams bm_from_me_params{};
4393 bm_from_me_params.calc_face_normal = true;
4394 bm_from_me_params.calc_vert_normal = true;
4395 BM_mesh_bm_from_me(bm, mesh, &bm_from_me_params);
4396
4397 /* Must run after conversion, which restores the mesh's UV sync-select state. */
4399
4400 /* Select all UVs for cube_project. */
4402 /* A cube size of 2.0 maps [-1..1] vertex coords to [0.0..1.0] in UV coords. */
4403 uvedit_unwrap_cube_project(scene, bm, 2.0, false, false, nullptr);
4404
4405 /* Pack UVs. */
4408 params.only_selected_uvs = false;
4409 params.only_selected_faces = false;
4410 params.correct_aspect = false;
4411 params.use_seams = true;
4413 params.margin = 0.001f;
4414
4415 uvedit_pack_islands_multi(scene, {ob}, &bm, nullptr, false, true, nullptr, &params);
4416
4417 /* Write back from BMesh to Mesh. */
4418 BMeshToMeshParams bm_to_me_params{};
4419 BM_mesh_bm_to_me(bmain, bm, mesh, &bm_to_me_params);
4421
4422 if (sync_selection) {
4424 }
4425}
4426
4428
4429} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define ORIGINDEX_NONE
support for deformation groups and hooks.
#define G_MAIN
#define ATTR_FALLTHROUGH
#define DEG2RADF(_deg)
#define M_PI
#define mul_m4_series(...)
#define BLI_MEMARENA_STD_BUFSIZE
#define SNPRINTF_UTF8(dst, format,...)
#define STRNCPY_UTF8(dst, src)
Platform independent time functions.
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define MAX_VGROUP_NAME
Read Blender memory allocation and freeing.
#define RNA_ENUM_ITEM_SEPR
Definition RNA_types.hh:762
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define UI_MAX_DRAW_STR
#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 NC_SPACE
Definition WM_types.hh:405
#define INIT_MINMAX2(min, max)
#define INIT_MINMAX(min, max)
#define BLI_assert_unreachable()
#define ARRAY_SET_ITEMS(...)
#define UNPACK3(a)
#define ELEM(...)
#define BM_ELEM_CD_GET_BOOL(ele, offset)
#define BM_ELEM_SELECT_UV_EDGE
#define BM_ELEM_CD_GET_FLOAT_P(ele, offset)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_index_get(ele)
#define BM_elem_flag_set(ele, hflag, val)
#define BM_elem_flag_test(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_MESH_INDEX(ele, iter, bm, itype, indexvar)
#define BM_ITER_ELEM_INDEX(ele, iter, data, itype, indexvar)
#define MEM_realloc_uninitialized(vmemh, len)
btScalar max
const T * data() const
Definition BLI_array.hh:323
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
void append(const T &value)
bool is_empty() const
IndexRange index_range() const
bool can_scale_(const UVPackIsland_Params &params) const
Definition uv_pack.cc:2461
void add_polygon(Span< float2 > uvs, MemArena *arena, Heap *heap)
Definition uv_pack.cc:155
void build_transformation(float scale, double angle, float r_matrix[2][2]) const
Definition uv_pack.cc:2384
eUVPackIsland_RotationMethod rotate_method
eUVPackIsland_MarginMethod margin_method
eUVPackIsland_ShapeMethod shape_method
void setUDIMOffsetFromSpaceImage(const SpaceImage *sima)
eUVPackIsland_PinMethod pin_method
void setFromUnwrapOptions(const UnwrapOptions &options)
CCL_NAMESPACE_BEGIN struct Options options
const int faceMap[6][4]
#define str(s)
#define FLT_MAX
#define INT_MAX
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
float3x3 scale(float3x3 mat, float2 scale)
#define T
void free(Subdiv *subdiv)
Definition subdiv.cc:192
Subdiv * new_from_mesh(const Settings *settings, const Mesh *mesh)
Definition subdiv.cc:133
Mesh * subdiv_to_mesh(Subdiv *subdiv, const ToMeshSettings *settings, const Mesh *coarse_mesh)
bool uv_parametrizer_is_slim(const ParamHandle *phandle)
void uv_parametrizer_construct_end(ParamHandle *phandle, bool fill_holes, bool topology_from_uvs, int *r_count_failed=nullptr)
void uv_parametrizer_edge_set_seam(ParamHandle *phandle, const ParamKey *vkeys)
void uv_parametrizer_average(ParamHandle *handle, bool ignore_pinned, bool scale_uv, bool shear)
void uv_parametrizer_flush(ParamHandle *handle)
void uv_parametrizer_lscm_begin(ParamHandle *handle, bool live, bool abf, bool use_original_bounds)
void uv_parametrizer_slim_live_begin(ParamHandle *phandle, const ParamSlimOptions *slim_options)
void uv_parametrizer_slim_live_solve_iteration(ParamHandle *phandle)
ParamKey uv_find_pin_index(ParamHandle *handle, const int bmvertindex, const float uv[2])
void uv_prepare_pin_index(ParamHandle *handle, const int bmvertindex, const float uv[2])
void uv_parametrizer_slim_live_end(ParamHandle *phandle)
void uv_parametrizer_stretch_blend(ParamHandle *handle, float blend)
void mul_v2_m2_add_v2v2(float r[2], const float mat[2][2], const float a[2], const float b[2])
Definition uv_pack.cc:46
void uv_parametrizer_lscm_end(ParamHandle *handle)
void uv_parametrizer_aspect_ratio(ParamHandle *handle, float aspect_y)
void uv_parametrizer_stretch_begin(ParamHandle *handle)
void uv_parametrizer_flush_restore(ParamHandle *handle)
float pack_islands(Span< PackIsland * > islands, const UVPackIsland_Params &params)
void uv_parametrizer_lscm_solve(ParamHandle *handle, int *count_changed, int *count_failed)
void uv_parametrizer_stretch_iter(ParamHandle *handle)
void uv_parametrizer_slim_solve(ParamHandle *phandle, const ParamSlimOptions *slim_options, bool use_original_bounds, int *count_changed, int *count_failed)
void uv_parametrizer_original_bounds(ParamHandle *phandle)
void uv_parametrizer_stretch_end(ParamHandle *handle)
void uv_parametrizer_face_add(ParamHandle *handle, const ParamKey key, const int nverts, const ParamKey *vkeys, const float **co, float **uv, const float *weight, const bool *pin, const bool *select)
BMesh Iterators.
MINLINE float min_ff(float a, float b)
void WM_main_add_notifier(uint type, void *reference)
static ParamHandle * construct_param_handle_subsurfed(const Scene *scene, Object *ob, const UnwrapOptions *options, int *r_count_failed=nullptr)
static const EnumPropertyItem pack_rotate_method_items[]
void BM_face_calc_center_median(const BMFace *f, float r_cent[3])
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
void EDBM_mesh_elem_index_ensure_multi(Span< Object * > objects, char htype)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void mul_m3_v3(const float M[3][3], float r[3])
@ UVCALC_UNWRAP_USE_WEIGHTS
@ UVCALC_NO_ASPECT_CORRECT
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
bool ED_operator_uvedit(bContext *C)
void UV_OT_minimize_stretch(wmOperatorType *ot)
ScrArea * CTX_wm_area(const bContext *C)
__int64 int64_t
Definition BKE_text.h:90
static wmOperatorStatus cube_project_exec(bContext *C, wmOperator *op)
static void unwrap_options_sync_toolsettings(wmOperator *op, ToolSettings *ts)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void ED_uvedit_get_aspect_from_material(Object *ob, const int material_index, float *r_aspx, float *r_aspy)
static wmOperatorStatus cylinder_project_exec(bContext *C, wmOperator *op)
static void uvedit_unwrap(const Scene *scene, Object *obedit, const UnwrapOptions *options, int *r_count_changed, int *r_count_failed)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void UV_OT_project_from_view(wmOperatorType *ot)
@ RPT_WARNING
Definition BKE_report.hh:40
bool map_to_tube(float *r_u, float *r_v, float x, float y, float z)
int bm_mesh_calc_uv_islands(const Scene *scene, BMesh *bm, ListBaseT< FaceIsland > *island_list, const bool only_selected_faces, const bool only_selected_uvs, const bool use_seams, const float aspect_y, const BMUVOffsets &offsets)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
static void uv_map_operator_property_correct_aspect(wmOperatorType *ot)
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
bool uv_stitch_selected_islands_for_original_bounds(const Scene *scene, Span< Object * > objects)
void ED_uvedit_get_aspect(Object *obedit, float *r_aspx, float *r_aspy)
static void pack_islands_freejob(void *pidv)
static float uv_sphere_project(const Scene *scene, BMesh *bm, BMFace *efa_init, const float center[3], const float rotmat[3][3], const bool fan, const BMUVOffsets &offsets, const bool only_selected_uvs, const bool use_seams, const float branch_init)
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
void UV_OT_pack_islands(wmOperatorType *ot)
static wmOperatorStatus reset_exec(bContext *C, wmOperator *)
Span< float3 > BKE_editmesh_vert_coords_when_deformed(Depsgraph *depsgraph, Scene *scene, Object *obedit, Array< float3 > &r_alloc)
Definition editmesh.cc:214
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
static void uv_map_transform_center(const Scene *scene, View3D *v3d, Object *ob, BMesh *bm, float r_center[3], float r_bounds[2][3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static struct blender::@105341326230115065046224055270215277364215147174 g_live_unwrap
void BM_editselection_center(BMEditSelection *ese, float r_center[3])
void copy_m3_m4(float m1[3][3], const float m2[4][4])
float ED_uvedit_get_aspect_y(Object *obedit)
@ REGION_DRAW_LOCK_RENDER
void ED_uvedit_add_simple_uvs(Main *bmain, const Scene *scene, Object *ob)
static void uvedit_unwrap_multi(const Scene *scene, const Span< Object * > objects, const UnwrapOptions *options, int *r_count_changed=nullptr, int *r_count_failed=nullptr)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
static Vector< float3 > smart_uv_project_calculate_project_normals(const ThickFace *thick_faces, const uint thick_faces_len, BMesh *bm, const float project_angle_limit_half_cos, const float project_angle_limit_cos, const float area_weight)
static UnwrapOptions unwrap_options_get(wmOperator *op, Object *ob, const ToolSettings *ts)
BLI_INLINE BMEdge * BM_edge_at_index(BMesh *bm, const int index)
static void uv_map_transform_calc_bounds(BMesh *bm, float r_min[3], float r_max[3])
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
static void uv_map_clip_correct_properties_ex(wmOperatorType *ot, bool clip_to_bounds)
static void pack_islands_endjob(void *pidv)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
void RNA_property_float_set(PointerRNA *ptr, PropertyRNA *prop, float value)
static void uvedit_prepare_pinned_indices(ParamHandle *handle, const Scene *scene, const BMesh *bm, BMFace *efa, const UnwrapOptions *options, const BMUVOffsets &offsets)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
void uvedit_select_prepare_custom_data(const Scene *scene, BMesh *bm)
bool uv_coords_isect_udim(const Image *image, const int udim_grid[2], const float coords[2])
@ WM_JOB_TYPE_UV_PACK
Definition WM_api.hh:2051
int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
static void uv_map_mirror(BMFace *efa, const bool *regular, const bool fan, const int cd_loop_uv_offset)
void uvedit_face_select_disable(const Scene *scene, BMesh *bm, BMFace *efa)
eUVPackIsland_PinMethod
@ ED_UVPACK_PIN_IGNORE
@ ED_UVPACK_PIN_LOCK_ROTATION
@ ED_UVPACK_PIN_LOCK_SCALE
@ ED_UVPACK_PIN_LOCK_ROTATION_SCALE
@ ED_UVPACK_PIN_NONE
@ ED_UVPACK_PIN_LOCK_ALL
void BKE_uvproject_from_view(float target[2], const float source[3], const float persmat[4][4], const float rotmat[4][4], float winx, float winy)
Definition uvproject.cc:86
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void mul_v2_m3v3(float r[2], const float M[3][3], const float a[3])
@ V3D_AROUND_CENTER_BOUNDS
@ V3D_AROUND_LOCAL_ORIGINS
@ V3D_AROUND_CENTER_MEDIAN
bool ED_uvedit_live_unwrap_timer_check(const wmTimer *timer)
static void island_uv_transform(FaceIsland *island, const float matrix[2][2], const float pre_translate[2])
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void BLI_linklist_prepend_arena(LinkNode **listp, void *ptr, struct MemArena *ma) ATTR_NONNULL(1
void ED_mesh_uv_ensure(Mesh *mesh, const char *name)
Definition mesh_data.cc:256
void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp) ATTR_NONNULL(1)
Definition BLI_heap.cc:193
std::string RNA_property_string_get(PointerRNA *ptr, PropertyRNA *prop)
int RNA_int_get(PointerRNA *ptr, const char *name)
static wmOperatorStatus minimize_stretch_exec(bContext *C, wmOperator *op)
bool is_negative_m4(const float mat[4][4])
wmWindow * CTX_wm_window(const bContext *C)
void BKE_uvproject_from_camera(float target[2], const float source[3], const struct ProjCameraInfo *uci)
void BM_mesh_elem_hflag_disable_all(BMesh *bm, const char htype, const char hflag, const bool respecthide)
static Mesh * subdivide_edit_mesh(const Object *object, BMesh *bm, const SubsurfModifierData *smd)
static const EnumPropertyItem pack_shape_method_items[]
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
static wmOperatorStatus minimize_stretch_invoke(bContext *C, wmOperator *op, const wmEvent *)
bool map_to_sphere(float *r_u, float *r_v, float x, float y, float z)
@ WM_JOB_PROGRESS
Definition WM_api.hh:1997
void ED_mesh_uv_loop_reset(bContext *C, Mesh *mesh)
Definition mesh_data.cc:147
static void uv_map_transform(bContext *C, wmOperator *op, float rotmat[3][3])
void ED_uvedit_live_unwrap_re_solve()
static float uv_cylinder_project(const Scene *scene, BMesh *bm, BMFace *efa_init, const float center[3], const float rotmat[3][3], const bool fan, const BMUVOffsets &offsets, const bool only_selected_uvs, const bool use_seams, const float branch_init)
static bool minimize_stretch_init(bContext *C, wmOperator *op)
static void uv_map_transform_calc_center_median(BMesh *bm, float r_center[3])
static bool rna_property_sync_flag(PointerRNA *ptr, const char *prop_name, T flag, bool flipped, T *value_p)
void BKE_uvproject_camera_info_free(ProjCameraInfo *uci)
Definition uvproject.cc:179
ViewLayer * CTX_data_view_layer(const bContext *C)
static void modifier_unwrap_state(Object *obedit, const UnwrapOptions *options, bool *r_use_subsurf)
MINLINE void mul_v3_fl(float r[3], float f)
static void uv_pack_islands_ui(bContext *, wmOperator *op)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
void RNA_def_property_float_default(PropertyRNA *prop, float value)
static void uv_transform_properties(wmOperatorType *ot, int radius)
static wmOperatorStatus uv_from_view_invoke(bContext *C, wmOperator *op, const wmEvent *)
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
Global G
Definition blender.cc:57
bool uvedit_uv_select_test(const Scene *scene, const BMesh *bm, const BMLoop *l, const BMUVOffsets &offsets)
static wmOperatorStatus smart_project_exec(bContext *C, wmOperator *op)
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
void WM_locked_interface_set(wmWindowManager *wm, bool lock)
void BM_mesh_bm_from_me(BMesh *bm, const Mesh *mesh, const BMeshFromMeshParams *params)
Mesh -> BMesh.
PropertyRNA * RNA_def_float_factor(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)
eUVPackIsland_MarginMethod
@ ED_UVPACK_MARGIN_ADD
@ ED_UVPACK_MARGIN_FRACTION
@ ED_UVPACK_MARGIN_SCALED
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
void UV_OT_sphere_project(wmOperatorType *ot)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
int BKE_image_find_nearest_tile_with_offset(const Image *image, const float co[2], float r_uv_offset[2]) ATTR_NONNULL(2
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
wmOperatorStatus WM_operator_props_popup_call(bContext *C, wmOperator *op, const wmEvent *)
void ED_uvedit_live_unwrap(const Scene *scene, Span< Object * > objects)
static const EnumPropertyItem pinned_islands_method_items[]
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void WM_locked_interface_set_with_flags(wmWindowManager *wm, short lock_flags)
void BKE_uvproject_from_view_ortho(float target[2], const float source[3], const float rotmat[4][4])
Definition uvproject.cc:184
static void construct_param_handle_face_add(ParamHandle *handle, const Scene *scene, const BMesh *bm, BMFace *efa, geometry::ParamKey face_index, const UnwrapOptions *options, const BMUVOffsets &offsets, const int cd_weight_offset, const int cd_weight_index)
bool BLI_rctf_is_empty(const struct rctf *rect)
@ UVCALC_UNWRAP_METHOD_ANGLE
@ UVCALC_UNWRAP_METHOD_CONFORMAL
@ UVCALC_UNWRAP_METHOD_MINIMUM_STRETCH
static const float smart_uv_project_area_ignore
@ eSubsurfModifierFlag_ControlEdges
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
static float uv_nearest_grid_tile_distance(const int udim_grid[2], const float coords[2], float nearest_tile_co[2])
float BM_face_calc_area(const BMFace *f)
void minmax_v2v2_v2(float min[2], float max[2], const float vec[2])
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
MINLINE void add_v4_v4(float r[4], const float a[4])
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
void ED_image_get_uv_aspect(Image *ima, ImageUser *iuser, float *r_aspx, float *r_aspy)
static ParamHandle * construct_param_handle(const Scene *scene, Object *ob, const UnwrapOptions *options, int *r_count_failed=nullptr)
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
wmWindowManager * CTX_wm_manager(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
void BLI_memarena_clear(MemArena *ma) ATTR_NONNULL(1)
void UV_OT_unwrap(wmOperatorType *ot)
static void correct_uv_aspect_per_face(Object *ob, BMesh *bm)
Object * CTX_data_edit_object(const bContext *C)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
static const EnumPropertyItem pack_margin_method_items[]
static void pack_islands_startjob(void *pidv, wmJobWorkerStatus *worker_status)
bool uvedit_face_select_test(const Scene *scene, const BMesh *bm, const BMFace *efa)
int BKE_object_defgroup_name_index(const Object *ob, StringRef name)
Definition deform.cc:597
void mul_v2_m2v2(float r[2], const float mat[2][2], const float vec[2])
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
static bool rna_property_sync_string(PointerRNA *ptr, const char *prop_name, char value_p[])
const BMAllocTemplate bm_mesh_allocsize_default
Definition bmesh_mesh.cc:26
void unit_m4(float m[4][4])
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void ED_area_status_text(ScrArea *area, const char *str)
Definition area.cc:975
RegionView3D * CTX_wm_region_view3d(const bContext *C)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
static void unwrap_draw(bContext *, wmOperator *op)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
static bool rna_property_sync_float(PointerRNA *ptr, const char *prop_name, float *value_p)
MINLINE void zero_v3(float r[3])
void BKE_id_free(Main *bmain, void *idv)
static int smart_uv_project_thickface_area_cmp_fn(const void *tf_a_p, const void *tf_b_p)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
static bool uvedit_is_face_affected(const Scene *scene, const BMesh *bm, BMFace *efa, const UnwrapOptions *options, const BMUVOffsets &offsets)
void ED_uvedit_select_all(const ToolSettings *ts, BMesh *bm)
Camera * ED_view3d_camera_data_get(View3D *v3d, RegionView3D *rv3d)
void ED_uvedit_live_unwrap_end(bool cancel)
void mat4_to_size(float size[3], const float M[4][4])
void UV_OT_cube_project(wmOperatorType *ot)
bool ED_operator_uvmap(bContext *C)
void BM_face_calc_bounds_expand(const BMFace *f, float min[3], float max[3])
static void uvedit_unwrap_cube_project(const Scene *scene, BMesh *bm, float cube_size, const bool use_select, const bool only_selected_uvs, const float center[3])
static wmOperatorStatus uv_pack_islands_invoke(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus WM_operator_props_popup_confirm_ex(bContext *C, wmOperator *op, const wmEvent *, std::optional< std::string > title, std::optional< std::string > confirm_text, const bool cancel_default, std::optional< std::string > message)
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
MINLINE float normalize_v3(float n[3])
SpaceImage * CTX_wm_space_image(const bContext *C)
void UV_OT_average_islands_scale(wmOperatorType *ot)
static wmOperatorStatus average_islands_scale_exec(bContext *C, wmOperator *op)
static void uv_map_clip_correct_properties(wmOperatorType *ot)
MINLINE void add_v3_v3(float r[3], const float a[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
void UV_OT_cylinder_project(wmOperatorType *ot)
static bool uvedit_ensure_uvs(Object *obedit)
static void shrink_loop_uv_by_aspect_ratio(BMFace *efa, const int cd_loop_uv_offset, const float aspect_y)
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)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
static void construct_param_edge_set_seams(ParamHandle *handle, BMesh *bm, const UnwrapOptions *options)
static bool uv_from_view_poll(bContext *C)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
Main * CTX_data_main(const bContext *C)
static bool island_has_pins(const Scene *scene, const BMesh *bm, FaceIsland *island, const geometry::UVPackIsland_Params *params)
void unit_m3(float m[3][3])
BMFace * BM_mesh_active_face_get(BMesh *bm, const bool is_sloppy, const bool is_selected)
static wmOperatorStatus minimize_stretch_modal(bContext *C, wmOperator *op, const wmEvent *event)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
PropertyRNA * RNA_def_float_rotation(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 correct_uv_aspect(Object *ob, BMesh *bm)
MINLINE void clamp_v2(float vec[2], float min, float max)
Mesh * BKE_mesh_from_bmesh_for_eval_nomain(BMesh *bm, const CustomData_MeshMasks *cd_mask_extra, const Mesh *me_settings)
MINLINE float max_ff(float a, float b)
MINLINE void axis_dominant_v3(int *r_axis_a, int *r_axis_b, const float axis[3])
static void minimize_stretch_iteration(bContext *C, wmOperator *op, bool interactive)
void mid_v2_v2v2(float r[2], const float a[2], const float b[2])
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void ED_uvedit_live_unwrap_begin(Scene *scene, Object *obedit, struct wmWindow *win_modal)
void BM_face_uv_minmax(const BMFace *f, float min[2], float max[2], const int cd_loop_uv_offset)
static float uv_nearest_image_tile_distance(const Image *image, const float coords[2], float nearest_tile_co[2])
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
void UV_OT_reset(wmOperatorType *ot)
Heap * BLI_heap_new() ATTR_WARN_UNUSED_RESULT
Definition BLI_heap.cc:188
void BKE_mesh_ensure_default_orig_index_customdata(Mesh *mesh)
double BLI_time_now_seconds()
Definition time.cc:30
std::optional< std::string > ED_mesh_uv_add(Mesh *mesh, const char *name, bool active_set, bool do_init, ReportList *reports)
Definition mesh_data.cc:154
void zero_m4(float m[4][4])
static bool uvedit_have_selection_multi(const Scene *scene, const Span< Object * > objects, const UnwrapOptions *options)
bool ED_uvedit_test(Object *obedit)
Definition uvedit_ops.cc:74
static wmOperatorStatus sphere_project_exec(bContext *C, wmOperator *op)
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
static bool rna_property_sync_enum_char(PointerRNA *ptr, const char *prop_name, T *value_p)
Scene * CTX_data_scene(const bContext *C)
static bool uvedit_have_selection(const Scene *scene, BMesh *bm, const UnwrapOptions *options)
void WM_event_timer_remove(wmWindowManager *wm, wmWindow *, wmTimer *timer)
static void minimize_stretch_cancel(bContext *C, wmOperator *op)
eUVPackIsland_RotationMethod
@ ED_UVPACK_ROTATION_CARDINAL
@ ED_UVPACK_ROTATION_AXIS_ALIGNED_Y
@ ED_UVPACK_ROTATION_AXIS_ALIGNED
@ ED_UVPACK_ROTATION_NONE
@ ED_UVPACK_ROTATION_AXIS_ALIGNED_X
@ ED_UVPACK_ROTATION_ANY
static ParamHandle * construct_param_handle_multi(const Scene *scene, const Span< Object * > objects, const UnwrapOptions *options)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
@ OPTYPE_GRAB_CURSOR_XY
Definition WM_types.hh:196
void BM_mesh_bm_to_me(Main *bmain, BMesh *bm, Mesh *mesh, const BMeshToMeshParams *params)
std::string RNA_string_get(PointerRNA *ptr, const char *name)
wmTimer * WM_event_timer_add(wmWindowManager *wm, wmWindow *win, const wmEventType event_type, const double time_step)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
BLI_INLINE BMFace * BM_face_at_index(BMesh *bm, const int index)
BMUVOffsets BM_uv_map_offsets_get(const BMesh *bm)
static void uvedit_pack_islands_multi(const Scene *scene, const Span< Object * > objects, BMesh **bmesh_override, const SpaceImage *udim_source_closest, const bool original_selection, const bool notify_wm, const rctf *custom_region, geometry::UVPackIsland_Params *params)
MINLINE void mul_v4_fl(float r[4], float f)
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)
MINLINE void negate_v4_v4(float r[4], const float a[4])
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
MINLINE void add_v2_fl(float r[2], float f)
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
bool BM_select_history_active_get(BMesh *bm, BMEditSelection *ese)
static void minimize_stretch_exit(bContext *C, wmOperator *op, bool cancel)
int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
bool invert_m2_m2(float inverse[2][2], const float mat[2][2])
eUVPackIsland_ShapeMethod
@ ED_UVPACK_SHAPE_CONVEX
@ ED_UVPACK_SHAPE_CONCAVE
@ ED_UVPACK_SHAPE_AABB
void WM_cursor_wait(bool val)
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const BMeshCreateParams *params)
BMesh Make Mesh.
static wmOperatorStatus smart_project_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void uv_map_rotation_matrix_ex(float result[4][4], RegionView3D *rv3d, Object *ob, float upangledeg, float sideangledeg, float radius, const float offset[4])
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)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
@ BM_ELEM_SELECT_UV
MINLINE void zero_v2(float r[2])
ARegion * CTX_wm_region(const bContext *C)
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
static void texface_from_original_index(const Scene *scene, const BMesh *bm, const BMUVOffsets &offsets, BMFace *efa, int index, float **r_uv, bool *r_pin, bool *r_select)
struct ProjCameraInfo * BKE_uvproject_camera_info(const struct Object *ob, const float rotmat[4][4], float winx, float winy)
float RNA_float_get(PointerRNA *ptr, const char *name)
static void uv_map_clip_correct(const Scene *scene, const Span< Object * > objects, wmOperator *op, bool per_face_aspect, bool only_selected_uvs)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
static bool uvedit_live_unwrap_timer_validate(const wmWindowManager *wm)
static wmOperatorStatus pack_islands_exec(bContext *C, wmOperator *op)
View3D * CTX_wm_view3d(const bContext *C)
void UV_OT_smart_project(wmOperatorType *ot)
static wmOperatorStatus uv_from_view_exec(bContext *C, wmOperator *op)
void axis_dominant_v3_to_m3(float r_mat[3][3], const float normal[3])
Normal to x,y matrix.
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
static bool rna_property_sync_int(PointerRNA *ptr, const char *prop_name, int *value_p)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bke::subdiv::Settings BKE_subsurf_modifier_settings_init(const SubsurfModifierData *smd, bool use_render_params)
static wmOperatorStatus unwrap_exec(bContext *C, wmOperator *op)
static bool rna_property_sync_enum(PointerRNA *ptr, const char *prop_name, int *value_p)
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
#define floorf
#define fabsf
#define sinf
#define cosf
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
struct BMLoop * next
struct BMEdge * e
struct BMVert * v
CustomData vdata
CustomData ldata
BMUVOffsets offsets
Definition ED_uvedit.hh:377
const c_style_mat & ptr() const
CustomData face_data
CustomData edge_data
CustomData vert_data
Vector< Object * > objects_edit
ListBaseT< ModifierData > modifiers
bke::ObjectRuntime * runtime
View3DCursor cursor
struct RenderData r
struct ToolSettings * toolsettings
eTool_UvCalcFlag uvcalc_flag
eTool_Unwrapper unwrapper
geometry::UVPackIsland_Params pack_island_params
char weight_group[MAX_VGROUP_NAME]
struct Object * camera
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & row(bool align)
wmEventType type
Definition WM_types.hh:817
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
void(*) ui(bContext *C, wmOperator *op)
Definition WM_types.hh:1176
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
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
struct wmOperatorType * type
#define POLAR_ZX
#define VIEW_ON_POLES
#define PACK_ROTATE_METHOD_AXIS_ALIGNED_OFFSET
#define ALIGN_TO_OBJECT
#define POLAR_ZY
#define VIEW_ON_EQUATOR