forked from nortikin/sverchok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_Bakery.py
184 lines (157 loc) · 6.63 KB
/
node_Bakery.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
import bpy
from node_s import *
from util import *
sverchok_bakery_cache = {}
class BakeryNode(Node, SverchCustomTreeNode):
''' Bakery node to bake geometry every time '''
bl_idname = 'BakeryNode'
bl_label = 'Bakery'
bl_icon = 'OUTLINER_OB_EMPTY'
activate = bpy.props.BoolProperty(name='Show', description='Activate node?', default=True,update=updateNode)
def draw_buttons(self, context, layout):
row = layout.row(align=True)
row.prop(self, "activate", text="Show")
pass
def init(self, context):
self.inputs.new('VerticesSocket', 'vertices', 'vertices')
self.inputs.new('StringsSocket', 'edg_pol', 'edg_pol')
self.inputs.new('MatrixSocket', 'matrix', 'matrix')
def update(self):
# check if running during startup, cancel if True
try:
l=bpy.data.node_groups[self.id_data.name]
except Exception as e:
print("Bakery cannot run during startup",e)
return
if self.inputs['vertices'].links and self.inputs['edg_pol'].links and self.activate:
if 'vertices' in self.inputs and self.inputs['vertices'].links and \
type(self.inputs['vertices'].links[0].from_socket) == VerticesSocket:
propv = SvGetSocketAnyType(self, self.inputs['vertices'])
vertices = dataCorrect(propv, nominal_dept=2)
else:
vertices = []
if 'edg_pol' in self.inputs and self.inputs['edg_pol'].links and \
type(self.inputs['edg_pol'].links[0].from_socket) == StringsSocket:
prope = SvGetSocketAnyType(self, self.inputs['edg_pol'])
edges = dataCorrect(prope)
else:
edges = []
if 'matrix' in self.inputs and self.inputs['matrix'].links and \
type(self.inputs['matrix'].links[0].from_socket) == MatrixSocket:
propm = SvGetSocketAnyType(self, self.inputs['matrix'])
matrices = dataCorrect(propm)
else:
matrices = []
if vertices and edges:
for i in vertices:
matrices.append(Matrix())
if vertices and edges:
self.makeobjects(vertices, edges, matrices)
self.use_custom_color=True
self.color = (1,0.3,0)
else:
self.use_custom_color=True
self.color = (0.1,0.05,0)
for obj in bpy.context.scene.objects:
nam = 'Sv_' + self.name
if nam in obj.name:
bpy.context.scene.objects[obj.name].select = True
bpy.ops.object.delete(use_global=False)
global sverchok_bakery_cache
sverchok_bakery_cache[self.name] = []
def makeobjects(self, vers, edg_pol, mats):
# fht = предохранитель от перебора рёбер и полигонов.
fht = []
if len(edg_pol[0][0]) == 2:
pols = []
for edgs in edg_pol:
maxi = max(max(a) for a in edgs)
fht.append(maxi)
elif len(edg_pol[0][0]) > 2:
edgs = []
for pols in edg_pol:
maxi = max(max(a) for a in pols)
fht.append(maxi)
vertices = Vector_generate(vers)
matrixes = Matrix_generate(mats)
objects = {}
fhtagn = []
for u, f in enumerate(fht):
fhtagn.append(min(len(vertices[u]), fht[u]))
#lenmesh = len(vertices) - 1
# name space for particular bakery node
# defined only by matrices count (without .001 etc)
global sverchok_bakery_cache
try:
cache = sverchok_bakery_cache[self.name]
except:
cache = []
names = ['Sv_' + self.name + str(i) for i, t in enumerate(mats)]
#print('bakery'+str(names)+str(cache))
# delete previous objects удаляет предыдущие объекты, если есть, если надо.
bpy.ops.object.select_all(action='DESELECT')
for i, obj in enumerate(bpy.context.scene.objects):
nam = 'Sv_' + self.name
if nam in obj.name:
if obj.name not in names:
bpy.context.scene.objects[obj.name].select = True
bpy.ops.object.delete(use_global=False)
for i, m in enumerate(matrixes):
# solution to reduce number of vertices respect to edges/pols
########
k = i
lenver = len(vertices) - 1
if i > lenver:
v = vertices[-1]
k = lenver
else:
v = vertices[k]
if (len(v)-1) < fhtagn[k]:
continue
elif fhtagn[k] < (len(v)-1):
nonneed = (len(v)-1) - fhtagn[k]
for q in range(nonneed):
v.pop((fhtagn[k]+1))
#########
# end of solution to reduce vertices
e = edg_pol[k] if edgs else []
p = edg_pol[k] if pols else []
# to change old, create new separately
if names[i] not in cache:
objects[str(i)] = self.makemesh(names[i],v,e,p,m)
elif bpy.context.scene.objects.find(names[i]) >= 0:
objects[str(i)] = self.makemesh_exist(names[i],v,e,p,m)
else:
objects[str(i)] = self.makemesh(names[i],v,e,p,m)
for i, ite in enumerate(objects.values()):
me = ite[1]
ob = ite[0]
calcedg = True
if edgs: calcedg = False
me.update(calc_edges=calcedg)
if ob.name not in cache:
bpy.context.scene.objects.link(ob)
# save cache
sverchok_bakery_cache[self.name] = names
def makemesh(self,i,v,e,p,m):
name = i
me = bpy.data.meshes.new(name)
me.from_pydata(v, e, p)
ob = bpy.data.objects.new(name, me)
ob.matrix_world = m
ob.show_name = False
ob.hide_select = False
return [ob,me]
def makemesh_exist(self,i,v,e,p,m):
name = i
me = bpy.data.meshes.new(name)
me.from_pydata(v, e, p)
ob = bpy.data.objects[name]
ob.matrix_world = m
return [ob,me]
def register():
bpy.utils.register_class(BakeryNode)
def unregister():
bpy.utils.unregister_class(BakeryNode)
if __name__ == "__main__":
register()