forked from mattebb/3delightblender
-
Notifications
You must be signed in to change notification settings - Fork 132
/
rman_render.py
1653 lines (1427 loc) · 68.2 KB
/
rman_render.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
import time
import os
import rman
import ice
import bpy
import sys
from .rman_constants import RFB_VIEWPORT_MAX_BUCKETS, RMAN_RENDERMAN_BLUE, USE_GPU_MODULE, BLENDER_41
from .rman_scene import RmanScene
from .rman_scene_sync import RmanSceneSync
from. import rman_spool
from. import chatserver
from .rfb_logger import rfb_log
import socketserver
import threading
import subprocess
import ctypes
import numpy
import traceback
# for viewport buckets
import gpu
from gpu_extras.batch import batch_for_shader
from gpu_extras.presets import draw_texture_2d
# utils
from .rfb_utils.envconfig_utils import envconfig
from .rfb_utils import string_utils
from .rfb_utils import display_utils
from .rfb_utils import scene_utils
from .rfb_utils import transform_utils
from .rfb_utils.prefs_utils import get_pref
from .rfb_utils.timer_utils import time_this
# config
from .rman_config import __RFB_CONFIG_DICT__ as rfb_config
# roz stats
from .rman_stats import RfBStatsManager
# handlers
from .rman_handlers.rman_it_handlers import add_ipr_to_it_handlers, remove_ipr_to_it_handlers
__RMAN_RENDER__ = None
__RMAN_IT_PORT__ = -1
__BLENDER_DSPY_PLUGIN__ = None
__D_QUICKLYNOISELESS__ = None
__DRAW_THREAD__ = None
__RMAN_STATS_THREAD__ = None
# map Blender display file format
# to ice format
__BLENDER_TO_ICE_DSPY__ = {
'TIFF': ice.constants.FMT_TIFFFLOAT,
'TARGA': ice.constants.FMT_TGA,
'TARGA_RAW': ice.constants.FMT_TGA,
'JPEG': ice.constants.FMT_JPEG,
'JPEG2000': ice.constants.FMT_JPEG,
'OPEN_EXR': ice.constants.FMT_EXRFLOAT,
'CINEON': ice.constants.FMT_CINEON,
'PNG': ice.constants.FMT_PNG
}
# map ice format to a file extension
__ICE_EXT_MAP__ = {
ice.constants.FMT_TIFFFLOAT: 'tif',
ice.constants.FMT_TGA: 'tga',
ice.constants.FMT_JPEG: 'jpg',
ice.constants.FMT_EXRFLOAT: 'exr',
ice.constants.FMT_CINEON: 'cin',
ice.constants.FMT_PNG: 'png'
}
# map rman display to ice format
__RMAN_TO_ICE_DSPY__ = {
'tiff': ice.constants.FMT_TIFFFLOAT,
'targa': ice.constants.FMT_TGA,
'openexr': ice.constants.FMT_EXRFLOAT,
'png': ice.constants.FMT_PNG
}
def __update_areas__():
for window in bpy.context.window_manager.windows:
for area in window.screen.areas:
area.tag_redraw()
def __draw_callback__():
# callback function for the display driver to call tag_redraw
global __RMAN_RENDER__
if __RMAN_RENDER__.rman_is_viewport_rendering and __RMAN_RENDER__.bl_engine:
try:
__RMAN_RENDER__.bl_engine.tag_redraw()
pass
except ReferenceError as e:
return False
return True
return False
DRAWCALLBACK_FUNC = ctypes.CFUNCTYPE(ctypes.c_bool)
__CALLBACK_FUNC__ = DRAWCALLBACK_FUNC(__draw_callback__)
class ItHandler(chatserver.ItBaseHandler):
def dspyRender(self):
global __RMAN_RENDER__
if not __RMAN_RENDER__.is_running:
bpy.ops.render.render(layer=bpy.context.view_layer.name)
def dspyIPR(self):
global __RMAN_RENDER__
if __RMAN_RENDER__.rman_interactive_running:
crop = []
for c in self.msg.getOpt('crop').split(' '):
crop.append(float(c))
if len(crop) == 4:
__RMAN_RENDER__.rman_scene_sync.update_cropwindow(crop)
def stopRender(self):
global __RMAN_RENDER__
rfb_log().debug("Stop Render Requested.")
if __RMAN_RENDER__.rman_interactive_running:
__RMAN_RENDER__.stop_render(stop_draw_thread=False)
__RMAN_RENDER__.del_bl_engine()
def selectObjectById(self):
global __RMAN_RENDER__
obj_id = int(self.msg.getOpt('id', '0'))
if obj_id < 0 or not (obj_id in __RMAN_RENDER__.rman_scene.obj_hash):
return
name = __RMAN_RENDER__.rman_scene.obj_hash[obj_id]
rfb_log().debug('ID: %d Obj Name: %s' % (obj_id, name))
obj = bpy.context.scene.objects[name]
if obj:
if bpy.context.view_layer.objects.active:
bpy.context.view_layer.objects.active.select_set(False)
obj.select_set(True)
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
def start_cmd_server():
global __RMAN_IT_PORT__
if __RMAN_IT_PORT__ != -1:
return __RMAN_IT_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()
__RMAN_IT_PORT__ = port
return __RMAN_IT_PORT__
def draw_threading_func(db):
refresh_rate = get_pref('rman_viewport_refresh_rate', default=0.01)
while db.rman_is_live_rendering:
if db.bl_viewport.shading.type != 'RENDERED':
# if the viewport is not rendering, stop IPR
db.del_bl_engine()
break
if db.xpu_slow_mode:
if db.has_buffer_updated():
try:
db.bl_engine.tag_redraw()
db.reset_buffer_updated()
except ReferenceError as e:
# calling tag_redraw has failed. This might mean
# that there are no more view_3d areas that are shading. Try to
# stop IPR.
#rfb_log().debug("Error calling tag_redraw (%s). Aborting..." % str(e))
db.del_bl_engine()
return
time.sleep(refresh_rate)
else:
time.sleep(1.0)
def call_stats_export_payloads(db):
while db.rman_is_exporting:
db.stats_mgr.update_payloads()
time.sleep(0.1)
def call_stats_update_payloads(db):
while db.rman_running:
if not db.bl_engine:
break
if db.rman_is_xpu and db.is_regular_rendering():
# stop the render if we are rendering in XPU mode
# and we've reached ~100%
if float(db.stats_mgr._progress) > 98.0:
db.rman_is_live_rendering = False
break
db.stats_mgr.update_payloads()
time.sleep(0.1)
def progress_cb(e, d, db):
if not db.stats_mgr.is_connected():
# set the progress in stats_mgr
# we can at least get progress from the event callback
# in case the stats listener is not connected
db.stats_mgr._progress = int(d)
if db.rman_is_live_rendering and int(d) == 100:
db.rman_is_live_rendering = False
def bake_progress_cb(e, d, db):
if not db.stats_mgr.is_connected():
db.stats_mgr._progress = int(d)
def batch_progress_cb(e, d, db):
# just tell the stats mgr to draw
db.stats_mgr._progress = int(d)
db.stats_mgr.draw_render_stats()
print("R90000 %4d%%" % int(d), file = sys.stderr )
sys.stderr.flush()
def render_cb(e, d, db):
if d == 0:
rfb_log().debug("RenderMan has exited.")
if db.rman_is_live_rendering:
db.rman_is_live_rendering = False
def live_render_cb(e, d, db):
if d == 0:
db.rman_is_refining = False
else:
db.rman_is_refining = True
def preload_dsos(rman_render):
"""On linux there is a problem with std::call_once and
blender, by default, being linked with a static libstdc++.
The loader seems to not be able to get the right tls key
for the __once_call global when libprman loads libxpu etc. By preloading
we end up calling the proxy in the blender executable and
that works.
Arguments:
rman_render (RmanRender) - instance of RmanRender where we want to store
the ctypes.CDLL
"""
if sys.platform != 'linux':
return
plugins = [
'lib/libxpu.so',
'lib/plugins/impl_openvdb.so',
'lib/plugins/d_blender.so',
'lib/plugins/PxrSurface.so',
'lib/plugins/PxrDisneyBsdf.so',
'lib/libstats.so',
'lib/plugins/d_quicklyNoiseless.so'
]
tree = envconfig().rmantree
for plugin in plugins:
plugin_path = os.path.join(tree, plugin)
try:
rman_render.preloaded_dsos.append(ctypes.CDLL(plugin_path))
except OSError as error:
rfb_log().debug('Failed to preload {0}: {1}'.format(plugin_path, error))
def preload_quicklynoiseless():
global __D_QUICKLYNOISELESS__
if __D_QUICKLYNOISELESS__ is None:
if sys.platform != 'linux':
return
plugin = 'lib/plugins/d_quicklyNoiseless.so'
tree = envconfig().rmantree
plugin_path = os.path.join(tree, plugin)
try:
__D_QUICKLYNOISELESS__ = ctypes.CDLL(plugin_path)
except OSError as error:
rfb_log().debug('Failed to preload {0}: {1}'.format(plugin_path, error))
class BlRenderResultHelper:
def __init__(self, rman_render, bl_scene, dspy_dict):
self.rman_render = rman_render
self.bl_scene = bl_scene
self.dspy_dict = dspy_dict
self.width = -1
self.height = -1
self.size_x = -1
self.size_y = -1
self.bl_result = None
self.bl_image_rps = dict()
self.render = None
self.render_view = None
self.image_scale = -1
self.write_aovs = False
@staticmethod
def write_empty_result(rman_render):
scale = rman_render.bl_scene.render.resolution_percentage / 100.0
size_x = int(rman_render.bl_scene.render.resolution_x * scale)
size_y = int(rman_render.bl_scene.render.resolution_y * scale)
pixel_count = size_x * size_y
rect = numpy.zeros((pixel_count, 4))
result = rman_render.bl_engine.begin_result(0, 0, size_x, size_y)
layer = result.layers[0].passes["Combined"]
layer.rect = rect
rman_render.bl_engine.end_result(result)
def register_passes(self):
self.render = self.rman_render.rman_scene.bl_scene.render
self.render_view = self.rman_render.bl_engine.active_view_get()
self.image_scale = self.render.resolution_percentage * 0.01
self.width = int(self.render.resolution_x * self.image_scale)
self.height = int(self.render.resolution_y * self.image_scale)
# register any AOV's as passes
for i, dspy_nm in enumerate(self.dspy_dict['displays'].keys()):
if i == 0:
continue
num_channels = -1
while num_channels == -1:
num_channels = self.rman_render.get_numchannels(i)
dspy = self.dspy_dict['displays'][dspy_nm]
dspy_chan = dspy['params']['displayChannels'][0]
chan_info = self.dspy_dict['channels'][dspy_chan]
chan_type = chan_info['channelType']['value']
if num_channels == 4:
self.rman_render.bl_engine.add_pass(dspy_nm, 4, 'RGBA')
elif num_channels == 3:
if chan_type == 'color':
self.rman_render.bl_engine.add_pass(dspy_nm, 3, 'RGB')
else:
self.rman_render.bl_engine.add_pass(dspy_nm, 3, 'XYZ')
elif num_channels == 2:
self.rman_render.bl_engine.add_pass(dspy_nm, 2, 'XY')
else:
self.rman_render.bl_engine.add_pass(dspy_nm, 1, 'X')
self.size_x = self.width
self.size_y = self.height
if self.render.use_border:
start_x = 0
end_x = self.width
start_y = 0
end_y = self.height
if self.render.border_min_y > 0.0:
start_y = round(self.height * self.render.border_min_y)-1
if self.render.border_max_y > 0.0:
end_y = round(self.height * self.render.border_max_y)-1
if self.render.border_min_x > 0.0:
start_x = round(self.width * self.render.border_min_x)-1
if self.render.border_max_x < 1.0:
end_x = round(self.width * self.render.border_max_x)-2
self.size_x = end_x - start_x
self.size_y = end_y - start_y
self.bl_result = self.rman_render.bl_engine.begin_result(0, 0,
self.size_x,
self.size_y,
view=self.render_view)
for i, dspy_nm in enumerate(self.dspy_dict['displays'].keys()):
if i == 0:
render_pass = self.bl_result.layers[0].passes.find_by_name("Combined", self.render_view)
else:
render_pass = self.bl_result.layers[0].passes.find_by_name(dspy_nm, self.render_view)
self.bl_image_rps[i] = render_pass
def update_passes(self):
for i, rp in self.bl_image_rps.items():
buffer = self.rman_render._get_buffer(self.width, self.height, image_num=i,
num_channels=rp.channels,
as_flat=False,
back_fill=False,
render=self.render)
if buffer is None:
continue
rp.rect = buffer
if self.rman_render.bl_engine:
self.rman_render.bl_engine.update_result(self.bl_result)
def finish_passes(self):
if self.bl_result:
if self.rman_render.bl_engine:
self.rman_render.bl_engine.end_result(self.bl_result)
# check if we should write out the AOVs
if self.write_aovs:
use_ice = hasattr(ice, 'FromArray')
for i, dspy_nm in enumerate(self.dspy_dict['displays'].keys()):
if dspy_nm in ['optix_denoiser_albedo', 'optix_denoiser_normal']:
# don't write out these displays; they're only for the opti
continue
filepath = self.dspy_dict['displays'][dspy_nm]['filePath']
if i == 0:
continue
# write out the beauty with a 'raw' substring
toks = os.path.splitext(filepath)
filepath = '%s_beauty_raw.exr' % (toks[0])
if use_ice:
buffer = self.rman_render._get_buffer(self.width, self.height, image_num=i, raw_buffer=True, as_flat=False)
if buffer is None:
continue
# use ice to save out the image
img = ice.FromArray(buffer)
img = img.Flip(False, True, False)
img_format = ice.constants.FMT_EXRFLOAT
if not display_utils.using_rman_displays():
img_format = __BLENDER_TO_ICE_DSPY__.get(self.bl_scene.render.image_settings.file_format, img_format)
# change file extension
toks = os.path.splitext(filepath)
ext = __ICE_EXT_MAP__.get(img_format)
filepath = '%s.%s' % (toks[0], ext)
img.Save(filepath, img_format)
class RmanRender(object):
'''
RmanRender class. This class is responsible for starting and stopping
the renderer. There should only be one instance of this class per session.
Do not create an instance of this class directly. Use RmanRender.get_rman_render()
'''
def __init__(self):
global __RMAN_RENDER__
self.rictl = rman.RiCtl.Get()
self.sgmngr = rman.SGManager.Get()
self.rman = rman
self.sg_scene = None
self.rman_scene = RmanScene(rman_render=self)
self.rman_scene_sync = RmanSceneSync(rman_render=self, rman_scene=self.rman_scene)
self.bl_engine = None
self.rman_running = False
self.rman_is_exporting = False
self.rman_interactive_running = False
self.rman_swatch_render_running = False
self.rman_is_live_rendering = False
self.rman_is_viewport_rendering = False
self.rman_is_xpu = False
self.rman_is_refining = False
self.rman_render_into = 'blender'
self.rman_license_failed = False
self.rman_license_failed_message = ''
self.it_port = -1
self.rman_callbacks = dict()
self.viewport_res_x = -1
self.viewport_res_y = -1
self.viewport_buckets = list()
self._draw_viewport_buckets = False
self.stats_mgr = RfBStatsManager(self)
self.deleting_bl_engine = threading.Lock()
self.stop_render_mtx = threading.Lock()
self.bl_viewport = None
self.xpu_slow_mode = False
self.use_qn = False
self._start_prman_begin()
# hold onto this or python will unload it
self.preloaded_dsos = list()
preload_dsos(self)
@classmethod
def get_rman_render(self):
global __RMAN_RENDER__
if __RMAN_RENDER__ is None:
__RMAN_RENDER__ = RmanRender()
return __RMAN_RENDER__
@property
def bl_engine(self):
return self.__bl_engine
@bl_engine.setter
def bl_engine(self, bl_engine):
self.__bl_engine = bl_engine
def _start_prman_begin(self):
argv = []
argv.append("prman")
woffs = ',' . join(rfb_config['woffs'])
if woffs:
argv.append('-woff')
argv.append(woffs)
self.rictl.PRManSystemBegin(argv)
def __del__(self):
self.rictl.PRManSystemEnd()
def _do_prman_render_begin(self):
argv = []
self.stats_mgr.stats_add_session()
argv.append("-statssession")
argv.append(self.stats_mgr.rman_stats_session_name)
argv.append("-dspyserver")
argv.append("%s" % envconfig().rman_it_path)
err = self.rictl.PRManRenderBegin(argv)
if err:
rfb_log().error("Error initializing RenderMan")
return err
def _do_prman_render_end(self):
self.rictl.PRManRenderEnd()
self.stats_mgr.stats_remove_session()
def del_bl_engine(self):
if not self.bl_engine:
return
if not self.deleting_bl_engine.acquire(timeout=2.0):
return
self.bl_engine = None
self.deleting_bl_engine.release()
def _append_render_cmd(self, render_cmd):
return render_cmd
def _dump_rib_(self, frame=1):
if envconfig().getenv('RFB_DUMP_RIB'):
rfb_log().debug("Writing to RIB...")
rib_time_start = time.time()
if sys.platform == ("win32"):
self.sg_scene.Render("rib C:/tmp/blender.%04d.rib -format ascii -indent" % frame)
else:
self.sg_scene.Render("rib /var/tmp/blender.%04d.rib -format ascii -indent" % frame)
rfb_log().debug("Finished writing RIB. Time: %s" % string_utils._format_time_(time.time() - rib_time_start))
def _load_placeholder_image(self):
placeholder_image = os.path.join(envconfig().rmantree, 'lib', 'textures', 'placeholder.png')
render = self.bl_scene.render
image_scale = 100.0 / render.resolution_percentage
result = self.bl_engine.begin_result(0, 0,
render.resolution_x * image_scale,
render.resolution_y * image_scale)
lay = result.layers[0]
try:
lay.load_from_file(placeholder_image)
except:
pass
self.bl_engine.end_result(result)
def _call_brickmake_for_selected(self):
rm = self.bl_scene.renderman
ob = bpy.context.active_object
if rm.external_animation:
for frame in range(self.bl_scene.frame_start, self.bl_scene.frame_end + 1):
expanded_str = string_utils.expand_string(ob.renderman.bake_filename_attr, frame=self.bl_scene.frame_current)
ptc_file = '%s.ptc' % expanded_str
bkm_file = '%s.bkm' % expanded_str
args = []
args.append('%s/bin/brickmake' % envconfig().rmantree)
args.append('-progress')
args.append('2')
args.append(ptc_file)
args.append(bkm_file)
subprocess.run(args)
else:
expanded_str = string_utils.expand_string(ob.renderman.bake_filename_attr, frame=self.bl_scene.frame_current)
ptc_file = '%s.ptc' % expanded_str
bkm_file = '%s.bkm' % expanded_str
args = []
args.append('%s/bin/brickmake' % envconfig().rmantree)
args.append('-progress')
args.append('2')
args.append(ptc_file)
args.append(bkm_file)
subprocess.run(args)
string_utils.update_frame_token(self.bl_scene.frame_current)
def _check_prman_license(self):
if not envconfig().is_valid_license:
self.rman_license_failed = True
self.rman_license_failed_message = 'Cannot find a valid RenderMan license. Aborting.'
elif not envconfig().has_rps_license:
self.rman_license_failed = True
self.rman_license_failed_message = 'Cannot find RPS-%s license feature. Aborting.' % (envconfig().feature_version)
else:
# check for any available PhotoRealistic-RenderMan licenses
status = envconfig().get_prman_license_status()
if not(status.found and status.is_available):
self.rman_license_failed = True
self.rman_license_failed_message = 'No PhotoRealistic-RenderMan licenses available. Aborting.'
elif status.is_expired():
self.rman_license_failed = True
self.rman_license_failed_message = 'PhotoRealistic-RenderMan licenses have expired (%s).' % str(status.exp_date)
if self.rman_license_failed:
rfb_log().error(self.rman_license_failed_message)
if not self.rman_interactive_running:
self.bl_engine.report({'ERROR'}, self.rman_license_failed_message)
self.stop_render()
return False
return True
def is_regular_rendering(self):
# return if we are doing a regular render and not interactive
return (self.rman_running and not self.rman_interactive_running)
def is_ipr_to_it(self):
return (self.rman_interactive_running and self.rman_scene.ipr_render_into == 'it')
def do_draw_buckets(self):
if self.use_qn:
return False
return get_pref('rman_viewport_draw_bucket', default=True) and self.rman_is_refining
def do_draw_progressbar(self):
return get_pref('rman_viewport_draw_progress') and self.stats_mgr.is_connected() and self.stats_mgr._progress < 100
def start_export_stats_thread(self):
# start an export stats thread
global __RMAN_STATS_THREAD__
__RMAN_STATS_THREAD__ = threading.Thread(target=call_stats_export_payloads, args=(self, ))
__RMAN_STATS_THREAD__.start()
def start_stats_thread(self):
# start a stats thread so we can periodically call update_payloads
global __RMAN_STATS_THREAD__
if __RMAN_STATS_THREAD__:
__RMAN_STATS_THREAD__.join()
__RMAN_STATS_THREAD__ = None
__RMAN_STATS_THREAD__ = threading.Thread(target=call_stats_update_payloads, args=(self, ))
if self.rman_is_xpu:
# FIXME: for now, add a 1 second delay before starting the stats thread
# for some reason, XPU doesn't seem to reset the progress between renders
time.sleep(1.0)
self.stats_mgr.reset()
__RMAN_STATS_THREAD__.start()
def reset(self):
self.rman_license_failed = False
self.rman_license_failed_message = ''
self.rman_is_xpu = False
self.rman_is_refining = False
self.bl_viewport = None
self.xpu_slow_mode = False
self.use_qn = False
def create_scene(self, config, render_config):
self.sg_scene = self.sgmngr.CreateScene(config, render_config, self.stats_mgr.rman_stats_session)
return self.stats_mgr.attach()
def start_render(self, depsgraph, for_background=False):
self.reset()
if self._do_prman_render_begin():
return False
self.bl_scene = depsgraph.scene_eval
rm = self.bl_scene.renderman
self.it_port = start_cmd_server()
rfb_log().info("Parsing scene...")
time_start = time.time()
if not self._check_prman_license():
return False
self.rman_running = True
do_persistent_data = rm.do_persistent_data
use_compositor = scene_utils.should_use_bl_compositor(self.bl_scene)
if for_background:
self.rman_render_into = ''
is_external = True
if use_compositor:
self.rman_render_into = 'blender'
is_external = False
self.rman_callbacks.clear()
ec = rman.EventCallbacks.Get()
ec.RegisterCallback("Render", render_cb, self)
self.rman_callbacks["Render"] = render_cb
if envconfig().getenv('RFB_BATCH_NO_PROGRESS') is None:
ec.RegisterCallback("Progress", batch_progress_cb, self)
self.rman_callbacks["Progress"] = batch_progress_cb
rman.Dspy.DisableDspyServer()
else:
self.rman_render_into = rm.render_into
is_external = False
self.rman_callbacks.clear()
ec = rman.EventCallbacks.Get()
ec.RegisterCallback("Progress", progress_cb, self)
self.rman_callbacks["Progress"] = progress_cb
ec.RegisterCallback("Render", render_cb, self)
self.rman_callbacks["Render"] = render_cb
try:
if self.rman_render_into == 'it':
rman.Dspy.EnableDspyServer()
else:
rman.Dspy.DisableDspyServer()
except:
pass
config = rman.Types.RtParamList()
render_config = rman.Types.RtParamList()
rendervariant = scene_utils.get_render_variant(self.bl_scene)
scene_utils.set_render_variant_config(self.bl_scene, config, render_config)
self.rman_is_xpu = (rendervariant == 'xpu')
boot_strapping = False
bl_rr_helper = None
if self.sg_scene is None:
boot_strapping = True
if not self.create_scene(config, render_config):
self.bl_engine.report({'ERROR'}, 'Could not connect to the stats server. Aborting...' )
self.stop_render(stop_draw_thread=False)
self.del_bl_engine()
return False
# Export the scene
try:
bl_layer = depsgraph.view_layer
self.rman_is_exporting = True
self.start_export_stats_thread()
if boot_strapping:
# This is our first time exporting
self.rman_scene.export_for_final_render(depsgraph, self.sg_scene, bl_layer, is_external=is_external)
else:
# Scene still exists, which means we're in persistent data mode
# Try to get the scene diffs.
self.rman_scene_sync.batch_update_scene(bpy.context, depsgraph)
self.rman_is_exporting = False
self.stats_mgr.reset_progress()
self._dump_rib_(self.bl_scene.frame_current)
rfb_log().info("Finished parsing scene. Total time: %s" % string_utils._format_time_(time.time() - time_start))
self.rman_is_live_rendering = True
except Exception as e:
self.bl_engine.report({'ERROR'}, 'Export failed: %s' % str(e))
rfb_log().error('Export Failed:\n%s' % traceback.format_exc())
self.stop_render(stop_draw_thread=False)
self.del_bl_engine()
return False
# Start the render
render_cmd = "prman -live"
if self.rman_render_into == 'blender' or do_persistent_data:
render_cmd = "prman -live"
render_cmd = self._append_render_cmd(render_cmd)
if boot_strapping:
self.sg_scene.Render(render_cmd)
if self.rman_render_into == 'blender':
dspy_dict = display_utils.get_dspy_dict(self.rman_scene, include_holdouts=False)
bl_rr_helper = BlRenderResultHelper(self, self.bl_scene, dspy_dict)
if for_background:
bl_rr_helper.write_aovs = (use_compositor and rm.use_bl_compositor_write_aovs)
else:
bl_rr_helper.write_aovs = True
bl_rr_helper.register_passes()
self.start_stats_thread()
while self.bl_engine and not self.bl_engine.test_break() and self.rman_is_live_rendering:
time.sleep(0.01)
if bl_rr_helper:
bl_rr_helper.update_passes()
if bl_rr_helper:
bl_rr_helper.finish_passes()
elif for_background and not use_compositor:
# if we're background mode and not using the compositor,
# i.e.: we're not using the Blender display driver
# make sure we create a black/empty image to as the Blender
# render result
BlRenderResultHelper.write_empty_result(self)
self.del_bl_engine()
if not do_persistent_data:
# If we're not in persistent data mode, stop the renderer immediately
# If we are in persistent mode, we rely on the render_complete and
# render_cancel handlers to stop the renderer (see rman_handlers/__init__.py)
self.stop_render()
return True
def start_external_render(self, depsgraph):
if self._do_prman_render_begin():
return False
bl_scene = depsgraph.scene_eval
rm = bl_scene.renderman
self.rman_running = True
self.rman_render_into = ''
rib_options = ""
if rm.rib_compression == "gzip":
rib_options += " -compression gzip"
rib_format = 'ascii'
if rm.rib_format == 'binary':
rib_format = 'binary'
rib_options += " -format %s" % rib_format
if rib_format == "ascii":
rib_options += " -indent"
if rm.external_animation:
original_frame = bl_scene.frame_current
do_persistent_data = rm.do_persistent_data
rfb_log().debug("Writing to RIB...")
time_start = time.time()
if do_persistent_data:
for frame in range(bl_scene.frame_start, bl_scene.frame_end + 1, bl_scene.frame_step):
bl_view_layer = depsgraph.view_layer
config = rman.Types.RtParamList()
render_config = rman.Types.RtParamList()
if self.sg_scene is None:
self.create_scene(config, render_config)
try:
self.bl_engine.frame_set(frame, subframe=0.0)
rfb_log().debug("Frame: %d" % frame)
if frame == bl_scene.frame_start:
self.rman_is_exporting = True
self.rman_scene.export_for_final_render(depsgraph, self.sg_scene, bl_view_layer, is_external=True)
self.rman_is_exporting = False
else:
self.rman_scene_sync.batch_update_scene(bpy.context, depsgraph)
rib_output = string_utils.expand_string(rm.path_rib_output,
asFilePath=True)
self.sg_scene.Render("rib %s %s" % (rib_output, rib_options))
except Exception as e:
self.bl_engine.report({'ERROR'}, 'Export failed: %s' % str(e))
rfb_log().error('Export Failed:\n%s' % traceback.format_exc())
self.stop_render(stop_draw_thread=False)
self.del_bl_engine()
return False
self.sgmngr.DeleteScene(self.sg_scene)
self.sg_scene = None
self.rman_scene.reset()
else:
for frame in range(bl_scene.frame_start, bl_scene.frame_end + 1):
bl_view_layer = depsgraph.view_layer
config = rman.Types.RtParamList()
render_config = rman.Types.RtParamList()
self.create_scene(config, render_config)
try:
self.bl_engine.frame_set(frame, subframe=0.0)
rfb_log().debug("Frame: %d" % frame)
self.rman_is_exporting = True
self.rman_scene.export_for_final_render(depsgraph, self.sg_scene, bl_view_layer, is_external=True)
self.rman_is_exporting = False
rib_output = string_utils.expand_string(rm.path_rib_output,
asFilePath=True)
self.sg_scene.Render("rib %s %s" % (rib_output, rib_options))
self.sgmngr.DeleteScene(self.sg_scene)
self.sg_scene = None
self.rman_scene.reset()
except Exception as e:
self.bl_engine.report({'ERROR'}, 'Export failed: %s' % str(e))
rfb_log().error('Export Failed:\n%s' % traceback.format_exc())
self.stop_render(stop_draw_thread=False)
self.del_bl_engine()
return False
if self.sg_scene:
self.sgmngr.DeleteScene(self.sg_scene)
self.sg_scene = None
self.rman_scene.reset()
rfb_log().info("Finished parsing scene. Total time: %s" % string_utils._format_time_(time.time() - time_start))
self.bl_engine.frame_set(original_frame, subframe=0.0)
else:
config = rman.Types.RtParamList()
render_config = rman.Types.RtParamList()
if not self.create_scene(config, render_config):
self.bl_engine.report({'ERROR'}, 'Could not connect to the stats server. Aborting...' )
self.stop_render(stop_draw_thread=False)
self.del_bl_engine()
return False
try:
time_start = time.time()
bl_view_layer = depsgraph.view_layer
rfb_log().info("Parsing scene...")
self.rman_is_exporting = True
self.rman_scene.export_for_final_render(depsgraph, self.sg_scene, bl_view_layer, is_external=True)
self.rman_is_exporting = False
rib_output = string_utils.expand_string(rm.path_rib_output,
asFilePath=True)
rfb_log().debug("Writing to RIB: %s..." % rib_output)
rib_time_start = time.time()
self.sg_scene.Render("rib %s %s" % (rib_output, rib_options))
rfb_log().debug("Finished writing RIB. Time: %s" % string_utils._format_time_(time.time() - rib_time_start))
rfb_log().info("Finished parsing scene. Total time: %s" % string_utils._format_time_(time.time() - time_start))
self.sgmngr.DeleteScene(self.sg_scene)
self.sg_scene = None
self.rman_scene.reset()
except Exception as e:
self.bl_engine.report({'ERROR'}, 'Export failed: %s' % str(e))
rfb_log().error('Export Failed:\n%s' % traceback.format_exc())
self.stop_render(stop_draw_thread=False)
self.del_bl_engine()
return False
if rm.queuing_system != 'none':
spooler = rman_spool.RmanSpool(self, self.rman_scene, depsgraph)
spooler.batch_render()
self.rman_running = False
self.del_bl_engine()
self._do_prman_render_end()
return True
def start_bake_render(self, depsgraph, for_background=False):
self.reset()
if self._do_prman_render_begin():
return False
self.rman_running = True
self.bl_scene = depsgraph.scene_eval
rm = self.bl_scene.renderman
self.it_port = start_cmd_server()
rfb_log().info("Parsing scene...")
time_start = time.time()
if not self._check_prman_license():
return False
if for_background:
is_external = True
self.rman_callbacks.clear()
ec = rman.EventCallbacks.Get()
ec.RegisterCallback("Render", render_cb, self)
self.rman_callbacks["Render"] = render_cb
rman.Dspy.DisableDspyServer()
else:
is_external = False
self.rman_callbacks.clear()
ec = rman.EventCallbacks.Get()
ec.RegisterCallback("Progress", bake_progress_cb, self)
self.rman_callbacks["Progress"] = bake_progress_cb
ec.RegisterCallback("Render", render_cb, self)
self.rman_callbacks["Render"] = render_cb
self.rman_render_into = ''
rman.Dspy.DisableDspyServer()
config = rman.Types.RtParamList()
render_config = rman.Types.RtParamList()
if not self.create_scene(config, render_config):
self.bl_engine.report({'ERROR'}, 'Could not connect to the stats server. Aborting...' )
self.stop_render(stop_draw_thread=False)
self.del_bl_engine()
return False
try:
bl_layer = depsgraph.view_layer
self.rman_is_exporting = True
self.start_export_stats_thread()
self.rman_scene.export_for_bake_render(depsgraph, self.sg_scene, bl_layer, is_external=is_external)
self.rman_is_exporting = False
self._dump_rib_(self.bl_scene.frame_current)
rfb_log().info("Finished parsing scene. Total time: %s" % string_utils._format_time_(time.time() - time_start))
render_cmd = "prman -blocking"
render_cmd = self._append_render_cmd(render_cmd)
self.sg_scene.Render(render_cmd)