1
import bpy, mathutils, math
2
from mathutils import geometry
4
# Get a matrix for the selected faces that you can use to do local transforms
5
def get_selection_matrix(faces=False):
7
me = bpy.context.active_object.data
10
faces = get_selected_faces()
12
yVec = mathutils.Vector()
13
zVec = mathutils.Vector()
15
# Ok so we have a basic matrix, but lets base it more on the mesh!
18
v1 = me.vertices[f.vertices[0]].co
19
v2 = me.vertices[f.vertices[1]].co
24
if len(f.vertices) == 4:
25
v1 = me.vertices[f.vertices[2]].co
26
v2 = me.vertices[f.vertices[3]].co
31
zVec += mathutils.Vector(f.normal)
34
quat = zVec.to_track_quat('-Z', 'Y')
35
tMat = quat.to_matrix()
37
yVec = yVec.normalized()
39
yVec = yVec.normalized()
40
zVec = zVec.normalized()
42
# Rotate yVec so it's 90 degrees to zVec
43
cross =yVec.cross(zVec)
44
vec = float(yVec.angle(zVec) - math.radians(90))
45
mat = mathutils.Matrix.Rotation(vec, 3, cross)
48
xVec = yVec.cross(zVec)
50
xVec = xVec.normalized()
52
nMat = mathutils.Matrix((xVec, yVec, zVec))
58
# Get the selection radius (minimum distance of an outer edge to the centre)
59
def get_selection_radius():
61
ob = bpy.context.active_object
65
# no use continueing if nothing is selected
66
if contains_selected_item(ob.data.polygons):
68
# Find the center of the selection
69
cent = mathutils.Vector()
73
for f in ob.data.polygons:
78
nonVerts.extend(f.vertices)
84
# Now that we know the center.. we can figure out how close the nearest point on an outer edge is
85
for e in get_selected_edges():
87
nonSection = [v for v in e.vertices if v in nonVerts]
90
v0 = ob.data.vertices[e.vertices[0]].co
91
v1 = ob.data.vertices[e.vertices[1]].co
93
# If there's more than 1 vert of this edge on the outside... we need the edge length to be long enough too!
94
if len(nonSection) > 1:
96
edgeRad = edge.length * 0.5
98
if edgeRad < radius or not chk:
102
int = geometry.intersect_point_line(cent, v0, v1)
107
if l < radius or not chk:
115
# Get the average length of the outer edges of the current selection
116
def get_shortest_outer_edge_length():
118
ob = bpy.context.active_object
126
delVerts.extend(f.vertices)
127
selEdges = [e.vertices for e in me.edges if e.select]
129
if len(selEdges) and len(delVerts):
131
for eVerts in selEdges:
136
if v0 in delVerts and v1 in delVerts:
137
ln = (me.vertices[v0].co - me.vertices[v1].co).length
138
if min is False or (ln > 0.0 and ln < min):
144
# Get the average length of the outer edges of the current selection
145
def get_average_outer_edge_length():
147
ob = bpy.context.active_object
152
delFaces = [f.vertices for f in me.faces if not f.select]
153
selEdges = [e.vertices for e in me.edges if e.select]
155
if len(selEdges) and len(delFaces):
159
for eVerts in selEdges:
164
for fVerts in delFaces:
165
if v0 in fVerts and v1 in fVerts:
167
ave += (me.vertices[v0].co - me.vertices[v1].co).length
177
# Get the selected (or deselected items)
178
def get_selected(type='vertices',invert=False):
180
mesh = bpy.context.active_object.data
182
if type == 'vertices':
183
items = mesh.vertices
184
elif type == 'edges':
187
items = mesh.polygons
190
L = [i for i in items if not i.select]
192
L = [i for i in items if i.select]
197
# See if the mesh has something selected
198
def has_selected(type='vertices',invert=False):
200
mesh = bpy.context.active_object.data
202
if type == 'vertices':
203
items = mesh.vertices
204
elif type == 'edges':
207
items = mesh.polygons
210
if not invert and i.select:
212
elif invert and not i.select:
219
# Get all the selected vertices (mode is selected or deselected)
220
def get_selected_vertices(mode='selected'):
222
vertices = bpy.context.active_object.data.vertices
224
if mode == 'deselected':
225
L = [v for v in vertices if not v.select]
227
L = [v for v in vertices if v.select]
232
# Get all the selected edges (mode is selected or deselected)
233
def get_selected_edges(mode='selected'):
235
edges = bpy.context.active_object.data.edges
237
if mode == 'deselected':
238
L = [e for e in edges if not e.select]
240
L = [e for e in edges if e.select]
245
# Get all the selected faces (mode is selected or deselected)
246
def get_selected_faces(mode='selected'):
248
polygons = bpy.context.active_object.data.polygons
250
if mode == 'deselected':
251
L = [f for f in polygons if not f.select]
253
L = [f for f in polygons if f.select]
258
# See if there is at least one selected item in 'items'
259
def contains_selected_item(items):
b'\\ No newline at end of file'