-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathmatrix_in_mk3.py
303 lines (247 loc) · 11.5 KB
/
matrix_in_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
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
# ##### 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 EnumProperty, FloatProperty, BoolProperty, StringProperty, FloatVectorProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode, match_long_repeat
from mathutils import Quaternion, Matrix, Euler
from math import pi
mode_items = [
("QUATERNION", "Quaternion", "Rotation given as a Quaternion", 0),
("EULER", "Euler Angles", "Rotation given as Euler Angles", 1),
("AXISANGLE", "Axis Angle", "Rotation given as Axis & Angle", 2),
]
euler_order_items = [
('XYZ', "XYZ", "", 0),
('XZY', 'XZY', "", 1),
('YXZ', 'YXZ', "", 2),
('YZX', 'YZX', "", 3),
('ZXY', 'ZXY', "", 4),
('ZYX', 'ZYX', "", 5)
]
angle_unit_items = [
("RAD", "Rad", "Radians", "", 0),
("DEG", "Deg", "Degrees", "", 1),
("UNI", "Uni", "Unities", "", 2)
]
angle_unit_conversion = {
"RAD": {"RAD": 1, "DEG": 180/pi, "UNI": 1/(2*pi)},
"DEG": {"RAD": pi/180, "DEG": 1, "UNI": 1/360},
"UNI": {"RAD": 2*pi, "DEG": 360, "UNI": 1}
}
input_sockets = {
"QUATERNION": ["Quaternion"],
"EULER": ["Angle X", "Angle Y", "Angle Z"],
"AXISANGLE": ["Axis", "Angle"],
}
mat_t = Matrix().Identity(4) # pre-allocate once for performance (translation)
mat_s = Matrix().Identity(4) # pre-allocate once for performance (scale)
class SvMatrixInNodeMK3(SverchCustomTreeNode, bpy.types.Node):
"""
Triggers: Loc, Rot, Scale, Angle
Tooltip: Generate matrix from various components
"""
bl_idname = 'SvMatrixInNodeMK3'
bl_label = 'Matrix In'
sv_icon = 'SV_MATRIX_IN'
replacement_nodes = [('SvMatrixInNodeMK4', None, None)]
def update_mode(self, context):
# hide all input sockets
for k, names in input_sockets.items():
for name in names:
self.inputs[name].hide_safe = True
# show mode specific input sockets
for name in input_sockets[self.mode]:
self.inputs[name].hide_safe = False
updateNode(self, context)
def update_angle_units(self, context):
''' Update all the angles to preserve their values in the new units '''
auc = angle_unit_conversion[self.last_angle_units][self.angle_units]
self.last_angle_units = self.angle_units # keep track of the last units
self.syncing = True # deactivate updates
self.angle = self.angle * auc
self.angle_x = self.angle_x * auc
self.angle_y = self.angle_y * auc
self.angle_z = self.angle_z * auc
self.syncing = False # reactivate updates
updateNode(self, context)
def update_angle(self, context):
''' Wrapper to suppress angle updates when units are changed '''
if self.syncing:
return
updateNode(self, context)
mode: EnumProperty(
name='Mode', description='The input component format of the matrix',
items=mode_items, default="AXISANGLE", update=update_mode)
euler_order: EnumProperty(
name="Euler Order", description="Order of the Euler rotations",
default="XYZ", items=euler_order_items, update=updateNode)
angle_units: EnumProperty(
name="Angle Units", description="Angle units (radians/degrees/unities)",
default="DEG", items=angle_unit_items, update=update_angle_units)
last_angle_units: EnumProperty(
name="Last Angle Units", description="Angle units (radians/degrees/unities)",
default="DEG", items=angle_unit_items)
scale: FloatVectorProperty(
name='Scale', description='Scale component of the matrix',
size=3, default=(1.0, 1.0, 1.0), precision=3, subtype="XYZ", update=updateNode)
location_: FloatVectorProperty(
name='Location', description='Location component of the matrix',
size=3, default=(0.0, 0.0, 0.0), precision=3, subtype="XYZ", update=updateNode)
quaternion: FloatVectorProperty(
name="Quaternion", description="Quaternion to convert to rotation matrix",
size=4, subtype="QUATERNION", default=(1.0, 0.0, 0.0, 0.0), precision=3,
update=updateNode)
angle_x: FloatProperty(
name='Angle X', description='Rotation angle about X axis',
default=0.0, precision=3, update=update_angle)
angle_y: FloatProperty(
name='Angle Y', description='Rotation angle about Y axis',
default=0.0, precision=3, update=update_angle)
angle_z: FloatProperty(
name='Angle Z', description='Rotation angle about Z axis',
default=0.0, precision=3, update=update_angle)
angle: FloatProperty(
name='Angle', description='Rotation angle about the given axis',
default=0.0, precision=3, update=update_angle)
axis: FloatVectorProperty(
name='Axis', description='Axis of rotation',
size=3, default=(0.0, 0.0, 1.0), precision=3, subtype="XYZ", update=updateNode)
flat_output: BoolProperty(
name="Flat output", description="Flatten output by list-joining level 1",
default=True, update=updateNode)
syncing: BoolProperty(
name='Syncing', description='Syncing flag', default=False)
def migrate_from(self, old_node):
''' Migration from MK2 (attributes mapping) '''
if old_node.bl_idname == "SvMatrixGenNodeMK2":
self.location_ = old_node.l_
self.scale = old_node.s_
self.axis = old_node.r_
self.angle = old_node.a_
def sv_init(self, context):
self.inputs.new('SvVerticesSocket', "Location").prop_name = "location_"
self.inputs.new('SvVerticesSocket', "Scale").prop_name = 'scale'
self.inputs.new('SvQuaternionSocket', "Quaternion").prop_name = "quaternion"
self.inputs.new('SvStringsSocket', "Angle X").prop_name = 'angle_x'
self.inputs.new('SvStringsSocket', "Angle Y").prop_name = 'angle_y'
self.inputs.new('SvStringsSocket', "Angle Z").prop_name = 'angle_z'
self.inputs.new('SvVerticesSocket', "Axis").prop_name = "axis"
self.inputs.new('SvStringsSocket', "Angle").prop_name = 'angle'
self.outputs.new('SvMatrixSocket', "Matrices")
self.update_mode(context)
def draw_buttons(self, context, layout):
layout.prop(self, "mode", expand=False, text="")
if self.mode == "EULER":
col = layout.column(align=True)
col.prop(self, "euler_order", text="")
if self.mode in {"EULER", "AXISANGLE"}:
row = layout.row(align=True)
row.prop(self, "angle_units", expand=True)
def draw_buttons_ext(self, context, layout):
layout.prop(self, "flat_output", text="Flat Output", expand=False)
def rclick_menu(self, context, layout):
layout.prop(self, "flat_output", text="Flat Output", expand=False)
self.node_replacement_menu(context, layout)
def process(self):
if not self.outputs['Matrices'].is_linked:
return
inputs = self.inputs
matrix_list = []
add_matrix = matrix_list.extend if self.flat_output else matrix_list.append
if self.mode == "QUATERNION":
input_l = inputs["Location"].sv_get(deepcopy=False)
input_s = inputs["Scale"].sv_get(deepcopy=False)
input_q = inputs["Quaternion"].sv_get(deepcopy=False)
if inputs["Quaternion"].is_linked:
input_q = [input_q]
else:
input_q = [[Quaternion(input_q[0][0])]]
I = [input_l, input_q, input_s]
params1 = match_long_repeat(I)
for ll, ql, sl in zip(*params1):
params2 = match_long_repeat([ll, ql, sl])
matrices = []
for location, quaternion, scale in zip(*params2):
# translation
mat_t[0][3] = location[0]
mat_t[1][3] = location[1]
mat_t[2][3] = location[2]
# rotation
mat_r = quaternion.to_matrix().to_4x4()
# scale
mat_s[0][0] = scale[0]
mat_s[1][1] = scale[1]
mat_s[2][2] = scale[2]
# composite matrix
m = mat_t @ mat_r @ mat_s
matrices.append(m)
add_matrix(matrices)
elif self.mode == "EULER":
socket_names = ["Location", "Angle X", "Angle Y", "Angle Z", "Scale"]
I = [inputs[name].sv_get(deepcopy=False) for name in socket_names]
params1 = match_long_repeat(I)
auc = angle_unit_conversion[self.angle_units]["RAD"] # convert to radians
for ll, axl, ayl, azl, sl in zip(*params1):
params2 = match_long_repeat([ll, axl, ayl, azl, sl])
matrices = []
for location, angleX, angleY, angleZ, scale in zip(*params2):
# translation
mat_t[0][3] = location[0]
mat_t[1][3] = location[1]
mat_t[2][3] = location[2]
# rotation
angles = (angleX * auc, angleY * auc, angleZ * auc)
euler = Euler(angles, self.euler_order)
mat_r = euler.to_quaternion().to_matrix().to_4x4()
# scale
mat_s[0][0] = scale[0]
mat_s[1][1] = scale[1]
mat_s[2][2] = scale[2]
# composite matrix
m = mat_t @ mat_r @ mat_s
matrices.append(m)
add_matrix(matrices)
elif self.mode == "AXISANGLE":
socket_names = ["Location", "Axis", "Angle", "Scale"]
I = [inputs[name].sv_get(deepcopy=False) for name in socket_names]
params1 = match_long_repeat(I)
auc = angle_unit_conversion[self.angle_units]["RAD"] # convert to radians
for ll, xl, al, sl in zip(*params1):
params2 = match_long_repeat([ll, xl, al, sl])
matrices = []
for location, axis, angle, scale in zip(*params2):
# translation
mat_t[0][3] = location[0]
mat_t[1][3] = location[1]
mat_t[2][3] = location[2]
# rotation
mat_r = Quaternion(axis, angle * auc).to_matrix().to_4x4()
# scale
mat_s[0][0] = scale[0]
mat_s[1][1] = scale[1]
mat_s[2][2] = scale[2]
# composite matrix
m = mat_t @ mat_r @ mat_s
matrices.append(m)
add_matrix(matrices)
self.outputs['Matrices'].sv_set(matrix_list)
def register():
bpy.utils.register_class(SvMatrixInNodeMK3)
def unregister():
bpy.utils.unregister_class(SvMatrixInNodeMK3)