-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathline_mk3.py
247 lines (205 loc) · 8.24 KB
/
line_mk3.py
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
# ##### 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 #####
import bpy
from bpy.props import IntProperty, FloatProperty, BoolProperty, EnumProperty, FloatVectorProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode, fullList, match_long_repeat
from sverchok.utils.modules.geom_utils import interp_v3_v3v3, normalize, add_v3_v3v3, sub_v3_v3v3
directionItems = [
("X", "X", "Along X axis", 0),
("Y", "Y", "Along Y axis", 1),
("Z", "Z", "Along Z axis", 2),
("AB", "AB", "Between 2 points", 3),
("OD", "OD", "Origin and Direction", 4),
]
def make_line(steps, center, direction, vert_a, vert_b):
if direction == "X":
vec = lambda l: (l, 0.0, 0.0)
elif direction == "Y":
vec = lambda l: (0.0, l, 0.0)
elif direction == "Z":
vec = lambda l: (0.0, 0.0, l)
elif direction in ["AB", "OD"]:
vec = lambda l: interp_v3_v3v3(vert_a, vert_b, l)
verts = []
add_vert = verts.append
x = -sum(steps) / 2 if center else 0
for s in [0.0] + steps:
x = x + s
add_vert(vec(x))
edges = [[i, i + 1] for i in range(len(steps))]
return verts, edges
class SvLineNodeMK3(SverchCustomTreeNode, bpy.types.Node):
"""
Triggers: Line, segment.
Tooltip: Generate line.
"""
bl_idname = 'SvLineNodeMK3'
bl_label = 'Line'
bl_icon = 'GRIP'
sv_icon = 'SV_LINE'
def update_size_socket(self, context):
""" need to do UX transformation before updating node"""
size_socket = self.inputs["Size"]
size_socket.hide_safe = not self.normalize
updateNode(self, context)
def update_vect_socket(self, context):
""" need to do UX transformation before updating node"""
si = self.inputs
sd = self.direction
if sd == "OD" and not si[3].name[0] == "O":
si[3].name = "Origin"
si[4].name = "Direction"
si[3].prop_name = 'v3_origin'
si[4].prop_name = 'v3_dir'
elif sd == "AB" and not si[3].name[0] == "A":
si[3].name = "A"
si[4].name = "B"
si[3].prop_name = 'v3_input_0'
si[4].prop_name = 'v3_input_1'
ortho = sd not in ["AB", "OD"]
if (not ortho and si[3].hide_safe) or ortho:
si[3].hide_safe = ortho
si[4].hide_safe = ortho
updateNode(self, context)
@property
def replacement_nodes(self):
if self.direction == 'AB' and not self.normalize:
return [('SvSegmentGenerator', {'Num': 'Cuts'}, {'Vertices': 'Verts'})]
else:
return [('SvLineNodeMK4', None, {'Vertices': 'Verts'})]
direction: EnumProperty(
name="Direction", items=directionItems,
default="X", update=update_vect_socket)
num: IntProperty(
name='Num Verts', description='Number of Vertices',
default=2, min=2, update=updateNode)
step: FloatProperty(
name='Step', description='Step length',
default=1.0, update=updateNode)
center: BoolProperty(
name='Center', description='Center the line',
default=False, update=updateNode)
normalize: BoolProperty(
name='Normalize', description='Normalize line to size',
default=False, update=update_size_socket)
size: FloatProperty(
name='Size', description='Size of line',
default=10.0, update=updateNode)
v3_input_0: FloatVectorProperty(
name='A', description='Starting point',
size=3, default=(0, 0, 0),
update=updateNode)
v3_input_1: FloatVectorProperty(
name='B', description='End point',
size=3, default=(0.5, 0.5, 0.5),
update=updateNode)
v3_origin: FloatVectorProperty(
name='Origin', description='Origin of line',
size=3, default=(0, 0, 0),
update=updateNode)
v3_dir: FloatVectorProperty(
name='Direction', description='Direction',
size=3, default=(1, 1, 1),
update=updateNode)
def set_size_socket(self):
size_socket = self.inputs.new('SvStringsSocket', "Size")
size_socket.prop_name = 'size'
size_socket.hide_safe = not self.normalize
def set_vector_sockets(self):
si = self.inputs
si.new('SvVerticesSocket', "A").prop_name = 'v3_input_0'
si.new('SvVerticesSocket', "B").prop_name = 'v3_input_1'
si[3].hide_safe = self.direction not in ["AB", " OD"]
si[4].hide_safe = self.direction not in ["AB", " OD"]
def sv_init(self, context):
si = self.inputs
si.new('SvStringsSocket', "Num").prop_name = 'num'
si.new('SvStringsSocket', "Step").prop_name = 'step'
self.set_size_socket()
self.set_vector_sockets()
self.outputs.new('SvVerticesSocket', "Vertices")
self.outputs.new('SvStringsSocket', "Edges")
def draw_buttons(self, context, layout):
col = layout.column(align=True)
row = col.row(align=True)
row.prop(self, "direction", expand=True)
row = col.row(align=True)
row.prop(self, "center", toggle=True)
row.prop(self, "normalize", toggle=True)
def get_data(self):
c, d = self.center, self.direction
input_num = self.inputs["Num"].sv_get()
input_step = self.inputs["Step"].sv_get()
normal_size = [2.0 if c else 1.0]
if self.normalize:
normal_size = self.inputs["Size"].sv_get()[0]
params = [input_num, input_step, normal_size]
if d in ["AB", "OD"]:
v_a = self.inputs[3].sv_get()[0]
v_b = self.inputs[4].sv_get()[0]
params.append(v_a)
params.append(v_b)
return match_long_repeat(params)
def define_steplist(self, step_list, s, n, nor, normal):
for num in n:
num = max(2, num)
s = s[:(num - 1)] # shorten if needed
fullList(s, num - 1) # extend if needed
step_list.append([S * nor / sum(s) for S in s] if normal else s)
def process_vectors(self, pts_list, d, va, vb):
if d == "AB" and self.normalize:
vb = add_v3_v3v3(normalize(sub_v3_v3v3(vb, va)), va)
elif d == "OD":
vb = add_v3_v3v3(normalize(vb), va)
pts_list.append((va, vb))
def process(self):
if not any(s.is_linked for s in self.outputs):
return
c, d = self.center, self.direction
step_list = []
pts_list = []
verts_out, edges_out = [], []
normal = self.normalize or d == "AB"
advanced = d in ["AB", "OD"]
params = self.get_data()
if advanced:
for p in zip(*params):
n, s, nor, va, vb = p
self.define_steplist(step_list, s, n, nor, normal)
self.process_vectors(pts_list, d, va, vb)
for s, vc in zip(step_list, pts_list):
r1, r2 = make_line(s, c, d, vc[0], vc[1])
verts_out.append(r1)
edges_out.append(r2)
else:
for p in zip(*params):
n, s, nor = p
self.define_steplist(step_list, s, n, nor, normal)
for s in step_list:
r1, r2 = make_line(s, c, d, [], [])
verts_out.append(r1)
edges_out.append(r2)
if self.outputs['Vertices'].is_linked:
self.outputs['Vertices'].sv_set(verts_out)
if self.outputs['Edges'].is_linked:
self.outputs['Edges'].sv_set(edges_out)
def register():
bpy.utils.register_class(SvLineNodeMK3)
def unregister():
bpy.utils.unregister_class(SvLineNodeMK3)