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
|
# gpl author: Giuseppe De Marco [BlenderLab] inspired by NirenYang
bl_info = {
"name": "Set edges length",
"description": "Edges length",
"author": "Giuseppe De Marco [BlenderLab] inspired by NirenYang",
"version": (0, 1, 0),
"blender": (2, 7, 1),
"location": "Toolbar > Tools > Mesh Tools: set Length(Shit+Alt+E)",
"warning": "",
"wiki_url": "",
"category": "Mesh",
}
import bpy
import bmesh
from mathutils import Vector
from bpy.types import Operator
from bpy.props import (
FloatProperty,
EnumProperty,
)
# GLOBALS
edge_length_debug = False
_error_message = "Please select at least one edge to fill select history"
_error_message_2 = "Edges with shared vertices are not allowed. Please, use scale instead"
# Note : Refactor - removed all the operators apart from LengthSet
# and merged the other ones as options of length (lijenstina)
def get_edge_vector(edge):
verts = (edge.verts[0].co, edge.verts[1].co)
vector = verts[1] - verts[0]
return vector
def get_selected(bmesh_obj, geometry_type):
# geometry type should be edges, verts or faces
selected = []
for i in getattr(bmesh_obj, geometry_type):
if i.select:
selected.append(i)
return tuple(selected)
def get_center_vector(verts):
# verts = [Vector((x,y,z)), Vector((x,y,z))]
center_vector = Vector((((verts[1][0] + verts[0][0]) / 2.),
((verts[1][1] + verts[0][1]) / 2.),
((verts[1][2] + verts[0][2]) / 2.)))
return center_vector
class LengthSet(Operator):
bl_idname = "object.mesh_edge_length_set"
bl_label = "Set edge length"
bl_description = ("Change one selected edge length by a specified target,\n"
"existing length and different modes\n"
"Note: works only with Edges that not share a vertex")
bl_options = {'REGISTER', 'UNDO'}
old_length: FloatProperty(
name="Original length",
options={'HIDDEN'},
)
set_length_type: EnumProperty(
items=[
('manual', "Manual",
"Input manually the desired Target Length"),
('existing', "Existing Length",
"Use existing geometry Edges' characteristics"),
],
name="Set Type of Input",
)
target_length: FloatProperty(
name="Target Length",
description="Input a value for an Edges Length target",
default=1.00,
unit='LENGTH',
precision=5
)
existing_length: EnumProperty(
items=[
('min', "Shortest",
"Set all to shortest Edge of selection"),
('max', "Longest",
"Set all to the longest Edge of selection"),
('average', "Average",
"Set all to the average Edge length of selection"),
('active', "Active",
"Set all to the active Edge's one\n"
"Needs a selection to be done in Edge Select mode"),
],
name="Existing length"
)
mode: EnumProperty(
items=[
('fixed', "Fixed", "Fixed"),
('increment', "Increment", "Increment"),
('decrement', "Decrement", "Decrement"),
],
name="Mode"
)
behaviour: EnumProperty(
items=[
('proportional', "Proportional",
"Move vertex locations proportionally to the center of the Edge"),
('clockwise', "Clockwise",
"Compute the Edges' vertex locations in a clockwise fashion"),
('unclockwise', "Counterclockwise",
"Compute the Edges' vertex locations in a counterclockwise fashion"),
],
name="Resize behavior"
)
originary_edge_length_dict = {}
edge_lengths = []
selected_edges = ()
@classmethod
def poll(cls, context):
return (context.edit_object and context.object.type == 'MESH')
def check(self, context):
return True
def draw(self, context):
layout = self.layout
layout.label("Original Active length is: {:.3f}".format(self.old_length))
layout.label("Input Mode:")
layout.prop(self, "set_length_type", expand=True)
if self.set_length_type == 'manual':
layout.prop(self, "target_length")
else:
layout.prop(self, "existing_length", text="")
layout.label("Mode:")
layout.prop(self, "mode", text="")
layout.label("Resize Behavior:")
layout.prop(self, "behaviour", text="")
def get_existing_edge_length(self, bm):
if self.existing_length != "active":
if self.existing_length == "min":
return min(self.edge_lengths)
if self.existing_length == "max":
return max(self.edge_lengths)
elif self.existing_length == "average":
return sum(self.edge_lengths) / float(len(self.selected_edges))
else:
bm.edges.ensure_lookup_table()
active_edge_length = None
for elem in reversed(bm.select_history):
if isinstance(elem, bmesh.types.BMEdge):
active_edge_length = elem.calc_length()
break
return active_edge_length
return 0.0
def invoke(self, context, event):
wm = context.window_managerlength
obj = context.edit_object
bm = bmesh.from_edit_mesh(obj.data)
bpy.ops.mesh.select_mode(type="EDGE")
self.selected_edges = get_selected(bm, 'edges')
if self.selected_edges:
vertex_set = []
for edge in self.selected_edges:
vector = get_edge_vector(edge)
if edge.verts[0].index not in vertex_set:
vertex_set.append(edge.verts[0].index)
else:
self.report({'ERROR_INVALID_INPUT'}, _error_message_2)
return {'CANCELLED'}
if edge.verts[1].index not in vertex_set:
vertex_set.append(edge.verts[1].index)
else:
self.report({'ERROR_INVALID_INPUT'}, _error_message_2)
return {'CANCELLED'}
# warning, it's a constant !
verts_index = ''.join((str(edge.verts[0].index), str(edge.verts[1].index)))
self.originary_edge_length_dict[verts_index] = vector
self.edge_lengths.append(vector.length)
self.old_length = vector.length
else:
self.report({'ERROR'}, _error_message)
return {'CANCELLED'}
if edge_length_debug:
self.report({'INFO'}, str(self.originary_edge_length_dict))
if bpy.context.scene.unit_settings.system == 'IMPERIAL':
# imperial to metric conversion
vector.length = (0.9144 * vector.length) / 3
self.target_length = vector.length
return wm.invoke_props_dialog(self)
def execute(self, context):
bpy.ops.mesh.select_mode(type="EDGE")
self.context = context
obj = context.edit_object
bm = bmesh.from_edit_mesh(obj.data)
self.selected_edges = get_selected(bm, 'edges')
if not self.selected_edges:
self.report({'ERROR'}, _error_message)
return {'CANCELLED'}
for edge in self.selected_edges:
vector = get_edge_vector(edge)
# what we should see in original length dialog field
self.old_length = vector.length
if self.set_length_type == 'manual':
vector.length = abs(self.target_length)
else:
get_lengths = self.get_existing_edge_length(bm)
# check for edit mode
if not get_lengths:
self.report({'WARNING'},
"Operation Cancelled. "
"Active Edge could not be determined (needs selection in Edit Mode)")
return {'CANCELLED'}
vector.length = get_lengths
if vector.length == 0.0:
self.report({'ERROR'}, "Operation cancelled. Target length is set to zero")
return {'CANCELLED'}
center_vector = get_center_vector((edge.verts[0].co, edge.verts[1].co))
verts_index = ''.join((str(edge.verts[0].index), str(edge.verts[1].index)))
if edge_length_debug:
self.report({'INFO'},
' - '.join(('vector ' + str(vector),
'originary_vector ' +
str(self.originary_edge_length_dict[verts_index])
)))
verts = (edge.verts[0].co, edge.verts[1].co)
if edge_length_debug:
self.report({'INFO'},
'\n edge.verts[0].co ' + str(verts[0]) +
'\n edge.verts[1].co ' + str(verts[1]) +
'\n vector.length' + str(vector.length))
# the clockwise direction have v1 -> v0, unclockwise v0 -> v1
if self.target_length >= 0:
if self.behaviour == 'proportional':
edge.verts[1].co = center_vector + vector / 2
edge.verts[0].co = center_vector - vector / 2
if self.mode == 'decrement':
edge.verts[0].co = (center_vector + vector / 2) - \
(self.originary_edge_length_dict[verts_index] / 2)
edge.verts[1].co = (center_vector - vector / 2) + \
(self.originary_edge_length_dict[verts_index] / 2)
elif self.mode == 'increment':
edge.verts[1].co = (center_vector + vector / 2) + \
self.originary_edge_length_dict[verts_index] / 2
edge.verts[0].co = (center_vector - vector / 2) - \
self.originary_edge_length_dict[verts_index] / 2
elif self.behaviour == 'unclockwise':
if self.mode == 'increment':
edge.verts[1].co = \
verts[0] + (self.originary_edge_length_dict[verts_index] + vector)
elif self.mode == 'decrement':
edge.verts[0].co = \
verts[1] - (self.originary_edge_length_dict[verts_index] - vector)
else:
edge.verts[1].co = verts[0] + vector
else:
# clockwise
if self.mode == 'increment':
edge.verts[0].co = \
verts[1] - (self.originary_edge_length_dict[verts_index] + vector)
elif self.mode == 'decrement':
edge.verts[1].co = \
verts[0] + (self.originary_edge_length_dict[verts_index] - vector)
else:
edge.verts[0].co = verts[1] - vector
if bpy.context.scene.unit_settings.system == 'IMPERIAL':
"""
# yards to metric conversion
vector.length = ( 3. * vector.length ) / 0.9144
# metric to yards conversion
vector.length = ( 0.9144 * vector.length ) / 3.
"""
for mvert in edge.verts:
# school time: 0.9144 : 3 = X : mvert
mvert.co = (0.9144 * mvert.co) / 3
if edge_length_debug:
self.report({'INFO'},
'\n edge.verts[0].co' + str(verts[0]) +
'\n edge.verts[1].co' + str(verts[1]) +
'\n vector' + str(vector) + '\n v1 > v0:' + str((verts[1] >= verts[0]))
)
bmesh.update_edit_mesh(obj.data, True)
return {'FINISHED'}
def register():
bpy.utils.register_class(LengthSet)
def unregister():
bpy.utils.unregister_class(LengthSet)
if __name__ == "__main__":
register()
|