~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to release/scripts/op/mesh.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# ##### BEGIN GPL LICENSE BLOCK #####
2
 
#
3
 
#  This program is free software; you can redistribute it and/or
4
 
#  modify it under the terms of the GNU General Public License
5
 
#  as published by the Free Software Foundation; either version 2
6
 
#  of the License, or (at your option) any later version.
7
 
#
8
 
#  This program is distributed in the hope that it will be useful,
9
 
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
#  GNU General Public License for more details.
12
 
#
13
 
#  You should have received a copy of the GNU General Public License
14
 
#  along with this program; if not, write to the Free Software Foundation,
15
 
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
 
#
17
 
# ##### END GPL LICENSE BLOCK #####
18
 
 
19
 
# <pep8 compliant>
20
 
 
21
 
import bpy
22
 
 
23
 
 
24
 
class MeshSelectInteriorFaces(bpy.types.Operator):
25
 
    '''Select faces where all edges have more then 2 face users.'''
26
 
 
27
 
    bl_idname = "mesh.faces_select_interior"
28
 
    bl_label = "Select Interior Faces"
29
 
    bl_options = {'REGISTER', 'UNDO'}
30
 
 
31
 
    def poll(self, context):
32
 
        ob = context.active_object
33
 
        return (ob and ob.type == 'MESH')
34
 
 
35
 
    def execute(self, context):
36
 
        ob = context.active_object
37
 
        bpy.ops.mesh.selection_type(type='FACE')
38
 
        is_editmode = (ob.mode == 'EDIT')
39
 
        if is_editmode:
40
 
            bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
41
 
 
42
 
        mesh = ob.data
43
 
 
44
 
        face_list = [face for face in mesh.faces]
45
 
        face_edge_keys = [face.edge_keys for face in face_list]
46
 
 
47
 
        edge_face_count = mesh.edge_face_count_dict
48
 
 
49
 
        def test_interior(index):
50
 
            for key in face_edge_keys[index]:
51
 
                if edge_face_count[key] < 3:
52
 
                    return False
53
 
            return True
54
 
 
55
 
        for index, face in enumerate(face_list):
56
 
            if(test_interior(index)):
57
 
                face.select = True
58
 
            else:
59
 
                face.select = False
60
 
 
61
 
        if is_editmode:
62
 
            bpy.ops.object.mode_set(mode='EDIT', toggle=False)
63
 
        return {'FINISHED'}
64
 
 
65
 
 
66
 
class MeshMirrorUV(bpy.types.Operator):
67
 
    '''Copy mirror UV coordinates on the X axis based on a mirrored mesh'''
68
 
    bl_idname = "mesh.faces_miror_uv"
69
 
    bl_label = "Copy Mirrored UV coords"
70
 
    bl_options = {'REGISTER', 'UNDO'}
71
 
 
72
 
    def poll(self, context):
73
 
        ob = context.active_object
74
 
        return (ob and ob.type == 'MESH')
75
 
 
76
 
    def execute(self, context):
77
 
        DIR = 1 # TODO, make an option
78
 
 
79
 
        from mathutils import Vector
80
 
 
81
 
        ob = context.active_object
82
 
        is_editmode = (ob.mode == 'EDIT')
83
 
        if is_editmode:
84
 
            bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
85
 
 
86
 
        mesh = ob.data
87
 
 
88
 
        # mirror lookups
89
 
        mirror_gt = {}
90
 
        mirror_lt = {}
91
 
 
92
 
        vcos = [v.co.to_tuple(5) for v in mesh.verts]
93
 
 
94
 
        for i, co in enumerate(vcos):
95
 
            if co[0] > 0.0:
96
 
                mirror_gt[co] = i
97
 
            elif co[0] < 0.0:
98
 
                mirror_lt[co] = i
99
 
            else:
100
 
                mirror_gt[co] = i
101
 
                mirror_lt[co] = i
102
 
 
103
 
        #for i, v in enumerate(mesh.verts):
104
 
        vmap = {}
105
 
        for mirror_a, mirror_b in (mirror_gt, mirror_lt), (mirror_lt, mirror_gt):
106
 
            for co, i in mirror_a.items():
107
 
                nco = (-co[0], co[1], co[2])
108
 
                j = mirror_b.get(nco)
109
 
                if j is not None:
110
 
                    vmap[i] = j
111
 
 
112
 
 
113
 
        active_uv_layer = None
114
 
        for lay in mesh.uv_textures:
115
 
            if lay.active:
116
 
                active_uv_layer = lay.data
117
 
                break
118
 
 
119
 
        fuvs = [(uv.uv1, uv.uv2, uv.uv3, uv.uv4) for uv in active_uv_layer]
120
 
        fuvs_cpy = [(uv[0].copy(), uv[1].copy(), uv[2].copy(), uv[3].copy()) for uv in fuvs]
121
 
 
122
 
        # as a list
123
 
        faces = mesh.faces[:]
124
 
 
125
 
        fuvsel = [(False not in uv.select_uv) for uv in active_uv_layer]
126
 
        fcents = [f.center for f in faces]
127
 
 
128
 
        # find mirror faces
129
 
        mirror_fm = {}
130
 
        for i, f in enumerate(faces):
131
 
            verts = f.verts[:]
132
 
            verts.sort()
133
 
            verts = tuple(verts)
134
 
            mirror_fm[verts] = i
135
 
 
136
 
        fmap = {}
137
 
        for i, f in enumerate(faces):
138
 
            verts = [vmap.get(j) for j in f.verts]
139
 
            if None not in verts:
140
 
                verts.sort()
141
 
                j = mirror_fm.get(tuple(verts))
142
 
                if j is not None:
143
 
                    fmap[i] = j
144
 
 
145
 
        done = [False] * len(faces)
146
 
        for i, j in fmap.items():
147
 
 
148
 
            if not fuvsel[i] or not fuvsel[j]:
149
 
                continue
150
 
            elif DIR == 0 and fcents[i][0] < 0.0:
151
 
                continue
152
 
            elif DIR == 1 and fcents[i][0] > 0.0:
153
 
                continue
154
 
 
155
 
            # copy UVs
156
 
            uv1 = fuvs[i]
157
 
            uv2 = fuvs_cpy[j]
158
 
 
159
 
            # get the correct rotation
160
 
            v1 = faces[j].verts[:]
161
 
            v2 = [vmap[k] for k in faces[i].verts[:]]
162
 
 
163
 
 
164
 
            for k in range(len(uv1)):
165
 
                k_map = v1.index(v2[k])
166
 
                uv1[k].x = - (uv2[k_map].x - 0.5) + 0.5
167
 
                uv1[k].y = uv2[k_map].y
168
 
 
169
 
        if is_editmode:
170
 
            bpy.ops.object.mode_set(mode='EDIT', toggle=False)
171
 
 
172
 
        return {'FINISHED'}
173
 
 
174
 
 
175
 
# Register the operator
176
 
classes = [
177
 
    MeshSelectInteriorFaces,
178
 
    MeshMirrorUV]
179
 
 
180
 
 
181
 
def register():
182
 
    register = bpy.types.register
183
 
    for cls in classes:
184
 
        register(cls)
185
 
 
186
 
 
187
 
def unregister():
188
 
    unregister = bpy.types.unregister
189
 
    for cls in classes:
190
 
        unregister(cls)
191
 
 
192
 
if __name__ == "__main__":
193
 
    register()