1 # ##### BEGIN GPL LICENSE BLOCK #####
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.
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.
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.
17 # ##### END GPL LICENSE BLOCK #####
23 "author": "Bart Crouch, Alexander Nedovizin, Paul Kotelevets "
26 "blender": (2, 70, 0),
27 "location": "Editmode > F",
29 "description": "Extends the 'Make Edge/Face' functionality",
30 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
31 "Scripts/Modeling/F2",
40 from bpy_extras
import view3d_utils
43 # returns a custom data layer of the UV map, or None
44 def get_uv_layer(ob
, bm
, mat_index
):
47 if not ob
.material_slots
:
50 uv
= me
.uv_textures
.active
.name
52 mat
= ob
.material_slots
[mat_index
].material
54 slot
= mat
.texture_slots
[mat
.active_texture_index
]
55 if slot
and slot
.uv_layer
:
58 for tex_slot
in mat
.texture_slots
:
59 if tex_slot
and tex_slot
.uv_layer
:
60 uv
= tex_slot
.uv_layer
63 uv_layer
= bm
.loops
.layers
.uv
.get(uv
)
68 # create a face from a single selected edge
69 def quad_from_edge(bm
, edge_sel
, context
, event
):
70 ob
= context
.active_object
71 region
= context
.region
72 region_3d
= context
.space_data
.region_3d
74 # find linked edges that are open (<2 faces connected) and not part of
75 # the face the selected edge belongs to
76 all_edges
= [[edge
for edge
in edge_sel
.verts
[i
].link_edges
if \
77 len(edge
.link_faces
) < 2 and edge
!= edge_sel
and \
78 sum([face
in edge_sel
.link_faces
for face
in edge
.link_faces
]) == 0] \
80 if not all_edges
[0] or not all_edges
[1]:
83 # determine which edges to use, based on mouse cursor position
84 mouse_pos
= mathutils
.Vector([event
.mouse_region_x
, event
.mouse_region_y
])
86 for edges
in all_edges
:
89 vert
= [vert
for vert
in edge
.verts
if not vert
.select
][0]
90 world_pos
= ob
.matrix_world
* vert
.co
.copy()
91 screen_pos
= view3d_utils
.location_3d_to_region_2d(region
,
93 dist
= (mouse_pos
- screen_pos
).length
94 if not min_dist
or dist
< min_dist
[0]:
95 min_dist
= (dist
, edge
, vert
)
96 optimal_edges
.append(min_dist
)
98 # determine the vertices, which make up the quad
99 v1
= edge_sel
.verts
[0]
100 v2
= edge_sel
.verts
[1]
101 edge_1
= optimal_edges
[0][1]
102 edge_2
= optimal_edges
[1][1]
103 v3
= optimal_edges
[0][2]
104 v4
= optimal_edges
[1][2]
109 if not normal_edge
.link_faces
:
111 if not normal_edge
.link_faces
:
112 normal_edge
= edge_sel
113 if not normal_edge
.link_faces
:
114 # no connected faces, so no need to flip the face normal
116 if flip_align
: # there is a face to which the normal can be aligned
117 ref_verts
= [v
for v
in normal_edge
.link_faces
[0].verts
]
121 elif normal_edge
== edge_sel
:
127 if (va_1
== ref_verts
[0] and va_2
== ref_verts
[-1]) or \
128 (va_2
== ref_verts
[0] and va_1
== ref_verts
[-1]):
129 # reference verts are at start and end of the list -> shift list
130 ref_verts
= ref_verts
[1:] + [ref_verts
[0]]
131 if ref_verts
.index(va_1
) > ref_verts
.index(va_2
):
132 # connected face has same normal direction, so don't flip
135 # material index detection
136 ref_faces
= edge_sel
.link_faces
138 ref_faces
= edge_sel
.verts
[0].link_faces
140 ref_faces
= edge_sel
.verts
[1].link_faces
145 mat_index
= ref_faces
[0].material_index
146 smooth
= ref_faces
[0].smooth
151 # triangle (usually at end of quad-strip
154 # normal face creation
155 verts
= [v3
, v1
, v2
, v4
]
158 face
= bm
.faces
.new(verts
)
160 face
.material_index
= mat_index
163 # face already exists
167 edge_sel
.select
= False
168 for vert
in edge_sel
.verts
:
170 for edge
in face
.edges
:
177 if __name__
!= '__main__':
178 addon_prefs
= context
.user_preferences
.addons
[__name__
].preferences
179 if addon_prefs
.adjustuv
:
180 uv_layer
= get_uv_layer(ob
, bm
, mat_index
)
183 for vert
in [v1
, v2
, v3
, v4
]:
184 for loop
in vert
.link_loops
:
185 if loop
.face
.index
> -1:
186 uv_ori
[loop
.vert
.index
] = loop
[uv_layer
].uv
187 if len(uv_ori
) == 4 or len(uv_ori
) == 3:
188 for loop
in face
.loops
:
189 loop
[uv_layer
].uv
= uv_ori
[loop
.vert
.index
]
191 # toggle mode, to force correct drawing
192 bpy
.ops
.object.mode_set(mode
='OBJECT')
193 bpy
.ops
.object.mode_set(mode
='EDIT')
196 # create a face from a single selected vertex, if it is an open vertex
197 def quad_from_vertex(bm
, vert_sel
, context
, event
):
198 ob
= context
.active_object
200 region
= context
.region
201 region_3d
= context
.space_data
.region_3d
203 # find linked edges that are open (<2 faces connected)
204 edges
= [edge
for edge
in vert_sel
.link_edges
if len(edge
.link_faces
) < 2]
208 # determine which edges to use, based on mouse cursor position
210 mouse_pos
= mathutils
.Vector([event
.mouse_region_x
, event
.mouse_region_y
])
211 for a
, b
in itertools
.combinations(edges
, 2):
212 other_verts
= [vert
for edge
in [a
, b
] for vert
in edge
.verts \
214 mid_other
= (other_verts
[0].co
.copy() + other_verts
[1].co
.copy()) \
216 new_pos
= 2 * (mid_other
- vert_sel
.co
.copy()) + vert_sel
.co
.copy()
217 world_pos
= ob
.matrix_world
* new_pos
218 screen_pos
= view3d_utils
.location_3d_to_region_2d(region
, region_3d
,
220 dist
= (mouse_pos
- screen_pos
).length
221 if not min_dist
or dist
< min_dist
[0]:
222 min_dist
= (dist
, (a
, b
), other_verts
, new_pos
)
224 # create vertex at location mirrored in the line, connecting the open edges
226 other_verts
= min_dist
[2]
227 new_pos
= min_dist
[3]
228 vert_new
= bm
.verts
.new(new_pos
)
232 normal_edge
= edges
[0]
233 if not normal_edge
.link_faces
:
234 normal_edge
= edges
[1]
235 if not normal_edge
.link_faces
:
236 # no connected faces, so no need to flip the face normal
238 if flip_align
: # there is a face to which the normal can be aligned
239 ref_verts
= [v
for v
in normal_edge
.link_faces
[0].verts
]
240 if other_verts
[0] in ref_verts
:
241 va_1
= other_verts
[0]
245 va_2
= other_verts
[1]
246 if (va_1
== ref_verts
[0] and va_2
== ref_verts
[-1]) or \
247 (va_2
== ref_verts
[0] and va_1
== ref_verts
[-1]):
248 # reference verts are at start and end of the list -> shift list
249 ref_verts
= ref_verts
[1:] + [ref_verts
[0]]
250 if ref_verts
.index(va_1
) > ref_verts
.index(va_2
):
251 # connected face has same normal direction, so don't flip
254 # material index detection
255 ref_faces
= vert_sel
.link_faces
260 mat_index
= ref_faces
[0].material_index
261 smooth
= ref_faces
[0].smooth
263 # create face between all 4 vertices involved
264 verts
= [other_verts
[0], vert_sel
, other_verts
[1], vert_new
]
267 face
= bm
.faces
.new(verts
)
269 face
.material_index
= mat_index
273 vert_new
.select
= True
274 vert_sel
.select
= False
277 if __name__
!= '__main__':
278 addon_prefs
= context
.user_preferences
.addons
[__name__
].preferences
279 if addon_prefs
.adjustuv
:
280 uv_layer
= get_uv_layer(ob
, bm
, mat_index
)
285 # get original uv coordinates
287 for loop
in other_verts
[i
].link_loops
:
288 if loop
.face
.index
> -1:
289 uv_others
[loop
.vert
.index
] = loop
[uv_layer
].uv
291 if len(uv_others
) == 2:
292 mid_other
= (list(uv_others
.values())[0] +
293 list(uv_others
.values())[1]) / 2
294 for loop
in vert_sel
.link_loops
:
295 if loop
.face
.index
> -1:
296 uv_sel
= loop
[uv_layer
].uv
299 uv_new
= 2 * (mid_other
- uv_sel
) + uv_sel
301 # set uv coordinates for new loops
303 for loop
in face
.loops
:
304 if loop
.vert
.index
== -1:
306 elif loop
.vert
.index
in uv_others
:
307 x
, y
= uv_others
[loop
.vert
.index
]
310 loop
[uv_layer
].uv
= (x
, y
)
312 # toggle mode, to force correct drawing
313 bpy
.ops
.object.mode_set(mode
='OBJECT')
314 bpy
.ops
.object.mode_set(mode
='EDIT')
317 # autograb preference in addons panel
318 class F2AddonPreferences(bpy
.types
.AddonPreferences
):
320 adjustuv
= bpy
.props
.BoolProperty(
322 description
= "Automatically update UV unwrapping",
324 autograb
= bpy
.props
.BoolProperty(
326 description
= "Automatically puts a newly created vertex in grab mode",
329 def draw(self
, context
):
331 layout
.prop(self
, "autograb")
332 layout
.prop(self
, "adjustuv")
335 class MeshF2(bpy
.types
.Operator
):
337 bl_idname
= "mesh.f2"
338 bl_label
= "Make Edge/Face"
339 bl_description
= "Extends the 'Make Edge/Face' functionality"
340 bl_options
= {'REGISTER', 'UNDO'}
343 def poll(cls
, context
):
344 # check we are in mesh editmode
345 ob
= context
.active_object
346 return(ob
and ob
.type == 'MESH' and context
.mode
== 'EDIT_MESH')
348 def invoke(self
, context
, event
):
349 bm
= bmesh
.from_edit_mesh(context
.active_object
.data
)
350 sel
= [v
for v
in bm
.verts
if v
.select
]
352 # original 'Make Edge/Face' behaviour
354 bpy
.ops
.mesh
.edge_face_add('INVOKE_DEFAULT')
358 # single vertex selected -> mirror vertex and create new face
359 quad_from_vertex(bm
, sel
[0], context
, event
)
360 if __name__
!= '__main__':
361 addon_prefs
= context
.user_preferences
.addons
[__name__
].\
363 if addon_prefs
.autograb
:
364 bpy
.ops
.transform
.translate('INVOKE_DEFAULT')
366 edges_sel
= [ed
for ed
in bm
.edges
if ed
.select
]
367 if len(edges_sel
) != 1:
368 # 2 vertices selected, but not on the same edge
369 bpy
.ops
.mesh
.edge_face_add()
371 # single edge selected -> new face from linked open edges
372 quad_from_edge(bm
, edges_sel
[0], context
, event
)
389 bpy
.utils
.register_class(c
)
392 kcfg
= bpy
.context
.window_manager
.keyconfigs
.addon
394 km
= kcfg
.keymaps
.new(name
='Mesh', space_type
='EMPTY')
395 kmi
= km
.keymap_items
.new("mesh.f2", 'F', 'PRESS')
396 addon_keymaps
.append((km
, kmi
))
400 # remove keymap entry
401 for km
, kmi
in addon_keymaps
:
402 km
.keymap_items
.remove(kmi
)
403 addon_keymaps
.clear()
405 # remove operator and preferences
406 for c
in reversed(classes
):
407 bpy
.utils
.unregister_class(c
)
410 if __name__
== "__main__":