forked from nortikin/sverchok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_VectorDrop.py
93 lines (69 loc) · 2.81 KB
/
node_VectorDrop.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
import bpy
from node_s import *
from mathutils import *
from util import *
class VectorDropNode(Node, SverchCustomTreeNode):
''' Drop vertices depending on matrix, as on default rotation, drops to zero matrix '''
bl_idname = 'VectorDropNode'
bl_label = 'Vector Drop'
bl_icon = 'OUTLINER_OB_EMPTY'
def init(self, context):
self.inputs.new('VerticesSocket', "Vectors", "Vectors")
self.inputs.new('MatrixSocket', "Matrixes", "Matrixes")
self.outputs.new('VerticesSocket', "Vectors", "Vectors")
def update(self):
# inputs
if 'Vectors' in self.outputs and len(self.outputs['Vectors'].links)>0:
if self.inputs['Vectors'].links and \
type(self.inputs['Vectors'].links[0].from_socket) == VerticesSocket \
and self.inputs['Matrixes'] and \
type(self.inputs['Matrixes'].links[0].from_socket) == MatrixSocket:
vecs_ = SvGetSocketAnyType(self,self.inputs['Vectors'])
vecs = Vector_generate(vecs_)
#print (vecs)
mats_ = dataCorrect(SvGetSocketAnyType(self,self.inputs['Matrixes']))
mats = Matrix_generate(mats_)
else:
vecs = [[]]
mats = [Matrix()]
# outputs
vectors_ = self.vecscorrect(vecs, mats)
vectors = Vector_degenerate(vectors_)
SvSetSocketAnyType(self,'Vectors', vectors)
def vecscorrect(self, vecs, mats):
out = []
lengthve = len(vecs)-1
for i, m in enumerate(mats):
out_ = []
k = i
if k > lengthve:
k = lengthve
vec_c=Vector((0,0,0))
for v in vecs[k]:
vec = v*m
out_.append(vec)
vec_c+=vec
vec_c = vec_c / len(vecs[k])
v = out_[1]-out_[0]
w = out_[2]-out_[0]
A = v.y*w.z - v.z*w.y
B = -v.x*w.z + v.z*w.x
C = v.x*w.y - v.y*w.x
#D = -out_[0].x*A - out_[0].y*B - out_[0].z*C
norm = Vector((A,B,C)).normalized()
vec0 = Vector((0,0,1))
mat_rot_norm = vec0.rotation_difference(norm).to_matrix().to_4x4()
out_pre=[]
for v in out_:
v_out = (v-vec_c)* mat_rot_norm
out_pre.append(v_out)
out.append(out_pre)
return out
def update_socket(self, context):
updateNode(self,context)
def register():
bpy.utils.register_class(VectorDropNode)
def unregister():
bpy.utils.unregister_class(VectorDropNode)
if __name__ == "__main__":
register()