forked from ChaosGroup/vray_for_blender_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preset.py
618 lines (459 loc) · 20.3 KB
/
preset.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
#
# 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 bpy
from vb30.plugins import PLUGINS, PLUGINS_ID
from vb30.vray_tools.VRaySceneParser import ParseVrscene
from vb30.lib import VRayStream
from vb30.lib import ExportUtils, SysUtils, LibUtils, PathUtils, BlenderUtils
from vb30.nodes import importing as NodesImport
from vb30.nodes import tools as NodesTools
from vb30.nodes import export as NodesExport
from vb30.nodes import sockets as NodesSockets
from vb30.ui import classes
from vb30 import debug
import _vray_for_blender
###### ######## ######## ######## #### ## ## ###### ######
## ## ## ## ## ## ### ## ## ## ## ##
## ## ## ## ## #### ## ## ##
###### ###### ## ## ## ## ## ## ## #### ######
## ## ## ## ## ## #### ## ## ##
## ## ## ## ## ## ## ### ## ## ## ##
###### ######## ## ## #### ## ## ###### ######
PresetTypePlugins = {
'gi' : (
'SettingsGI',
'SettingsLightCache',
'SettingsDMCGI',
'SettingsIrradianceMap',
'SphericalHarmonicsRenderer',
'SphericalHarmonicsExporter',
),
'im' : (
'SettingsIrradianceMap',
),
}
## ## ######## ## ## ## ## ######## ### ###### ########
### ### ## ### ## ## ## ## ## ## ## ## ## ##
#### #### ## #### ## ## ## ## ## ## ## ## ##
## ### ## ###### ## ## ## ## ## ######## ## ## ###### ######
## ## ## ## #### ## ## ## ## ######### ## ##
## ## ## ## ### ## ## ## ## ## ## ## ## ##
## ## ######## ## ## ####### ######## ## ## ###### ########
class VRayPresetMenuBase(bpy.types.Menu):
preset_operator = None
def path_menu(self, searchpaths):
filter_ext = lambda ext: ext.lower() == ".vrscene"
if not searchpaths:
self.layout.label("* No Preset Data *")
files = []
for directory in searchpaths:
files.extend([(f, os.path.join(directory, f))
for f in os.listdir(directory)
if (not f.startswith("."))
if ((filter_ext is None) or
(filter_ext(os.path.splitext(f)[1])))
])
files.sort()
for f, filepath in files:
props = self.layout.operator(self.preset_operator,
text=bpy.path.display_name(f),
translate=False)
props.filepath = filepath
props.menu_idname = self.bl_idname
def draw(self, context):
presetPaths = {
os.path.join(SysUtils.GetExporterPath(), "presets", self.preset_subdir),
os.path.join(SysUtils.GetUserConfigDir(), "presets", self.preset_subdir),
}
paths = []
for path in presetPaths:
if os.path.exists(path):
paths.append(path)
if hasattr(self, 'menu_item_save') and self.menu_item_save:
op = self.layout.operator('vray.export_asset', text="Save Selected", icon='FILE_TICK')
op.asset_type = self.preset_subdir
self.layout.separator()
self.path_menu(paths)
### ######## ######## ## ## ##
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ####
## ## ######## ######## ## ##
######### ## ## ## ##
## ## ## ## ## ##
## ## ## ## ######## ##
class VRayPresetExecuteBase:
filepath = bpy.props.StringProperty(
subtype = 'FILE_PATH',
options = {'SKIP_SAVE'},
)
menu_idname = bpy.props.StringProperty(
name = "Menu ID Name",
description = "ID name of the menu this was called from",
options = {'SKIP_SAVE'},
)
def _execute(self, context, vrsceneDict):
return {'FINISHED'}
def execute(self, context):
filepath = self.filepath
# Сhange the menu title to the most recently chosen option
preset_class = getattr(bpy.types, self.menu_idname)
preset_class.bl_label = bpy.path.display_name(os.path.basename(filepath))
# Apply preset
#
debug.PrintInfo('Applying preset from "%s"' % filepath)
vrsceneDict = ParseVrscene(filepath)
return self._execute(context, vrsceneDict)
class VRayPresetApply(VRayPresetExecuteBase, bpy.types.Operator):
bl_idname = "vray.preset_apply"
bl_label = "Apply V-Ray preset"
def _execute(self, context, vrsceneDict):
for pluginDesc in vrsceneDict:
pluginID = pluginDesc['ID']
pluginName = pluginDesc['Name']
pluginAttrs = pluginDesc['Attributes']
pluginModule = PLUGINS_ID.get(pluginID)
if pluginModule is None:
continue
if not hasattr(context.scene.vray, pluginID):
# TODO: Add warning?
continue
propGroup = getattr(context.scene.vray, pluginID)
for attrName in pluginAttrs:
attrDesc = NodesImport.getParamDesc(pluginModule.PluginParams, attrName)
if attrDesc is None:
continue
attrValue = pluginAttrs[attrName]
if attrDesc['type'] == 'ENUM':
attrValue = str(attrValue)
setattr(propGroup, attrName, attrValue)
return {'FINISHED'}
class VRayPresetApplyNode(VRayPresetExecuteBase, bpy.types.Operator):
bl_idname = "vray.preset_node_apply"
bl_label = "Apply V-Ray node preset"
def _execute(self, context, vrsceneDict):
space = context.space_data
ntree = space.edit_tree
# Deselect before import
NodesTools.deselectNodes(ntree)
# Get fake asset node
assetDesc = NodesImport.getPluginByType(vrsceneDict, "Asset")
assetType = assetDesc['Name']
# Import template
lastNode = None
if assetType == 'Material':
maName = assetDesc['Attributes']['material']
maDesc = NodesImport.getPluginByName(vrsceneDict, maName)
maNode = NodesImport.createNode(ntree, None, vrsceneDict, maDesc)
# NOTE: Depends on how user saves the preset
if 'Material' not in maNode.outputs:
lastNode = maNode
else:
outputNode = ntree.nodes.new('VRayNodeOutputMaterial')
ntree.links.new(maNode.outputs['Material'], outputNode.inputs['Material'])
lastNode = outputNode
elif assetType == 'Texture':
texName = assetDesc['Attributes']['texture']
texDesc = NodesImport.getPluginByName(vrsceneDict, texName)
lastNode = NodesImport.createNode(ntree, None, vrsceneDict, texDesc)
elif assetType == 'Channel':
chanNames = assetDesc['Attributes']['channel']
if len(chanNames):
channContainer = ntree.nodes.new('VRayNodeRenderChannels')
for i,chanName in enumerate(reversed(chanNames)):
chanSockName = "Channel %i" % i
NodesSockets.AddInput(channContainer, 'VRaySocketRenderChannel', chanSockName)
chanDesc = NodesImport.getPluginByName(vrsceneDict, chanName)
chanNode = NodesImport.createNode(ntree, None, vrsceneDict, chanDesc)
ntree.links.new(chanNode.outputs['Channel'], channContainer.inputs[chanSockName])
lastNode = channContainer
if lastNode:
NodesTools.rearrangeTree(ntree, lastNode)
return {'FINISHED'}
### ######## ######## ## ######## ######## ## ## ####### ## ## ########
## ## ## ## ## ## ## ## ## ## ### ### ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## #### #### ## ## ## ## ##
## ## ## ## ## ## ## ######## ###### ## ### ## ## ## ## ## ######
######### ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
## ## ######## ######## ## ## ## ######## ## ## ####### ### ########
class VRayPresetAddBase:
name = bpy.props.StringProperty(
name = "Name",
description = "Name of the preset, used to make the path name",
maxlen = 64,
options = {'SKIP_SAVE'},
default = ""
)
preset_menu = bpy.props.StringProperty(
name = "Preset Menu",
options = {'SKIP_SAVE'},
)
remove_active = bpy.props.BoolProperty(
default = False,
options = {'HIDDEN', 'SKIP_SAVE'},
)
def draw(self, context):
self.layout.prop(self, 'name')
def invoke(self, context, event):
if not self.remove_active:
wm = context.window_manager
return wm.invoke_props_dialog(self)
else:
return self.execute(context)
def execute(self, context):
preset_menu_class = getattr(bpy.types, self.preset_menu)
preset_type = preset_menu_class.preset_subdir
presetSubdir = PathUtils.CreateDirectory(os.path.join(SysUtils.GetUserConfigDir(), "presets"))
exportPath = PathUtils.CreateDirectory(os.path.join(presetSubdir, preset_type))
presetName = preset_menu_class.bl_label if self.remove_active else self.name
fileName = "%s.vrscene" % LibUtils.CleanString(bpy.path.display_name(presetName))
outputFilepath = os.path.normpath(os.path.join(exportPath, fileName))
if self.remove_active:
# NOTE: Remove function is locked to user config directory,
# so system settings are safe
debug.PrintInfo('Removing preset file: "%s"' % outputFilepath)
if not os.path.exists(outputFilepath):
return {'CANCELLED'}
try:
os.remove(outputFilepath)
except:
debug.PrintError('Error removing preset file: "%s"!' % outputFilepath)
# Set default menu name
preset_menu_class.bl_label = bpy.path.display_name(preset_type)
else:
bus = {
'output' : VRayStream.VRaySimplePluginExporter(outputFilepath),
'scene' : context.scene,
'camera' : context.scene.camera,
'preview' : False,
}
pluginPresetIDs = None
if preset_type == 'global':
pluginPresetIDs = (pID for pID in sorted(PLUGINS['SETTINGS']))
else:
pluginPresetIDs = PresetTypePlugins[preset_type]
for pluginID in pluginPresetIDs:
pluginModule = PLUGINS_ID.get(pluginID)
if pluginModule is None:
continue
if not hasattr(context.scene.vray, pluginID):
continue
propGroup = getattr(context.scene.vray, pluginID)
ExportUtils.WritePlugin(bus, pluginModule, pluginID.lower(), propGroup, {})
return {'FINISHED'}
## ## ######## ## ## ## ##
### ### ## ### ## ## ##
#### #### ## #### ## ## ##
## ### ## ###### ## ## ## ## ##
## ## ## ## #### ## ##
## ## ## ## ### ## ##
## ## ######## ## ## #######
class VRAY_MT_PresetGlobal(VRayPresetMenuBase):
bl_label = "Global Presets"
preset_subdir = "global"
preset_operator = "vray.preset_apply"
class VRayPresetAddGlobal(VRayPresetAddBase, bpy.types.Operator):
bl_idname = "vray.preset_add_global"
bl_label = "Save Global Preset"
bl_description = "Save global preset"
def __init__(self):
self.preset_menu = 'VRAY_MT_PresetGlobal'
class VRAY_MT_GI(VRayPresetMenuBase):
bl_label = "GI Presets"
preset_subdir = "gi"
preset_operator = "vray.preset_apply"
class VRayPresetAddGI(VRayPresetAddBase, bpy.types.Operator):
bl_idname = "vray.preset_add_gi"
bl_label = "Save GI Preset"
bl_description = "Save GI preset"
def __init__(self):
self.preset_menu = 'VRAY_MT_GI'
class VRAY_MT_GI_IM(VRayPresetMenuBase):
bl_label = "Irradiance Map Presets"
preset_subdir = "im"
preset_operator = "vray.preset_apply"
class VRayPresetMenuNodeBase(VRayPresetMenuBase):
preset_operator = "vray.preset_node_apply"
class VRAY_MT_NodeTexture(VRayPresetMenuNodeBase):
bl_label = "Texture"
preset_subdir = "texture"
menu_item_save = True
class VRAY_MT_NodeMaterial(VRayPresetMenuNodeBase):
bl_label = "Material"
preset_subdir = "material"
menu_item_save = True
class VRAY_MT_RenderChannel(VRayPresetMenuNodeBase):
bl_label = "Render Channel"
preset_subdir = "channel"
menu_item_save = True
class VRAY_MT_NodeEffects(VRayPresetMenuNodeBase):
bl_label = "Effects"
preset_subdir = "effects"
menu_item_save = True
######## ######## ### ## ##
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ##
## ## ######## ## ## ## ## ##
## ## ## ## ######### ## ## ##
## ## ## ## ## ## ## ## ##
######## ## ## ## ## ### ###
def PresetBase(layout, menuName, menuOperator):
menuClass = getattr(bpy.types, menuName)
row = layout.row(align=True)
row.menu(menuName, text=menuClass.bl_label)
row.operator(menuOperator, text="", icon="ZOOMIN")
row.operator(menuOperator, text="", icon="ZOOMOUT").remove_active = True
layout.separator()
def WidgetPresetGlobal(layout):
PresetBase(layout, 'VRAY_MT_PresetGlobal', "vray.preset_add_global")
def WidgetPresetGI(layout):
PresetBase(layout, 'VRAY_MT_GI', "vray.preset_add_gi")
class VRAY_MT_TemplatesSubMenus(bpy.types.Menu):
bl_idname = "VRAY_MT_TemplatesSubMenus"
bl_label = "Templates"
def draw(self, context):
self.layout.menu("VRAY_MT_NodeMaterial", icon='MATERIAL')
self.layout.menu("VRAY_MT_NodeTexture", icon='TEXTURE')
self.layout.menu("VRAY_MT_RenderChannel", icon='SCENE_DATA')
self.layout.menu("VRAY_MT_NodeEffects", icon='GHOST_ENABLED')
def VRayNodeTemplatesMenu(self, context):
if classes.PollTreeType(None, context):
self.layout.menu("VRAY_MT_TemplatesSubMenus", icon='NODETREE')
### ###### ###### ######## ########
## ## ## ## ## ## ## ##
## ## ## ## ## ##
## ## ###### ###### ###### ##
######### ## ## ## ##
## ## ## ## ## ## ## ##
## ## ###### ###### ######## ##
class VRayNodeExportAsset(bpy.types.Operator):
bl_idname = "vray.export_asset"
bl_label = "Export Node Asset"
bl_description = ""
asset_name = bpy.props.StringProperty(
name = "Name",
description = "Name of the asset, used to make the path name",
maxlen = 64,
options = {'SKIP_SAVE'},
default = ""
)
asset_type = bpy.props.EnumProperty(
name = "Type",
description = "Type of the asset, used to make menu name",
items = (
('texture', "Texture", ""),
('material', "Material", ""),
('channel', "Render Channel", ""),
('effects', "Effects", ""),
),
default = "texture"
)
def draw(self, context):
self.layout.prop(self, 'asset_name')
self.layout.prop(self, 'asset_type')
def invoke(self, context, event):
if len(context.selected_nodes) > 1:
self.report({'ERROR'}, "Select only one last node!")
return {'CANCELLED'}
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
if not self.asset_name:
self.report({'ERROR'}, "Asset name is not set!")
return {'CANCELLED'}
presetsPath = PathUtils.CreateDirectory(os.path.join(SysUtils.GetUserConfigDir(), "presets"))
userNodeAssetPath = PathUtils.CreateDirectory(os.path.join(presetsPath, self.asset_type))
fileName = "%s.vrscene" % LibUtils.CleanString(bpy.path.display_name(self.asset_name))
outputFilepath = os.path.normpath(os.path.join(userNodeAssetPath, fileName))
# Create exporter (output)
o = VRayStream.VRaySimplePluginExporter(outputFilepath)
exporter = _vray_for_blender.init(
engine = 0,
context = bpy.context.as_pointer(),
scene = bpy.context.scene.as_pointer(),
data = bpy.data.as_pointer(),
mainFile = o.output,
objectFile = o.output,
envFile = o.output,
geometryFile = o.output,
lightsFile = o.output,
materialFile = o.output,
textureFile = o.output,
drSharePath = "",
)
# Get selected nodes
ntree = context.space_data.edit_tree
selectedNode = context.selected_nodes[0]
if selectedNode.bl_idname == 'VRayNodeRenderChannels':
pluginNames = []
for inSock in selectedNode.inputs:
pluginNames.append(NodesExport.WriteConnectedNode(None, ntree, inSock))
pluginName = "List(%s)" % ",".join(pluginNames)
else:
if selectedNode.bl_idname == 'VRayNodeOutputMaterial':
selectedNode = NodesExport.GetConnectedNode(ntree, selectedNode.inputs['Material'])
pluginName = _vray_for_blender.exportNode(
ntree.as_pointer(),
selectedNode.as_pointer(),
None
)
# Write fake Asset node
o.set('MAIN', 'Asset', self.asset_type.capitalize())
o.writeHeader()
o.writeAttibute(self.asset_type, pluginName)
o.writeFooter()
o.done()
_vray_for_blender.exit(exporter)
return {'FINISHED'}
######## ######## ###### #### ###### ######## ######## ### ######## #### ####### ## ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ##
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## #### ##
######## ###### ## #### ## ###### ## ######## ## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ## ## ## ######### ## ## ## ## ## ####
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ###
## ## ######## ###### #### ###### ## ## ## ## ## ## #### ####### ## ##
def GetRegClasses():
return (
VRayPresetAddGlobal,
VRAY_MT_PresetGlobal,
VRayPresetAddGI,
VRAY_MT_GI,
VRAY_MT_GI_IM,
VRayPresetApply,
VRayPresetApplyNode,
VRAY_MT_NodeEffects,
VRAY_MT_RenderChannel,
VRAY_MT_NodeTexture,
VRAY_MT_NodeMaterial,
VRAY_MT_TemplatesSubMenus,
VRayNodeExportAsset,
)
def register():
for regClass in GetRegClasses():
bpy.utils.register_class(regClass)
bpy.types.NODE_MT_add.append(VRayNodeTemplatesMenu)
def unregister():
for regClass in GetRegClasses():
bpy.utils.unregister_class(regClass)
bpy.types.NODE_MT_add.remove(VRayNodeTemplatesMenu)