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

mesh.py « op « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1074ee7b482b15eabb93dbc6e3a3ec7b2cf03f6d (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
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

# <pep8 compliant>

import bpy


class MeshSelectInteriorFaces(bpy.types.Operator):
    '''Select faces where all edges have more then 2 face users.'''

    bl_idname = "mesh.faces_select_interior"
    bl_label = "Select Interior Faces"
    bl_options = {'REGISTER', 'UNDO'}

    @staticmethod
    def poll(context):
        ob = context.active_object
        return (ob and ob.type == 'MESH')

    def execute(self, context):
        ob = context.active_object
        bpy.ops.mesh.selection_type(type='FACE')
        is_editmode = (ob.mode == 'EDIT')
        if is_editmode:
            bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

        mesh = ob.data

        face_list = [face for face in mesh.faces]
        face_edge_keys = [face.edge_keys for face in face_list]

        edge_face_count = mesh.edge_face_count_dict

        def test_interior(index):
            for key in face_edge_keys[index]:
                if edge_face_count[key] < 3:
                    return False
            return True

        for index, face in enumerate(face_list):
            if(test_interior(index)):
                face.select = True
            else:
                face.select = False

        if is_editmode:
            bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        return {'FINISHED'}


class MeshMirrorUV(bpy.types.Operator):
    '''Copy mirror UV coordinates on the X axis based on a mirrored mesh'''
    bl_idname = "mesh.faces_miror_uv"
    bl_label = "Copy Mirrored UV coords"
    bl_options = {'REGISTER', 'UNDO'}

    @staticmethod
    def poll(context):
        ob = context.active_object
        return (ob and ob.type == 'MESH')

    def execute(self, context):
        DIR = 1 # TODO, make an option

        from mathutils import Vector

        ob = context.active_object
        is_editmode = (ob.mode == 'EDIT')
        if is_editmode:
            bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

        mesh = ob.data

        # mirror lookups
        mirror_gt = {}
        mirror_lt = {}

        vcos = [v.co.to_tuple(5) for v in mesh.verts]

        for i, co in enumerate(vcos):
            if co[0] > 0.0:
                mirror_gt[co] = i
            elif co[0] < 0.0:
                mirror_lt[co] = i
            else:
                mirror_gt[co] = i
                mirror_lt[co] = i

        #for i, v in enumerate(mesh.verts):
        vmap = {}
        for mirror_a, mirror_b in (mirror_gt, mirror_lt), (mirror_lt, mirror_gt):
            for co, i in mirror_a.items():
                nco = (-co[0], co[1], co[2])
                j = mirror_b.get(nco)
                if j is not None:
                    vmap[i] = j


        active_uv_layer = None
        for lay in mesh.uv_textures:
            if lay.active:
                active_uv_layer = lay.data
                break

        fuvs = [(uv.uv1, uv.uv2, uv.uv3, uv.uv4) for uv in active_uv_layer]
        fuvs_cpy = [(uv[0].copy(), uv[1].copy(), uv[2].copy(), uv[3].copy()) for uv in fuvs]

        # as a list
        faces = mesh.faces[:]

        fuvsel = [(False not in uv.select_uv) for uv in active_uv_layer]
        fcents = [f.center for f in faces]

        # find mirror faces
        mirror_fm = {}
        for i, f in enumerate(faces):
            verts = f.verts[:]
            verts.sort()
            verts = tuple(verts)
            mirror_fm[verts] = i

        fmap = {}
        for i, f in enumerate(faces):
            verts = [vmap.get(j) for j in f.verts]
            if None not in verts:
                verts.sort()
                j = mirror_fm.get(tuple(verts))
                if j is not None:
                    fmap[i] = j

        done = [False] * len(faces)
        for i, j in fmap.items():

            if not fuvsel[i] or not fuvsel[j]:
                continue
            elif DIR == 0 and fcents[i][0] < 0.0:
                continue
            elif DIR == 1 and fcents[i][0] > 0.0:
                continue

            # copy UVs
            uv1 = fuvs[i]
            uv2 = fuvs_cpy[j]

            # get the correct rotation
            v1 = faces[j].verts[:]
            v2 = [vmap[k] for k in faces[i].verts[:]]


            for k in range(len(uv1)):
                k_map = v1.index(v2[k])
                uv1[k].x = - (uv2[k_map].x - 0.5) + 0.5
                uv1[k].y = uv2[k_map].y

        if is_editmode:
            bpy.ops.object.mode_set(mode='EDIT', toggle=False)

        return {'FINISHED'}


def register():
    pass


def unregister():
    pass

if __name__ == "__main__":
    register()