forked from nortikin/sverchok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_MatrixInterpolation.py
114 lines (85 loc) · 3.92 KB
/
node_MatrixInterpolation.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
# ##### 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 node_s import *
from util import *
from mathutils import Vector, Matrix
# Matrix are assumed to be in format
# [M1 M2 Mn ...] per Matrix_generate and Matrix_listing
# Instead of empty matrix input identity matrix is used.
# So only one matrix input is needed for useful result
# Factor a list of value float values between 0.0 and 1.0,
class MatrixInterpolationNode(Node, SverchCustomTreeNode):
''' Interpolate between two matrices '''
bl_idname = 'MatrixInterpolationNode'
bl_label = 'Matrix Interpolation'
bl_icon = 'OUTLINER_OB_EMPTY'
factor_ = bpy.props.FloatProperty(name = 'factor1_', description='Factor1', default=0.5,\
min=0.0,max=1.0, options={'ANIMATABLE'}, update=updateNode)
def draw_buttons(self, context, layout):
layout.prop(self,"factor_","Factor:");
def init(self, context):
self.inputs.new('StringsSocket', "Factor", "Factor")
self.inputs.new('MatrixSocket', "A", "A")
self.inputs.new('MatrixSocket', "B", "B")
self.outputs.new('MatrixSocket', "C", "C")
def update(self):
#inputs
A = []
B = []
factor = [] # 0 is valid value so I use [] as placeholder
if 'A' in self.inputs and self.inputs['A'].links and \
type(self.inputs['A'].links[0].from_socket) == MatrixSocket:
A = Matrix_generate(SvGetSocketAnyType(self,self.inputs['A']))
if not A:
A = [Matrix.Identity(4)]
if 'B' in self.inputs and self.inputs['B'].links and \
type(self.inputs['B'].links[0].from_socket) == MatrixSocket:
B = Matrix_generate(SvGetSocketAnyType(self,self.inputs['B']))
if not B:
B = [Matrix.Identity(4)]
if 'Factor' in self.inputs and self.inputs['Factor'].links and \
type(self.inputs['Factor'].links[0].from_socket) == StringsSocket:
factor = SvGetSocketAnyType(self,self.inputs['Factor'])
if not factor:
factor = [[self.factor_]]
if 'C' in self.outputs and self.outputs['C'].links:
matrixes_=[]
# match inputs, first matrix A and B using fullList
# then extend the factor list if necessary,
# A and B should control length of list, not interpolation lists
max_l = max(len(A),len(B))
fullList(A,max_l)
fullList(B,max_l)
if len(factor) < max_l:
fullList(factor,max_l)
for i in range(max_l):
for k in range(len(factor[i])):
matrixes_.append(A[i].lerp(B[i], factor[i][k]))
if not matrixes_:
return
matrixes = Matrix_listing(matrixes_)
SvSetSocketAnyType(self, 'C', matrixes)
def update_socket(self, context):
self.update()
def register():
bpy.utils.register_class(MatrixInterpolationNode)
def unregister():
bpy.utils.unregister_class(MatrixInterpolationNode)
if __name__ == "__main__":
register()