Welcome to mirror list, hosted at ThFree Co, Russian Federation.

select_uv.py « op « magic_uv - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bc1b17ea1c0b319f7a6f87ebeb9ea6bcbeb8691e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# SPDX-License-Identifier: GPL-2.0-or-later

__author__ = "Nutti <nutti.metro@gmail.com>"
__status__ = "production"
__version__ = "6.6"
__date__ = "22 Apr 2022"

import bpy
from bpy.props import BoolProperty, FloatProperty, EnumProperty
import bmesh

from .. import common
from ..utils.bl_class_registry import BlClassRegistry
from ..utils.property_class_registry import PropertyClassRegistry
from ..utils import compatibility as compat


def _is_valid_context(context):
    # 'IMAGE_EDITOR' and 'VIEW_3D' space is allowed to execute.
    # If 'View_3D' space is not allowed, you can't find option in Tool-Shelf
    # after the execution
    if not common.is_valid_space(context, ['IMAGE_EDITOR', 'VIEW_3D']):
        return False

    objs = common.get_uv_editable_objects(context)
    if not objs:
        return False

    # only edit mode is allowed to execute
    if context.object.mode != 'EDIT':
        return False

    return True


@PropertyClassRegistry()
class _Properties:
    idname = "select_uv"

    @classmethod
    def init_props(cls, scene):
        scene.muv_select_uv_enabled = BoolProperty(
            name="Select UV Enabled",
            description="Select UV is enabled",
            default=False
        )
        scene.muv_select_uv_same_polygon_threshold = FloatProperty(
            name="Same Polygon Threshold",
            description="Threshold to distinguish same polygons",
            default=0.00001,
            min=0.00001,
            max=0.01,
            step=0.00001
        )
        scene.muv_select_uv_selection_method = EnumProperty(
            name="Selection Method",
            description="How to select faces which have overlapped UVs",
            items=[
                ('EXTEND', "Extend",
                 "Select faces without unselecting selected faces"),
                ('RESET', "Reset", "Select faces and unselect selected faces"),
            ],
            default='RESET'
        )
        scene.muv_select_uv_sync_mesh_selection = BoolProperty(
            name="Sync Mesh Selection",
            description="Select the mesh's faces as well as UV's faces",
            default=False
        )

    @classmethod
    def del_props(cls, scene):
        del scene.muv_select_uv_enabled
        del scene.muv_select_uv_same_polygon_threshold


@BlClassRegistry()
@compat.make_annotations
class MUV_OT_SelectUV_SelectOverlapped(bpy.types.Operator):
    """
    Operation class: Select faces which have overlapped UVs
    """

    bl_idname = "uv.muv_select_uv_select_overlapped"
    bl_label = "Overlapped"
    bl_description = "Select faces which have overlapped UVs"
    bl_options = {'REGISTER', 'UNDO'}

    same_polygon_threshold = FloatProperty(
        name="Same Polygon Threshold",
        description="Threshold to distinguish same polygons",
        default=0.00001,
        min=0.00001,
        max=0.01,
        step=0.00001
    )
    selection_method = EnumProperty(
        name="Selection Method",
        description="How to select faces which have overlapped UVs",
        items=[
            ('EXTEND', "Extend",
             "Select faces without unselecting selected faces"),
            ('RESET', "Reset", "Select faces and unselect selected faces"),
        ],
        default='RESET'
    )
    sync_mesh_selection = BoolProperty(
        name="Sync Mesh Selection",
        description="Select mesh's faces as well as UV's faces",
        default=False
    )

    @classmethod
    def poll(cls, context):
        # we can not get area/space/region from console
        if common.is_console_mode():
            return True
        return _is_valid_context(context)

    @staticmethod
    def setup_argument(ops, scene):
        ops.same_polygon_threshold = scene.muv_select_uv_same_polygon_threshold
        ops.selection_method = scene.muv_select_uv_selection_method
        ops.sync_mesh_selection = scene.muv_select_uv_sync_mesh_selection

    def execute(self, context):
        objs = common.get_uv_editable_objects(context)

        bm_list = []
        uv_layer_list = []
        faces_list = []
        for obj in objs:
            bm = bmesh.from_edit_mesh(obj.data)
            if common.check_version(2, 73, 0) >= 0:
                bm.faces.ensure_lookup_table()
            uv_layer = bm.loops.layers.uv.verify()

            if context.tool_settings.use_uv_select_sync:
                sel_faces = [f for f in bm.faces]
            else:
                sel_faces = [f for f in bm.faces if f.select]
            bm_list.append(bm)
            uv_layer_list.append(uv_layer)
            faces_list.append(sel_faces)

        overlapped_info = common.get_overlapped_uv_info(
            bm_list, faces_list, uv_layer_list, 'FACE',
            self.same_polygon_threshold)

        if self.selection_method == 'RESET':
            if context.tool_settings.use_uv_select_sync:
                for faces in faces_list:
                    for f in faces:
                        f.select = False
            else:
                for uv_layer, faces in zip(uv_layer_list, faces_list):
                    for f in faces:
                        if self.sync_mesh_selection:
                            f.select = False
                        for l in f.loops:
                            l[uv_layer].select = False

        for info in overlapped_info:
            if context.tool_settings.use_uv_select_sync:
                info["subject_face"].select = True
            else:
                if self.sync_mesh_selection:
                    info["subject_face"].select = True
                for l in info["subject_face"].loops:
                    l[info["subject_uv_layer"]].select = True

        for obj in objs:
            bmesh.update_edit_mesh(obj.data)

        return {'FINISHED'}


