-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathexport.py
2376 lines (1747 loc) · 80.5 KB
/
export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ##### BEGIN MIT LICENSE BLOCK #####
#
# Copyright (c) 2011 Matt Ebb
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
# ##### END MIT LICENSE BLOCK #####
import bpy
import bpy_types
import math
import os
import time
import subprocess
import mathutils
from mathutils import Matrix, Vector, Quaternion
from . import bl_info
from .util import bpy_newer_257
from .util import BlenderVersionError
from .util import rib, rib_path, rib_ob_bounds
from .util import make_frame_path
from .util import init_env
from .util import get_sequence_path
from .util import user_path
from .util import get_path_list_converted
from .util import path_list_convert
addon_version = bl_info['version']
# global dictionaries
from .shader_parameters import exclude_lamp_params
# helper functions for parameters
from .shader_parameters import shaderparameters_from_class
from .shader_parameters import path_win_to_unixy
from .shader_parameters import rna_to_shaderparameters
from .shader_parameters import get_parameters_shaderinfo
from .shader_parameters import rna_types_initialise
from .shader_parameters import shader_recompile
from .shader_parameters import shader_requires_shadowmap
from .shader_parameters import tex_source_path
from .shader_parameters import tex_optimised_path
from .nodes import export_shader_nodetree
class RPass:
def __init__(self, scene, objects=[], paths={}, type="", motion_blur=False):
self.type = type if type != "" else "default"
self.objects = objects
self.archives = archive_objects(scene)
self.paths = paths
self.do_render = True
self.options = []
self.emit_photons = False
self.resolution = []
self.motion_blur = scene.renderman.motion_blur
self.surface_shaders = True
self.displacement_shaders = True
self.interior_shaders = True
self.atmosphere_shaders = True
self.light_shaders = True
if type == 'shadowmap':
self.interior_shaders = False
self.atmosphere_shaders = False
self.light_shaders = False
# force motion blur on for shadows for the time being
self.motion_blur = True
elif type == 'ptc_indirect':
self.motion_blur = False
'''
def print_options(self, file):
#if self.type == 'ptc_indirect':
# file.write('Option "user" "uniform string delight_renderpass_type" "bakePass" \n')
#file.write('Option "user" "uniform string delight_renderpass_name" "%s" \n' % self.type)
pass
'''
# ------------- Texture optimisation -------------
# 3Delight specific tdlmake stuff
def make_optimised_texture_3dl(tex, texture_optimiser, srcpath, optpath):
rm = tex.renderman
print("Optimising Texture: %s --> %s" % (tex.name, optpath))
cmd = [texture_optimiser]
if rm.format == 'ENV_LATLONG':
cmd.append('-envlatl')
# Wrapping
cmd.append('-smode')
cmd.append(rm.wrap_s)
cmd.append('-tmode')
cmd.append(rm.wrap_t)
if rm.flip_s:
cmd.append('-flips')
if rm.flip_t:
cmd.append('-flipt')
# Filtering
if rm.filter_type != 'DEFAULT':
cmd.append('-filter')
cmd.append(rm.filter_type)
if rm.filter_type in ('catmull-rom', 'bessel') and rm.filter_window != 'DEFAULT':
cmd.append('-window')
cmd.append(rm.filter_window)
if rm.filter_width_s != 1.0:
cmd.append('-sfilterwidth')
cmd.append(str(rm.filter_width_s))
if rm.filter_width_t != 1.0:
cmd.append('-tfilterwidth')
cmd.append(str(rm.filter_width_t))
if (rm.filter_blur != 1.0):
cmd.append('-blur')
cmd.append(str(rm.filter_blur))
# Colour space
if rm.input_color_space == 'GAMMA':
cmd.append('-gamma')
cmd.append(str(rm.input_gamma))
else:
cmd.append('-colorspace')
cmd.append(rm.input_color_space)
# Colour depth
if rm.output_color_depth == 'UBYTE':
cmd.append('-ubyte')
elif rm.output_color_depth == 'SBYTE':
cmd.append('-sbyte')
elif rm.output_color_depth == 'USHORT':
cmd.append('-ushort')
elif rm.output_color_depth == 'SSHORT':
cmd.append('-sshort')
elif rm.output_color_depth == 'FLOAT':
cmd.append('-float')
if rm.output_compression == 'LZW':
cmd.append('-lzw')
elif rm.output_compression == 'ZIP':
cmd.append('-zip')
elif rm.output_compression == 'PACKBITS':
cmd.append('-packbits')
elif rm.output_compression == 'LOGLUV' and rm.output_color_depth == 'FLOAT':
cmd.append('-logluv')
elif rm.output_compression == 'UNCOMPRESSED':
cmd.append('-c-')
# add preview
cmd.append('-preview')
cmd.append('256')
# Filenames
cmd.append(srcpath)
cmd.append(optpath)
proc = subprocess.Popen(cmd).wait()
def auto_optimise_textures(paths, scene):
rm_textures = [tex for tex in bpy.data.textures if tex.renderman.auto_generate_texture == True]
for tex in rm_textures:
rm = tex.renderman
srcpath = tex_source_path(tex, scene.frame_current)
optpath = tex_optimised_path(tex, scene.frame_current)
generate = False
if not os.path.exists(srcpath):
continue
if rm.generate_if_nonexistent and not os.path.exists(optpath):
generate = True
elif rm.generate_if_older and os.path.getmtime(optpath) < os.path.getmtime(srcpath):
generate = True
if not generate: continue
make_optimised_texture_3dl(tex, paths['texture_optimiser'], srcpath, optpath)
# ------------- Filtering -------------
def is_visible_layer(scene, ob):
for i in range(len(scene.layers)):
if scene.layers[i] == True and ob.layers[i] == True:
return True
return False
def is_renderable(scene, ob):
return (is_visible_layer(scene, ob) and not ob.hide_render)
# and not ob.type in ('CAMERA', 'ARMATURE', 'LATTICE'))
def renderable_objects(scene):
return [ob for ob in scene.objects if is_renderable(scene, ob)]
# ------------- Archive Helpers -------------
# Generate an automatic path to write an archive when 'Export as Archive' is enabled
def auto_archive_path(paths, objects, create_folder=False):
filename = objects[0].name + ".rib"
if os.getenv("ARCHIVE") != None:
archive_dir = os.getenv("ARCHIVE")
else:
archive_dir = os.path.join(paths['export_dir'], "archives")
if create_folder and not os.path.exists(archive_dir):
os.mkdir(archive_dir)
return os.path.join(archive_dir, filename)
def archive_objects(scene):
archive_obs = []
for ob in renderable_objects(scene):
# explicitly set
if ob.renderman.export_archive == True:
archive_obs.append(ob)
# particle instances
for psys in ob.particle_systems:
rm = psys.settings.renderman
if rm.particle_type == 'OBJECT':
try:
ob = bpy.data.objects[rm.particle_instance_object]
archive_obs.append(ob)
except:
pass
# dupli objects (TODO)
return archive_obs
# ------------- Data Access Helpers -------------
def get_subframes(segs):
return [i * 1.0/segs for i in range(segs+1)]
def get_ob_subframes(scene, ob):
if ob.renderman.motion_segments_override:
return get_subframes(ob.renderman.motion_segments)
else:
return get_subframes(scene.renderman.motion_segments)
def is_subd_last(ob):
return ob.modifiers and ob.modifiers[len(ob.modifiers)-1].type == 'SUBSURF'
def is_subd_displace_last(ob):
if len(ob.modifiers) < 2: return False
return (ob.modifiers[len(ob.modifiers)-2].type == 'SUBSURF' and
ob.modifiers[len(ob.modifiers)-1].type == 'DISPLACE')
def is_subdmesh(ob):
return (is_subd_last(ob) or is_subd_displace_last(ob))
# XXX do this better, perhaps by hooking into modifier type data in RNA?
# Currently assumes too much is deforming when it isn't
def is_deforming(ob):
deforming_modifiers = ['ARMATURE', 'CAST', 'CLOTH', 'CURVE', 'DISPLACE', 'HOOK', 'LATTICE', 'MESH_DEFORM',
'SHRINKWRAP', 'SIMPLE_DEFORM', 'SMOOTH', 'WAVE', 'SOFT_BODY', 'SURFACE']
if ob.modifiers:
# special cases for auto subd/displace detection
if len(ob.modifiers) == 1 and is_subd_last(ob):
return False
if len(ob.modifiers) == 2 and is_subd_displace_last(ob):
return False
for mod in ob.modifiers:
if mod.type in deforming_modifiers:
return True
return False
# handle special case of fluid sim a bit differently
def is_deforming_fluid(ob):
if ob.modifiers:
mod = ob.modifiers[len(ob.modifiers)-1]
if mod.type == 'FLUID_SIMULATION' and mod.settings.type == 'DOMAIN':
return True
def psys_motion_name(ob, psys):
return ob.name + "_" + psys.name
# ------------- Geometry Access -------------
def get_strands(ob, psys):
nstrands = 0
nvertices = []
P = []
for particle in psys.particles:
hair = particle.hair_keys
nvertices += [len(hair)+2]
i=0
# hair keys are stored as local offsets from the
# particle location (on surface)
for key in particle.hair_keys:
P.extend( key.co )
# double up start and end points
if i == 0 or i == len(hair)-1:
P.extend( key.co )
i += 1
return (nvertices, P)
# only export particles that are alive,
# or have been born since the last frame
def valid_particle(pa, cfra):
return not (pa.birth_time > cfra or (pa.birth_time + pa.die_time) < cfra)
def get_particles(scene, ob, psys):
P = []
rot = []
width = []
cfra = scene.frame_current
for pa in [p for p in psys.particles if valid_particle(p, cfra)]:
P.extend( pa.location )
rot.extend( pa.rotation )
if pa.alive_state != 'ALIVE':
width.append(0.0)
else:
width.append(pa.size)
return (P, rot, width)
# Mesh data access
def get_mesh(mesh):
nverts = []
verts = []
P = []
for v in mesh.vertices:
P.extend( v.co )
for p in mesh.polygons:
nverts.append( p.loop_total )
verts.extend( p.vertices )
return (nverts, verts, P)
def get_mesh_vertex_N(mesh):
N = []
for v in mesh.vertices:
N.extend( v.normal )
return N
# requires facevertex interpolation
def get_mesh_uv(mesh, name=""):
uvs = []
if name == "":
uv_loop_layer = mesh.uv_layers.active
else:
# assuming uv loop layers and uv textures share identical indices
idx = mesh.uv_textures.keys().index(name)
uv_loop_layer = mesh.uv_layers[idx]
if uv_loop_layer == None:
return None
for uvloop in uv_loop_layer.data:
uvs.append( uvloop.uv.x )
uvs.append( 1.0 - uvloop.uv.y ) # renderman expects UVs flipped vertically from blender
return uvs
# requires facevertex interpolation
def get_mesh_vcol(mesh, name=""):
vcol_layer = mesh.vertex_colors[name] if name != "" else mesh.vertex_colors.active
cols = []
if vcol_layer == None:
return None
for vcloop in vcol_layer.data:
cols.extend( vcloop.color )
return cols
# requires per-vertex interpolation
def get_mesh_vgroup(ob, mesh, name=""):
vgroup = ob.vertex_groups[name] if name != "" else ob.vertex_groups.active
weights = []
if vgroup == None:
return None
for v in mesh.vertices:
if len(v.groups) == 0:
weights.append(0.0)
else:
weights.extend( [g.weight for g in v.groups if g.group == vgroup.index ] )
return weights
def export_primvars(file, ob, geo, interpolation=""):
if ob.type != 'MESH':
return
rm = ob.data.renderman
interpolation = 'facevertex' if interpolation == '' else interpolation
# default hard-coded prim vars
if rm.export_smooth_normals and ob.renderman.primitive in ('AUTO', 'POLYGON_MESH', 'SUBDIVISION_MESH'):
N = get_mesh_vertex_N(geo)
if N is not None:
file.write(' "varying normal N" %s \n' % rib(N) )
if rm.export_default_uv:
uvs = get_mesh_uv(geo)
if uvs is not None:
file.write(' "%s float[2] st" %s \n' % (interpolation, rib(uvs)) )
if rm.export_default_vcol:
vcols = get_mesh_vcol(geo)
if vcols is not None:
file.write(' "%s color Cs" %s \n' % (interpolation, rib(vcols)) )
# custom prim vars
for p in rm.prim_vars:
if p.data_source == 'VERTEX_COLOR':
vcols = get_mesh_vcol(geo, p.data_name)
if vcols is not None:
file.write(' "%s color %s" %s \n' % (interpolation, p.name, rib(vcols)) )
elif p.data_source == 'UV_TEXTURE':
uvs = get_mesh_uv(geo, p.data_name)
if uvs is not None:
file.write(' "%s float[2] %s" %s \n' % (interpolation, p.name, rib(uvs)) )
elif p.data_source == 'VERTEX_GROUP':
weights = get_mesh_vgroup(ob, geo, p.data_name)
if weights is not None:
file.write(' "vertex float %s" %s \n' % (p.name, rib(weights)) )
def export_primvars_particle(file, scene, psys):
rm = psys.settings.renderman
cfra = scene.frame_current
for p in rm.prim_vars:
vars = []
if p.data_source in ('VELOCITY', 'ANGULAR_VELOCITY'):
if p.data_source == 'VELOCITY':
for pa in [p for p in psys.particles if valid_particle(p, cfra)]:
vars.extend ( pa.velocity )
elif p.data_source == 'ANGULAR_VELOCITY':
for pa in [p for p in psys.particles if valid_particle(p, cfra)]:
vars.extend ( pa.angular_velocity )
file.write(' "varying float[3] %s" %s \n' % (p.name, rib(vars)) )
elif p.data_source in ('SIZE', 'AGE', 'BIRTH_TIME', 'DIE_TIME', 'LIFE_TIME'):
if p.data_source == 'SIZE':
for pa in [p for p in psys.particles if valid_particle(p, cfra)]:
vars.append ( pa.size )
elif p.data_source == 'AGE':
for pa in [p for p in psys.particles if valid_particle(p, cfra)]:
vars.append ( (cfra - pa.birth_time) / pa.lifetime )
elif p.data_source == 'BIRTH_TIME':
for pa in [p for p in psys.particles if valid_particle(p, cfra)]:
vars.append ( pa.birth_time )
elif p.data_source == 'DIE_TIME':
for pa in [p for p in psys.particles if valid_particle(p, cfra)]:
vars.append ( pa.die_time )
elif p.data_source == 'LIFE_TIME':
for pa in [p for p in psys.particles if valid_particle(p, cfra)]:
vars.append ( pa.lifetime )
file.write(' "varying float %s" %s \n' % (p.name, rib(vars)) )
def get_fluid_mesh(scene, ob):
subframe = scene.frame_subframe
fluidmod = [m for m in ob.modifiers if m.type == 'FLUID_SIMULATION'][0]
fluidmeshverts = fluidmod.settings.fluid_mesh_vertices
mesh = create_mesh(scene, ob)
(nverts, verts, P) = get_mesh(mesh)
bpy.data.meshes.remove(mesh)
# use fluid vertex velocity vectors to reconstruct moving points
P = [P[i] + fluidmeshverts[int(i/3)].velocity[i%3] * subframe * 0.5 for i in range(len(P))]
return (nverts, verts, P)
def get_subd_creases(mesh):
creases = []
# only do creases 1 edge at a time for now, detecting chains might be tricky..
for e in mesh.edges:
if e.crease > 0.0:
creases.append( (e.vertices[0], e.vertices[1], e.crease*e.crease * 10) ) # squared, to match blender appareance better : range 0 - 10 (infinitely sharp)
return creases
def create_mesh(scene, ob, matrix=None):
# 2 special cases to ignore:
# subsurf last or subsurf 2nd last +displace last
#if is_subd_last(ob):
# ob.modifiers[len(ob.modifiers)-1].show_render = False
#elif is_subd_displace_last(ob):
# ob.modifiers[len(ob.modifiers)-2].show_render = False
# ob.modifiers[len(ob.modifiers)-1].show_render = False
mesh = ob.to_mesh(scene, True, 'RENDER')
if matrix != None:
mesh.transform(matrix)
return mesh
# RIB Exporting functions
def shadowmap_path(scene, ob):
if ob.type != 'LAMP': return ''
rm = ob.data.renderman
path = user_path(rm.path_shadow_map, scene=scene, ob=ob)
if rm.shadow_transparent:
path += '.dsm'
else:
path += '.tdl'
return path
def export_light(rpass, scene, file, ob):
lamp = ob.data
rm = lamp.renderman
m = ob.parent.matrix_world * ob.matrix_local if ob.parent else ob.matrix_world
loc = m.to_translation()
lvec = loc - (m.to_quaternion() * mathutils.Vector((0,0,1)))
params = []
file.write(' AttributeBegin\n')
file.write(' TransformBegin\n')
file.write(' Transform %s\n' % rib(m))
if rm.emit_photons and lamp.type in ('SPOT', 'POINT', 'SUN'):
file.write(' Attribute "light" "string emitphotons" [ "on" ]\n' )
# BBM addition begin
# export light coshaders
'''
file.write('\n ## Light Co-shaders\n')
for cosh_item in rm.coshaders.items():
coshader_handle = cosh_item[0]
coshader_name = cosh_item[1].shader_shaders.active
file.write(' Shader "%s" \n "%s"\n' % (coshader_name, coshader_handle) )
parameterlist = rna_to_shaderparameters(scene, cosh_item[1], 'shader')
for sp in parameterlist:
if sp.is_array:
file.write(' "%s %s[%d]" %s\n' % (sp.data_type, sp.name, len(sp.value), rib(sp.value,is_cosh_array=True)))
else:
file.write(' "%s %s" %s\n' % (sp.data_type, sp.name, rib(sp.value)))
file.write('\n ## Light shader\n')
'''
# BBM addition end
'''
# user defined light shader
if rm.nodetree == '' and rm.light_shaders.active != '':
file.write(' LightSource "%s" ' % rm.light_shaders.active)
params = rna_to_shaderparameters(scene, rm, 'light')
# automatic shaders per blender lamp type
elif lamp.type == 'POINT':
file.write(' LightSource "pointlight" \n')
name, params = get_parameters_shaderinfo(rpass.paths['shader'], 'pointlight', 'light')
elif lamp.type == 'SPOT':
if rm.shadow_method == 'SHADOW_MAP':
file.write(' LightSource "shadowspot" \n')
name, params = get_parameters_shaderinfo(rpass.paths['shader'], 'shadowspot', 'light')
else:
file.write(' LightSource "spotlight" \n')
name, params = get_parameters_shaderinfo(rpass.paths['shader'], 'spotlight', 'light')
elif lamp.type == 'SUN':
file.write(' LightSource "h_distantshadow" \n')
name, params = get_parameters_shaderinfo(rpass.paths['shader'], 'h_distantshadow', 'light')
elif lamp.type == 'HEMI':
file.write(' LightSource "ambientlight" \n')
name, params = get_parameters_shaderinfo(rpass.paths['shader'], 'ambientlight', 'light')
# file.write(' "%s" \n' % ob.name) # handle
'''
if rm.nodetree != '':
print('export nodetree ')
export_shader_nodetree(file, scene, lamp, output_node='OutputLightShaderNode', handle=ob.name)
params = []
# parameter list
for sp in params:
# special exceptions since they're not an actual properties on lamp datablock
if sp.name == 'from':
value = rib(loc)
elif sp.name == 'to':
value = rib(lvec)
elif sp.meta == 'shadow_map_path':
if shader_requires_shadowmap(scene, rm, 'light'):
path = rib_path(shadowmap_path(scene, ob))
print(path)
file.write(' "string %s" "%s" \n' % (sp.name, path))
''' XXX old shaders
elif sp.name == 'coneangle':
if hasattr(lamp, "spot_size"):
coneangle = lamp.spot_size / 2.0
value = rib(coneangle)
else:
value = rib(45)
elif sp.name in ('shadowmap', 'shadowname', 'shadowmapname'):
if rm.shadow_method == 'SHADOW_MAP':
shadowmapname = rib_path(shadowmap_path(scene, ob))
file.write(' "string %s" "%s" \n' % (sp.name, shadowmapname))
elif rm.shadow_method == 'RAYTRACED':
file.write(' "string %s" ["raytrace"] \n' % sp.name)
continue
'''
# more exceptions, use blender's built in equivalent parameters (eg. spot size)
elif sp.name in exclude_lamp_params.keys():
value = rib(getattr(lamp, exclude_lamp_params[sp.name]))
# otherwise use the stored raw shader parameters
else:
value = rib(sp.value)
# BBM addition begin
if sp.is_array:
file.write(' "%s %s[%d]" %s\n' % (sp.data_type, sp.name, len(sp.value), rib(sp.value,is_cosh_array=True)))
else:
# BBM addition end
file.write(' "%s %s" %s\n' % (sp.data_type, sp.name, value))
file.write(' TransformEnd\n')
file.write(' AttributeEnd\n')
file.write(' Illuminate "%s" %d \n' % (ob.name, rm.illuminates_by_default))
file.write(' \n')
def export_sss_bake(file, rpass, mat):
rm = mat.renderman
if not rm.sss_do_bake: return
group = mat.name if rm.sss_group == "" else rm.sss_group
file.write(' \n')
file.write(' Attribute "visibility" "string subsurface" "%s" \n\n' % group)
file.write(' Attribute "subsurface" \n')
file.write(' "color meanfreepath" %s \n' % rib(rm.sss_meanfreepath))
if rm.sss_use_reflectance:
file.write(' "color reflectance" %s \n' % rib(rm.sss_reflectance))
file.write(' "refractionindex" %s \n' % rm.sss_ior)
file.write(' "shadingrate" %s \n' % rm.sss_shadingrate)
file.write(' "scale" %s \n' % rm.sss_scale)
file.write(' \n')
def export_material(file, rpass, scene, mat):
export_sss_bake(file, rpass, mat)
export_shader_init(file, rpass, mat)
rm = mat.renderman
if rm.nodetree != '':
file.write(' Color %s\n' % rib(mat.diffuse_color))
file.write(' Opacity %s\n' % rib([mat.alpha for i in range(3)]))
if rm.displacementbound > 0.0:
file.write(' Attribute "displacementbound" "sphere" %f \n' % rm.displacementbound)
export_shader_nodetree(file, scene, mat)
else:
#export_shader(file, scene, rpass, mat, 'shader') # BBM addition
export_shader(file, scene, rpass, mat, 'surface')
export_shader(file, scene, rpass, mat, 'displacement')
export_shader(file, scene, rpass, mat, 'interior')
'''
# allow overriding with global world atmosphere shader
if mat.renderman.inherit_world_atmosphere:
export_shader(file, scene, rpass, scene.world, 'atmosphere')
else:
export_shader(file, scene, rpass, mat, 'atmosphere')
'''
#file.write(' Shader "brdf_specular" "brdf_specular" \n')
#file.write(' Shader "btdf_specular" "btdf_specular" \n')
def export_strands(file, rpass, scene, ob, motion):
for psys in ob.particle_systems:
pname = psys_motion_name(ob, psys)
rm = psys.settings.renderman
if psys.settings.type != 'HAIR':
continue
# use 'material_id' index to decide which material
if ob.data.materials and len(ob.data.materials) > 0:
if ob.data.materials[rm.material_id-1] != None:
mat = ob.data.materials[rm.material_id-1]
export_material(file, rpass, scene, mat)
motion_blur = pname in motion['deformation']
if motion_blur:
file.write(' MotionBegin %s\n' % rib(get_ob_subframes(scene, ob)))
samples = motion['deformation'][pname]
else:
samples = [get_strands(ob, psys)]
for nverts, P in samples:
file.write(' Basis "catmull-rom" 1 "catmull-rom" 1\n')
file.write(' Curves "cubic" \n')
file.write(' %s \n' % rib(nverts))
file.write(' "nonperiodic" \n')
file.write(' "P" %s \n' % rib(P))
file.write(' "constantwidth" [ %f ] \n' % rm.width)
if motion_blur:
file.write(' MotionEnd\n')
def geometry_source_rib(scene, ob):
rm = ob.renderman
anim = rm.archive_anim_settings
blender_frame = scene.frame_current
rib = ""
if rm.geometry_source == 'ARCHIVE':
archive_path = rib_path(get_sequence_path(rm.path_archive, blender_frame, anim))
rib = ' ReadArchive "%s"\n' % archive_path
else:
if rm.procedural_bounds == 'MANUAL':
min = rm.procedural_bounds_min
max = rm.procedural_bounds_max
bounds = [min[0], max[0], min[1], max[1], min[2], max[2]]
else:
bounds = rib_ob_bounds(ob.bound_box)
if rm.geometry_source == 'DELAYED_LOAD_ARCHIVE':
archive_path = rib_path(get_sequence_path(rm.path_archive, blender_frame, anim))
rib = ' Procedural "DelayedReadArchive" ["%s"] %s\n' \
% (archive_path, rib(bounds))
elif rm.geometry_source == 'PROCEDURAL_RUN_PROGRAM':
path_runprogram = rib_path(rm.path_runprogram)
rib = ' Procedural "RunProgram" ["%s" "%s"] %s\n' \
% (path_runprogram, rm.path_runprogram_args, rib(bounds))
elif rm.geometry_source == 'DYNAMIC_LOAD_DSO':
path_dso = rib_path(rm.path_dso)
rib = ' Procedural "DynamicLoad" ["%s" "%s"] %s\n' \
% (path_dso, rm.path_dso_initial_data, rib(bounds))
return rib
def export_particle_instances(file, rpass, scene, ob, psys, motion):
rm = psys.settings.renderman
pname = psys_motion_name(ob, psys)
# Precalculate archive path for object instances
try:
instance_ob = bpy.data.objects[rm.particle_instance_object]
except:
return
if instance_ob.renderman.geometry_source == 'BLENDER_SCENE_DATA':
archive_path = rib_path(auto_archive_path(rpass.paths, [instance_ob]))
instance_geometry_rib = ' ReadArchive "%s"\n' % archive_path
else:
instance_geometry_rib = geometry_source_rib(scene, instance_ob)
motion_blur = pname in motion['deformation']
cfra = scene.frame_current
for i in range(len( [ p for p in psys.particles if valid_particle(p, cfra) ] )):
if motion_blur:
file.write(' MotionBegin %s\n' % rib(get_ob_subframes(scene, ob)))
samples = motion['deformation'][pname]
else:
samples = [get_particles(scene, ob, psys)]
for P, rot, width in samples:
loc = Vector((P[i*3+0], P[i*3+1], P[i*3+2]))
rot = Quaternion((rot[i*4+0], rot[i*4+1], rot[i*4+2], rot[i*4+3]))
mtx = Matrix.Translation(loc) * rot.to_matrix().to_4x4() * Matrix.Scale(width[i], 4)
file.write(' Transform %s \n' % rib(mtx))
if motion_blur:
file.write(' MotionEnd\n')
file.write( instance_geometry_rib )
def export_particle_points(file, scene, ob, psys, motion):
rm = psys.settings.renderman
pname = psys_motion_name(ob, psys)
motion_blur = pname in motion['deformation']
if motion_blur:
file.write(' MotionBegin %s\n' % rib(get_ob_subframes(scene, ob)))
samples = motion['deformation'][pname]
else:
samples = [get_particles(scene, ob, psys)]
for P, rot, width in samples:
file.write(' Points \n')
file.write(' "P" %s \n' % rib(P))
file.write(' "uniform string type" [ "%s" ] \n' % rm.particle_type)
if rm.constant_width:
file.write(' "constantwidth" [%f] \n' % rm.width)
elif rm.export_default_size:
file.write(' "varying float width" %s \n' % rib(width))
export_primvars_particle(file, scene, psys)
if motion_blur:
file.write(' MotionEnd\n')
def export_particles(file, rpass, scene, ob, motion):
for psys in ob.particle_systems:
rm = psys.settings.renderman
pname = psys_motion_name(ob, psys)
if psys.settings.type != 'EMITTER':
continue
file.write(' AttributeBegin\n')
file.write(' Attribute "identifier" "name" [ "%s" ]\n' % pname)
# use 'material_id' index to decide which material
if ob.data.materials:
if ob.data.materials[rm.material_id-1] != None:
mat = ob.data.materials[rm.material_id-1]
export_material(file, rpass, scene, mat)
# Write object instances or points
if rm.particle_type == 'OBJECT':
export_particle_instances(file, rpass, scene, ob, psys, motion)
else:
export_particle_points(file, scene, ob, psys, motion)
file.write('AttributeEnd\n\n')
def export_scene_lights(file, rpass, scene):
if not rpass.light_shaders: return
file.write(' ## Lights \n\n')
for ob in [o for o in rpass.objects if o.type == 'LAMP']:
export_light(rpass, scene, file, ob)
file.write(' \n')
def export_shader_init(file, rpass, mat):
rm = mat.renderman
if rpass.emit_photons:
file.write(' Attribute "photon" "string shadingmodel" "%s" \n' % rm.photon_shadingmodel)
def export_shader(file, scene, rpass, idblock, shader_type):
rm = idblock.renderman
file.write('\n # %s\n' % shader_type ) # BBM addition
parameterlist = rna_to_shaderparameters(scene, rm, shader_type)
for sp in parameterlist:
print('sp.meta[\'data_type\'] %s ' % sp.meta['data_type'])
if sp.meta['data_type'] == 'shader':
if sp.value == 'null':
continue
if sp.is_array:
collection = sp.value
for item in collection:
file.write(' Shader "%s" "%s"\n' % (item.value, idblock.name+'_'+sp.name) )
else:
file.write(' Shader "%s" "%s"\n' % (sp.value, sp.value)) #idblock.name+'_'+sp.name) )
if shader_type == 'surface':
mat = idblock
if rm.surface_shaders.active == '' or not rpass.surface_shaders: return
file.write(' Color %s\n' % rib(mat.diffuse_color))
file.write(' Opacity %s\n' % rib([mat.alpha for i in range(3)]))
file.write(' Surface "%s" \n' % rm.surface_shaders.active)
elif shader_type == 'displacement':
if rm.displacement_shaders.active == '' or not rpass.displacement_shaders: return
if rm.displacementbound > 0.0:
file.write(' Attribute "displacementbound" "sphere" %f \n' % rm.displacementbound)
file.write(' Displacement "%s" \n' % rm.displacement_shaders.active)
elif shader_type == 'interior':
if rm.interior_shaders.active == '' or not rpass.interior_shaders: return
file.write(' Interior "%s" \n' % rm.interior_shaders.active)
elif shader_type == 'atmosphere':
if rpass.type == 'ptc_indirect':
# use a relative path to pointcloud_dir to work around windows paths issue -
# bake3d() doesn't seem to like baking windows absolute paths
relpath = os.path.relpath( rpass.paths["gi_ptc_bake_path"], start=rpass.paths["export_dir"] )
file.write(' # ptc_file is exported as relative path to export directory \n')
file.write(' # to work around a problem with windows absolute paths in bake3d() \n')
file.write(' Atmosphere "vol_ptcbake" \n')
file.write(' "string ptc_file" "%s" \n' % relpath)