-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnode_manager.py
868 lines (851 loc) · 52 KB
/
node_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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# Nikita Akimov
#
# GitHub
# https://github.com/Korchy/BIS
import bpy
import base64
import json
import os
from distutils.version import StrictVersion
from shutil import copyfile
import tempfile
import zlib
from .file_manager import FileManager
from . import cfg
from .node_group import NodeGroup
from .material import Material
from .node_tree import NodeTree
from .addon import Addon
from .blender_ex import BlenderEx
from .WebRequests import WebRequest, WebAuthVars
from .bis_items import BISItems
class NodeManager:
_material_limit_file_size = 3*1024*1024 # max zipped file with textures size (3 Mb)
@classmethod
def items_from_bis(cls, context, search_filter, page, update_preview=False):
# get page of items list from BIS
rez = None
storage_subtype = Material.get_subtype(context)
storage_subtype2 = Material.get_subtype2(context)
request = WebRequest.send_request(
context=context,
data={
'for': 'get_items',
'search_filter': search_filter,
'page': page,
'storage': cls.storage_type(context=context),
'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 materials.\n \
Please log in your account on the BIS web site,\n \
Add some materials 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(context)
)
if preview_to_update:
request = WebRequest.send_request(
context=context,
data={
'for': 'update_previews',
'preview_list': preview_to_update,
'storage': cls.storage_type(context),
'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(context)
)
BISItems.create_items_list(
data=request_rez['data']['items'],
list_name=cls.storage_type(context)
)
context.window_manager.bis_get_nodes_info_from_storage_vars.current_page = page
context.window_manager.bis_get_nodes_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:
if cls.active_object(context=context, use_selected=True):
subtype = Material.get_subtype(context=context)
request = WebRequest.send_request(
context=context,
data={
'for': 'get_item',
'storage': cls.storage_type(context=context),
'storage_subtype': subtype,
'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':
# attachment
attachments_path = ''
item_version = request_rez['data']['addon_version']
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(context=context),
'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)
if cfg.from_server_to_file:
copyfile(
zip_file_path,
os.path.join(FileManager.project_dir(), zip_file_name)
)
# unzip to project directory
attachments_path = os.path.join(FileManager.attachments_path(), str(bis_item_id))
FileManager.unzip_files(source_zip_path=zip_file_path, dest_dir=attachments_path)
# item body
# decompress
item_json_compressed = base64.b64decode(request_rez['data']['item'])
item_json_str = zlib.decompress(item_json_compressed).decode('utf-8')
item_in_json = json.loads(item_json_str)
# to file (debug)
if cfg.from_server_to_file:
with open(os.path.join(FileManager.project_dir(), 'received_from_server.json'), 'w') \
as currentFile:
json.dump(item_in_json, currentFile, indent=4)
item_in_json['bis_version'] = item_version
item_type_got = item_in_json['instance']['type']
item_name_got = item_in_json['instance']['name']
item_node_tree_got = item_in_json['instance']['node_tree']
# if Addon.node_group_version_higher(item_version, Addon.current_version()):
if StrictVersion(item_version) > StrictVersion(Addon.current_version()):
bpy.ops.bis.messagebox(
'INVOKE_DEFAULT',
message='This material item was saved in higher BIS version and may not load correctly.\
Please download the last BIS add-on version!'
)
if item_type_got == 'Material':
# got Material (can be only object material)
if item_type == 'MATERIAL':
# Material as Material
material = Material.from_json(
context=context,
material_json=item_in_json,
attachments_path=attachments_path
)
cls._deselect_all_nodes(node_tree=material.node_tree)
elif item_type == 'NODEGROUP':
# Material as Node Group
active_node_tree = cls.active_node_tree(context=context)
cls._deselect_all_nodes(node_tree=active_node_tree)
# NodeGroup.new(parent_node_tree=active_node_tree, name=item_name_got)
# wrap material to node group
node_group_json = {
"class": "ShaderNodeGroup",
"instance": {
"inputs": [],
"outputs": [
{
"class": "NodeSocketShader",
"instance": {
"name": "Surface",
"type": "SHADER"
}
},
{
"class": "NodeSocketShader",
"instance": {
"name": "Volume",
"type": "SHADER"
}
},
{
"class": "NodeSocketVector",
"instance": {
"name": "Displacement",
"type": "VECTOR"
}
}
],
"location": {
"class": "Vector",
"instance": {
"x": 0.0,
"y": 0.0
}
},
# "name": item_in_json['instance']['node_tree']['instance']['name'],
"name": item_in_json['instance']['name'],
"width": 300.0,
'node_tree': item_node_tree_got,
"type": "GROUP"
},
'bis_version': item_version
}
input_node_json = {
"class": "NodeGroupInput",
"instance": {
"inputs": [],
"outputs": [],
"location": {
"class": "Vector",
"instance": {
"x": -300.0,
"y": 0.0
}
},
"name": "Group Input"
}
}
output_node_json = {
"class": "NodeGroupOutput",
"instance": {
"inputs": [
{
"class": "NodeSocketShader",
"instance": {
"name": "Surface",
"type": "SHADER"
}
},
{
"class": "NodeSocketShader",
"instance": {
"name": "Volume",
"type": "SHADER"
}
},
{
"class": "NodeSocketVector",
"instance": {
"name": "Displacement",
"type": "VECTOR"
}
}
],
"outputs": [],
"is_active_output": True,
"location": {
"class": "Vector",
"instance": {
"x": 300.0,
"y": 0.0
}
},
"name": "Group Output"
}
}
node_tree_outputs_json = [
{
"class": "NodeSocketInterfaceShader",
"instance": {
"name": "Surface"
},
"bl_socket_idname": "NodeSocketShader"
},
{
"class": "NodeSocketInterfaceShader",
"instance": {
"name": "Volume"
},
"bl_socket_idname": "NodeSocketShader"
},
{
"class": "NodeSocketInterfaceVector",
"instance": {
"name": "Displacement"
},
"bl_socket_idname": "NodeSocketVector"
}
]
node_group_json['instance']['node_tree']['instance']['nodes'].append(input_node_json)
node_group_json['instance']['node_tree']['instance']['nodes'].append(output_node_json)
node_group_json['instance']['node_tree']['instance']['outputs'] += node_tree_outputs_json
node_group_json['instance']['node_tree']['instance']['name'] = item_in_json['instance']['name']
# create node group with material inside
node_group = NodeGroup.from_json(
node_group_json=node_group_json,
parent_node_tree=active_node_tree,
attachments_path=attachments_path
)
# create links from material output nodes to group output node
node_group_output_node = [node for node in node_group.node_tree.nodes
if node.type == 'GROUP_OUTPUT'][0]
for link in node_group.node_tree.links:
if link.to_node.type == 'OUTPUT_MATERIAL':
if link.to_socket.name == 'Surface':
node_group.node_tree.links.new(
link.from_socket,
node_group_output_node.inputs['Surface']
)
elif link.to_socket.name == 'Volume':
node_group.node_tree.links.new(
link.from_socket,
node_group_output_node.inputs['Volume']
)
elif link.to_socket.name == 'Displacement':
node_group.node_tree.links.new(
link.from_socket,
node_group_output_node.inputs['Displacement']
)
# remove material output nodes in node_group
for node in node_group.node_tree.nodes:
if node.type == 'OUTPUT_MATERIAL':
node_group.node_tree.nodes.remove(node)
elif item_type_got == 'GROUP':
# got Node Group (can be object node group or compositor node group)
if item_type == 'NODEGROUP' or subtype == 'CompositorNodeTree':
# Node Group as Node Group (for object material and compositor material)
if subtype == 'CompositorNodeTree' and not context.window.scene.use_nodes:
context.window.scene.use_nodes = True
if subtype == 'ShaderNodeTree':
if context.active_object and not context.active_object.active_material:
Material.new(context=context)
active_node_tree = cls.active_node_tree(context=context)
cls._deselect_all_nodes(node_tree=active_node_tree)
if item_in_json and active_node_tree:
nodegroup = NodeGroup.from_json(
node_group_json=item_in_json,
parent_node_tree=active_node_tree,
attachments_path=attachments_path
)
if nodegroup:
nodegroup['bis_uid'] = bis_item_id
nodegroup.location = (0.0, 0.0)
elif item_type == 'MATERIAL':
# Node Group as Material (only for object material)
material = Material.new(context=context)
if material:
material.name = item_name_got
Material.clear(
material=material,
exclude_output_nodes=True
)
active_node_tree = cls.active_node_tree(context=context)
# create node group
nodegroup = NodeGroup.from_json(
node_group_json=item_in_json,
parent_node_tree=active_node_tree,
attachments_path=attachments_path
)
if nodegroup:
nodegroup['bis_uid'] = bis_item_id
nodegroup.location = (0.0, 0.0)
# additional nodes and links
# node group output
shader_output = next(iter([i for i in nodegroup.outputs
if i.type == 'SHADER'
and 'volume' not in i.name.lower()]), None)
color_output = next(iter([i for i in nodegroup.outputs
if i.type == 'RGBA']), None)
factor_output = next(iter([i for i in nodegroup.outputs
if i.type == 'VALUE'
and 'displacement' not in i.name.lower()]), None)
volume_output = next(iter([i for i in nodegroup.outputs
if i.type == 'SHADER'
and 'volume' in i.name.lower()]), None)
vector_displacement_output = next(iter([i for i in nodegroup.outputs
if i.type in ['VECTOR']
and i.name.lower() in ['displace', 'displacement', 'смещение']]), None)
factor_displacement_output = next(iter([i for i in nodegroup.outputs
if i.type in ['VALUE'] and
i.name.lower() in ['displace', 'displacement', 'смещение']]), None)
normal_output = next(iter([i for i in nodegroup.outputs
if i.type == 'VECTOR'
and i.name.lower() not in ['displace', 'displacement', 'смещение']]), None)
# output node
output_node = next((node for node in active_node_tree.nodes if node.name in
['Material Output', 'Light Output', 'World Output']), None)
if output_node:
# connect outputs
if shader_output:
active_node_tree.links.new(
shader_output,
output_node.inputs['Surface']
)
elif color_output:
diffuse_node = active_node_tree.nodes.new(
type='ShaderNodeBsdfDiffuse'
)
diffuse_node.location = (500.0, 0.0)
active_node_tree.links.new(
color_output,
diffuse_node.inputs['Color']
)
active_node_tree.links.new(
diffuse_node.outputs['BSDF'],
output_node.inputs['Surface']
)
if normal_output:
active_node_tree.links.new(
normal_output,
diffuse_node.inputs['Normal']
)
elif factor_output:
diffuse_node = active_node_tree.nodes.new(
type='ShaderNodeBsdfDiffuse'
)
diffuse_node.location = (500.0, 0.0)
active_node_tree.links.new(
factor_output,
diffuse_node.inputs['Color']
)
active_node_tree.links.new(
diffuse_node.outputs['BSDF'],
output_node.inputs['Surface']
)
if normal_output:
active_node_tree.links.new(
normal_output,
diffuse_node.inputs['Normal']
)
if volume_output:
active_node_tree.links.new(volume_output, output_node.inputs['Volume'])
if vector_displacement_output:
displacement_input = next(
(i for i in output_node.inputs if i.name == 'Displacement'), None)
if displacement_input:
active_node_tree.links.new(
vector_displacement_output,
displacement_input
)
if factor_displacement_output:
displacement_input = next(
(i for i in output_node.inputs if i.name == 'Displacement'), None)
if displacement_input:
# convert factor displacement to vector displacement
# add nodes
combine_xyz_node = active_node_tree.nodes.new(
type='ShaderNodeCombineXYZ'
)
combine_xyz_node.location = (
output_node.location.x - 400.0,
output_node.location.y - 100.0
)
vector_displacement_node = active_node_tree.nodes.new(
type='ShaderNodeVectorDisplacement'
)
vector_displacement_node.location = (
combine_xyz_node.location.x + 200.0,
combine_xyz_node.location.y
)
vector_displacement_node.inputs['Scale'].default_value = 0.1
# add links
active_node_tree.links.new(
factor_displacement_output,
combine_xyz_node.inputs['Y']
)
active_node_tree.links.new(
combine_xyz_node.outputs['Vector'],
vector_displacement_node.inputs['Vector']
)
active_node_tree.links.new(
vector_displacement_node.outputs['Displacement'],
displacement_input
)
# connect inputs
vector_input = [i for i in nodegroup.inputs if i.type == 'VECTOR'
and i.name.lower() in ['vector']]
if vector_input:
texture_coordinates_node = active_node_tree.nodes.new(
type='ShaderNodeTexCoord'
)
texture_coordinates_node.location = (-200.0, 0.0)
active_node_tree.links.new(
texture_coordinates_node.outputs['UV'],
vector_input[0]
)
cls._deselect_all_nodes(node_tree=material.node_tree)
else:
request_rez['data']['text'] = 'BIS server not request'
else:
request_rez['data']['text'] = 'No selected objects to set material'
return request_rez
@classmethod
def to_bis(cls, context, item, item_type, tags=''):
# item = material or nodegroup
# item_type = 'MATERIAL' or 'NODEGROUP'
request_rez = {'stat': 'ERR', 'data': {'text': 'Error to save'}}
item_json = None
subtype = Material.get_subtype(context=context)
if item:
if item_type == 'NODEGROUP' or subtype == 'CompositorNodeTree':
item_json = NodeGroup.to_json(node_group=item)
elif item_type == 'MATERIAL':
item_json = Material.to_json(context=context, material=item)
if item_json:
# to file (debug)
if cfg.to_server_to_file:
with open(os.path.join(FileManager.project_dir(), 'send_to_server.json'), 'w') as currentFile:
json.dump(item_json, currentFile, indent=4)
if cls.is_procedural(material=item):
# send to server
if not cfg.no_sending_to_server:
# TODO check and test with internal text scripts
bis_links = list(cls.get_bis_linked_items('bis_linked_item', item_json))
# zip json to reduce size and convert to base64 to have a string
# future improvement - don't convert to base64 string, store to db raw binary after zlib
# (need to use BLOB field in mysql)
item_json_compressed = zlib.compress(json.dumps(item_json).encode('utf-8'))
item_json_compressed_b64 = base64.b64encode(item_json_compressed)
request = WebRequest.send_request(
context=context,
data={
'for': 'add_item',
'item_body': item_json_compressed_b64,
'storage': cls.storage_type(context=context),
'storage_subtype': subtype,
'storage_subtype2': Material.get_subtype2(context=context),
'procedural': 1 if cls.is_procedural(material=item) else 0,
'engine': context.window.scene.render.engine,
'bis_links': json.dumps(bis_links),
'item_name': item_json['instance']['name'],
'item_tags': tags.strip(),
'addon_version': Addon.current_version(),
'blender_version': BlenderEx.version_str_short()
}
)
if request:
request_rez = json.loads(request.text)
else:
# attach file with external items (textures,... etc)
file_attachment = NodeTree.external_items(node_tree=item.node_tree)
if file_attachment:
with tempfile.TemporaryDirectory() as temp_dir:
zip_file = FileManager.zip_files(
source_files_list=file_attachment,
temp_dir=temp_dir,
zip_name=item_json['instance']['name']
)
if zip_file and os.path.exists(zip_file):
# to file (debug)
if cfg.to_server_to_file:
copyfile(
zip_file,
os.path.join(FileManager.project_dir(),item_json['instance']['name']+'.zip')
)
if os.stat(zip_file).st_size < cls._material_limit_file_size:
# send to server
if not cfg.no_sending_to_server:
bis_links = list(cls.get_bis_linked_items('bis_linked_item', item_json))
# zip json to reduce size and convert to base64 to have a string
# future improvement - don't convert to base64 string,
# store to db raw binary after zlib
# (need to use BLOB field in mysql)
item_json_compressed = zlib.compress(json.dumps(item_json).encode('utf-8'))
item_json_compressed_b64 = base64.b64encode(item_json_compressed)
request = WebRequest.send_request(
context=context,
data={
'for': 'add_item',
'item_body': item_json_compressed_b64,
'storage': cls.storage_type(context=context),
'storage_subtype': subtype,
'storage_subtype2': Material.get_subtype2(context=context),
'procedural': 1 if cls.is_procedural(material=item) else 0,
'engine': context.window.scene.render.engine,
'bis_links': json.dumps(bis_links),
'item_name': item_json['instance']['name'],
'item_tags': tags.strip(),
'addon_version': Addon.current_version(),
'blender_version': BlenderEx.version_str_short()
},
files={
'attachment_file': open(zip_file, 'rb')
}
)
if request:
request_rez = json.loads(request.text)
else:
request_rez['data']['text'] = 'Saving material must be less ' + \
str(round(cls._material_limit_file_size/1024/1024)) + \
' Mb with textures after zip export!'
else:
# non procedural but without external items
if not cfg.no_sending_to_server:
bis_links = list(cls.get_bis_linked_items('bis_linked_item', item_json))
# zip json to reduce size and convert to base64 to have a string
# future improvement - don't convert to base64 string, store to db raw binary after zlib
# (need to use BLOB field in mysql)
item_json_compressed = zlib.compress(json.dumps(item_json).encode('utf-8'))
item_json_compressed_b64 = base64.b64encode(item_json_compressed)
request = WebRequest.send_request(
context=context,
data={
'for': 'add_item',
'item_body': item_json_compressed_b64,
'storage': cls.storage_type(context=context),
'storage_subtype': subtype,
'storage_subtype2': Material.get_subtype2(context=context),
'procedural': 1 if cls.is_procedural(material=item) else 0,
'engine': context.window.scene.render.engine,
'bis_links': json.dumps(bis_links),
'item_name': item_json['instance']['name'],
'item_tags': tags.strip(),
'addon_version': Addon.current_version(),
'blender_version': BlenderEx.version_str_short()
}
)
if request:
request_rez = json.loads(request.text)
if request_rez['stat'] == 'OK':
item['bis_uid'] = request_rez['data']['id']
return request_rez
@classmethod
def update_in_bis(cls, context, item, item_type):
# item = material or nodegroup
# item_type = 'MATERIAL' or 'NODEGROUP'
request_rez = {'stat': 'ERR', 'data': {'text': 'Error to update'}}
item_json = None
subtype = Material.get_subtype(context=context)
if item:
if 'bis_uid' in item and item['bis_uid']:
if item_type == 'NODEGROUP' or subtype == 'CompositorNodeTree':
item_json = NodeGroup.to_json(node_group=item)
elif item_type == 'MATERIAL':
item_json = Material.to_json(context=context, material=item)
else:
request_rez['data']['text'] = 'Save this Material item to the BIS first!'
else:
request_rez['data']['text'] = 'Undefined material item to update'
# send to server
if item_json:
# to file (debug)
if cfg.to_server_to_file:
with open(os.path.join(FileManager.project_dir(), 'send_to_server.json'), 'w') as currentFile:
json.dump(item_json, currentFile, indent=4)
if cls.is_procedural(material=item):
# send to server
if not cfg.no_sending_to_server:
bis_links = list(cls.get_bis_linked_items('bis_linked_item', item_json))
# zip json to reduce size and convert to base64 to have a string
# future improvement - don't convert to base64 string, store to db raw binary after zlib
# (need to use BLOB field in mysql)
item_json_compressed = zlib.compress(json.dumps(item_json).encode('utf-8'))
item_json_compressed_b64 = base64.b64encode(item_json_compressed)
request = WebRequest.send_request(
context=context,
data={
'for': 'update_item',
'item_body': item_json_compressed_b64,
'storage': cls.storage_type(context=context),
'storage_subtype': subtype,
'storage_subtype2': Material.get_subtype2(context=context),
'procedural': 1 if cls.is_procedural(material=item) else 0,
'engine': context.window.scene.render.engine,
'bis_links': json.dumps(bis_links),
'item_id': item['bis_uid'],
'item_name': item_json['instance']['name'],
'addon_version': Addon.current_version(),
'blender_version': BlenderEx.version_str_short()
}
)
if request:
request_rez = json.loads(request.text)
else:
# attach file with external items (textures,... etc)
file_attachment = NodeTree.external_items(node_tree=item.node_tree)
if file_attachment:
with tempfile.TemporaryDirectory() as temp_dir:
zip_file = FileManager.zip_files(
source_files_list=file_attachment,
temp_dir=temp_dir,
zip_name=item_json['instance']['name']
)
if zip_file and os.path.exists(zip_file):
if cfg.to_server_to_file:
copyfile(
zip_file,
os.path.join(FileManager.project_dir(), item_json['instance']['name']+'.zip')
)
if os.stat(zip_file).st_size < cls._material_limit_file_size:
# send to server
if not cfg.no_sending_to_server:
bis_links = list(cls.get_bis_linked_items('bis_linked_item', item_json))
# zip json to reduce size and convert to base64 to have a string
# future improvement - don't convert to base64 string,
# store to db raw binary after zlib
# (need to use BLOB field in mysql)
item_json_compressed = zlib.compress(json.dumps(item_json).encode('utf-8'))
item_json_compressed_b64 = base64.b64encode(item_json_compressed)
request = WebRequest.send_request(
context=context,
data={
'for': 'update_item',
'item_body': item_json_compressed_b64,
'storage': cls.storage_type(context=context),
'storage_subtype': subtype,
'storage_subtype2': Material.get_subtype2(context=context),
'procedural': 1 if cls.is_procedural(material=item) else 0,
'engine': context.window.scene.render.engine,
'bis_links': json.dumps(bis_links),
'item_id': item['bis_uid'],
'item_name': item_json['instance']['name'],
'addon_version': Addon.current_version(),
'blender_version': BlenderEx.version_str_short()
},
files={
'attachment_file': open(zip_file, 'rb')
}
)
if request:
request_rez = json.loads(request.text)
else:
request_rez['data']['text'] = 'Saving material must be less ' + \
str(round(cls._material_limit_file_size/1024/1024)) + \
' Mb with textures after zip export!'
else:
# non procedural but without external items
if not cfg.no_sending_to_server:
bis_links = list(cls.get_bis_linked_items('bis_linked_item', item_json))
# zip json to reduce size and convert to base64 to have a string
# future improvement - don't convert to base64 string, store to db raw binary after zlib
# (need to use BLOB field in mysql)
item_json_compressed = zlib.compress(json.dumps(item_json).encode('utf-8'))
item_json_compressed_b64 = base64.b64encode(item_json_compressed)
request = WebRequest.send_request(
context=context,
data={
'for': 'update_item',
'item_body': item_json_compressed_b64,
'storage': cls.storage_type(context=context),
'storage_subtype': subtype,
'storage_subtype2': Material.get_subtype2(context=context),
'procedural': 1 if cls.is_procedural(material=item) else 0,
'engine': context.window.scene.render.engine,
'bis_links': json.dumps(bis_links),
'item_id': item['bis_uid'],
'item_name': item_json['instance']['name'],
'addon_version': Addon.current_version(),
'blender_version': BlenderEx.version_str_short()
}
)
if request:
request_rez = json.loads(request.text)
return request_rez
@staticmethod
def storage_type(context):
# return context.area.spaces.active.type
return 'NODE_EDITOR'
@staticmethod
def active_object(context, use_selected=False):
# return current active object
if use_selected and context.selected_objects and not context.active_object:
# if no active object but exists some selected objects - make active from first selected
context.view_layer.objects.active = context.selected_objects[0]
return context.active_object
@staticmethod
def active_node_tree(context):
# returns currently opened node tree in NODE_EDITOR window
active_node_tree = None
subtype = Material.get_subtype(context=context)
if subtype == 'ShaderNodeTree':
subtype2 = Material.get_subtype2(context=context)
if subtype2 == 'OBJECT':
if context.active_object and context.active_object.active_material:
active_node_tree = context.active_object.active_material.node_tree
elif subtype2 == 'WORLD':
if context.scene.world:
active_node_tree = context.scene.world.node_tree
elif subtype == 'CompositorNodeTree':
if context.window.scene.use_nodes:
active_node_tree = context.area.spaces.active.node_tree
elif subtype == 'GeometryNodeTree':
if context.active_object.modifiers and \
context.active_object.modifiers.active and \
context.active_object.modifiers.active.type == 'NODES':
active_node_tree = context.active_object.modifiers.active.node_group
# if node tree in opened node group
if active_node_tree and NodeTree.has_node_groups(active_node_tree) and hasattr(context.space_data, 'path'):
for i in range(len(context.space_data.path) - 1):
active_node_tree = active_node_tree.nodes.active.node_tree
return active_node_tree
@classmethod
def active_node(cls, context):
# returns currently active node in NODE_EDITOR window
active_node = None
active_node_tree = cls.active_node_tree(context=context)
if active_node_tree:
active_node = active_node_tree.nodes.active
return active_node
@staticmethod
def active_material(context):
# returns currently active node tree container (material or modifier) for NODE_EDITOR window
container = None
if context.active_object:
subtype = Material.get_subtype(context=context)
if subtype == 'ShaderNodeTree':
# material
if context.active_object.active_material:
container = context.active_object.active_material
elif subtype == 'GeometryNodeTree':
# geometry nodes modifier
if context.active_object.modifiers and context.active_object.modifiers.active.type == 'NODES':
container = context.active_object.modifiers.active
return container
@classmethod
def is_procedural(cls, material):
# check if material (nodegroup) is fully procedural
return NodeTree.is_procedural(node_tree=material.node_tree)
@classmethod
def cpu_render_required(cls, material):
# check if material (nodegroup) required only CPU render
rez = False
for node in material.node_tree.nodes:
if node.type == 'GROUP':
rez = cls.cpu_render_required(node)
if rez:
break
elif node.type == 'SCRIPT':
rez = True
break
return rez
@classmethod
def get_bis_linked_items(cls, key, nodegroup_in_json):
# returns generator to crate list with all linked items (texts, ...) to current item (nodegroup)
for k, v in nodegroup_in_json.items():
if k == key:
yield v
elif isinstance(v, dict):
for result in cls.get_bis_linked_items(key, v):
yield result
elif isinstance(v, list):
for d in v:
if isinstance(d, dict):
for result in cls.get_bis_linked_items(key, d):
yield result
@staticmethod
def _deselect_all_nodes(node_tree):
# deselect all nodes in node_tree
for node in node_tree.nodes:
node.select = False