-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgeometry_nodes_manager.py
438 lines (416 loc) · 20.5 KB
/
geometry_nodes_manager.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# Nikita Akimov
#
# GitHub
# https://github.com/Korchy/BIS
import bpy
import json
import os
import re
import tempfile
from . import cfg
from .addon import Addon
from .bis_items import BISItems
from .file_manager import FileManager
from .blender_ex import BlenderEx
from .bpy_plus.names import Names
from .data_block_manager import DataBlockManager
from .material import Material
from .node_tree import NodeTree
from .WebRequests import WebRequest, WebAuthVars
class GeometryNodesManager(DataBlockManager):
# Geometry Nodes
_limit_file_size = 3*1024*1024 # max exported to .blend and zipped file size (3 Mb)
_storage_type = 'NODE_EDITOR'
@classmethod
def items_from_bis(cls, context, search_filter, page, update_preview):
# get page of items list from BIS
rez = None
storage_subtype = cls.subtype(context) # GeometryNodeTree
storage_subtype2 = Material.get_subtype2(context) # OBJECT
request = WebRequest.send_request(
context=context,
data={
'for': 'get_items',
'search_filter': search_filter,
'page': page,
'storage': cls._storage_type,
'storage_subtype': storage_subtype,
'storage_subtype2': storage_subtype2,
'update_preview': update_preview,
'addon_version': Addon.current_version()
}
)
if request:
request_rez = json.loads(request.text)
rez = request_rez['stat']
if request_rez['stat'] == 'OK':
if not request_rez['data']['items']:
if WebAuthVars.userProStatus:
bpy.ops.bis.messagebox(
'INVOKE_DEFAULT',
message='Nothing found'
)
else:
bpy.ops.bis.messagebox(
'INVOKE_DEFAULT',
message='You do not have any active geometry nodes.\n \
Please log in your account on the BIS web site,\n \
Add some meshes to the active palette,\n \
And press this button again.'
)
preview_to_update = BISItems.update_previews_from_data(
data=request_rez['data']['items'],
list_name=cls._storage_type
)
if preview_to_update:
request = WebRequest.send_request(
context=context,
data={
'for': 'update_previews',
'preview_list': preview_to_update,
'storage': cls._storage_type,
'storage_subtype': storage_subtype,
'storage_subtype2': storage_subtype2,
'addon_version': Addon.current_version()
}
)
if request:
previews_update_rez = json.loads(request.text)
if previews_update_rez['stat'] == 'OK':
BISItems.update_previews_from_data(
data=previews_update_rez['data']['items'],
list_name=cls._storage_type
)
BISItems.create_items_list(
data=request_rez['data']['items'],
list_name=cls._storage_type
)
context.window_manager.bis_get_meshes_info_from_storage_vars.current_page = page
context.window_manager.bis_get_meshes_info_from_storage_vars.current_page_status = \
request_rez['data']['status']
return rez
@classmethod
def from_bis(cls, context, bis_item_id, item_type):
# item_type = 'MATERIAL' or 'NODEGROUP'
request_rez = {'stat': 'ERR', 'data': {'text': 'No Id', 'content': None}}
if bis_item_id:
request = WebRequest.send_request(
context=context,
data={
'for': 'get_item',
'storage': cls._storage_type,
'storage_subtype': cls.subtype(context),
'storage_subtype2': Material.get_subtype2(context),
'id': bis_item_id,
'addon_version': Addon.current_version()
}
)
if request:
request_rez = json.loads(request.text)
if request_rez['stat'] == 'OK':
item_in_json = json.loads(request_rez['data']['item'])
if 'file_attachment' in request_rez['data'] and request_rez['data']['file_attachment']:
with tempfile.TemporaryDirectory() as temp_dir:
request_file = WebRequest.send_request(
context=context,
data={
'for': 'get_item_file_attachment',
'storage': cls._storage_type,
'id': bis_item_id
}
)
if request_file:
zip_file_name = str(bis_item_id) + '.zip'
zip_file_path = os.path.join(temp_dir, zip_file_name)
with open(zip_file_path, 'wb') as temp_item_file_attachment:
temp_item_file_attachment.write(request_file.content)
# to file (debug)
FileManager.from_server_to_file(
json_data=item_in_json,
attachment_file_path=zip_file_path
)
if os.path.exists(zip_file_path):
# if already exists node group with the same name - rename it
if item_in_json['data_block_name'] in context.blend_data.node_groups:
context.blend_data.node_groups[item_in_json['data_block_name']].name = \
Names.increase(name=item_in_json['data_block_name'])
# import from attachment .blend file
cls.import_from_blend(
context=context,
zip_file_path=zip_file_path,
file_name=cls._gn_name_validated(name=item_in_json['data_block_name']),
data_block_type=item_in_json['data_block_type'],
data_block_name=item_in_json['data_block_name']
)
geometry_node_tree = context.blend_data.node_groups[item_in_json['data_block_name']]
if geometry_node_tree:
geometry_node_tree['bis_uid'] = bis_item_id
# add to active object geometry nodes modifier
gn_modifier = cls.active_gn_modifier(
context=context,
obj=context.active_object
)
if not gn_modifier:
gn_modifier = cls.new_gn_modifier(
obj=context.active_object
)
if item_type == 'MATERIAL':
# fully replace modifier node tree
gn_modifier.node_group = geometry_node_tree
# set attribute names
cls.set_output_attributes_names(
gn_modifier=gn_modifier,
names=item_in_json['output_attributes']
)
cls.set_input_attributes_names(
gn_modifier=gn_modifier,
names=item_in_json['input_attributes']
)
elif item_type == 'NODEGROUP':
# just add as separate node group to the current modifier node tree
node_group = gn_modifier.node_group.nodes.new(
type='GeometryNodeGroup'
)
node_group.node_tree = geometry_node_tree
node_group.location = (0.0, 0.0)
else:
bpy.ops.bis.messagebox(
'INVOKE_DEFAULT',
message=request_rez['stat'] + ': ' + request_rez['data']['text']
)
return request_rez
@classmethod
def to_bis(cls, context, node_tree_container, tags=''):
request_rez = {'stat': 'ERR', 'data': {'text': 'Error to save'}}
if node_tree_container:
name = cls._gn_name(node_tree_container=node_tree_container)
name_validated = cls._gn_name_validated(name=name)
# store node tree anyway
data_block = cls.node_tree(node_tree_container=node_tree_container)
data_in_json = {
'data_block_type': 'node_groups',
'data_block_name': name
}
# if saving as whole node tree - add input/output attributes names
if node_tree_container.type == 'NODES':
# modifier
data_in_json['input_attributes'] = cls.get_input_attributes_names(
gn_modifier=node_tree_container
)
data_in_json['output_attributes'] = cls.get_output_attributes_names(
gn_modifier=node_tree_container
)
with tempfile.TemporaryDirectory() as temp_dir:
attachments_path = cls.export_to_blend(
context=context,
data_block={data_block},
export_path=temp_dir,
export_file_name=name_validated
)
if attachments_path and os.path.exists(attachments_path):
tags += (';' if tags else '') + ';'.join(cls.tags(node_tree_container=node_tree_container))
# save to file (debug)
FileManager.to_server_to_file(
json_data=data_in_json,
attachment_file_path=attachments_path
)
# send request to server
if not cfg.no_sending_to_server:
request = WebRequest.send_request(
context=context,
data={
'for': 'add_item',
'storage': cls._storage_type,
'storage_subtype': cls.subtype(context=context),
'storage_subtype2': Material.get_subtype2(context=context),
'item_body': json.dumps(data_in_json),
'item_name': name,
'item_tags': tags.strip(),
'procedural': 1 if NodeTree.is_procedural(
node_tree=cls.node_tree(node_tree_container=node_tree_container)) else 0,
'addon_version': Addon.current_version(),
'blender_version': BlenderEx.version_str_short()
},
files={
'attachment_file': open(attachments_path, 'rb')
}
)
if request:
request_rez = json.loads(request.text)
if request_rez['stat'] == 'OK':
data_block['bis_uid'] = request_rez['data']['id']
else:
request_rez['data']['text'] = 'No data to save'
return request_rez
@classmethod
def update_in_bis(cls, context, node_tree_container):
# update current geometry node tree
request_rez = {"stat": "ERR", "data": {"text": "Error to update"}}
if node_tree_container:
name = cls._gn_name(node_tree_container=node_tree_container)
name_validated = cls._gn_name_validated(name=name)
gn_node_tree = cls.node_tree(node_tree_container=node_tree_container)
bis_uid = None
if gn_node_tree:
if 'bis_uid' in gn_node_tree and gn_node_tree['bis_uid']:
bis_uid = gn_node_tree['bis_uid']
else:
request_rez['data']['text'] = 'Save this GN item to the BIS first!'
else:
request_rez['data']['text'] = 'Undefined GN item to update'
if bis_uid:
data_in_json = {
'data_block_type': 'node_groups',
'data_block_name': name
}
# if saving as whole node tree - add input/output attributes names
if node_tree_container.type == 'NODES':
# modifier
data_in_json['input_attributes'] = cls.get_input_attributes_names(
gn_modifier=node_tree_container
)
data_in_json['output_attributes'] = cls.get_output_attributes_names(
gn_modifier=node_tree_container
)
with tempfile.TemporaryDirectory() as temp_dir:
attachments_path = cls.export_to_blend(
context=context,
data_block={gn_node_tree},
export_path=temp_dir,
export_file_name=name_validated
)
if attachments_path and os.path.exists(attachments_path):
# send to server
# save to file (debug)
FileManager.to_server_to_file(
json_data=data_in_json,
attachment_file_path=attachments_path
)
# send request
if not cfg.no_sending_to_server:
request = WebRequest.send_request(
context=context,
data={
'for': 'update_item',
'storage': cls._storage_type,
'storage_subtype': cls.subtype(context=context),
'storage_subtype2': Material.get_subtype2(context=context),
'item_id': bis_uid,
'item_body': json.dumps(data_in_json),
'item_name': name,
'procedural': 1 if NodeTree.is_procedural(
node_tree=cls.node_tree(node_tree_container=node_tree_container)) else 0,
'addon_version': Addon.current_version(),
'blender_version': BlenderEx.version_str_short()
},
files={
'attachment_file': open(attachments_path, 'rb')
}
)
if request:
request_rez = json.loads(request.text)
else:
request_rez['data']['text'] = 'Cant get object to update - no active object from BIS'
return request_rez
@staticmethod
def subtype(context):
# return subtype
if context.area and context.area.spaces.active.type == 'NODE_EDITOR':
return context.area.spaces.active.tree_type
else:
return 'GeometryNodeTree'
@staticmethod
def node_tree(node_tree_container):
# get node tree from container
node_tree = None
if node_tree_container.type == 'GROUP':
node_tree = node_tree_container.node_tree
elif node_tree_container.type == 'NODES':
node_tree = node_tree_container.node_group
return node_tree
@classmethod
def tags(cls, node_tree_container):
# additional tags for geometry nodes
tags = {'geometry nodes', BlenderEx.version_str_short()}
if NodeTree.is_procedural(node_tree=cls.node_tree(node_tree_container=node_tree_container)):
tags.add('procedural')
return tags
@staticmethod
def _gn_name(node_tree_container):
# name
name = ''
if node_tree_container.type == 'GROUP':
name = node_tree_container.node_tree.name
elif node_tree_container.type == 'NODES':
name = node_tree_container.node_group.name
return name
@classmethod
def _gn_name_validated(cls, name):
# validated name
name = ''.join((x if x.isalnum() else '_' for x in name))
return name
@staticmethod
def new_gn_modifier(obj):
# add new geometry nodes modifier for object
gn = obj.modifiers.new(
type='NODES',
name='Geometry Nodes'
)
return gn
@staticmethod
def new_gn_node_tree(context):
# create new geometry node tree
gn_node_tree = context.blend_data.node_groups.new(
type='GeometryNodeTree',
name='Geometry Nodes'
)
return gn_node_tree
@classmethod
def active_gn_modifier(cls, context, obj):
# get active geometry nodes modifier from object
gn_modifier = next((modifier for modifier in obj.modifiers
if modifier.type == 'NODES' and modifier == obj.modifiers.active), None)
if gn_modifier and not gn_modifier.node_group:
gn_modifier.node_group = cls.new_gn_node_tree(
context=context
)
return gn_modifier
@staticmethod
def get_output_attributes_names(gn_modifier):
# get output attributes names
# same order as outputs
return [output[1] for output in gn_modifier.items() if 'Output' in output[0]]
@staticmethod
def get_input_attributes_names(gn_modifier):
# get input attributes names
# same order as inputs
regexp = re.compile(r'^Input_\d.?$')
inputs = [input[0] for input in gn_modifier.items() if regexp.match(input[0])] # [Input_2, Input_6, ...]
inputs_keys = [input for input in gn_modifier.keys() if 'Input' in input]
rez = []
for input in inputs:
if input + '_attribute_name' in inputs_keys:
rez.append(gn_modifier[input + '_attribute_name'])
else:
rez.append('')
return rez
@staticmethod
def set_output_attributes_names(gn_modifier, names: list):
# set names for output attributes
# same order as saved
id_names = [output[0] for output in gn_modifier.items() if 'Output' in output[0]]
for i, name in enumerate(id_names):
gn_modifier[name] = names[i]
@staticmethod
def set_input_attributes_names(gn_modifier, names: list):
# set names for input attributes
# same order as saved
regexp = re.compile(r'^Input_\d.?$')
inputs = [input[0] for input in gn_modifier.items() if regexp.match(input[0])] # [Input_2, Input_6, ...]
for i, name in enumerate(names):
if name:
if inputs[i] + '_use_attribute' in gn_modifier:
gn_modifier[inputs[i] + '_use_attribute'] = 1
if inputs[i] + '_attribute_name' in gn_modifier:
gn_modifier[inputs[i] + '_attribute_name'] = name