forked from mattebb/3delightblender
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathrman_scene.py
1656 lines (1377 loc) · 74.3 KB
/
rman_scene.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
# Translators
from .rman_translators.rman_camera_translator import RmanCameraTranslator
from .rman_translators.rman_light_translator import RmanLightTranslator
from .rman_translators.rman_lightfilter_translator import RmanLightFilterTranslator
from .rman_translators.rman_mesh_translator import RmanMeshTranslator
from .rman_translators.rman_material_translator import RmanMaterialTranslator
from .rman_translators.rman_hair_translator import RmanHairTranslator
from .rman_translators.rman_group_translator import RmanGroupTranslator
from .rman_translators.rman_points_translator import RmanPointsTranslator
from .rman_translators.rman_quadric_translator import RmanQuadricTranslator
from .rman_translators.rman_blobby_translator import RmanBlobbyTranslator
from .rman_translators.rman_particles_translator import RmanParticlesTranslator
from .rman_translators.rman_procedural_translator import RmanProceduralTranslator
from .rman_translators.rman_dra_translator import RmanDraTranslator
from .rman_translators.rman_runprogram_translator import RmanRunProgramTranslator
from .rman_translators.rman_openvdb_translator import RmanOpenVDBTranslator
from .rman_translators.rman_gpencil_translator import RmanGPencilTranslator
from .rman_translators.rman_fluid_translator import RmanFluidTranslator
from .rman_translators.rman_curve_translator import RmanCurveTranslator
from .rman_translators.rman_nurbs_translator import RmanNurbsTranslator
from .rman_translators.rman_volume_translator import RmanVolumeTranslator
from .rman_translators.rman_brickmap_translator import RmanBrickmapTranslator
from .rman_translators.rman_emitter_translator import RmanEmitterTranslator
from .rman_translators.rman_empty_translator import RmanEmptyTranslator
from .rman_translators.rman_alembic_translator import RmanAlembicTranslator
from .rman_translators.rman_hair_curves_translator import RmanHairCurvesTranslator
from .rman_translators.rman_pointcloud_translator import RmanPointCloudTranslator
# utils
from .rfb_utils import object_utils
from .rfb_utils import transform_utils
from .rfb_utils import property_utils
from .rfb_utils import display_utils
from .rfb_utils import string_utils
from .rfb_utils import texture_utils
from .rfb_utils import filepath_utils
from .rfb_utils.envconfig_utils import envconfig
from .rfb_utils import scene_utils
from .rfb_utils.prefs_utils import get_pref
from .rfb_utils import shadergraph_utils
from .rfb_utils import color_manager_blender
from .rfb_utils import scenegraph_utils
# config
from .rman_config import __RFB_CONFIG_DICT__ as rfb_config
from . import rman_constants
from .rfb_logger import rfb_log
from .rman_sg_nodes.rman_sg_node import RmanSgNode
import bpy
import os
import sys
class RmanScene(object):
'''
The RmanScene handles translating the Blender scene.
Attributes:
rman_render (RmanRender) - pointer back to the current RmanRender object
rman () - rman python module
sg_scene (RixSGSCene) - the RenderMan scene graph object
context (bpy.types.Context) - the current Blender context object
depsgraph (bpy.types.Depsgraph) - the Blender dependency graph
bl_scene (bpy.types.Scene) - the current Blender scene object
bl_frame_current (int) - the current Blender frame
bl_view_layer (bpy.types.ViewLayer) - the current Blender view layer
rm_rl (RendermanRenderLayerSettings) - the current rman layer
do_motion_blur (bool) - user requested for motion blur
rman_bake (bool) - user requested a bake render
is_interactive (bool) - whether we are in interactive mode
external_render (bool) - whether we are exporting for external (RIB) renders
is_viewport_render (bool) - whether we are rendering into Blender's viewport
scene_solo_light (bool) - user has solo'd a light (all other lights are muted)
rman_materials (dict) - dictionary of scene's materials
rman_translators (dict) - dictionary of all RmanTranslator(s)
rman_particles (dict) - dictionary of all particle systems used
rman_cameras (dict) - dictionary of all cameras in the scene
obj_hash (dict) - dictionary of hashes to objects ( for object picking )
moving_objects (dict) - dictionary of objects that are moving/deforming in the scene
motion_steps (set) - the full set of motion steps for the scene, including
overrides from individual objects
main_camera (RmanSgCamera) - pointer to the main scene camera
rman_root_sg_node (RixSGGroup) - the main root RixSceneGraph node
render_default_light (bool) - whether to add a "headlight" light when there are no lights in the scene
world_df_node (RixSGShader) - a display filter shader that represents the world color
default_light (RixSGAnalyticLight) - the default "headlight" light
viewport_render_res_mult (float) - the current render resolution multiplier (for IPR)
num_object_instances (int) - the current number of object instances. This is used during IPR to
track the number of instances between edits. We try to use this to determine
when an object is added or deleted.
num_objects_in_viewlayer (int) - the current number of objects in the current view layer. We're using this
to keep track if an object was removed from a collection
objects_in_viewlayer (list) - the list of objects (bpy.types.Object) in this view layer.
'''
def __init__(self, rman_render=None):
self.rman_render = rman_render
self.rman = rman_render.rman
self.sg_scene = None
self.context = None
self.depsgraph = None
self.bl_scene = None
self.bl_frame_current = None
self.bl_view_layer = None
self.rm_rl = None
self.do_motion_blur = False
self.rman_bake = False
self.is_interactive = False
self.external_render = False
self.is_viewport_render = False
self.is_swatch_render = False
self.scene_solo_light = False
self.scene_any_lights = False
self.is_xpu = False
self.rman_materials = dict()
self.rman_translators = dict()
self.rman_particles = dict()
self.rman_cameras = dict()
self.obj_hash = dict()
self.moving_objects = dict()
self.rman_prototypes = dict()
self.motion_steps = set()
self.main_camera = None
self.rman_root_sg_node = None
self.render_default_light = False
self.world_df_node = None
self.default_light = None
self.viewport_render_res_mult = 1.0
self.num_object_instances = 0
self.num_objects_in_viewlayer = 0
self.objects_in_viewlayer = list()
self.ipr_render_into = 'blender'
self.create_translators()
def create_translators(self):
# Create our dictionary of translators. The object type is determined
# by the "_detect_primitive_" function in rfb_utils/object_utils.py
self.rman_translators['CAMERA'] = RmanCameraTranslator(rman_scene=self)
self.rman_translators['LIGHT'] = RmanLightTranslator(rman_scene=self)
self.rman_translators['LIGHTFILTER'] = RmanLightFilterTranslator(rman_scene=self)
self.rman_translators['MATERIAL'] = RmanMaterialTranslator(rman_scene=self)
self.rman_translators['HAIR'] = RmanHairTranslator(rman_scene=self)
self.rman_translators['GROUP'] = RmanGroupTranslator(rman_scene=self)
self.rman_translators['EMPTY'] = RmanEmptyTranslator(rman_scene=self)
self.rman_translators['ARMATURE'] = self.rman_translators['EMPTY']
self.rman_translators['LATTICE'] = RmanEmptyTranslator(rman_scene=self)
self.rman_translators['EMPTY_INSTANCER'] = self.rman_translators['EMPTY']
self.rman_translators['POINTS'] = RmanPointsTranslator(rman_scene=self)
self.rman_translators['META'] = RmanBlobbyTranslator(rman_scene=self)
self.rman_translators['PARTICLES'] = RmanParticlesTranslator(rman_scene=self)
self.rman_translators['EMITTER'] = RmanEmitterTranslator(rman_scene=self)
self.rman_translators['DYNAMIC_LOAD_DSO'] = RmanProceduralTranslator(rman_scene=self)
self.rman_translators['DELAYED_LOAD_ARCHIVE'] = RmanDraTranslator(rman_scene=self)
self.rman_translators['PROCEDURAL_RUN_PROGRAM'] = RmanRunProgramTranslator(rman_scene=self)
self.rman_translators['OPENVDB'] = RmanOpenVDBTranslator(rman_scene=self)
self.rman_translators['GPENCIL'] = RmanGPencilTranslator(rman_scene=self)
self.rman_translators['MESH'] = RmanMeshTranslator(rman_scene=self)
self.rman_translators['QUADRIC'] = RmanQuadricTranslator(rman_scene=self)
self.rman_translators['FLUID'] = RmanFluidTranslator(rman_scene=self)
self.rman_translators['CURVE'] = RmanCurveTranslator(rman_scene=self)
self.rman_translators['NURBS'] = RmanNurbsTranslator(rman_scene=self)
self.rman_translators['RI_VOLUME'] = RmanVolumeTranslator(rman_scene=self)
self.rman_translators['BRICKMAP'] = RmanBrickmapTranslator(rman_scene=self)
self.rman_translators['ALEMBIC'] = RmanAlembicTranslator(rman_scene=self)
self.rman_translators['CURVES'] = RmanHairCurvesTranslator(rman_scene=self)
self.rman_translators['POINTCLOUD'] = RmanPointCloudTranslator(rman_scene=self)
def _find_renderman_layer(self):
self.rm_rl = None
if self.bl_view_layer.renderman.use_renderman:
self.rm_rl = self.bl_view_layer.renderman
def reset(self):
# clear out dictionaries etc.
self.rman_materials.clear()
self.rman_particles.clear()
self.rman_cameras.clear()
self.obj_hash.clear()
self.motion_steps = set()
self.moving_objects.clear()
self.rman_prototypes.clear()
self.main_camera = None
self.render_default_light = False
self.world_df_node = None
self.default_light = None
self.is_xpu = False
self.num_object_instances = 0
self.num_objects_in_viewlayer = 0
self.objects_in_viewlayer.clear()
try:
if self.is_viewport_render:
self.viewport_render_res_mult = float(self.context.scene.renderman.viewport_render_res_mult)
else:
self.viewport_render_res_mult = 1.0
except AttributeError as err:
rfb_log().debug("Cannot set viewport_render_res_mult: %s" % str(err))
def export_for_final_render(self, depsgraph, sg_scene, bl_view_layer, is_external=False):
self.sg_scene = sg_scene
self.context = bpy.context
self.bl_scene = depsgraph.scene_eval
self.bl_view_layer = bl_view_layer
self._find_renderman_layer()
self.depsgraph = depsgraph
self.external_render = is_external
self.is_interactive = False
self.is_viewport_render = False
self.do_motion_blur = self.bl_scene.renderman.motion_blur
self.export()
def export_for_bake_render(self, depsgraph, sg_scene, bl_view_layer, is_external=False):
self.sg_scene = sg_scene
self.context = bpy.context
self.bl_scene = depsgraph.scene_eval
self.bl_view_layer = bl_view_layer
self._find_renderman_layer()
self.depsgraph = depsgraph
self.external_render = is_external
self.is_interactive = False
self.is_viewport_render = False
self.do_motion_blur = self.bl_scene.renderman.motion_blur
self.rman_bake = True
if self.bl_scene.renderman.hider_type == 'BAKE_BRICKMAP_SELECTED':
self.export_bake_brickmap_selected()
else:
self.export_bake_render_scene()
def export_for_interactive_render(self, context, depsgraph, sg_scene):
self.sg_scene = sg_scene
self.context = context
self.bl_view_layer = depsgraph.view_layer_eval
self.bl_scene = depsgraph.scene_eval
self._find_renderman_layer()
self.depsgraph = depsgraph
self.external_render = False
self.is_interactive = True
self.is_viewport_render = False
self.rman_bake = False
if self.ipr_render_into == 'blender':
self.is_viewport_render = True
self.do_motion_blur = False
self.export()
def export_for_rib_selection(self, context, sg_scene):
self.reset()
self.bl_scene = context.scene
self.bl_frame_current = self.bl_scene.frame_current
self.sg_scene = sg_scene
self.context = context
self.depsgraph = context.evaluated_depsgraph_get()
self.bl_view_layer = self.depsgraph.view_layer_eval
self._find_renderman_layer()
self.rman_bake = False
self.external_render = False
self.is_interactive = False
self.is_viewport_render = False
self.export_root_sg_node()
self.export_materials([m for m in self.depsgraph.ids if isinstance(m, bpy.types.Material)])
self.export_data_blocks(selected_objects=True)
def export_for_swatch_render(self, depsgraph, sg_scene):
self.sg_scene = sg_scene
self.context = bpy.context #None
self.bl_scene = depsgraph.scene_eval
self.depsgraph = depsgraph
self.external_render = False
self.is_interactive = False
self.is_viewport_render = False
self.do_motion_blur = False
self.rman_bake = False
self.is_swatch_render = True
self.export_swatch_render_scene()
def export(self):
self.reset()
self.render_default_light = self.bl_scene.renderman.render_default_light
if sys.platform != "darwin":
self.is_xpu = (self.bl_scene.renderman.renderVariant != 'prman')
# update variables
string_utils.set_var('scene', self.bl_scene.name.replace(' ', '_'))
string_utils.set_var('layer', self.bl_view_layer.name.replace(' ', '_'))
self.bl_frame_current = self.bl_scene.frame_current
string_utils.update_frame_token(self.bl_frame_current)
rfb_log().debug("Creating root scene graph node")
self.export_root_sg_node()
rfb_log().debug("Calling export_materials()")
#self.export_materials(bpy.data.materials)
self.export_materials([m for m in self.depsgraph.ids if isinstance(m, bpy.types.Material)])
# tell the texture manager to start converting any unconverted textures
# normally textures are converted as they are added to the scene
rfb_log().debug("Calling txmake_all()")
texture_utils.get_txmanager().rman_scene = self
texture_utils.get_txmanager().txmake_all(blocking=True)
self.scene_any_lights = self._scene_has_lights()
rfb_log().debug("Calling export_data_blocks()")
self.export_data_blocks()
self.export_searchpaths()
self.export_global_options()
self.export_hider()
self.export_integrator()
self.export_cameras([c for c in self.depsgraph.objects if isinstance(c.data, bpy.types.Camera)])
# export default light
self.export_defaultlight()
self.main_camera.sg_node.AddChild(self.default_light)
self.export_displays()
self.export_samplefilters()
self.export_displayfilters()
if self.do_motion_blur:
rfb_log().debug("Calling export_instances_motion()")
self.export_instances_motion()
self.rman_render.stats_mgr.set_export_stats("Finished Export", 1.0)
self.num_object_instances = len(self.depsgraph.object_instances)
visible_objects = getattr(self.context, 'visible_objects', list())
self.num_objects_in_viewlayer = len(visible_objects)
self.objects_in_viewlayer = [o for o in visible_objects]
if self.is_interactive:
self.export_viewport_stats()
else:
self.export_stats()
def export_bake_render_scene(self):
self.reset()
# update tokens
string_utils.set_var('scene', self.bl_scene.name.replace(' ', '_'))
string_utils.set_var('layer', self.bl_view_layer.name.replace(' ', '_'))
self.bl_frame_current = self.bl_scene.frame_current
rfb_log().debug("Creating root scene graph node")
self.export_root_sg_node()
rfb_log().debug("Calling export_materials()")
self.export_materials([m for m in self.depsgraph.ids if isinstance(m, bpy.types.Material)])
rfb_log().debug("Calling txmake_all()")
texture_utils.get_txmanager().rman_scene = self
texture_utils.get_txmanager().txmake_all(blocking=True)
self.scene_any_lights = self._scene_has_lights()
rm = self.bl_scene.renderman
rman_root_sg_node = self.get_root_sg_node()
attrs = rman_root_sg_node.GetAttributes()
attrs.SetFloat("dice:worlddistancelength", rm.rman_bake_illlum_density)
rman_root_sg_node.SetAttributes(attrs)
rfb_log().debug("Calling export_data_blocks()")
self.export_data_blocks()
self.export_searchpaths()
self.export_global_options()
self.export_hider()
self.export_integrator()
self.export_cameras([c for c in self.depsgraph.objects if isinstance(c.data, bpy.types.Camera)])
# export default light
self.export_defaultlight()
self.main_camera.sg_node.AddChild(self.default_light)
self.export_bake_displays()
self.export_samplefilters()
self.export_displayfilters()
if self.do_motion_blur:
rfb_log().debug("Calling export_instances_motion()")
self.export_instances_motion()
options = self.sg_scene.GetOptions()
bake_resolution = int(rm.rman_bake_illlum_res)
options.SetIntegerArray(self.rman.Tokens.Rix.k_Ri_FormatResolution, (bake_resolution, bake_resolution), 2)
self.sg_scene.SetOptions(options)
def export_bake_brickmap_selected(self):
self.reset()
# update variables
string_utils.set_var('scene', self.bl_scene.name.replace(' ', '_'))
string_utils.set_var('layer', self.bl_view_layer.name.replace(' ', '_'))
self.bl_frame_current = self.bl_scene.frame_current
rfb_log().debug("Creating root scene graph node")
self.export_root_sg_node()
rfb_log().debug("Calling export_materials()")
self.export_materials([m for m in self.depsgraph.ids if isinstance(m, bpy.types.Material)])
rfb_log().debug("Calling txmake_all()")
texture_utils.get_txmanager().rman_scene = self
texture_utils.get_txmanager().txmake_all(blocking=True)
self.scene_any_lights = self._scene_has_lights()
rm = self.bl_scene.renderman
rman_root_sg_node = self.get_root_sg_node()
attrs = rman_root_sg_node.GetAttributes()
attrs.SetFloat("dice:worlddistancelength", rm.rman_bake_illlum_density)
rman_root_sg_node.SetAttributes(attrs)
self.export_searchpaths()
self.export_global_options()
self.export_hider()
self.export_integrator()
self.export_cameras([c for c in self.depsgraph.objects if isinstance(c.data, bpy.types.Camera)])
# export default light
self.export_defaultlight()
self.main_camera.sg_node.AddChild(self.default_light)
ob = self.context.active_object
self.export_materials([m for m in self.depsgraph.ids if isinstance(m, bpy.types.Material)])
objects_needed = [x.original for x in self.bl_scene.objects if object_utils._detect_primitive_(x) == 'LIGHT']
objects_needed.append(ob.original)
self.export_data_blocks(objects_list=objects_needed)
self.export_samplefilters()
self.export_displayfilters()
options = self.sg_scene.GetOptions()
bake_resolution = int(rm.rman_bake_illlum_res)
options.SetIntegerArray(self.rman.Tokens.Rix.k_Ri_FormatResolution, (bake_resolution, bake_resolution), 2)
self.sg_scene.SetOptions(options)
# Display
display_driver = 'pointcloud'
dspy_chan_Ci = self.rman.SGManager.RixSGDisplayChannel('color', 'Ci')
self.sg_scene.SetDisplayChannel([dspy_chan_Ci])
render_output = '%s.ptc' % ob.renderman.bake_filename_attr
render_output = string_utils.expand_string(render_output)
display = self.rman.SGManager.RixSGShader("Display", display_driver, render_output)
display.params.SetString("mode", 'Ci')
self.main_camera.sg_camera_node.SetDisplay(display)
def export_swatch_render_scene(self):
self.reset()
# options
options = self.sg_scene.GetOptions()
options.SetInteger(self.rman.Tokens.Rix.k_hider_minsamples, get_pref('rman_preview_renders_minSamples', default=0))
options.SetInteger(self.rman.Tokens.Rix.k_hider_maxsamples, get_pref('rman_preview_renders_minSamples', default=1))
options.SetInteger(self.rman.Tokens.Rix.k_hider_incremental, 1)
options.SetString("adaptivemetric", "variance")
scale = 100.0 / self.bl_scene.render.resolution_percentage
w = int(self.bl_scene.render.resolution_x * scale)
h = int(self.bl_scene.render.resolution_y * scale)
options.SetIntegerArray(self.rman.Tokens.Rix.k_Ri_FormatResolution, (w, h), 2)
options.SetFloat(self.rman.Tokens.Rix.k_Ri_PixelVariance, get_pref('rman_preview_renders_pixelVariance', default=0.15))
options.SetInteger(self.rman.Tokens.Rix.k_limits_threads, -2)
options.SetString(self.rman.Tokens.Rix.k_bucket_order, 'horizontal')
self.sg_scene.SetOptions(options)
# searchpaths
self.export_searchpaths()
# integrator
integrator_sg = self.rman.SGManager.RixSGShader("Integrator", "PxrPathTracer", "integrator")
self.sg_scene.SetIntegrator(integrator_sg)
# camera
self.export_cameras([c for c in self.depsgraph.objects if isinstance(c.data, bpy.types.Camera)])
# Display
display_driver = 'blender'
dspy_chan_Ci = self.rman.SGManager.RixSGDisplayChannel('color', 'Ci')
dspy_chan_a = self.rman.SGManager.RixSGDisplayChannel('float', 'a')
self.sg_scene.SetDisplayChannel([dspy_chan_Ci, dspy_chan_a])
display = self.rman.SGManager.RixSGShader("Display", display_driver, 'blender_preview')
display.params.SetString("mode", 'Ci,a')
self.main_camera.sg_camera_node.SetDisplay(display)
rfb_log().debug("Calling materials()")
self.export_materials([m for m in self.depsgraph.ids if isinstance(m, bpy.types.Material)])
rfb_log().debug("Calling export_data_blocks()")
self.export_data_blocks()
def set_root_lightlinks(self, rixattrs=None):
rm = self.bl_scene.renderman
root_sg = self.get_root_sg_node()
attrs = rixattrs
if rixattrs is None:
attrs = root_sg.GetAttributes()
all_lightfilters = [string_utils.sanitize_node_name(l.name) for l in scene_utils.get_all_lightfilters(self.bl_scene)]
if rm.invert_light_linking:
all_lights = [string_utils.sanitize_node_name(l.name) for l in scene_utils.get_all_lights(self.bl_scene, include_light_filters=False)]
for ll in rm.light_links:
light_ob = ll.light_ob
light_nm = string_utils.sanitize_node_name(light_ob.name)
light_props = shadergraph_utils.get_rman_light_properties_group(light_ob)
if light_props.renderman_light_role == 'RMAN_LIGHT':
if light_nm in all_lights:
all_lights.remove(light_nm)
elif light_nm in all_lightfilters:
all_lightfilters.remove(light_nm)
if all_lights:
attrs.SetString(self.rman.Tokens.Rix.k_lighting_subset, ' '. join(all_lights) )
else:
attrs.SetString(self.rman.Tokens.Rix.k_lighting_subset, '*')
if all_lightfilters:
attrs.SetString(self.rman.Tokens.Rix.k_lightfilter_subset, ' '. join(all_lightfilters) )
else:
attrs.SetString(self.rman.Tokens.Rix.k_lightfilter_subset, '*')
else:
attrs.SetString(self.rman.Tokens.Rix.k_lightfilter_subset, ','. join(all_lightfilters) )
if rixattrs is None:
root_sg.SetAttributes(attrs)
def export_root_sg_node(self):
rm = self.bl_scene.renderman
root_sg = self.get_root_sg_node()
attrs = root_sg.GetAttributes()
# set any properties marked riattr in the config file
for prop_name, meta in rm.prop_meta.items():
property_utils.set_riattr_bl_prop(attrs, prop_name, meta, rm, check_inherit=False, remove=False)
self.set_root_lightlinks(rixattrs=attrs)
root_sg.SetAttributes(attrs)
def get_root_sg_node(self):
return self.sg_scene.Root()
def export_materials(self, materials):
for mat in materials:
db_name = object_utils.get_db_name(mat)
rman_sg_material = self.rman_translators['MATERIAL'].export(mat.original, db_name)
if rman_sg_material:
self.rman_materials[mat.original] = rman_sg_material
def check_visibility(self, instance, ob_eval=None):
if not self.is_interactive:
return True
viewport = self.context.space_data
if viewport is None or viewport.type != 'VIEW_3D':
return True
if instance.is_instance:
ob_eval = instance.instance_object
ob_eval_visible = ob_eval.visible_in_viewport_get(viewport)
parent_visible = instance.parent.visible_in_viewport_get(viewport)
return (ob_eval_visible or parent_visible)
if ob_eval is None:
ob_eval = instance.object.evaluated_get(self.depsgraph)
visible = ob_eval.visible_in_viewport_get(viewport)
return visible
def is_instance_selected(self, instance):
ob = instance.object
parent = None
if instance.is_instance:
parent = instance.parent
if not ob.original.select_get():
if parent:
if not parent.original.select_get():
return False
else:
return False
if parent and not parent.original.select_get():
return False
return True
def get_rman_sg_instance(self, ob_inst, rman_sg_node, instance_parent, psys, create=True):
group_db_name = object_utils.get_group_db_name(ob_inst)
rman_parent_node = None
if psys and instance_parent:
rman_parent_node = self.get_rman_prototype(object_utils.prototype_key(instance_parent), ob=instance_parent, create=True)
if rman_parent_node:
if group_db_name in rman_parent_node.instances:
return rman_parent_node.instances[group_db_name]
else:
if group_db_name in rman_sg_node.instances:
return rman_sg_node.instances[group_db_name]
rman_sg_group = None
if create:
rman_group_translator = self.rman_translators['GROUP']
rman_sg_group = rman_group_translator.export(None, group_db_name)
rman_sg_group.sg_node.AddChild(rman_sg_node.sg_node)
if rman_parent_node:
# this is an instance that comes from a particle system
# add this instance to the rman_sg_node that owns the particle system
rman_parent_node.instances[group_db_name] = rman_sg_group
else:
rman_sg_node.instances[group_db_name] = rman_sg_group
return rman_sg_group
def update_instance_attributes(self, translator, rman_sg_node, ob_eval, ob_inst, remove=False):
# we want to export attributes for the instance
# as we still want the instance to be able override attributes
attrs = rman_sg_node.sg_node.GetAttributes()
translator.export_object_attributes_attrs(ob_eval, attrs, remove=remove)
rman_sg_node.sg_node.SetAttributes(attrs)
# export instance attributes
translator.export_instance_attributes(ob_eval, rman_sg_node, ob_inst)
def export_instance(self, ob_eval, ob_inst, rman_sg_node, rman_type, instance_parent, psys):
rman_group_translator = self.rman_translators['GROUP']
rman_sg_group = self.get_rman_sg_instance(ob_inst, rman_sg_node, instance_parent, psys, create=True)
is_empty_instancer = False
if instance_parent:
is_empty_instancer = object_utils.is_empty_instancer(instance_parent)
# Object attrs
translator = self.rman_translators.get(rman_type, None)
if translator:
self.update_instance_attributes(translator, rman_sg_group, ob_eval, ob_inst)
# Add any particles necessary
if rman_sg_node.rman_sg_particle_group_node:
if (len(ob_eval.particle_systems) > 0) and ob_inst.show_particles:
rman_sg_group.sg_node.AddChild(rman_sg_node.rman_sg_particle_group_node.sg_node)
# Attach any material overrides
if is_empty_instancer:
if instance_parent.renderman.rman_material_override:
rman_sg_group.sg_node.SetMaterial(None)
else:
# if there is not a material override, we want
# the material of the object
self.attach_material(ob_eval, rman_sg_group)
elif psys:
self.attach_particle_material(psys.settings, instance_parent, ob_eval, rman_sg_group)
rman_sg_group.bl_psys_settings = psys.settings.original
elif ob_eval.renderman.rman_material_override:
self.attach_material(ob_eval, rman_sg_group)
else:
rman_sg_group.sg_node.SetMaterial(None)
if is_empty_instancer:
# if this is an empty instancer, add as a child to the empty instancer
parent_proto_key = object_utils.prototype_key(instance_parent)
rman_parent_node = self.get_rman_prototype(parent_proto_key, ob=instance_parent, create=True)
rman_parent_node.sg_attributes.AddChild(rman_sg_group.sg_node)
else:
rman_sg_node.sg_attributes.AddChild(rman_sg_group.sg_node)
# check if instance has transform motion
if self.do_motion_blur and object_utils.is_transforming(ob_eval):
mb_segs = self.bl_scene.renderman.motion_segments
if ob_eval.renderman.motion_segments_override:
mb_segs = ob_eval.renderman.motion_segments
if mb_segs > 1:
subframes = scene_utils._get_subframes_(mb_segs, self.bl_scene)
rman_sg_group.motion_steps = subframes
rman_sg_group.is_transforming = True
self.motion_steps.update(subframes)
self.moving_objects[ob_inst.object.name_full] = ob_inst.object
if rman_type == "META":
# meta/blobbies are already in world space. Their instances don't need to
# set a transform.
return rman_sg_group
rman_sg_group.sg_node.SetInheritTransform(False) # we don't want to inherit the transform
rman_group_translator.update_transform(ob_inst, rman_sg_group)
return rman_sg_group
def export_data_blocks(self, selected_objects=False, objects_list=False):
total = len(self.depsgraph.object_instances)
for i, ob_inst in enumerate(self.depsgraph.object_instances):
ob = ob_inst.object
rfb_log().debug(" Exported %d/%d instances... (%s)" % (i, total, ob.name))
self.rman_render.stats_mgr.set_export_stats("Exporting instances",i/total)
if ob.type in ('CAMERA'):
continue
if selected_objects and not self.is_instance_selected(ob_inst):
continue
# only export these objects
if objects_list and ob.original not in objects_list:
continue
if not self.check_visibility(ob_inst):
rfb_log().debug(" Object (%s) not visible" % (ob.name))
continue
ob_eval = ob.evaluated_get(self.depsgraph)
psys = None
instance_parent = None
proto_key = object_utils.prototype_key(ob_inst)
if ob_inst.is_instance:
psys = ob_inst.particle_system
instance_parent = ob_inst.parent
rman_type = object_utils._detect_primitive_(ob_eval)
rman_sg_node = self.get_rman_prototype(proto_key, ob=ob_eval, create=True)
if not rman_sg_node:
continue
if rman_type == 'LIGHT':
self.check_solo_light(rman_sg_node, ob_eval)
if rman_type in object_utils._RMAN_NO_INSTANCES_:
continue
self.export_instance(ob_eval, ob_inst, rman_sg_node, rman_type, instance_parent, psys)
def export_data_block(self, proto_key, ob):
rman_type = object_utils._detect_primitive_(ob)
if rman_type == "META":
if rman_constants.META_AS_MESH:
return None
# only add the meta instance that matches the family name
if ob.name_full != object_utils.get_meta_family(ob):
return None
if proto_key in self.rman_prototypes:
return self.rman_prototypes[proto_key]
translator = self.rman_translators.get(rman_type, None)
if not translator:
return None
rman_sg_node = None
db_name = object_utils.get_db_name(ob)
rman_sg_node = translator.export(ob, db_name)
if not rman_sg_node:
return None
rman_sg_node.create_sg_attributes(ob)
rman_sg_node.rman_type = rman_type
self.rman_prototypes[proto_key] = rman_sg_node
# motion blur
# we set motion steps for this object, even if it's not moving
# it could be moving as part of a particle system
#
# FIXME: remove the checking of transform motion here, this should
# and is already being done in export_instance.
mb_segs = -1
mb_deform_segs = -1
if self.do_motion_blur:
mb_segs = self.bl_scene.renderman.motion_segments
mb_deform_segs = self.bl_scene.renderman.deform_motion_segments
if ob.renderman.motion_segments_override:
mb_segs = ob.renderman.motion_segments
if mb_segs > 1:
subframes = scene_utils._get_subframes_(mb_segs, self.bl_scene)
rman_sg_node.motion_steps = subframes
self.motion_steps.update(subframes)
if ob.renderman.motion_segments_override:
mb_deform_segs = ob.renderman.deform_motion_segments
if mb_deform_segs > 1:
subframes = scene_utils._get_subframes_(mb_deform_segs, self.bl_scene)
rman_sg_node.deform_motion_steps = subframes
self.motion_steps.update(subframes)
if rman_sg_node.is_transforming or rman_sg_node.is_deforming:
if mb_segs > 1 or mb_deform_segs > 1:
self.moving_objects[ob.name_full] = ob
if mb_segs < 1:
rman_sg_node.is_transforming = False
if mb_deform_segs < 1:
rman_sg_node.is_deforming = False
translator.update(ob, rman_sg_node)
# set object attributes
attrs = rman_sg_node.sg_attributes.GetAttributes()
translator.export_object_attributes_attrs(ob, attrs, remove=False)
rman_sg_node.sg_attributes.SetAttributes(attrs)
# set material
self.attach_material(ob, rman_sg_node, sg_node=rman_sg_node.sg_attributes)
if len(ob.particle_systems) > 0:
# Deal with any particles now.
subframes = []
if self.do_motion_blur:
subframes = scene_utils._get_subframes_(2, self.bl_scene)
self.motion_steps.update(subframes)
particles_group_db = ''
rman_sg_node.rman_sg_particle_group_node = self.rman_translators['GROUP'].export(None, particles_group_db)
psys_translator = self.rman_translators['PARTICLES']
for psys in ob.particle_systems:
psys_db_name = '%s' % psys.name
rman_sg_particles = psys_translator.export(ob, psys, psys_db_name)
if not rman_sg_particles:
continue
psys_translator.set_motion_steps(rman_sg_particles, subframes)
psys_translator.update(ob, psys, rman_sg_particles)
ob_psys = self.rman_particles.get(proto_key, dict())
ob_psys[psys.settings.original] = rman_sg_particles
self.rman_particles[proto_key] = ob_psys
rman_sg_node.rman_sg_particle_group_node.sg_node.AddChild(rman_sg_particles.sg_node)
if rman_type == 'EMPTY':
self._export_hidden_instance(ob, rman_sg_node)
if ob.original.parent:
ob_parent_eval = object_utils.find_parent(ob.original)
if ob_parent_eval:
ob_parent_eval = ob_parent_eval.evaluated_get(self.depsgraph)
parent_proto_key = object_utils.prototype_key(ob.original.parent)
rman_parent_node = self.get_rman_prototype(parent_proto_key, ob=ob_parent_eval, create=True)
rman_parent_node.sg_attributes.AddChild(rman_sg_node.sg_attributes)
else:
# doesn't have a parent, add to root
self.get_root_sg_node().AddChild(rman_sg_node.sg_attributes)
else:
self.get_root_sg_node().AddChild(rman_sg_node.sg_attributes)
return rman_sg_node
def export_instances_motion(self, selected_objects=False):
origframe = self.bl_scene.frame_current
mb_segs = self.bl_scene.renderman.motion_segments
origframe = self.bl_scene.frame_current
motion_steps = sorted(list(self.motion_steps))
first_sample = False
delta = 0.0
if len(motion_steps) > 0:
delta = -motion_steps[0]
psys_translator = self.rman_translators['PARTICLES']
rman_group_translator = self.rman_translators['GROUP']
for samp, seg in enumerate(motion_steps):
first_sample = (samp == 0)
if seg < 0.0:
self.rman_render.bl_engine.frame_set(origframe - 1, subframe=1.0 + seg)
else:
self.rman_render.bl_engine.frame_set(origframe, subframe=seg)
self.depsgraph.update()
time_samp = seg + delta # get the normlized version of the segment
total = len(self.depsgraph.object_instances)
objFound = False
# update camera
if not first_sample and self.main_camera.is_transforming and seg in self.main_camera.motion_steps:
cam_translator = self.rman_translators['CAMERA']
idx = 0
for i, s in enumerate(self.main_camera.motion_steps):
if s == seg:
idx = i
break
cam_translator.update_transform(self.depsgraph.scene_eval.camera, self.main_camera, idx, time_samp)
rfb_log().debug(" Export Sample: %i" % samp)
for i, ob_inst in enumerate(self.depsgraph.object_instances):
if selected_objects and not self.is_instance_selected(ob_inst):
continue
if not self.check_visibility(ob_inst):
continue
psys = None
ob = ob_inst.object.evaluated_get(self.depsgraph)
proto_key = object_utils.prototype_key(ob_inst)
rfb_log().debug(" Exported %d/%d motion instances... (%s)" % (i, total, ob.name))
self.rman_render.stats_mgr.set_export_stats("Exporting motion instances (%d) " % samp ,i/total)
instance_parent = None
if ob_inst.is_instance:
psys = ob_inst.particle_system
instance_parent = ob_inst.parent
rman_type = object_utils._detect_primitive_(ob)
if rman_type in object_utils._RMAN_NO_INSTANCES_:
continue
# check particles for motion
'''
for psys in ob.particle_systems:
ob_psys = self.rman_particles.get(proto_key, None)
if not ob_psys:
continue
rman_sg_particles = ob_psys.get(psys.settings.original, None)
if not rman_sg_particles:
continue
if not seg in rman_sg_particles.motion_steps:
continue
idx = 0
for i, s in enumerate(rman_sg_particles.motion_steps):
if s == seg:
idx = i
break
psys_translator.export_deform_sample(rman_sg_particles, ob, psys, idx)
'''
# object is not moving and not part of a particle system
if ob.name_full not in self.moving_objects and not psys:
continue
rman_sg_node = self.get_rman_prototype(proto_key, ob=ob)
if not rman_sg_node:
continue
rman_sg_group = self.get_rman_sg_instance(ob_inst, rman_sg_node, instance_parent, psys)
if not rman_sg_group:
continue
# transformation blur
if seg in rman_sg_group.motion_steps:
idx = 0
for i, s in enumerate(rman_sg_group.motion_steps):
if s == seg:
idx = i
break
if rman_sg_group.is_transforming or psys:
if first_sample:
rman_group_translator.update_transform_num_samples(rman_sg_group, rman_sg_group.motion_steps )
rman_group_translator.update_transform_sample( ob_inst, rman_sg_group, idx, time_samp)
# deformation blur
if rman_sg_node.is_deforming and seg in rman_sg_node.deform_motion_steps:
rman_type = rman_sg_node.rman_type
if rman_type in ['MESH', 'FLUID', 'CURVES']:
translator = self.rman_translators.get(rman_type, None)
if translator:
deform_idx = 0
for i, s in enumerate(rman_sg_node.deform_motion_steps):
if s == seg:
deform_idx = i
break
translator.export_deform_sample(rman_sg_node, ob, deform_idx)
self.rman_render.bl_engine.frame_set(origframe, subframe=0)
rfb_log().debug(" Finished exporting motion instances")
self.rman_render.stats_mgr.set_export_stats("Finished exporting motion instances", 100)
def export_defaultlight(self):
# Export a headlight light if needed
if not self.default_light:
self.default_light = self.sg_scene.CreateAnalyticLight('__defaultlight')
sg_node = self.rman.SGManager.RixSGShader("Light", 'PxrDistantLight' , "light")
self.default_light.SetLight(sg_node)
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]
self.default_light.SetOrientTransform(s_orientPxrLight)
if self.render_default_light and not self.scene_any_lights:
self.default_light.SetHidden(0)
else:
self.default_light.SetHidden(1)
def _scene_has_lights(self):
# Determine if there are any lights in the scene
num_lights = len(scene_utils.get_all_lights(self.bl_scene, include_light_filters=False))
return num_lights > 0
def get_rman_prototype(self, proto_key, ob=None, create=False):
if proto_key in self.rman_prototypes:
return self.rman_prototypes[proto_key]