forked from ChaosGroup/vray_for_blender_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
339 lines (264 loc) · 9.98 KB
/
engine.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
#
# V-Ray For Blender
#
# http://chaosgroup.com
#
# Author: Andrei Izrantcev
# E-Mail: andrei.izrantcev@chaosgroup.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# All Rights Reserved. V-Ray(R) is a registered trademark of Chaos Software.
#
import os
import sys
import subprocess
import ipaddress
import bpy
import _vray_for_blender
from vb30.lib import SysUtils
from vb30 import export, debug
from vb30.lib.VRayStream import VRayExportFiles
from vb30.lib.VRayStream import VRayFilePaths
# Check if current build support new RT exporter
HAS_VB35 = SysUtils.hasRtExporter()
HAS_ZMQ = SysUtils.hasZMQEnabled()
if HAS_VB35:
import _vray_for_blender_rt
# This will hold handle to subprocess.Popen to the zmq server if
# it is started in local mode, and it should be terminated on Shutdown()
#
class ZMQProcess:
log_lvl_translate = {
'ERROR': '4',
'WARNING': '3',
'DEBUG': '2',
'INFO': '1',
}
_zmq_process = None
_heartbeat_running = False
def _get_settings(self):
settings = bpy.context.scene.vray.Exporter
return settings.zmq_address, str(settings.zmq_port), settings.zmq_log_level
def start_heartbeat(self):
addr, port, _ = self._get_settings()
ip = 'localhost'
try:
ip = str(ipaddress.ip_address(str(addr)))
except:
debug.PrintError("Failed parsing ip addr from [%s], falling back to %s" % (addr, ip))
self._heartbeat_running = _vray_for_blender_rt.zmq_heartbeat_start("tcp://%s:%s" % (ip, port))
debug.Debug('ZMQ starting heartbeat = %s' % self._heartbeat_running)
def stop_heartbeat(self):
_vray_for_blender_rt.zmq_heartbeat_stop()
self._heartbeat_running = False
debug.Debug('ZMQ stopping heartbeat')
def use_zmq(self):
return SysUtils.hasZMQEnabled()
def is_local(self):
return bpy.context.scene.vray.Exporter.backend_worker == 'LOCAL'
def should_start(self):
should_start = self.use_zmq() and self.is_local()
debug.Debug('ZMQ should start %s' % should_start)
return should_start
def check_heartbeat(self):
if self.use_zmq():
if not self._heartbeat_running:
self.start_heartbeat()
def check_start(self):
if self.use_zmq():
if self.is_local():
self.start()
self.check_heartbeat()
def is_running(self):
running = False
if self._zmq_process is not None:
self._zmq_process.poll()
if self._zmq_process is not None\
and self._zmq_process.returncode is None:
running = True
if self._heartbeat_running:
running = _vray_for_blender_rt.zmq_heartbeat_check()
if not running:
self.stop_heartbeat()
return running
def start(self):
if not self.is_running():
self._check_process()
def stop(self):
if self.is_running():
try:
debug.Debug('Zmq stopped - stopping all viewports')
self.stop_heartbeat()
if self._zmq_process:
debug.Debug('Zmq terminating process')
self._zmq_process.terminate()
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D' and space.viewport_shade == 'RENDERED':
space.viewport_shade = 'SOLID'
except Exception as e:
debug.PrintError(e)
else:
debug.Debug("Not running - cant stop")
def restart(self):
self.stop()
self.check_start()
def _check_process(self):
_, port, log_lvl = self._get_settings()
if self._zmq_process is not None:
self._zmq_process.poll()
debug.Debug("ZMQ: %s -> code[%s]" % (self._zmq_process, self._zmq_process.returncode))
if self._zmq_process is None or self._zmq_process.returncode is not None:
executable_path = SysUtils.GetZmqPath()
if not executable_path or not os.path.exists(executable_path):
debug.PrintError("Can't find V-Ray ZMQ Server!")
else:
try:
env = os.environ.copy()
if sys.platform == "win32":
if 'VRAY_ZMQSERVER_APPSDK_PATH' not in env:
debug.PrintError('Environment variable VRAY_ZMQSERVER_APPSDK_PATH is missing!')
else:
appsdk = os.path.dirname(env['VRAY_ZMQSERVER_APPSDK_PATH'])
env['PATH'] = env['PATH'] + os.pathsep + appsdk
env['VRAY_PATH'] = appsdk
old_ld = (os.pathsep + env['LD_LIBRARY_PATH']) if 'LD_LIBRARY_PATH' in env else ''
env['LD_LIBRARY_PATH'] = appsdk + old_ld
cmd = [
executable_path,
"-p", port,
"-log", self.log_lvl_translate[log_lvl],
"-vfb"
]
debug.Debug(' '.join(cmd))
self.start_heartbeat()
self._zmq_process = subprocess.Popen(cmd, env=env)
except Exception as e:
debug.PrintError(e)
if HAS_ZMQ:
ZMQ = ZMQProcess()
else:
ZMQ = None
def init():
jsonDirpath = os.path.join(SysUtils.GetExporterPath(), "plugins_desc")
_vray_for_blender.start(jsonDirpath)
if HAS_VB35:
_vray_for_blender_rt.load(jsonDirpath)
def shutdown():
_vray_for_blender.free()
if HAS_VB35:
_vray_for_blender_rt.unload()
if HAS_ZMQ:
ZMQ.stop();
class VRayRendererBase(bpy.types.RenderEngine):
def render(self, scene):
if self.is_preview:
if scene.render.resolution_x < 64: # Don't render icons
return
err = export.RenderScene(self, scene)
if err is not None:
self.report({'ERROR'}, err)
class VRayRendererPreview(VRayRendererBase):
bl_idname = 'VRAY_RENDER_PREVIEW'
bl_label = "V-Ray (With Material Preview)"
bl_use_preview = True
bl_preview_filepath = SysUtils.GetPreviewBlend()
class VRayRenderer(VRayRendererBase):
bl_idname = 'VRAY_RENDER'
bl_label = "V-Ray"
bl_use_preview = False
class VRayRendererRT(VRayRendererBase):
bl_idname = 'VRAY_RENDER_RT'
bl_label = "V-Ray"
bl_use_preview = True
bl_preview_filepath = SysUtils.GetPreviewBlend()
bl_use_shading_nodes = True
def _get_settings(self):
# In case of preview "scene" argument will point
# to the preview scene, but we need to use settings
# from the actual scene
#
return bpy.context.scene.vray.Exporter
def __init__(self):
debug.Debug("__init__()")
self.renderer = None
def __del__(self):
debug.Debug("__del__()")
if hasattr(self, 'renderer') and self.renderer is not None:
_vray_for_blender_rt.free(self.renderer)
# Production rendering
#
def update(self, data, scene):
debug.Debug("update()")
vrayExporter = self._get_settings()
if vrayExporter.backend != 'STD':
ZMQ.check_start()
# Init ZMQ exporter
if not self.renderer:
arguments = {
'context': bpy.context.as_pointer(),
'engine': self.as_pointer(),
'data': data.as_pointer(),
'scene': scene.as_pointer(),
}
self.renderer = _vray_for_blender_rt.init(**arguments)
if self.renderer:
_vray_for_blender_rt.update(self.renderer)
def render(self, scene):
debug.Debug("render()")
vrayExporter = self._get_settings()
use_std = vrayExporter.backend == 'STD'
if use_std:
super().render(scene)
elif self.renderer:
_vray_for_blender_rt.render(self.renderer)
# Interactive rendering
#
def view_update(self, context):
debug.Debug("view_update()")
ZMQ.check_start()
if not self.renderer:
self.renderer = _vray_for_blender_rt.init_rt(
context=context.as_pointer(),
engine=self.as_pointer(),
data=bpy.data.as_pointer(),
scene=bpy.context.scene.as_pointer(),
)
if self.renderer:
_vray_for_blender_rt.view_update(self.renderer)
def _view_draw(self, context):
if self.renderer:
_vray_for_blender_rt.view_draw(self.renderer)
# Internally blender check for 'view_draw' method to decide if it will show
# viewport 'RENDERED' mode
if HAS_ZMQ:
VRayRendererRT.view_draw = VRayRendererRT._view_draw
def GetRegClasses():
reg_classes = []
if SysUtils.hasRtExporter():
reg_classes.append(VRayRendererRT)
else:
reg_classes.append(VRayRenderer)
reg_classes.append(VRayRendererPreview)
return reg_classes
def register():
for regClass in GetRegClasses():
bpy.utils.register_class(regClass)
if HAS_ZMQ:
bpy.app.handlers.load_post.append(lambda: ZMQ.check_heartbeat())
def unregister():
for regClass in GetRegClasses():
bpy.utils.unregister_class(regClass)