forked from nizu/VB25-tests
-
Notifications
You must be signed in to change notification settings - Fork 1
/
render_ops.py
1365 lines (979 loc) · 31.7 KB
/
render_ops.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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
V-Ray/Blender
http://vray.cgdo.ru
Author: Andrey M. Izrantsev (aka bdancer)
E-Mail: izrantsev@cgdo.ru
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
All Rights Reserved. V-Ray(R) is a registered trademark of Chaos Software.
'''
''' Python modules '''
import os
import subprocess
import tempfile
import time
import zipfile
import urllib.request
import sys
''' Blender modules '''
import bpy
import bgl
import bmesh
from bpy.props import *
''' vb modules '''
import vb25.render
import vb25.proxy
from vb25.lib import VRaySocket
from vb25.utils import *
from vb25.plugins import *
from vb25.lib import VRayProxy
from vb25.lib.VRaySceneParser import GetMaterialsNames
from vb25.lib.VrmatParser import GetXMLMaterialsNames
VRAYBLENDER_MENU_ITEM= "V-Ray"
'''
SCRIPT AUTOUPDATE
'''
class VRAY_OT_update(bpy.types.Operator):
bl_idname = "vray.update"
bl_label = "Update Exporter"
bl_description = "Update exporter from github"
def execute(self, context):
# Check if target dir is writable
cur_vb25_dirpath = get_vray_exporter_path()
if not os.access(cur_vb25_dirpath, os.W_OK):
self.report({'ERROR'}, "Exporter directory is not writable!")
return {'CANCELLED'}
# Downloading file
self.report({'INFO'}, "Downloading 'master' branch archive...")
GIT_MASTER_URL = "https://github.com/bdancer/vb25/zipball/master"
# devnote: urllib2 not available, urllib's fancyurlopener returns errors anyways (when connection is not available)
# so this is a working 'ugly fix' that at leasts works. Sorry the ghetto fix.
try:
(filename, headers) = urllib.request.urlretrieve(GIT_MASTER_URL)
except urllib.error.URLError:
self.report({'ERROR'}, "Error retrieving the files! Check your connection!")
return {'CANCELLED'}
# Extracting archive
update_dir = create_dir(os.path.join(tempfile.gettempdir(), "vb25_update"))
try:
ziparchive = zipfile.ZipFile(filename)
ziparchive.extractall(update_dir)
ziparchive.close()
except:
self.report({'ERROR'}, "Error unpacking the archive!")
return {'CANCELLED'}
new_vb25_dirpath = ""
dirnames = os.listdir(update_dir)
for dirname in dirnames:
if dirname.startswith("bdancer-vb25-"):
new_vb25_dirpath = os.path.join(update_dir, dirname)
break
if not new_vb25_dirpath:
self.report({'ERROR'}, "Update files not found!")
return {'CANCELLED'}
# Copying new files
debug(context.scene, "Copying new files...")
if os.path.exists(cur_vb25_dirpath):
if sys.platform == 'win32':
for item in os.listdir(cur_vb25_dirpath):
s = os.path.join(cur_vb25_dirpath, item)
if os.path.isdir(s):
os.system("rmdir /Q /S %s" % s)
else:
os.system("del /Q /F %s" % s)
else:
shutil.rmtree(cur_vb25_dirpath)
copytree(new_vb25_dirpath, cur_vb25_dirpath)
if os.path.exists(filename):
self.report({'INFO'}, "Removing update archive: %s"%(filename))
os.remove(filename)
if os.path.exists(update_dir):
self.report({'INFO'}, "Removing update unpack directory: %s"%(update_dir))
shutil.rmtree(update_dir)
self.report({'INFO'}, "V-Ray/Blender updated! Please, restart Blender!")
return {'FINISHED'}
'''
Camera operators
'''
class VRAY_OT_lens_shift(bpy.types.Operator):
bl_idname= 'vray.lens_shift'
bl_label= "Get lens shift"
bl_description= "Calculate lens shift"
@classmethod
def poll(cls, context):
return (context.camera)
def execute(self, context):
VRayCamera= context.camera.vray
CameraPhysical= VRayCamera.CameraPhysical
CameraPhysical.lens_shift= PLUGINS['CAMERA']['CameraPhysical'].get_lens_shift(context.object)
return {'FINISHED'}
'''
Effects operators
'''
class VRAY_OT_effect_add(bpy.types.Operator):
bl_idname= 'vray.effect_add'
bl_label= "Add Effect"
bl_description= "Add effect"
def execute(self, context):
VRayScene= context.scene.vray
VRayEffects= VRayScene.VRayEffects
VRayEffects.effects.add()
VRayEffects.effects[-1].name= "Effect"
return {'FINISHED'}
class VRAY_OT_effect_remove(bpy.types.Operator):
bl_idname= 'vray.effect_remove'
bl_label= "Remove Effect"
bl_description= "Remove effect"
def execute(self, context):
VRayScene= context.scene.vray
VRayEffects= VRayScene.VRayEffects
if VRayEffects.effects_selected >= 0:
VRayEffects.effects.remove(VRayEffects.effects_selected)
VRayEffects.effects_selected-= 1
return {'FINISHED'}
class VRAY_OT_effect_up(bpy.types.Operator):
bl_idname= 'vray.effect_up'
bl_label= "Move effect up"
bl_description= "Move effect up"
def execute(self, context):
VRayScene= context.scene.vray
VRayEffects= VRayScene.VRayEffects
if VRayEffects.effects_selected <= 0:
return {'CANCELLED'}
VRayEffects.effects.move(VRayEffects.effects_selected,
VRayEffects.effects_selected - 1)
VRayEffects.effects_selected-= 1
return {'FINISHED'}
class VRAY_OT_effect_down(bpy.types.Operator):
bl_idname= 'vray.effect_down'
bl_label= "Move effect down"
bl_description= "Move effect down"
def execute(self, context):
VRayScene= context.scene.vray
VRayEffects= VRayScene.VRayEffects
if VRayEffects.effects_selected == len(VRayEffects.effects) - 1:
return {'CANCELLED'}
VRayEffects.effects.move(VRayEffects.effects_selected,
VRayEffects.effects_selected + 1)
VRayEffects.effects_selected+= 1
return {'FINISHED'}
'''
Includer operators
'''
class VRAY_OT_includer_add(bpy.types.Operator):
bl_idname= 'vray.includer_add'
bl_label= "Add Include"
bl_description= "Add Include *.vrsene"
def execute(self, context):
vs= context.scene.vray
module= vs.Includer
module.nodes.add()
module.nodes[-1].name= "Include Scene"
return {'FINISHED'}
class VRAY_OT_includer_remove(bpy.types.Operator):
bl_idname= 'vray.includer_remove'
bl_label= "Remove Include"
bl_description= "Remove Include *.vrsene"
def execute(self, context):
vs= context.scene.vray
module= vs.Includer
if module.nodes_selected >= 0:
module.nodes.remove(module.nodes_selected)
module.nodes_selected-= 1
return {'FINISHED'}
class VRAY_OT_includer_up(bpy.types.Operator):
bl_idname= 'vray.includer_up'
bl_label= "Up Include"
bl_description= "Up Include *.vrsene"
def execute(self, context):
vs= context.scene.vray
module= vs.Includer
if module.nodes_selected <= 0:
return {'CANCELLED'}
module.nodes.move(module.nodes_selected,
module.nodes_selected - 1)
module.nodes_selected-= 1
return {'FINISHED'}
class VRAY_OT_includer_down(bpy.types.Operator):
bl_idname= 'vray.includer_down'
bl_label= "Down Include"
bl_description= "Down Include *.vrsene"
def execute(self, context):
vs= context.scene.vray
module= vs.Includer
if module.nodes_selected <= 0:
return {'CANCELLED'}
module.nodes.move(module.nodes_selected,
module.nodes_selected + 1)
module.nodes_selected+= 1
return {'FINISHED'}
'''
Material operators
'''
def active_node_mat(mat):
if mat:
mat_node= mat.active_node_material
if mat_node:
return mat_node
else:
return mat
return None
def find_brdf_pointer(rna_pointer):
if rna_pointer.brdf_selected >= 0 and rna_pointer.brdfs[rna_pointer.brdf_selected].type == 'BRDFLayered':
return find_brdf_pointer(getattr(rna_pointer.brdfs[rna_pointer.brdf_selected], 'BRDFLayered'))
return rna_pointer
class VRAY_OT_brdf_add(bpy.types.Operator):
bl_idname= 'vray.brdf_add'
bl_label= "Add BRDF"
bl_description= "Add BRDF"
def execute(self, context):
ma= active_node_mat(context.material)
if ma:
rna_pointer= ma.vray.BRDFLayered
# rna_pointer= find_brdf_pointer(VRayMaterial)
rna_pointer.brdfs.add()
rna_pointer.brdfs[-1].name= "BRDF"
return {'FINISHED'}
return {'CANCELLED'}
class VRAY_OT_brdf_remove(bpy.types.Operator):
bl_idname= 'vray.brdf_remove'
bl_label= "Remove BRDF"
bl_description= "Remove BRDF"
def execute(self, context):
ma= active_node_mat(context.material)
if ma:
rna_pointer= ma.vray.BRDFLayered
# rna_pointer= find_brdf_pointer(VRayMaterial)
if rna_pointer.brdf_selected >= 0:
rna_pointer.brdfs.remove(rna_pointer.brdf_selected)
rna_pointer.brdf_selected-= 1
return {'FINISHED'}
return {'CANCELLED'}
class VRAY_OT_brdf_up(bpy.types.Operator):
bl_idname= 'vray.brdf_up'
bl_label= "Move BRDF up"
bl_description= "Move BRDF up"
def execute(self, context):
ma= active_node_mat(context.material)
if ma:
rna_pointer= ma.vray.BRDFLayered
# rna_pointer= find_brdf_pointer(VRayMaterial)
if rna_pointer.brdf_selected <= 0:
return {'FINISHED'}
rna_pointer.brdfs.move(rna_pointer.brdf_selected,
rna_pointer.brdf_selected - 1)
rna_pointer.brdf_selected-= 1
return {'FINISHED'}
return {'CANCELLED'}
class VRAY_OT_brdf_down(bpy.types.Operator):
bl_idname= 'vray.brdf_down'
bl_label= "Move BRDF down"
bl_description= "Move BRDF down"
def execute(self, context):
ma= active_node_mat(context.material)
if ma:
rna_pointer= ma.vray.BRDFLayered
# rna_pointer= find_brdf_pointer(VRayMaterial)
if rna_pointer.brdf_selected == len(rna_pointer.brdfs) - 1:
return {'FINISHED'}
rna_pointer.brdfs.move(rna_pointer.brdf_selected,
rna_pointer.brdf_selected + 1)
rna_pointer.brdf_selected+= 1
return {'FINISHED'}
'''
Render channel operators
'''
class VRAY_OT_channel_add(bpy.types.Operator):
bl_idname= 'vray.render_channels_add'
bl_label= "Add Render Channel"
bl_description= "Add render channel"
def execute(self, context):
sce= context.scene
vsce= sce.vray
render_channels= vsce.render_channels
render_channels.add()
render_channels[-1].name= "RenderChannel"
return {'FINISHED'}
class VRAY_OT_channel_del(bpy.types.Operator):
bl_idname= 'vray.render_channels_remove'
bl_label= "Remove Render Channel"
bl_description= "Remove render channel"
def execute(self, context):
sce= context.scene
vsce= sce.vray
render_channels= vsce.render_channels
if vsce.render_channels_index >= 0:
render_channels.remove(vsce.render_channels_index)
vsce.render_channels_index-= 1
return {'FINISHED'}
'''
DR node operators
'''
class VRAY_OT_node_add(bpy.types.Operator):
bl_idname= 'vray.render_nodes_add'
bl_label= "Add Render Node"
bl_description= "Add render node"
def execute(self, context):
vs= context.scene.vray
module= vs.VRayDR
module.nodes.add()
module.nodes[-1].name= "Render Node"
return {'FINISHED'}
class VRAY_OT_node_del(bpy.types.Operator):
bl_idname= 'vray.render_nodes_remove'
bl_label= "Remove Render Node"
bl_description= "Remove render node"
def execute(self, context):
vs= context.scene.vray
module= vs.VRayDR
if module.nodes_selected >= 0:
module.nodes.remove(module.nodes_selected)
module.nodes_selected -= 1
if module.nodes_selected == -1 and len(module.nodes):
module.nodes_selected = 0
return {'FINISHED'}
class VRAY_OT_dr_nodes_load(bpy.types.Operator):
bl_idname = "vray.dr_nodes_load"
bl_label = "Load DR Nodes"
bl_description = "Load distributed rendering nodes list"
def execute(self, context):
VRayScene = context.scene.vray
VRayDR = VRayScene.VRayDR
nodesFilepath = os.path.join(GetUserConfigDir(), "vray_render_nodes.txt")
if not os.path.exists(nodesFilepath):
return {'CANCELLED'}
with open(nodesFilepath, 'r') as nodesFile:
VRayDR.nodes.clear()
for line in nodesFile.readlines():
l = line.strip()
if not l:
continue
item = VRayDR.nodes.add()
item.name, item.address = l.split(":")
VRayDR.nodes_selected = 0
return {'FINISHED'}
class VRAY_OT_dr_nodes_save(bpy.types.Operator):
bl_idname = "vray.dr_nodes_save"
bl_label = "Save DR Nodes"
bl_description = "Save distributed rendering nodes list"
def execute(self, context):
VRayScene = context.scene.vray
VRayDR = VRayScene.VRayDR
nodesFilepath = os.path.join(GetUserConfigDir(), "vray_render_nodes.txt")
with open(nodesFilepath, 'w') as nodesFile:
for item in VRayDR.nodes:
nodesFile.write("%s:%s\n" % (item.name, item.address))
return {'FINISHED'}
'''
Some usefull utils
'''
class VRAY_OT_convert_scene(bpy.types.Operator):
bl_idname = "vray.convert_materials"
bl_label = "Convert materials"
bl_description = "Convert scene materials from Blender Internal to V-Ray"
CONVERT_BLEND_TYPE= {
'MIX': 'OVER',
'SCREEN': 'OVER',
'DIVIDE': 'OVER',
'HUE': 'OVER',
'VALUE': 'OVER',
'COLOR': 'OVER',
'SOFT LIGHT': 'OVER',
'LINEAR LIGHT': 'OVER',
'OVERLAY': 'OVER',
'ADD': 'ADD',
'SUBTRACT': 'SUBTRACT',
'MULTIPLY': 'MULTIPLY',
'DIFFERENCE': 'DIFFERENCE',
'DARKEN': 'DARKEN',
'LIGHTEN': 'LIGHTEN',
'SATURATION': 'SATURATE',
}
def execute(self, context):
for ma in bpy.data.materials:
debug(context.scene, "Converting material: %s" % ma.name)
rm= ma.raytrace_mirror
rt= ma.raytrace_transparency
VRayMaterial= ma.vray
BRDFVRayMtl= VRayMaterial.BRDFVRayMtl
BRDFVRayMtl.diffuse = ma.diffuse_color
if ma.emit > 0.0:
VRayMaterial.type= 'BRDFLight'
if rm.use:
BRDFVRayMtl.reflect_color= tuple([rm.reflect_factor]*3)
BRDFVRayMtl.reflect_glossiness= rm.gloss_factor
BRDFVRayMtl.reflect_subdivs= rm.gloss_samples
BRDFVRayMtl.reflect_depth= rm.depth
BRDFVRayMtl.option_cutoff= rm.gloss_threshold
BRDFVRayMtl.anisotropy= 1.0 - rm.gloss_anisotropic
if rm.fresnel > 0.0:
BRDFVRayMtl.fresnel= True
BRDFVRayMtl.fresnel_ior= rm.fresnel
for slot in ma.texture_slots:
if slot and slot.texture and slot.texture.type in TEX_TYPES:
VRaySlot= slot.texture.vray_slot
VRayTexture= slot.texture.vray
VRaySlot.blend_mode= self.CONVERT_BLEND_TYPE[slot.blend_type]
if slot.use_map_emit:
VRayMaterial.type= 'BRDFLight'
VRaySlot.map_diffuse= True
if slot.use_map_normal:
VRaySlot.map_normal= True
VRaySlot.BRDFBump.bump_tex_mult= slot.normal_factor
if slot.use_map_color_diffuse:
VRaySlot.map_diffuse= True
VRaySlot.diffuse_mult= slot.diffuse_color_factor
if slot.use_map_raymir:
VRaySlot.map_reflect= True
VRaySlot.reflect_mult= slot.raymir_factor
if slot.use_map_alpha:
VRaySlot.map_opacity= True
VRaySlot.opacity_mult= slot.alpha_factor
return {'FINISHED'}
class VRAY_OT_bake_procedural(bpy.types.Operator):
bl_idname= 'vray.bake_procedural'
bl_label= "Bake procedural"
bl_description= "Render procedural texture to file"
def execute(self, context):
debug(context.scene, "Bake procedural: In progress...")
return {'FINISHED'}
class VRAY_OT_settings_to_text(bpy.types.Operator):
bl_idname= 'vray.settings_to_text'
bl_label= "Settings to Text"
bl_description= "Export settings to Text"
bb_code= BoolProperty(
name= "Use BB-code",
description= "Use BB-code formatting",
default= True
)
def execute(self, context):
text= bpy.data.texts.new(name="Settings")
bus= {}
bus['scene']= context.scene
bus['preview']= False
bus['files']= {}
bus['files']['scene']= text
bus['filenames']= {}
bus['plugins']= PLUGINS
bus['effects']= {}
bus['effects']['fog']= {}
bus['effects']['toon']= {}
bus['effects']['toon']['effects']= []
bus['effects']['toon']['objects']= []
text.write("V-Ray/Blender 2.0 | Scene: %s | %s\n" % (context.scene.name,
time.strftime("%d %b %Y %H:%m:%S")))
for key in PLUGINS['SETTINGS']:
if key in ('BakeView', 'RenderView', 'SettingsEnvironment'):
# Skip some plugins
continue
plugin= PLUGINS['SETTINGS'][key]
if hasattr(plugin, 'write'):
plugin.write(bus)
return {'FINISHED'}
class VRAY_OT_flip_resolution(bpy.types.Operator):
bl_idname = "vray.flip_resolution"
bl_label = "Flip resolution"
bl_description = "Flip render resolution"
def execute(self, context):
scene= context.scene
rd= scene.render
VRayScene= scene.vray
if VRayScene.image_aspect_lock:
VRayScene.image_aspect= 1.0 / VRayScene.image_aspect
rd.resolution_x, rd.resolution_y = rd.resolution_y, rd.resolution_x
rd.pixel_aspect_x, rd.pixel_aspect_y = rd.pixel_aspect_y, rd.pixel_aspect_x
return {'FINISHED'}
def LoadProxyMeshToObject(ob, filepath, anim_type, anim_offset, anim_speed, anim_frame):
meshFile = VRayProxy.MeshFile(filepath)
err = meshFile.readFile()
if err is not None:
return "Error parsing VRayProxy file!"
meshData = meshFile.getPreviewMesh(anim_type, anim_offset, anim_speed, anim_frame)
if meshData is None:
return "Can't find preview voxel!"
meshName = bpy.path.clean_name(os.path.basename(filepath))
# Add new mesh
mesh = bpy.data.meshes.new(meshName)
mesh.from_pydata(meshData['vertices'], [], meshData['faces'])
mesh.update()
# Replace object's mesh
bm = bmesh.new()
bm.from_mesh(mesh)
bm.to_mesh(ob.data)
ob.data.update()
# Remove temp
bm.free()
bpy.data.meshes.remove(mesh)
class VRAY_OT_proxy_load_preview(bpy.types.Operator):
bl_idname = "vray.proxy_load_preview"
bl_label = "Load Preview"
bl_description = "Load VRayProxy mesh preview from file"
def execute(self, context):
GeomMeshFile = context.object.data.vray.GeomMeshFile
proxyFilepath = os.path.normpath(path_sep_to_unix(bpy.path.abspath(GeomMeshFile.file)))
err = LoadProxyMeshToObject(
context.object,
proxyFilepath,
GeomMeshFile.anim_type,
GeomMeshFile.anim_offset,
GeomMeshFile.anim_speed,
context.scene.frame_current-1
)
if err is not None:
self.report({'ERROR'}, err)
return {'CANCELLED'}
return {'FINISHED'}
class VRAY_OT_create_proxy(bpy.types.Operator):
bl_idname = "vray.create_proxy"
bl_label = "Create proxy"
bl_description = "Creates proxy from selection"
def execute(self, context):
sce = context.scene
VRayScene = sce.vray
VRayExporter = VRayScene.exporter
@TimeIt("Proxy generated in")
def _create_proxy(ob):
if ob.type in {'LAMP', 'CAMERA', 'ARMATURE', 'LATTICE', 'EMPTY'}:
return
GeomMeshFile= ob.data.vray.GeomMeshFile
vrmesh_filename= GeomMeshFile.filename if GeomMeshFile.filename else clean_string(ob.name)
vrmesh_filename+= ".vrmesh"
vrmesh_dirpath= bpy.path.abspath(GeomMeshFile.dirpath)
if not os.path.exists(vrmesh_dirpath):
os.mkdir(vrmesh_dirpath)
vrmesh_filepath= os.path.join(vrmesh_dirpath,vrmesh_filename)
if GeomMeshFile.animation:
selected_frame= sce.frame_current
frame_start= sce.frame_start
frame_end= sce.frame_end
if GeomMeshFile.animation_range == 'MANUAL':
frame_start= GeomMeshFile.frame_start
frame_end= GeomMeshFile.frame_end
# Export first frame to create file
frame= frame_start
sce.frame_set(frame)
vb25.proxy.generate_proxy(sce,ob,vrmesh_filepath)
frame+= 1
# Export all other frames
while(frame <= frame_end):
sce.frame_set(frame)
vb25.proxy.generate_proxy(sce,ob,vrmesh_filepath,append=True)
frame+= 1
sce.frame_set(selected_frame)
else:
if VRayExporter.experimental:
bpy.ops.vray.generate_vrayproxy(
filepath = vrmesh_filepath,
)
else:
vb25.proxy.generate_proxy(sce,ob,vrmesh_filepath)
ob_name= ob.name
ob_data_name= ob.data.name
VRayMesh= ob.data.vray
if GeomMeshFile.mode == 'NONE':
return
elif GeomMeshFile.mode in {'THIS', 'REPLACE'}:
if GeomMeshFile.add_suffix:
ob.name += '_proxy'
# Override settings
VRayMesh.override = True
VRayMesh.override_type = 'VRAYPROXY'
GeomMeshFile.file = RelPath(vrmesh_filepath)
# Load preview mesh
if GeomMeshFile.mode == 'REPLACE':
LoadProxyMeshToObject(
ob,
vrmesh_filepath,
GeomMeshFile.anim_type,
GeomMeshFile.anim_offset,
GeomMeshFile.anim_speed,
context.scene.frame_current-1
)
if GeomMeshFile.apply_transforms:
ob.select= True
sce.objects.active= ob
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
elif GeomMeshFile.mode == 'NEW':
# Create new object and copy materials
new_ob= bpy.data.objects.new(ob_name+'_proxy', bpy.data.meshes.new(ob_name+'_proxy'))
sce.objects.link(new_ob)
new_ob.matrix_world= ob.matrix_world
bpy.ops.object.select_all(action='DESELECT')
new_ob.select= True
sce.objects.active= new_ob
LoadProxyMeshToObject(
new_ob,
vrmesh_filepath,
GeomMeshFile.anim_type,
GeomMeshFile.anim_offset,
GeomMeshFile.anim_speed,
context.scene.frame_current-1
)
for slot in ob.material_slots:
if slot and slot.material:
new_ob.data.materials.append(slot.material)
if GeomMeshFile.apply_transforms:
ob.select= True
sce.objects.active= ob
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
VRayMesh= new_ob.data.vray
VRayMesh.override= True
VRayMesh.override_type= 'VRAYPROXY'
GeomMeshFile= VRayMesh.GeomMeshFile
GeomMeshFile.file= RelPath(vrmesh_filepath)
if len(bpy.context.selected_objects):
for ob in bpy.context.selected_objects:
_create_proxy(ob)
else:
_create_proxy(context.object)
return {'FINISHED'}
'''
EXPORT OPERATORS
'''
def init(context):
scene= context.scene
# Settings bus
bus= {}
# Plugins
bus['plugins']= PLUGINS
# Scene
bus['scene']= scene
# Preview
bus['preview']= False
# V-Ray uses UV indexes, Blender uses UV names
# Here we store UV name->index map
bus['uvs']= get_uv_layers_map(scene)
# Output files
bus['files']= {}
bus['filenames']= {}
init_files(bus)
return bus
class VRAY_OT_write_scene(bpy.types.Operator):
bl_idname = "vray.write_scene"
bl_label = "Export scene"
bl_description = "Export scene to \"vrscene\" file"
def execute(self, context):
vb25.render.write_scene(init(context))
return {'FINISHED'}
class VRAY_OT_write_geometry(bpy.types.Operator):
bl_idname = "vray.write_geometry"
bl_label = "Export meshes"
bl_description = "Export meshes into vrscene file"
dialog_width = 180
def draw(self, context):
layout = self.layout
split = layout.split()
col = split.column()
col.label(text = "Animation mode is active!")
col.label(text = "Are you sure to export meshes?")
def invoke(self, context, event):
wm = context.window_manager
scene = context.scene
VRayScene = scene.vray
VRayExporter = VRayScene.exporter
if not bpy.app.background:
if VRayExporter.animation and VRayExporter.animation_type == 'FULL':
return wm.invoke_props_dialog(self, self.dialog_width)
return self.execute(context)
def execute(self, context):
vb25.render.write_geometry(init(context))
return {'FINISHED'}
class VRAY_OT_write_vrscene(bpy.types.Operator):
bl_idname = "vray.write_vrscene"
bl_label = "Export Scene"
bl_description = "Export scene into a *.vrscene files"
def execute(self, context):
bpy.ops.vray.export_vrscene()
return {'FINISHED'}
class VRAY_OT_render(bpy.types.Operator):
bl_idname = "vray.render"
bl_label = "V-Ray Renderer"
bl_description = "Render operator"
def execute(self, context):
scene = context.scene
VRayScene = scene.vray
VRayExporter = VRayScene.exporter
vb25.render.render(None, scene)