forked from prman-pixar/RenderManForBlender
-
Notifications
You must be signed in to change notification settings - Fork 2
/
export_sg.py
5317 lines (4324 loc) · 217 KB
/
export_sg.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
# ##### BEGIN MIT LICENSE BLOCK #####
#
# Copyright (c) 2015 - 2017 Pixar
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
# ##### END MIT LICENSE BLOCK #####
import bpy
import math
import mathutils
import os
import sys
import time
import traceback
import platform
from mathutils import Matrix, Vector, Quaternion, Euler
from . import bl_info
from .util import rib, rib_path, rib_ob_bounds
from .util import make_frame_path
from .util import init_env
from .util import get_sequence_path
from .util import user_path
from .util import path_list_convert, get_real_path
from .util import get_properties, check_if_archive_dirty
from .util import locate_openVDB_cache
from .util import debug, get_addon_prefs
from .util import format_seconds_to_hhmmss
from .nodes_sg import is_renderman_nodetree, get_textures, get_textures_for_node, get_tex_file_name
from .nodes_sg import get_mat_name
from .nodes_sg import replace_frame_num
from . import nodes_sg
from . import engine
from. import chatserver
import socketserver
import threading
addon_version = bl_info['version']
port = -1
is_running = False
__RMAN_SG_INITED__ = False
__RMAN_SG_EXPORTER__ = None
try:
import rman
__RMAN_SG_INITED__ = True
except Exception as e:
print('Could not import rman modules: %s' % str(e))
# ------------- Atom's helper functions -------------
GLOBAL_ZERO_PADDING = 5
# Objects that can be exported as a polymesh via Blender to_mesh() method.
# ['MESH','CURVE','FONT']
SUPPORTED_INSTANCE_TYPES = ['MESH', 'CURVE', 'FONT', 'SURFACE']
SUPPORTED_DUPLI_TYPES = ['FACES', 'VERTS', 'COLLECTION'] # Supported dupli types.
# These object types can have materials.
MATERIAL_TYPES = ['MESH', 'CURVE', 'FONT']
# Objects without to_mesh() conversion capabilities.
EXCLUDED_OBJECT_TYPES = ['LIGHT', 'CAMERA', 'ARMATURE']
# Only these light types affect volumes.
VOLUMETRIC_LIGHT_TYPES = ['SPOT', 'AREA', 'POINT']
MATERIAL_PREFIX = "mat_"
TEXTURE_PREFIX = "tex_"
MESH_PREFIX = "me_"
CURVE_PREFIX = "cu_"
GROUP_PREFIX = "group_"
MESHLIGHT_PREFIX = "meshlight_"
PSYS_PREFIX = "psys_"
DUPLI_PREFIX = "dupli_"
DUPLI_SOURCE_PREFIX = "dup_src_"
s_orientTransform = [0, 0, -1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]
s_orientPxrLight = [-1.0, 0.0, -0.0, 0.0,
-0.0, -1.0, -0.0, 0.0,
0.0, 0.0, -1.0, 0.0,
0.0, 0.0, 0.0, 1.0]
s_orientPxrDomeLight = [0.0, 0.0, -1.0, 0.0,
-1.0, -0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0]
s_orientPxrEnvDayLight = [-0.0, 0.0, -1.0, 0.0,
1.0, 0.0, -0.0, -0.0,
-0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.0]
s_orientPxrEnvDayLightInv = [-0.0, 1.0, -0.0, 0.0,
-0.0, 0.0, 1.0, -0.0,
-1.0, -0.0, 0.0, -0.0,
0.0, -0.0, -0.0, 1.0]
def convert_matrix(m):
v = [m[0][0], m[1][0], m[2][0], m[3][0],
m[0][1], m[1][1], m[2][1], m[3][1],
m[0][2], m[1][2], m[2][2], m[3][2],
m[0][3], m[1][3], m[2][3], m[3][3]]
return v
def convert_ob_bounds(ob_bb):
return (ob_bb[0][0], ob_bb[7][0], ob_bb[0][1],
ob_bb[7][1], ob_bb[0][2], ob_bb[1][2])
def transform_points(transform_mtx, P):
transform_pts = []
mtx = convert_matrix( transform_mtx )
m = rman.Types.RtMatrix4x4( mtx[0],mtx[1],mtx[2],mtx[3],
mtx[4],mtx[5],mtx[6],mtx[7],
mtx[8],mtx[9],mtx[10],mtx[11],
mtx[12],mtx[13],mtx[14],mtx[15])
for i in range(0, len(P), 3):
pt = m.pTransform( rman.Types.RtFloat3(P[i], P[i+1], P[i+2]) )
transform_pts.append(pt.x)
transform_pts.append(pt.y)
transform_pts.append(pt.z)
return transform_pts
def __is_prman_running__():
global is_running
return is_running
class ItHandler(chatserver.ItBaseHandler):
def dspyRender(self):
if not __is_prman_running__():
bpy.ops.render.render()
def dspyIPR(self):
if __is_prman_running__():
crop = []
for c in self.msg.getOpt('crop').split(' '):
crop.append(float(c))
if len(crop) == 4:
rman_sg_exporter().issue_cropwindow_edits(crop)
def stopRender(self):
if engine.ipr:
engine.shutdown_ipr()
def selectObjectById(self):
obj_id = int(self.msg.getOpt('id', '0'))
if obj_id < 0 or not (obj_id in rman_sg_exporter().obj_hash):
return
name = rman_sg_exporter().obj_hash[obj_id]
obj = bpy.context.scene.objects[name]
bpy.context.view_layer.objects.active = obj
def selectSurfaceById(self):
self.selectObjectById()
window = bpy.context.window_manager.windows[0]
if window.screen:
for a in window.screen.areas:
if a.type == "PROPERTIES":
for s in a.spaces:
if s.type == "PROPERTIES":
try:
s.context = "MATERIAL"
except:
pass
return
class RmanSgExporter:
def __init__(self, **kwargs):
self.rictl = rman.RiCtl.Get()
self.sgmngr = rman.SGManager.Get()
self.sg_scene = None
self.sg_root = None
self.sg_global_obj = None
# blender scene and rpass
self.scene = None
self.rpass = None
self.rm = None
# exporters
self.shader_exporter = None
self.sg_nodes_dict = dict() # handles to sg_nodes
self.mat_networks = dict() # dict material to networks
self.light_filters_dict = dict() # dict light to light filters
self.lightfilters_dict = dict() # dict light filters to light
self.displayfilters_list = list()
self.samplefilters_list = list()
self.obj_hash = dict()
self.obj_id = 1
self.port = -1
self.main_camera = None
self.ipr_mode = False
self.use_python_dspy = False
self.cam_matrix = None
def export_ready(self):
self.sg_nodes_dict = dict()
self.mat_networks = dict()
self.light_filters_dict = dict()
self.lightfilters_dict = dict()
self.displayfilters_list = list()
self.samplefilters_list = list()
self.obj_hash = dict()
self.obj_id = 1
self.ipr_mode = False
self.use_python_dspy = False
if self.sg_scene:
self.sgmngr.DeleteScene(self.sg_scene.sceneId)
self.sg_scene = self.sgmngr.CreateScene()
self.sg_root = self.sg_scene.Root()
self.sg_global_obj = None
self.shader_exporter = nodes_sg.RmanSgShadingExporter(
scene=self.scene,
sg_scene=self.sg_scene,
rman=rman)
def start_cmd_server(self):
global port
if port != -1:
return port
# zero port makes the OS pick one
host, port = "localhost", 0
# install handler
chatserver.protocols['it'] = ItHandler
# Create the server, binding to localhost on some port
server = socketserver.TCPServer((host, port),
chatserver.CommandHandler)
ip, port = server.server_address
thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
thread.daemon = True
thread.start()
return port
def start_render(self, visible_objects, rpass, scene, progress_cb=None, check_point=-1, for_preview=False):
global is_running
if is_running:
debug('error', "ERROR render already running. Abort")
return False
self.rpass = rpass
self.scene = scene
self.rm = self.scene.renderman
self.ipr_mode = False
self.export_ready()
argv = []
argv.append("prman")
argv.append("-progress")
if not for_preview:
if self.rpass.display_driver == "it":
argv.append("-dspyserver")
argv.append("it")
argv.append("-t:%d" % self.rm.threads)
if check_point > 0:
argv.append("-checkpoint")
argv.append("%ds" % check_point)
print("Parsing scene...")
time_start = time.time()
if for_preview:
if self.write_preview_scene() is False:
# nothing to do?
return
else:
self.write_scene(visible_objects)
if progress_cb:
ec = rman.EventCallbacks.Get()
ec.RegisterCallback("Progress", progress_cb, self)
is_running = True
self.rictl.PRManBegin(argv)
if 'RFB_DUMP_RIB' in os.environ:
print("\tWriting to RIB...")
rib_time_start = time.time()
self.sg_scene.Render("rib /var/tmp/blender.rib")
print("\tFinished writing RIB. Time: %s" % format_seconds_to_hhmmss(time.time() - rib_time_start))
print("Finished parsing scene. Total time: %s" % format_seconds_to_hhmmss(time.time() - time_start))
self.sg_scene.Render("prman -blocking")
self.sgmngr.DeleteScene(self.sg_scene.sceneId)
self.rictl.PRManEnd()
print("PRManEnd called.")
is_running = False
ec.UnregisterCallback("Progress", progress_cb, self)
return True
def write_frame_rib(self, visible_objects, rpass, scene, ribfile):
global is_running
self.rpass = rpass
self.scene = scene
self.rm = self.scene.renderman
self.ipr_mode = False
self.export_ready()
self.write_scene(visible_objects)
rib_options = ""
if self.rm.rib_compression == "gzip":
rib_options += " -compression gzip"
rib_options += " -format %s" % self.rm.rib_format
if self.rm.rib_format == "ascii":
rib_options += " -indent"
is_running = True
print("Writing to RIB...")
rib_time_start = time.time()
self.sg_scene.Render("rib %s %s" % (ribfile, rib_options))
print("Finished parsing scene. Time: %s" % format_seconds_to_hhmmss(time.time() - rib_time_start))
self.sgmngr.DeleteScene(self.sg_scene.sceneId)
is_running = False
print("Wrote RIB to: %s" % ribfile)
def write_archive_rib(self, obj, rpass, scene, ribfile):
global is_running
self.rpass = rpass
self.scene = scene
self.rm = self.scene.renderman
self.ipr_mode = False
self.export_ready()
self.write_object_archive(obj)
rib_options = ""
if self.rm.rib_compression == "gzip":
rib_options += " -compression gzip"
rib_options += " -format %s" % self.rm.rib_format
if self.rm.rib_format == "ascii":
rib_options += " -indent"
is_running = True
self.sg_scene.Render("rib %s -archive" % (ribfile))
self.sgmngr.DeleteScene(self.sg_scene.sceneId)
is_running = False
print("Wrote RIB Archive to: %s" % ribfile)
def start_viewport(self, visible_objects, rpass, scene, progress_cb=None):
global is_running
self.rpass = rpass
self.scene = scene
self.rm = self.scene.renderman
self.export_ready()
self.ipr_mode = True
self.use_python_dspy = True
argv = []
argv.append("prman")
argv.append("-t:%d" % self.rm.threads)
print("Parsing scene...")
time_start = time.time()
self.write_scene(visible_objects)
#if self.write_preview_scene() is False:
# pass
#ec = rman.EventCallbacks.Get()
#ec.RegisterCallback("Render", progress_cb, self)
is_running = True
self.rictl.PRManBegin(argv)
rman.Dspy.Get()
def callback(e, d, ed):
ed["done"] = (d == 0)
eventData = { "done": False, "name": 'foobar', "frame": 0 }
ec = rman.EventCallbacks.Get()
ec.RegisterCallback("Render", callback, eventData)
if 'RFB_DUMP_RIB' in os.environ:
print("\tWriting to RIB...")
rib_time_start = time.time()
self.sg_scene.Render("rib /var/tmp/blender.rib")
print("\tFinished writing RIB. Time: %s" % format_seconds_to_hhmmss(time.time() - rib_time_start))
print("Finished parsing scene. Total time: %s" % format_seconds_to_hhmmss(time.time() - time_start))
print("START RENDER!")
self.sg_scene.Render("prman -live")
#self.EditTestFrame(eventData)
#self.sgmngr.DeleteScene(self.sg_scene.sceneId)
#self.rictl.PRManEnd()
#print("PRManEnd called.")
#is_running = False
#ec.UnregisterCallback("Progress", progress_cb, self)
def get_python_framebuffer(self):
if self.use_python_dspy:
return rman.Dspy.GetFloatFramebuffer()
def start_ipr(self, visible_objects, rpass, scene, progress_cb=None):
global is_running
self.rpass = rpass
self.scene = scene
self.rm = self.scene.renderman
self.export_ready()
self.ipr_mode = True
argv = []
argv.append("prman")
argv.append("-progress")
argv.append("-dspyserver")
argv.append("it")
argv.append("-t:%d" % self.rm.threads)
self.rictl.PRManBegin(argv)
print("Parsing scene...")
time_start = time.time()
self.write_scene(visible_objects)
is_running = True
if 'RFB_DUMP_RIB' in os.environ:
print("\tWriting to RIB...")
rib_time_start = time.time()
self.sg_scene.Render("rib /var/tmp/blender.rib")
print("\tFinished writing RIB. Time: %s" % format_seconds_to_hhmmss(time.time() - rib_time_start))
print("Finished parsing scene. Total time: %s" % format_seconds_to_hhmmss(time.time() - time_start))
self.sg_scene.Render("prman -live")
def stop_ipr(self):
global is_running
if is_running:
is_running = False
self.sgmngr.DeleteScene(self.sg_scene.sceneId)
self.rictl.PRManEnd()
print("PRManEnd called.")
def issue_visibility_edit(self, active, scene):
self.scene = scene
db_name = data_name(active, self.scene)
inst_sg = self.sg_nodes_dict.get(db_name)
if inst_sg:
hidden = 0
if (inst_sg.GetHidden() == -1 and active.hide_render):
hidden = 1
elif (inst_sg.GetHidden() == 1 and not(active.hide_render)):
hidden = -1
if hidden == 0:
return
with rman.SGManager.ScopedEdit(self.sg_scene):
inst_sg.SetHidden(hidden)
def mute_lights(self, lights):
with rman.SGManager.ScopedEdit(self.sg_scene):
for light in lights:
sg_node = self.sg_nodes_dict[light.name]
sg_node.SetHidden(1)
def reset_light_illum(self, lights, do_solo=True):
with rman.SGManager.ScopedEdit(self.sg_scene):
for light in lights:
rm = light.data.renderman
do_light = rm.illuminates_by_default and not rm.mute
if do_solo and self.rpass.scene.renderman.solo_light:
# check if solo
do_light = do_light and rm.solo
sg_node = self.sg_nodes_dict[light.name]
sg_node.SetHidden(not(do_light))
def solo_light(self):
solo_light = None
with rman.SGManager.ScopedEdit(self.sg_scene):
for light in self.rpass.scene.objects:
if light.type == "LIGHT":
rm = light.data.renderman
sg_node = self.sg_nodes_dict[light.name]
if rm.solo and not solo_light:
do_light = rm.illuminates_by_default and not rm.mute
solo_light = light
sg_node.SetHidden(not(do_light))
else:
sg_node.SetHidden(1)
return solo_light
def issue_cropwindow_edits(self, crop_window=[]):
if crop_window:
with rman.SGManager.ScopedEdit(self.sg_scene):
options = self.sg_scene.EditOptionBegin()
options.SetFloatArray(rman.Tokens.Rix.k_Ri_CropWindow, crop_window, 4)
self.sg_scene.EditOptionEnd(options)
def issue_camera_transform_edit(self, context, depsgraph):
camera_node_sg = self.sg_nodes_dict['camera']
cam = context.scene.camera
v = convert_matrix(cam.matrix_world)
if v == self.cam_matrix:
return
transforms = []
with rman.SGManager.ScopedEdit(self.sg_scene):
self.cam_matrix = v
transforms.append(v)
camera_node_sg.SetTransform( transforms[0] )
def issue_transform_edits(self, active, scene):
self.scene = scene
#if (active and active.is_updated):
if (active):
if active.type == 'CAMERA':
with rman.SGManager.ScopedEdit(self.sg_scene):
camera_node_sg = self.sg_nodes_dict['camera']
if camera_node_sg:
self.export_camera_transform(camera_node_sg, scene.camera, [])
else:
print("CANNOT FIND CAMERA!")
elif active.type == 'EMPTY':
with rman.SGManager.ScopedEdit(self.sg_scene):
self.export_duplis_instances(master=None, prnt=active)
elif active.type == 'LIGHT':
if active.name not in self.sg_nodes_dict:
return
if active.data.renderman.renderman_type == 'FILTER':
sg_lightfilter = self.sg_nodes_dict[active.name]
lights = self.lightfilters_dict[active.name]
coordsys_name = "%s_coordsys" % active.name
coordsys = self.sg_nodes_dict[coordsys_name]
with rman.SGManager.ScopedEdit(self.sg_scene):
m = convert_matrix( active.matrix_world )
coordsys.SetTransform(m)
rix_params = sg_lightfilter.EditParameterBegin()
rix_params.SetString("coordsys", coordsys_name)
sg_lightfilter.EditParameterEnd(rix_params)
self.sg_nodes_dict[coordsys_name] = coordsys
self.sg_nodes_dict[active.name] = sg_lightfilter
for l in lights:
sg_light = self.sg_nodes_dict[l]
lightfilter_sg = self.light_filters_dict[l]
sg_light.SetLightFilter(lightfilter_sg)
else:
sg_light = self.sg_nodes_dict[active.name]
with rman.SGManager.ScopedEdit(self.sg_scene):
sg_light.SetTransform( convert_matrix(active.matrix_world) )
else:
dupli_type = active.instance_type #active.dupli_type
if dupli_type != "NONE":
data_blocks, instances = cache_motion(self.scene, self.rpass, objects=[active], calc_mb=False)
for name, db in data_blocks.items():
if db.type != "DUPLI":
continue
sg_node = self.sg_nodes_dict.get(name)
if not sg_node:
with rman.SGManager.ScopedEdit(self.sg_scene):
self.export_objects(data_blocks, instances)
ob = db.data
ob.dupli_list_create(self.scene, "RENDER")
with rman.SGManager.ScopedEdit(self.sg_scene):
if ob.dupli_type == 'GROUP' and ob.dupli_group:
for dupob in ob.dupli_list:
dupli_name = "%s.DUPLI.%s.%d" % (ob.name, dupob.object.name,
dupob.index)
source_data_name = get_instance(dupob.object, self.scene, False).name
source_sg = self.sg_nodes_dict.get(source_data_name)
if dupli_name in self.sg_nodes_dict:
sg_dupli = self.sg_nodes_dict[dupli_name]
sg_dupli.SetTransform( convert_matrix(dupob.matrix))
else:
for num, dupob in enumerate(ob.dupli_list):
dupli_name = "%s.DUPLI.%s.%d" % (ob.name, dupob.object.name,
dupob.index)
source_data_name = get_instance(dupob.object, self.scene, False).name
source_sg = self.sg_nodes_dict.get(source_data_name)
if source_sg and source_sg.GetHidden() != 1:
source_sg.SetHidden(1)
if dupli_name in self.sg_nodes_dict:
sg_dupli = self.sg_nodes_dict[dupli_name]
sg_dupli.SetTransform( convert_matrix(dupob.matrix))
ob.dupli_list_clear()
else:
instance = get_instance(active, self.scene, False)
if instance:
inst_mesh_sg = None
db_name = data_name(active, self.scene)
instance_name = '%s.%s' % (instance.name, db_name)
if instance_name in self.sg_nodes_dict.keys():
inst_mesh_sg = self.sg_nodes_dict[instance_name]
else:
db_name = data_name(active, self.scene)
inst_mesh_sg = self.sg_nodes_dict.get(db_name)
if inst_mesh_sg:
with rman.SGManager.ScopedEdit(self.sg_scene):
self.export_transform(instance, inst_mesh_sg)
## FIXME IS THIS NEEDED?
#for psys in active.particle_systems:
# db_name = psys_name(active, psys)
# self.export_particle_system(active, psys, db_name, objectCorrectionMatrix=True, data=None)
# check if this object is part of a group/dupli
# update accordingly.
#self.export_duplis_instances(master=active)
"""
if active and scene.camera.name != active.name and scene.camera.is_updated:
with rman.SGManager.ScopedEdit(self.sg_scene):
camera_node_sg = self.sg_nodes_dict['camera']
if camera_node_sg:
self.export_camera_transform(camera_node_sg, scene.camera, [])
else:
print("CANNOT FIND CAMERA!")
"""
def issue_new_object_edits(self, active, scene):
self.scene = scene
if active.type == 'LIGHT':
# this is a new light
ob = active
light = ob.data
rm = light.renderman
handle = light.name
with rman.SGManager.ScopedEdit(self.sg_scene):
self.export_light(ob, light, handle, rm)
else:
ob = active
data_blocks, instances = cache_motion(self.scene, self.rpass, objects=[ob], calc_mb=False)
with rman.SGManager.ScopedEdit(self.sg_scene):
self.export_objects(data_blocks, instances)
def issue_delete_object_edits(self, obj_name, handle):
sg_node = self.sg_nodes_dict.get(handle)
if sg_node:
with rman.SGManager.ScopedEdit(self.sg_scene):
self.sg_scene.DeleteDagNode(sg_node)
self.sg_nodes_dict.pop(handle, None)
for k in self.sg_nodes_dict.keys():
if k.startswith(handle):
if k.endswith('-EMITTER') or k.endswith("-HAIR"):
sg_node = self.sg_nodes_dict[k]
for c in [ sg_node.GetChild(i) for i in range(0, sg_node.GetNumChildren())]:
sg_node.RemoveChild(c)
self.sg_root.RemoveChild(sg_node)
def issue_rman_prim_type_edit(self, active):
if active.type == "MESH":
db_name = data_name(active, self.scene)
mesh_sg = self.sg_nodes_dict.get(db_name)
if mesh_sg:
with rman.SGManager.ScopedEdit(self.sg_scene):
new_mesh_sg = self.export_geometry_data(active, db_name, data=None)
for p in [ mesh_sg.GetParent(i) for i in range(0, mesh_sg.GetNumParents())]:
p.RemoveChild(mesh_sg)
p.AddChild(new_mesh_sg)
def issue_rman_particle_prim_type_edit(self, active, psys):
if psys is None:
return
db_name_emitter = '%s.%s-EMITTER' % (active.name, psys.name)
sg_node = self.sg_nodes_dict.get(db_name_emitter)
if sg_node:
with rman.SGManager.ScopedEdit(self.sg_scene):
new_sg_node = self.export_particles(active, psys, db_name_emitter)
if new_sg_node:
for p in [ sg_node.GetParent(i) for i in range(0, sg_node.GetNumParents())]:
p.RemoveChild(sg_node)
p.AddChild(new_sg_node)
def issue_object_edits(self, active, scene, psys=None):
self.scene = scene
if psys:
db_name_emitter = '%s.%s-EMITTER' % (active.name, psys.name)
db_name_hair = '%s.%s-HAIR' % (active.name, psys.name)
rm = psys.settings.renderman
new_sg_node = None
with rman.SGManager.ScopedEdit(self.sg_scene):
if psys.settings.type == 'EMITTER':
db_name = psys_name(active, psys)
new_sg_node = self.export_particle_system(active, psys, db_name, objectCorrectionMatrix=True, data=None)
sg_node = self.sg_nodes_dict.get(db_name_hair)
if sg_node:
for p in [ sg_node.GetParent(i) for i in range(0, sg_node.GetNumParents())]:
p.RemoveChild(sg_node)
p.AddChild(new_sg_node)
else:
db_name = psys_name(active, psys)
new_sg_node = self.export_particle_system(active, psys, db_name, objectCorrectionMatrix=True, data=None)
sg_node = self.sg_nodes_dict.get(db_name_emitter)
if sg_node:
for p in [ sg_node.GetParent(i) for i in range(0, sg_node.GetNumParents())]:
p.RemoveChild(sg_node)
p.AddChild(new_sg_node)
if psys.settings.material:
psys_mat = active.material_slots[psys.settings.material -
1].material if psys.settings.material and psys.settings.material <= len(active.material_slots) else None
if psys_mat:
mat_handle = "material.%s" % psys_mat.name
sg_material = self.sg_nodes_dict.get(mat_handle)
if sg_material:
new_sg_node.SetMaterial(sg_material)
if new_sg_node and new_sg_node.GetNumParents() == 0:
# new_sg_node is an orphan
# probably because it's a new particle system
# add it to the active mesh
mesh_db_name = data_name(active, self.scene)
mesh_sg = self.sg_nodes_dict.get(mesh_db_name)
if mesh_sg:
mesh_sg.AddChild(new_sg_node)
elif active.type == "MESH":
db_name = data_name(active, self.scene)
mesh_sg = self.sg_nodes_dict.get(db_name)
if mesh_sg:
prim = active.renderman.primitive if active.renderman.primitive != 'AUTO' \
else detect_primitive(active)
if active.update_from_editmode():
with rman.SGManager.ScopedEdit(self.sg_scene):
if prim in ['POLYGON_MESH', 'SUBDIVISION_MESH']:
self.export_mesh(active, mesh_sg, active.data, prim)
self.export_object_primvars(active, mesh_sg)
#elif active.renderman.id_data.is_updated_data:
# with rman.SGManager.ScopedEdit(self.sg_scene):
# new_mesh_sg = self.export_geometry_data(active, db_name, data=None)
# for p in [ mesh_sg.GetParent(i) for i in range(0, mesh_sg.GetNumParents())]:
# p.RemoveChild(mesh_sg)
# p.AddChild(new_mesh_sg)
for psys in active.particle_systems:
if psys:
db_name_emitter = '%s.%s-EMITTER' % (active.name, psys.name)
db_name_hair = '%s.%s-HAIR' % (active.name, psys.name)
rm = psys.settings.renderman
new_sg_node = None
if psys.settings.type == 'EMITTER':
db_name = psys_name(active, psys)
new_sg_node = self.export_particle_system(active, psys, db_name, objectCorrectionMatrix=True, data=None)
sg_node = self.sg_nodes_dict.get(db_name_hair)
if sg_node:
for p in [ sg_node.GetParent(i) for i in range(0, sg_node.GetNumParents())]:
p.RemoveChild(sg_node)
p.AddChild(new_sg_node)
else:
db_name = psys_name(active, psys)
new_sg_node = self.export_particle_system(active, psys, db_name, objectCorrectionMatrix=True, data=None)
sg_node = self.sg_nodes_dict.get(db_name_emitter)
if sg_node:
for p in [ sg_node.GetParent(i) for i in range(0, sg_node.GetNumParents())]:
p.RemoveChild(sg_node)
p.AddChild(new_sg_node)
if psys.settings.material and not rm.use_object_material:
psys_mat = active.material_slots[psys.settings.material -
1].material if psys.settings.material and psys.settings.material <= len(active.material_slots) else None
if psys_mat:
mat_handle = "material.%s" % psys_mat.name
sg_material = self.sg_nodes_dict.get(mat_handle)
if sg_material:
new_sg_node.SetMaterial(sg_material)
if new_sg_node and new_sg_node.GetNumParents() == 0:
# new_sg_node is an orphan
# probably because it's a new particle system
# add it to the active mesh
mesh_db_name = data_name(active, self.scene)
mesh_sg = self.sg_nodes_dict.get(mesh_db_name)
if mesh_sg:
mesh_sg.AddChild(new_sg_node)
def issue_shader_edits(self, nt=None, node=None, ob=None):
if node is None:
mat = None
if bpy.context.object:
mat = bpy.context.object.active_material
if mat not in self.rpass.material_dict:
self.rpass.material_dict[mat] = [bpy.context.object]
light = None
world = bpy.context.scene.world
if mat is None and hasattr(bpy.context, 'light') and bpy.context.light:
light = bpy.context.object
mat = bpy.context.light
elif mat is None and nt and nt.name == 'World':
mat = world
if mat is None:
return
# do an attribute full rebind
# invalidate any textues that were re-made
tex_files_made = reissue_textures(self.rpass, mat)
if tex_files_made:
for f in tex_files_made:
self.rictl.InvalidateTexture(f)
# New material assignment. Loop over all objects:
if mat in self.rpass.material_dict and is_renderman_nodetree(mat):
mat_name = get_mat_name(mat.name)
sg_material = None
mat_handle = "material.%s" % mat_name
if mat_handle in self.sg_nodes_dict.keys():
sg_material = self.sg_nodes_dict[mat_handle]
with rman.SGManager.ScopedEdit(self.sg_scene):
sg_material, bxdfList = self.shader_exporter.export_shader_nodetree(
mat, sg_node=sg_material, mat_sg_handle=mat_handle, handle=None,
iterate_instance=False)
self.sg_nodes_dict['material.%s' % mat_name] = sg_material
if ob:
instance = get_instance(ob, self.scene, False)
if instance:
if instance.name in self.sg_nodes_dict:
with rman.SGManager.ScopedEdit(self.sg_scene):
inst_sg = self.sg_nodes_dict[instance.name]
inst_sg.SetMaterial(sg_material)
else:
for obj in self.rpass.material_dict[mat]:
if not is_multi_material(obj):
if obj.material_slots[0].name == mat.name:
instance = get_instance(obj, self.scene, False)
if instance:
if instance.name in self.sg_nodes_dict:
with rman.SGManager.ScopedEdit(self.sg_scene):
inst_sg = self.sg_nodes_dict[instance.name]
inst_sg.SetMaterial(sg_material)
for psys in obj.particle_systems:
if psys.settings.material:
psys_mat = obj.material_slots[psys.settings.material -
1].material if psys.settings.material and psys.settings.material <= len(obj.material_slots) else None
if psys_mat and psys_mat.name == mat.name:
db_name_emitter = '%s.%s-EMITTER' % (obj.name, psys.name)
db_name_hair = '%s.%s-HAIR' % (obj.name, psys.name)
psys_node = None
if db_name_emitter in self.sg_nodes_dict:
psys_node = self.sg_nodes_dict[db_name_emitter]
elif db_name_hair in self.sg_nodes_dict:
psys_node = self.sg_nodes_dict[db_name_hair]
if psys_node:
with rman.SGManager.ScopedEdit(self.sg_scene):
psys_node.SetMaterial(sg_material)
elif light:
light_ob = light
light = mat
print('EDITING LIGHT')
"""ri.EditBegin('attribute', {'string scopename': light.name})
export_light_filters(ri, light, do_coordsys=True)
export_object_transform(ri, light_ob)
export_light_shaders(ri, light, get_light_group(ob))
ri.EditEnd()"""
elif world:
pass
"""ri.EditBegin('attribute', {'string scopename': world.name})
export_world(ri, mat.data, do_geometry=True)
ri.EditEnd()"""
else:
world = bpy.context.scene.world
mat = None
instance_num = 0
mat_name = None
is_light = False
if bpy.context.object:
mat = bpy.context.object.active_material
if mat:
instance_num = mat.renderman.instance_num
mat_name = get_mat_name(mat.name)
# if this is a light use that for the mat/name
if mat is None and node and issubclass(type(node.id_data), bpy.types.Light):
mat = node.id_data
is_light = True
elif mat is None and nt and nt.name == 'World':
mat = bpy.context.scene.world
elif mat is None and bpy.context.object and bpy.context.object.type == 'CAMERA':
self.rpass.edit_num += 1
#edit_flush(ri, rpass.edit_num, prman)
#ri.EditBegin('option')
#export_camera(ri, rpass.scene, [],
# camera_to_use=rpass.scene.camera)
#ri.EditEnd()
return
elif mat is None \
and hasattr(node, "renderman_node_type") \
and node.renderman_node_type \
in {'displayfilter', 'displaysamplefilter'}:
idx = bpy.context.scene.renderman.display_filters_index
df = bpy.context.scene.renderman.display_filters[idx]
df_name = df.name
if df_name == "":
df_name = "rman_displayfilter_filter%d" % idx
with rman.SGManager.ScopedEdit(self.sg_scene):
if len(self.displayfilters_list) == 1:
if len(self.displayfilters_list) != len(bpy.context.scene.renderman.display_filters):
self.export_displayfilters()
return
elif len(bpy.context.scene.renderman.display_filters) > 1:
if len(self.displayfilters_list)-1 != len(bpy.context.scene.renderman.display_filters):
self.export_displayfilters()
return
if self.displayfilters_list:
df_node = self.displayfilters_list[idx]
if df_node.GetName().CStr() != df.get_filter_name():
self.export_displayfilters()
else:
params = df_node.EditParameterBegin()
property_group_to_rixparams(df.get_filter_node(), df_node)
df_node.EditParameterEnd(params)
self.sg_scene.SetDisplayFilter(self.displayfilters_list)
else:
self.export_displayfilters()
return
elif mat is None \
and hasattr(node, "renderman_node_type") \
and node.renderman_node_type \