@BlClassRegistry()
@compat.make_annotations
class MUV_OT_SelectUV_SelectFlipped(bpy.types.Operator):
    """
    Operation class: Select faces which have flipped UVs
    """

    bl_idname = "uv.muv_select_uv_select_flipped"
    bl_label = "Flipped"
    bl_description = "Select faces which have flipped UVs"
    bl_options = {'REGISTER', 'UNDO'}

    selection_method = EnumProperty(
        name="Selection Method",
        description="How to select faces which have overlapped UVs",
        items=[
            ('EXTEND', "Extend",
             "Select faces without unselecting selected faces"),
            ('RESET', "Reset", "Select faces and unselect selected faces"),
        ],
        default='RESET'
    )
    sync_mesh_selection = BoolProperty(
        name="Sync Mesh Selection",
        description="Select mesh's faces as well as UV's faces",
        default=False
    )

    @classmethod
    def poll(cls, context):
        # we can not get area/space/region from console
        if common.is_console_mode():
            return True
        return _is_valid_context(context)

    @staticmethod
    def setup_argument(ops, scene):
        ops.selection_method = scene.muv_select_uv_selection_method
        ops.sync_mesh_selection = scene.muv_select_uv_sync_mesh_selection

    def execute(self, context):
        objs = common.get_uv_editable_objects(context)

        bm_list = []
        uv_layer_list = []
        faces_list = []
        for obj in objs:
            bm = bmesh.from_edit_mesh(obj.data)
            if common.check_version(2, 73, 0) >= 0:
                bm.faces.ensure_lookup_table()
            uv_layer = bm.loops.layers.uv.verify()

            if context.tool_settings.use_uv_select_sync:
                sel_faces = [f for f in bm.faces]
            else:
                sel_faces = [f for f in bm.faces if f.select]
            bm_list.append(bm)
            uv_layer_list.append(uv_layer)
            faces_list.append(sel_faces)

        flipped_info = common.get_flipped_uv_info(
            bm_list, faces_list, uv_layer_list)

        if self.selection_method == 'RESET':
            if context.tool_settings.use_uv_select_sync:
                for faces in faces_list:
                    for f in faces:
                        f.select = False
            else:
                for uv_layer, faces in zip(uv_layer_list, faces_list):
                    for f in faces:
                        if self.sync_mesh_selection:
                            f.select = False
                        for l in f.loops:
                            l[uv_layer].select = False

        for info in flipped_info:
            if context.tool_settings.use_uv_select_sync:
                info["face"].select = True
            else:
                if self.sync_mesh_selection:
                    info["face"].select = True
                for l in info["face"].loops:
                    l[info["uv_layer"]].select = True

        for obj in objs:
            bmesh.update_edit_mesh(obj.data)

        return {'FINISHED'}


@BlClassRegistry()
class MUV_OT_SelectUV_ZoomSelectedUV(bpy.types.Operator):
    """
    Operation class: Zoom selected UV in View3D space
    """

    bl_idname = "uv.muv_select_uv_zoom_selected_uv"
    bl_label = "Zoom Selected UV"
    bl_description = "Zoom selected UV in View3D space"
    bl_options = {'REGISTER', 'UNDO'}

    @classmethod
    def poll(cls, context):
        # we can not get area/space/region from console
        if common.is_console_mode():
            return True
        return _is_valid_context(context)

    def _get_override_context(self, context):
        for window in context.window_manager.windows:
            screen = window.screen
            for area in screen.areas:
                if area.type == 'VIEW_3D':
                    for region in area.regions:
                        if region.type == 'WINDOW':
                            return {'window': window, 'screen': screen,
                                    'area': area, 'region': region}
        return None

    def execute(self, context):
        objs = common.get_uv_editable_objects(context)

        bm_list = []
        uv_layer_list = []
        verts_list = []
        for obj in objs:
            bm = bmesh.from_edit_mesh(obj.data)
            if common.check_version(2, 73, 0) >= 0:
                bm.verts.ensure_lookup_table()
            uv_layer = bm.loops.layers.uv.verify()

            sel_verts = [v for v in bm.verts if v.select]
            bm_list.append(bm)
            uv_layer_list.append(uv_layer)
            verts_list.append(sel_verts)

        # Get all selected UV vertices in UV Editor.
        sel_uv_verts = []
        for vlist, uv_layer in zip(verts_list, uv_layer_list):
            for v in vlist:
                for l in v.link_loops:
                    if l[uv_layer].select or \
                            context.tool_settings.use_uv_select_sync:
                        sel_uv_verts.append(v)
                        break

        # Select vertices only selected in UV Editor.
        for bm in bm_list:
            for v in bm.verts:
                v.select = False
        for v in sel_uv_verts:
            v.select = True
        for obj in objs:
            bmesh.update_edit_mesh(obj.data)

        # Zoom.
        override_context = self._get_override_context(context)
        if override_context is None:
            self.report({'WARNING'}, "More than one 'VIEW_3D' area must exist")
            return {'CANCELLED'}
        bpy.ops.view3d.view_selected(override_context, use_all_regions=False)

        # Revert selection of vertices.
        for v in sel_verts:
            v.select = True
        for obj in objs:
            bmesh.update_edit_mesh(obj.data)

        return {'FINISHED'}