Newer
Older
# ##### 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 #####
bl_info = {
"name": "LoopTools",
"author": "Bart Crouch",
"version": (4, 5, 2),
"location": "View3D > Toolbar and View3D > Specials (W-key)",
"warning": "",
"description": "Mesh modelling toolkit. Several tools to aid modelling",
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
"Scripts/Modeling/LoopTools",
"tracker_url": "http://projects.blender.org/tracker/index.php?"
"func=detail&aid=26189",
"category": "Mesh"}
import bmesh
import bpy
import collections
import mathutils
import math
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
##########################################
####### General functions ################
##########################################
# used by all tools to improve speed on reruns
looptools_cache = {}
# force a full recalculation next time
def cache_delete(tool):
if tool in looptools_cache:
del looptools_cache[tool]
# check cache for stored information
def cache_read(tool, object, bm, input_method, boundaries):
# current tool not cached yet
if tool not in looptools_cache:
return(False, False, False, False, False)
# check if selected object didn't change
if object.name != looptools_cache[tool]["object"]:
return(False, False, False, False, False)
# check if input didn't change
if input_method != looptools_cache[tool]["input_method"]:
return(False, False, False, False, False)
if boundaries != looptools_cache[tool]["boundaries"]:
return(False, False, False, False, False)
modifiers = [mod.name for mod in object.modifiers if mod.show_viewport \
and mod.type == 'MIRROR']
if modifiers != looptools_cache[tool]["modifiers"]:
return(False, False, False, False, False)
input = [v.index for v in bm.verts if v.select and not v.hide]
if input != looptools_cache[tool]["input"]:
return(False, False, False, False, False)
# reading values
single_loops = looptools_cache[tool]["single_loops"]
loops = looptools_cache[tool]["loops"]
derived = looptools_cache[tool]["derived"]
mapping = looptools_cache[tool]["mapping"]
return(True, single_loops, loops, derived, mapping)
# store information in the cache
def cache_write(tool, object, bm, input_method, boundaries, single_loops,
loops, derived, mapping):
# clear cache of current tool
if tool in looptools_cache:
del looptools_cache[tool]
# prepare values to be saved to cache
input = [v.index for v in bm.verts if v.select and not v.hide]
modifiers = [mod.name for mod in object.modifiers if mod.show_viewport \
and mod.type == 'MIRROR']
# update cache
looptools_cache[tool] = {"input": input, "object": object.name,
"input_method": input_method, "boundaries": boundaries,
"single_loops": single_loops, "loops": loops,
"derived": derived, "mapping": mapping, "modifiers": modifiers}
# calculates natural cubic splines through all given knots
def calculate_cubic_splines(bm_mod, tknots, knots):
# hack for circular loops
if knots[0] == knots[-1] and len(knots) > 1:
circular = True
k_new1 = []
for k in range(-1, -5, -1):
if k - 1 < -len(knots):
k += len(knots)
k_new1.append(knots[k-1])
k_new2 = []
for k in range(4):
if k + 1 > len(knots) - 1:
k -= len(knots)
k_new2.append(knots[k+1])
for k in k_new1:
knots.insert(0, k)
for k in k_new2:
knots.append(k)
t_new1 = []
total1 = 0
for t in range(-1, -5, -1):
if t - 1 < -len(tknots):
t += len(tknots)
total1 += tknots[t] - tknots[t-1]
t_new1.append(tknots[0] - total1)
t_new2 = []
total2 = 0
for t in range(4):
if t + 1 > len(tknots) - 1:
t -= len(tknots)
total2 += tknots[t+1] - tknots[t]
t_new2.append(tknots[-1] + total2)
for t in t_new1:
tknots.insert(0, t)
for t in t_new2:
tknots.append(t)
else:
circular = False
# end of hack
n = len(knots)
if n < 2:
return False
x = tknots[:]
locs = [bm_mod.verts[k].co[:] for k in knots]
result = []
for j in range(3):
a = []
for i in locs:
a.append(i[j])
h = []
for i in range(n-1):
if x[i+1] - x[i] == 0:
h.append(1e-8)
else:
h.append(x[i+1] - x[i])
q = [False]
for i in range(1, n-1):
q.append(3/h[i]*(a[i+1]-a[i]) - 3/h[i-1]*(a[i]-a[i-1]))
l = [1.0]
u = [0.0]
z = [0.0]
for i in range(1, n-1):
l.append(2*(x[i+1]-x[i-1]) - h[i-1]*u[i-1])
if l[i] == 0:
l[i] = 1e-8
u.append(h[i] / l[i])
z.append((q[i] - h[i-1] * z[i-1]) / l[i])
l.append(1.0)
z.append(0.0)
b = [False for i in range(n-1)]
c = [False for i in range(n)]
d = [False for i in range(n-1)]
c[n-1] = 0.0
for i in range(n-2, -1, -1):
c[i] = z[i] - u[i]*c[i+1]
b[i] = (a[i+1]-a[i])/h[i] - h[i]*(c[i+1]+2*c[i])/3
d[i] = (c[i+1]-c[i]) / (3*h[i])
for i in range(n-1):
result.append([a[i], b[i], c[i], d[i], x[i]])
splines = []
for i in range(len(knots)-1):
splines.append([result[i], result[i+n-1], result[i+(n-1)*2]])
if circular: # cleaning up after hack
knots = knots[4:-4]
tknots = tknots[4:-4]
return(splines)
# calculates linear splines through all given knots
def calculate_linear_splines(bm_mod, tknots, knots):
splines = []
for i in range(len(knots)-1):
a = bm_mod.verts[knots[i]].co
b = bm_mod.verts[knots[i+1]].co
d = b-a
Loading
Loading full blame...