forked from nortikin/sverchok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_CentersPolsNode.py
118 lines (96 loc) · 5.15 KB
/
node_CentersPolsNode.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
import bpy, bmesh, mathutils
from mathutils import Vector, Matrix
from node_s import *
from util import *
class CentersPolsNode(Node, SverchCustomTreeNode):
''' Centers of polygons of mesh (not including matrixes, so apply scale-rot-loc ctrl+A) '''
bl_idname = 'CentersPolsNode'
bl_label = 'Centers polygons'
bl_icon = 'OUTLINER_OB_EMPTY'
def init(self, context):
self.inputs.new('VerticesSocket', "Vertices", "Vertices")
self.inputs.new('StringsSocket', "Polygons", "Polygons")
self.outputs.new('VerticesSocket',"Normals","Normals")
self.outputs.new('MatrixSocket',"Centers","Centers")
def update(self):
# достаём два слота - вершины и полики
if 'Centers' in self.outputs and self.outputs['Centers'].links or self.outputs['Normals'].links:
if 'Polygons' in self.inputs and 'Vertices' in self.inputs \
and self.inputs['Polygons'].links and self.inputs['Vertices'].links:
pols_ = SvGetSocketAnyType(self,self.inputs['Polygons'])
#else:
# pols = []
#if type(self.inputs['Vertices'].links[0].from_socket) == VerticesSocket:
vers_ = SvGetSocketAnyType(self,self.inputs['Vertices'])
#else:
# vers = []
#print ('Центрист. полики, верики: ', pols, vers)
# output
# make mesh temp утилитарно - удалить в конце
mat_collect = []
normalsFORout = []
for i_obj, vers in enumerate(vers_):
pols = pols_[i_obj]
mesh_temp = bpy.data.meshes.new('temp')
mesh_temp.from_pydata(vers,[],pols)
mesh_temp.update(calc_edges=True)
# medians в векторах
medians = []
for p in pols:
v0 = Vector(vers[p[0]][:])
v1 = Vector(vers[p[1]][:])
v2 = Vector(vers[p[2]][:])
if len(p)>3:
v3 = Vector(vers[p[3]][:])
poi_2 = (v2+v3)/2
else:
poi_2 = v2
poi_1 = (v0+v1)/2
vm = poi_2 - poi_1
medians.append(vm)
# centers, normals - делает векторы из mesh temp
centrs = []
normals = []
normalsFORout_ = []
for p in mesh_temp.polygons:
centrs.append(Vector(p.center[:]))
normals.append(p.normal)
normalsFORout_.append(p.normal[:])
normalsFORout.append(normalsFORout_)
#print ('norm', normals, normalsFORout)
# centrs = mathutils.geometry.normal( lambda(vers[p[0]], vers[p[1]], vers[p[2]] for p in pols) ) # альтернатива
mat_collect_ = []
for i,c in enumerate(centrs):
mat_loc = Matrix.Translation(c)
aa = Vector((0,1e-6,1))
bb = Vector((normals[i][:]))
vec = aa
q_rot = vec.rotation_difference(bb).to_matrix().to_4x4()
vec2 = bb
q_rot2 = vec2.rotation_difference(aa).to_matrix().to_4x4()
a = Vector((1e-6,1,0))* q_rot2
b = medians[i]
vec1 = a
q_rot1 = vec1.rotation_difference(b).to_matrix().to_4x4()
M = mat_loc*q_rot1*q_rot
lM = []
for j in M:
lM.append((j[:]))
# отдаётся параметр матрицы на сокет. просто присвоение матрицы
mat_collect_.append(lM)
mat_collect.extend(mat_collect_)
#print ( 'M'+ str(M) + '\n' + 'lM' + str(lM) + '\n' + 'qrot' + str(q_rot1) + '\n' )
#print ( 'matrix: ' + str(mat_collect) )
SvSetSocketAnyType(self,'Centers',mat_collect)
# удаляем временный мусор
bpy.data.meshes.remove(mesh_temp)
if 'Normals' in self.outputs and len(self.outputs['Normals'].links)>0:
SvSetSocketAnyType(self,'Normals',normalsFORout)
def update_socket(self, context):
self.update()
def register():
bpy.utils.register_class(CentersPolsNode)
def unregister():
bpy.utils.unregister_class(CentersPolsNode)
if __name__ == "__main__":
register()