File size: 37,907 Bytes
03a982d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import fcntl
import stat
import json
import math
import subprocess
import fnmatch
import re
import tempfile
import shutil

OUTDIR = os.getcwd()
CAMFILE = OUTDIR + "/json/cameras/shotCam.json"
LIGHTFILE = OUTDIR + "/json/lights/lights.json"

#OBJ2PBRT = shutil.which("obj2pbrt")
#OCEAN2PBRT = shutil.which("obj2pbrt")
TMPDIR = '/tmp'
IDENTITY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]

# DISNEY: Solids has scattrDistR,G and B; Thin has flatness and diffTrans
# PBRT: difftrans and flatness are specific to the BSDF, enabled by the thin parameter
DIS2PBRT = {
    "metallic": "float metallic",
    "roughness": "float roughness",
    "anisotropic": "float anisotropic",
    "specularTint": "float speculartint",
    "sheen": "float sheen",
    "sheenTint": "float sheentint",
    "clearcoat" : "float clearcoat",
    "clearcoatGloss": "float clearcoatgloss",
    "specTrans" : "float spectrans",
    "ior" : "float eta"
}


def mon2lin(color):
    return [math.pow(color[0], 2.2), math.pow(color[1], 2.2), math.pow(color[2], 2.2)]


def readJsonFile(jsonFile):
    """
    Wrapper script to import json file as dict.
    :param jsonFile: file as string
    :return: dict
    """
    with open(os.path.abspath(jsonFile), "r") as jf:
        jsonDict = json.load(jf)
    return jsonDict if jsonDict else {}


def writeEmbreeEcsFile():
    ecsFilePath = os.path.abspath("embree/island.ecs")
    ecsFile = os.open(ecsFilePath, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
    fcntl.flock(ecsFile, fcntl.LOCK_EX)
    try:
        f = os.fdopen(ecsFile,'w')
    except IOError as (errno, strerror):
        print "Couldn't open %s for write: %s" % (ecsFilePath, strerror)
        return False

    f.writelines('-i island_flattened.xml --instancing flattened --verbose 1\n')

    os.fsync(ecsFile)
    fcntl.flock(ecsFile, fcntl.LOCK_UN)
    f.close()
    os.chmod(ecsFilePath, stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IWGRP|stat.S_IROTH|stat.S_IWOTH)
    print "Writing main ECS file: ", ecsFilePath
    
    return True

def writeMainPbrtFile(outPbrtFile="embree/island.xml"):
    """
    Create the main XML file with has all the camera, lights and geometry includes and WorldBegin/End
    :param outPbrtFile: file to create
    :return: success
    """
    pbrtFile = os.path.abspath(outPbrtFile)
    pbrtFileObj = os.open(pbrtFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_EX)

    try:
        f = os.fdopen(pbrtFileObj,'w')
    except IOError as (errno, strerror):
        print "Couldn't open %s for write: %s" % (pbrtFile, strerror)
        return False

    f.writelines('<?xml version="1.0"?>\n\n')
    f.writelines('<scene>\n')
    #f.writelines(createDefaultHeader())

    with open(CAMFILE, "r") as cf:
      cameraDict = json.load(cf)

    f.writelines('  <PerspectiveCamera name="default"\n')
    f.writelines('    from="%f %f %f"\n' % (cameraDict["eye"][0], cameraDict["eye"][1], cameraDict["eye"][2]))
    f.writelines('    to="%f %f %f"\n' % (cameraDict["look"][0], cameraDict["look"][1], cameraDict["look"][2]))
    f.writelines('    up="%f %f %f"\n' % (cameraDict["up"][0], cameraDict["up"][1], cameraDict["up"][2]))
    f.writelines('    fov="%f"\n' % cameraDict["fov"])
    f.writelines('  />\n')
    f.writelines('\n')
    
    f.writelines('  <DirectionalLight>\n')
    f.writelines('    <AffineSpace>\n')
    f.writelines('      1 0 -1 0\n')
    f.writelines('      0 0 -1 0\n')
    f.writelines('      0 1  0 0\n')
    f.writelines('    </AffineSpace>\n')
    f.writelines('    <E>10 10 10</E>\n')
    f.writelines('  </DirectionalLight>\n')
    f.writelines('\n')

    f.writelines('  <AmbientLight>\n')
    f.writelines('    <AffineSpace>\n')
    f.writelines('      1 0 0 0\n')
    f.writelines('      0 1 0 0\n')
    f.writelines('      0 0 1 0\n')
    f.writelines('    </AffineSpace>\n')
    f.writelines('    <L>0.5 0.5 0.5</L>\n')
    f.writelines('  </AmbientLight>\n')
    f.writelines('\n')

    # recommended after much testing...
    #f.writelines('Sampler "sobol" "integer pixelsamples" 1024\n')  # went up to 8192
    #f.writelines('Integrator "volpath" "integer maxdepth" [10]\n')

    #f.writelines(createLights(LIGHTFILE))
    f.writelines(insertElements())
    f.writelines('</scene>\n\n')

    os.fsync(pbrtFileObj)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_UN)
    f.close()
    os.chmod(pbrtFile, stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IWGRP|stat.S_IROTH|stat.S_IWOTH)
    print "Writing main XML file: ", pbrtFile
    return True


def createDefaultHeader():
    header = 'Scale -1 1 1\n'
    header += 'Film "image" "string filename" ["island.png"]\n'
    header += '     "integer xresolution" [1024] "integer yresolution" [429]\n'
    header += '     "float maxsampleluminance" 10\n\n'
    return header


def createCamera(f,jsonCamFile):
    """
    :param: camFile: Json file with raw camera data from maya.
    :return:
    """
                
    #cameraTxt = "LookAt %f %f %f" % (cameraDict["eye"][0], cameraDict["eye"][1], cameraDict["eye"][2])
    #cameraTxt += " %f %f %f" % (cameraDict["look"][0], cameraDict["look"][1], cameraDict["look"][2])
    #cameraTxt += " %f %f %f\n" % (cameraDict["up"][0], cameraDict["up"][1], cameraDict["up"][2])
    #cameraTxt += """Camera "perspective" "float fov" [%f]\n""" % cameraDict["fov"]
    #cameraTxt += """                     "float screenwindow" [%f %f %f %f]\n""" % (cameraDict["screenwindow"][0], cameraDict["screenwindow"][1], cameraDict["screenwindow"][2], cameraDict["screenwindow"][3])
    #cameraTxt += """                     "float frameaspectratio" [%f]\n""" % cameraDict["ratio"]
    #cameraTxt += """                     "float lensradius" [%f]\n""" % cameraDict["lensRadius"]
    #cameraTxt += """                     "float focaldistance" [%f]\n""" % cameraDict["centerOfInterest"]
    #return cameraTxt


def createLights(jsonLightsFile):
    lightText = ""

    with open(os.path.abspath(jsonLightsFile), "r") as lf:
        lightsDict = json.load(lf)

    for lightName, lightDict in lightsDict.iteritems():
        # common values
        lc = mon2lin(lightDict["color"])
        le = math.pow(2, lightDict["exposure"])
        rot = lightDict["rotation"]
        loc = lightDict["location"]
        tm = lightDict["translationMatrix"]

        if lightDict["type"] == "dome":
            #mapname = lightDict["map"]
            mapname = os.path.basename(lightDict["envmapCamera"])
            lightText += "# skydome: %s\n" % lightName
            lightText += "AttributeBegin\n"
            #lightText += "    Translate %f %f %f\n" % (loc[0], loc[1], loc[2])
            #lightText += "    Rotate %f %f %f 1\n" % (rot[0], rot[1], rot[2])
            # This is what it took to line up our HDR image. YMMV
            lightText += "    Scale -1 1 1\n"
            lightText += "    Rotate 90 -1 0 0\n"
            lightText += "    Rotate 65 0 0 1\n"
            lightText += """    LightSource "infinite" "string mapname" "%s"\n""" % os.path.join("..","textures",mapname)
            lightText += "AttributeEnd\n\n"

        elif lightDict["type"] == "distant":
            lightText += "# distant light: %s\n" % lightName
            lightText += "AttributeBegin\n"
            lightText += "    Translate %f %f %f\n" % (loc[0], loc[1], loc[2])
            lightText += "    Rotate %f %f %f 1\n" % (rot[0], rot[1], rot[2])
            lightText += """    LightSource "distant" "point from" [ 0.0 0.0 0.0]\n"""
            lightText += """    "point to" [ 0.0 0.0 -1.0 ]\n"""
            lightText += """    "rgb L" [ %f %f %f ]\n""" % (lc[0], lc[1], lc[2])
            lightText += "AttributeEnd\n\n"

        elif lightDict["type"] == "quad":
            hw = float(lightDict["width"]) / 2
            hh = float(lightDict["height"]) / 2
            lightText += "# quad light w/ simple triangle mesh: %s\n" % lightName
            lightText += "AttributeBegin\n"
            lightText += """    AreaLightSource "diffuse" "rgb L" [%f %f %f]\n""" % (lc[0]*le, lc[1]*le, lc[2]*le)
            lightText += "    Transform [%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f]\n" % tuple(tm)
            lightText += """    Shape "trianglemesh"  "integer indices" [0 2 1 0 3 2] "float alpha" 0\n"""
            lightText += """    "point P"[%f  %f 0    %f  %f 0    %f %f 0     %f %f 0]\n""" % (hw, hh, -hw, hh, -hw, -hh, hw, -hh)
            lightText += "AttributeEnd\n\n"

        else:
            print "Light type not supported:", lightName, lightDict["type"]

    return lightText


def createDefaultTail():
    return "\nWorldEnd\n"


def createGeometryHeader(gp, jsonFile, materialsName):
    gp.write("<!--Created from %s-->\n" % jsonFile)
    gp.write("AttributeBegin\n")
    gp.write("    Include \"%s\"\n\n" % materialsName)


def createGeometryTail(gp):
    gp.write("AttributeEnd\n")


def insertElements():
    elementText = ''
    for element in os.listdir(os.path.abspath("obj")):
        elementText += '  <xml src="%s"/>\n'% (element + ".xml")
    return elementText


def geomToPbrt(objFile, pbrtFile, matFile, root_folder):
    """
    That's an obj file and converts it to pbrt using 'obj2pbrt'. In addition, to support a binding hack, it comments
    out the MakeNamedMaterial lines left by 'obj2pbrt'. We are just using the bindings, since OBJ format does not
    support our materials.
    [NEW] Added hardcoded materials where a mesh-specific texture is required.
    :param objFile:
    :param pbrtFile:
    :param bakeTextures:
    :return:
    """
    # Make sure we have absolute path to both files.
    fpObjFile = os.path.abspath(objFile)
    fpPbrtFile = os.path.abspath(pbrtFile)
    # in order to find the mtl file, we need to be >in< the obj directory.
    dwd = os.getcwd()
    os.chdir(os.path.dirname(fpObjFile))
    print "Creating pbrt geometry from", objFile

    xmlFile = "xml/"+objFile[4:-4]+".xml"
    
    f = open(pbrtFile,'w')
    f.writelines('<?xml version="1.0"?>\n\n')
    f.writelines('<scene>\n');
    if matFile != "": f.writelines('  <xml src="'+root_folder+'%s"/>\n' % matFile);
    f.writelines('  <xml src="'+root_folder+'%s"/>\n' % xmlFile);
    f.writelines('</scene>\n');
    f.close()

    os.chdir(dwd)

    # if we're using textures, we need to insert the texture overrides directly into the geom.
    #insertTextureOverrides(fpPbrtFile, matFile)

def writeMaterial(f, matName, valueDict):

    f.writelines('<assign type="material" id="%s">\n'%matName)
    f.writelines('  <material>\n')
    f.writelines('    <code>"OBJ"</code>\n')
    f.writelines('    <parameters>\n')

    if 'baseColor' in valueDict:
        color = mon2lin(valueDict['baseColor'])
        f.writelines('      <float3 name="Kd">%1.2f %1.2f %1.2f</float3>\n'% (color[0], color[1],color[2]))
    else:
        print "WARNING WARNING WARNING: Can not find 'baseColor' in", element, matName
        f.writelines('      <float3 name="Kd">1 0 0</float3>\n')

    #for val in valueDict:
    #    if val in DIS2PBRT:
    #        # common parameters
    #        f.writelines('    "%s" [%1.2f]\n' % (DIS2PBRT[val], valueDict[val]))

    #if "type" in valueDict and valueDict["type"] == "thin":
    #    f.writelines('    "bool thin" ["true"]\n')
    #    f.writelines('    "float difftrans" [%1.2f]\n' % valueDict["diffTrans"])
    #    f.writelines('    "float flatness" [%1.2f]\n\n' % valueDict['flatness'])
    #else: # solid
    #    f.writelines('    "rgb scatterdistance" [%1.2f %1.2f %1.2f]\n\n' % (valueDict["scatterDistance"][0],
    #                                                                 valueDict["scatterDistance"][1],
    #                                                                 valueDict["scatterDistance"][2]))

    f.writelines('    </parameters>\n')
    f.writelines('  </material>\n\n')
    f.writelines('</assign>\n\n')
        
def matToPbrt(element, matFile, pbrtFile):
    """
    Take a json file and convert it into a pbrt "Named" material file which will be used for reference.
    :param element: Element name -- Used for error codes and top-of-file comments.
    :param matFile: Json material file to be parsed.
    :param pbrtFile: Pbrt material file to be output.
    :return: None
    """
    with open(matFile, "r") as jf:
        matDict = json.load(jf)

    if not matDict:
        print "ERROR: Unable to parse matfile: ", matFile
        return
    print "Converting ", matFile

    pbrtFileObj = os.open(pbrtFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_EX)

    try:
        f = os.fdopen(pbrtFileObj,'w')
    except IOError as (errno, strerror):
        print "Couldn't open %s for write: %s" % (pbrtFile, strerror)
        return

    f.writelines('<?xml version="1.0"?>\n\n')
    f.writelines("<!--Materials for %s-->\n"%element)
    f.writelines("<!--Converted from %s-->\n\n"%matFile)

    f.writelines('<scene>\n\n')

    f.writelines('<assign type="material" id="default">\n')
    f.writelines('  <material>\n')
    f.writelines('    <code>"OBJ"</code>\n')
    f.writelines('    <parameters>\n')
    f.writelines('      <float3 name="Kd">1 0 0</float3>\n')
    f.writelines('    </parameters>\n')
    f.writelines('  </material>\n\n')
    f.writelines('</assign>\n\n')

    for matName, valueDict in matDict.iteritems():

        if matName == "hidden":
            f.writelines('<assign type="material" id="%s">\n'%matName)
            f.writelines('  <material>\n')
            f.writelines('    <code>"OBJ"</code>\n')
            f.writelines('    <parameters>\n')
            f.writelines('      <float3 name="Kd">1 0 0</float3>\n')
            f.writelines('    </parameters>\n')
            f.writelines('  </material>\n')
            f.writelines('</assign>\n\n')
            continue

        writeMaterial(f,matName,valueDict)
        
    f.writelines('</scene>\n\n')
        
    os.fsync(pbrtFileObj)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_UN)
    f.close()
    os.chmod(pbrtFile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
    print "Writing XML file: ", pbrtFile


def jsonPrimitiveToPbrt(primType, element, primitiveName, iDict):
    if primType == "curve":
        return jsonPrimitiveCurveToPbrt(element, primitiveName, iDict)
    elif primType == "element":
        return jsonPrimitiveElementToPbrt(element, primitiveName, iDict)
    else:
        return jsonPrimitiveArchiveToPbrt(element, primitiveName, primitiveJsonFile=iDict["jsonFile"])


def jsonPrimitiveArchiveToPbrt(element, primitiveName, primitiveJsonFile):
    """
    Create the pbrt reference file for a particular primitive instance. This will be a raw list
    of all the instances that make up this particular primitive with the transformations.
    :param element: The name of the element (used the build paths)
    :param primitiveName: The name of the primitive description we're converting.
    :param primitiveJsonFile: Json file containing the archives and all instances of them.
    :return: The 'short' path name, which is the partial path without the pbrt directory in front
    """
    fileName = os.path.basename(primitiveJsonFile)[:-4] + "xml" if primitiveJsonFile else primitiveName + ".xml"
    pbrtPrimitiveFile = os.path.join("embree", element, primitiveName, fileName)
    root = "../../"
    shortFileName = os.path.join(element, primitiveName, fileName)
    iDict = readJsonFile(primitiveJsonFile)

    pbrtFileObj = os.open(pbrtPrimitiveFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_EX)

    try:
        f = os.fdopen(pbrtFileObj,'w')
    except IOError as (errno, strerror):
        print "Couldn't open %s for write: %s" % (pbrtPrimitiveFile, strerror)
        return

    f.writelines('<?xml version="1.0"?>\n\n')
    f.writelines("<!--Converted from %s-->\n\n" % primitiveJsonFile)
    f.writelines('<scene>\n\n')

    for archive, primitiveDict in iDict.iteritems():
        # Create one file per archive xgen Description
        archiveName = os.path.basename(archive)[:-4]
        for instanceName, transform in primitiveDict.iteritems():
            writeInstance(f, root, archiveName, instanceName, transform, concat=True)

    f.writelines('</scene>\n\n')
    os.fsync(pbrtFileObj)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_UN)
    f.close()
    os.chmod(pbrtPrimitiveFile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
    print "Writing primitive archive XML file: ", pbrtPrimitiveFile
    return shortFileName


def jsonPrimitiveCurveToPbrt(element, primitiveName, cDict):
    """
    Convert json primitive curve to pbrt curve
    :param element: used to find json files
    :param primitiveName: used to find json files
    :param cDict: dictionary of all curves
    :return:
    """
    primitiveJsonFile = cDict["jsonFile"]
    matFile = os.path.join("json", element, "materials.json")
    fileName = os.path.basename(primitiveJsonFile)[:-4] + "xml" if primitiveJsonFile else primitiveName + ".xml"
    pbrtPrimitiveFile = os.path.join("embree", element, primitiveName, fileName)
    shortFileName = os.path.join(element, primitiveName, fileName)
    curveList = readJsonFile(primitiveJsonFile)

    pbrtFileObj = os.open(pbrtPrimitiveFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_EX)

    try:
        f = os.fdopen(pbrtFileObj,'w')
    except IOError as (errno, strerror):
        print "Couldn't open %s for write: %s" % (pbrtPrimitiveFile, strerror)
        return

    f.writelines('<?xml version="1.0"?>\n\n')
    f.writelines("<!--Converted from %s-->\n\n" % primitiveJsonFile)
    f.writelines('<scene>\n\n')

    f.writelines('  <Curves type="round" basis="bspline">\n\n')

    # Curves don't inherit materials like the OBJs so, so we need to insert the material at the top.
    mat = findMaterial(primitiveName, readJsonFile(matFile))
    if mat:
      f.writelines('    <material id="%s"/>\n'%mat)
    else:
      f.writelines('    <material id="default"/>\n')
      print "WARNING: curve has no material"
    f.writelines('\n')
    
    i=0
    f.writelines('    <indices>\n')
    for curve in curveList:
      i=writeCurveIndices(f, i, curve, cDict)
      f.writelines('\n')
    f.writelines('    </indices>\n')
    f.writelines('\n')

    f.writelines('    <positions>\n')
    for curve in curveList:
      writeCurveVertices(f, curve, cDict)
      f.writelines('\n')
    f.writelines('    </positions>\n')

    f.writelines('  </Curves>\n\n')

    f.writelines('</scene>\n\n')
    os.fsync(pbrtFileObj)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_UN)
    f.close()
    os.chmod(pbrtPrimitiveFile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
    print "Writing primitive curve XML file: ", pbrtPrimitiveFile
    return shortFileName


def jsonPrimitiveElementToPbrt(element, primitiveName, eDict):
    """
    The primitive being instanced around is a copy of another element, which could include its own primitive data.
    :param element: The name of the element (used the build paths)
    :param primitiveName: The name of the primitive description we're converting.
    :param eDict: Dictionary includes reference element, possible variant and the source json file with the transforms
    by variant. "base" variant is assumed to the the top level element definition.
    :return: The 'short' path name, which is the partial path without the pbrt directory in front
    """
    primitiveJsonFile = eDict["jsonFile"]
    fileName = os.path.basename(primitiveJsonFile)[:-4] + "xml" if primitiveJsonFile else primitiveName + ".xml"
    pbrtPrimitiveFile = os.path.join("embree", element, primitiveName, fileName)
    root = "../../"
    shortFileName = os.path.join(element, primitiveName, fileName)
    iDict = readJsonFile(primitiveJsonFile)

    # we also need to load the source element json file so we can access it's information for inclusion.
    sourceJsonFile = os.path.abspath(os.path.join("json", eDict["element"],  eDict["element"] + ".json"))
    sourceDict = readJsonFile(sourceJsonFile)

    pbrtFileObj = os.open(pbrtPrimitiveFile, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_EX)

    try:
        f = os.fdopen(pbrtFileObj,'w')
    except IOError as (errno, strerror):
        print "Couldn't open %s for write: %s" % (pbrtPrimitiveFile, strerror)
        return

    f.writelines('<?xml version="1.0"?>\n\n')
    f.writelines("<!--Converted from %s-->\n\n" % primitiveJsonFile)
    f.writelines('<scene>\n\n')

    for variant, primitiveDict in iDict.iteritems(): # each variant is the key to a dictionary of instance name/transforms
        inclFileList = []  # TODO:
        if variant == "base":
            instanceRef = os.path.basename(sourceDict["geomObjFile"])[:-4]
            if "instancedPrimitiveJsonFiles" in sourceDict:
                for primitiveName, ipDict in sourceDict["instancedPrimitiveJsonFiles"].iteritems():
                    fileName = os.path.basename(ipDict["jsonFile"])[:-4] + "xml"
                    inclFileList.append(os.path.join(eDict["element"], primitiveName, fileName))

        elif variant in sourceDict["variants"]:
            instanceRef = os.path.basename(sourceDict["variants"][variant]["geomObjFile"])[:-4]
            if "instancedPrimitiveJsonFiles" in sourceDict["variants"][variant]:
                for primitiveName, ipDict in sourceDict["variants"][variant]["instancedPrimitiveJsonFiles"].iteritems():
                    fileName = os.path.basename(ipDict["jsonFile"])[:-4] + "xml"
                    inclFileList.append(os.path.join(eDict["element"], primitiveName, fileName))

        else:
            print "Error: Problems finding variant %s for element %s\n" %(variant, eDict["element"])
            sys.exit()

        for instanceName, transform in primitiveDict.iteritems():
            writeInstance(f, root, instanceRef, instanceName, transform, inclFileList=inclFileList, concat=True)

    f.writelines('</scene>\n\n')
    os.fsync(pbrtFileObj)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_UN)
    f.close()
    os.chmod(pbrtPrimitiveFile, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
    print "Writing primitive archive embree file: ", pbrtPrimitiveFile
    return shortFileName


def writeObject(f, objectName, pbrtGeometryFile, matFile=None):
    """
    Create named object definition around single geometry file.
    :param f:
    :param objectName: What to call the object
    :param pbrtGeometryFile: The file being referenced
    :param matFile: Some variants need a separate matFile, include override for that here.
    :return:
    """
    f.writelines('    <assign type="scene" id="%s">\n' % objectName)
    f.writelines('        <xml src="../%s"/>\n' % pbrtGeometryFile)
    f.writelines('    </assign>\n')

def writeInstance(f, root, objectName, instanceName, xfm, inclFileList=[], concat=False):
    """
    Create instances of previously named object
    :param f:
    :param objectName: Name of object
    :param instanceName: Name of instance (for reference back to json file)
    :param transform: Transform
    :param inclFileList: For primitive files that need to be referenced after object
    :param concat: Use ConcatTransform to properly inherit final instance position of copies of elements.
    :return:
    """
    f.writelines('<!-- %s -->\n' % instanceName)
    f.writelines('<Transform>\n')
    if concat:
        f.writelines('  <AffineSpace>%f %f %f %f %f %f %f %f %f %f %f %f</AffineSpace>\n' % (xfm[0],xfm[4],xfm[8],xfm[12],xfm[1],xfm[5],xfm[9],xfm[13],xfm[2],xfm[6],xfm[10],xfm[14]) ) 
    else:
        f.writelines('  <AffineSpace>%f %f %f %f %f %f %f %f %f %f %f %f</AffineSpace>\n' % (xfm[0],xfm[4],xfm[8],xfm[12],xfm[1],xfm[5],xfm[9],xfm[13],xfm[2],xfm[6],xfm[10],xfm[14]) ) 
    f.writelines('  <ref id="%s"/>\n' % objectName)
    for inclFile in inclFileList:
        f.writelines('  <xml src="' + root + '%s"/>\n' % inclFile)
    f.writelines('</Transform>\n\n')


def writeCurveIndices(f, i, curve, cDict):
    for j in range(0, len(curve)-1):
      f.writelines('      %i\n'%i)
      i = i+1
    i = i+3
    return i

def writeCurveVertices(f, curve, cDict):
    """
    Updated to work with new pbrt bspline format.
    :param f:
    :return:
    """
    w0 = cDict["widthRoot"]
    w1 = cDict["widthTip"]
    # TODO: Should be change cylinder to flat?
    # ctype = "cylinder" if cDict["faceCamera"] else "flat"

    i=0
    f.writelines('      %f %f %f %f\n' % (curve[0][0], curve[0][1], curve[0][2], w0))  # stack
    for p in curve:
      x=i/float(len(curve)-1)
      w=(1-x)*w0 + x*w1
      f.writelines('      %f %f %f %f\n' % (p[0], p[1], p[2], w))
      i=i+1
    f.writelines('      %f %f %f %f\n' % (curve[-1][0], curve[-1][1], curve[-1][2], w1))  # stack

def writeObjectFile(element, pbrtOutGeometry, instCopies = None, primDict = None):
    """
    Keep all the object references in a single file to keep the main element file readable. Because we can't embed
    instances in pbrt, we need to include all references here, both the main element and all primitives or variants
    it might be referring too.
    :param element: main element
    :param pbrtOutGeometry: main element geometry file
    :param instCopies: if an instanced copy has its own geometry, we need to reference that as well.
    :param primDict: And all primitives used by the geometry.
    :return:
    """
    # put all the primitive objects at the top of the file since we can embed objects within objects.
    if not pbrtOutGeometry and not primDict and not instCopies:
        return ""

    shortName = os.path.join(element, "objects.xml")
    pbrtOutObjects = os.path.abspath(os.path.join("embree", shortName))

    pbrtFileObj = os.open(pbrtOutObjects, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_EX)

    try:
        f = os.fdopen(pbrtFileObj,'w')
    except IOError as (errno, strerror):
        print "Couldn't open %s for write: %s" % (pbrtOutObjects, strerror)
        return

    f.writelines('<?xml version="1.0"?>\n\n')
    f.writelines('<scene>\n\n')
    
    # there are all the archives used by the primitive description
    if primDict:
        for primName, primInstanceDict in primDict.iteritems(): # for each xgen description
            f.writelines("\n<!-- archives used by %s -->\n" % primName)
            # for each archives used by this description
            if "archives" in primInstanceDict:
                for archive in primInstanceDict["archives"]:
                    # the object reference is the archive or geo file base name
                    archiveName = os.path.basename(archive)[:-4]
                    # the object file is the archive or geo file base name + _geometry
                    pbrtArchiveFile = os.path.join(element, primName, archiveName + "_geometry.xml")
                    # if this is type 'element', we need to drop in the element's material file #HACKY
                    matFile = os.path.join(primInstanceDict["element"], "materials.xml")\
                        if primInstanceDict["type"] == "element" else ""
                    writeObject(f, archiveName, pbrtArchiveFile, matFile=matFile)

    # This is the main element object
    if pbrtOutGeometry:
        f.writelines("\n<!-- main object for the element: %s -->\n" % element)
        writeObject(f, element, pbrtOutGeometry)

    # Instanced copies here.
    if instCopies:
        f.writelines("\n<!-- instanced copies of the element with their own geometry -->\n")
        for instanceName, icDict in instCopies.iteritems():
            if "geomObjFile" in icDict and icDict["geomObjFile"]:
                # convert obj name to pbrt location
                pbrtOutFile =icDict["geomObjFile"][4:-4] + "_geometry.xml"
                writeObject(f, icDict["name"], pbrtOutFile)

    f.writelines('</scene>\n\n')
    os.fsync(pbrtFileObj)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_UN)
    f.close()
    os.chmod(pbrtOutObjects, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
    print "Writing XML file: ", pbrtOutObjects
    return shortName


def writeMainElement(pbrtOutElement, pbrtOutMaterials, pbrtOutGeometry, primitiveFiles, sourceJsonFile=""):

    jDict = readJsonFile(sourceJsonFile)
    element = jDict["name"]
    transform = jDict["transformMatrix"]

    pbrtFileObj = os.open(pbrtOutElement, os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_EX)

    try:
        f = os.fdopen(pbrtFileObj,'w')
    except IOError as (errno, strerror):
        print "Couldn't open %s for write: %s" % (pbrtOutElement, strerror)
        return

    f.writelines('<?xml version="1.0"?>\n\n')
    f.writelines('<scene>\n\n')
    
    if sourceJsonFile:
        f.writelines("<!-- Converted from %s -->\n\n" % sourceJsonFile)

    ################################## Materials

    #f.writelines("AttributeBegin\n\n")
    f.writelines("<xml src=\"../%s\"/>\n" % pbrtOutMaterials)

    ################################## Objects
    # Notes about objects: Since pbrt can instance within an instance, only _geometry files should be defined as objects.
    # For simplicity, lets defines all _geometry files as objects.

    primitiveDict = jDict["instancedPrimitiveJsonFiles"] if "instancedPrimitiveJsonFiles" in jDict else {}
    instancedCopiesDict = jDict["instancedCopies"] if "instancedCopies" in jDict else {}

    objectsFile = writeObjectFile(element, pbrtOutGeometry, instCopies=instancedCopiesDict, primDict=primitiveDict)
    if objectsFile:
        f.writelines("<xml src=\"../%s\"/>\n" % objectsFile)

    ############################ Main elements

    f.writelines("\n<!-- Main element -->\n\n")

    primList = []  # a list of all the primitive names associated with this element, i.e. xgBonsai, xgGrass, xgShells
    primFileList = []  # a list of the primitive files, i.e. isBayCedarA1/xgBonsai/isBayCedarA1_xgBonsai.xml
    if primitiveDict:
        for primName, primInstanceDict in primitiveDict.iteritems():
            primList.append(primName)
            primFileList.append(os.path.join(element, primName, element + "_" + primName + ".xml"))

    # create the default instance (we have at least once instance of every element)
    writeInstance(f, "../", element, element, transform, primFileList)

    if instancedCopiesDict:
        f.writelines("\n<!-- Instanced copies of main element -->\n\n")

        # all the other instances of the element and all primitive files used by element
        # Note: we can't instance the primitive files themsevles, because they contain references to instanced archives.
        for instance, icDict in instancedCopiesDict.iteritems():
            primOverrides = icDict["instancedPrimitiveJsonFiles"].keys() if "instancedPrimitiveJsonFiles" in icDict else []
            includedPrimFileList = []
            for prim in primList: # For each primitive associated with this element,
                # is the primName the instance or element? Depends on is the instancedCopies instance
                if prim in primOverrides:
                    primName = instance + '_' + prim + ".xml"
                else:
                    primName = element + '_' + prim + ".xml"
                includedPrimFileList.append(os.path.join(element, prim, primName)) # isBayCedarA2/xgBonsai/isBayCedarA2_xgBonsai.xml
            if "geomObjFile" in icDict and icDict["geomObjFile"]:
                # print "This instance has its own geometry."
                writeInstance(f, "../", instance, instance, icDict["transformMatrix"], includedPrimFileList)
            else:
                writeInstance(f, "../", element, instance, icDict["transformMatrix"], includedPrimFileList)

    #f.writelines("AttributeEnd\n")

    f.writelines('</scene>\n\n')
    os.fsync(pbrtFileObj)
    fcntl.flock(pbrtFileObj, fcntl.LOCK_UN)
    f.close()
    os.chmod(pbrtOutElement, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH | stat.S_IWOTH)
    print "Writing XML file: ", pbrtOutElement
    return pbrtOutElement


def findMaterial(mesh, matDict, hierDict="", quiet=False):
    """
    Given a patch, a materials dictionary and an optional hierarchy dictionary, this finds the correct
    material for that patch.
    :param mesh: The name of the mesh
    :param matDict: A dictionary of materials, created from the json materials file.
    :param hierDict: A dictionary of hierarchy expansions, created from the json .hier file.
    :param quiet: Suppress the no bindings error message
    :return: Matching material as string
    """
    longNameMesh = hierDict[mesh] + '|' + mesh if mesh in hierDict else mesh

    found = []
    for k, v in matDict.iteritems():
        # find an assignment that matches
        bindings = v['assignment']
        for binding in bindings:
            if fnmatch.fnmatchcase(longNameMesh, "*" + binding + "*"):
                found.append(k)
    if found:
        # if there's only one match, we found the binding.
        if len(found) == 1:
            return found[0]
        else:
            # if one is an exact string match, return that one (or the first
            # occurrence of an exact string match)
            for f in found:
                if mesh in matDict[f]['assignment']:
                    return f
            # if there's not an exact string match, go for the match furthest up the hierarchy chain (this will solve
            # a problem with the bonsai matching for trunks vs branches vs tips.
            for f in found:
                print "f"
            sys.exit()
            # otherwise, return the last match. #makingshitup
            return found[-1]
    else:
        if not quiet:
            print "ERROR: Unable to find binding for mesh: ", mesh
        return ""


def insertTextureOverrides(pbrtGeomFile, matFile):
    """
    Find a mesh that's using a material with a texture and insert the texture override that includes the specific
    mesh name. i.e. textures/isGardeniaA/Color/archiveflowerbud0001_geo.ptx
    :param pbrtGeomFile: The pbrt geometry file created with obj2pbrt
    :param matFile: The json material file
    :param hierFile: The file containing the full hierarchy of the meshes (stripped out to create the obj files)
    :return: None
    """
    COMMENT = "# Inserted materials directly above Shape for custom texture per mesh. v2.\n"
    handle, tempPbrtFile = tempfile.mkstemp(prefix='IS', suffix='.xml', dir=TMPDIR)
    tof = open(tempPbrtFile,"w")
    print "Inserting pbrt materials into: ", tempPbrtFile

    # Create a sub-dictionary of just the materials with textures.
    mDict = readJsonFile(matFile) # keys are primitive description name, i.e. xgGrass.
    textureMats = [key for key in mDict if "colorMap" in mDict[key] and mDict[key]["colorMap"]] # keys with materials
    matDict = {mat: mDict[mat] for mat in textureMats}

    # warning. these can be big files!
    with open(os.path.abspath(pbrtGeomFile), "r") as pgf:
        tof.write(COMMENT)
        meshName = ""
        colorName = ""
        dispName = ""
        colorMap = ""
        dispMap = ""
        material = ""
        for line in pgf.xreadlines():  # use generator
            if line.startswith(COMMENT[:20]):
                # Don't duplicate the comment.
                continue

            if line.startswith("# Name"):
                m = re.search('# Name "(.+?)"$', line)
                if not m:
                    tof.write(line)
                    continue
                meshName = m.group(1)
                tof.write(line)
                continue

            if line.startswith("NamedMaterial"):
                m = re.search('^NamedMaterial "(.+?)"$', line)
                if not m:
                    tof.write(line)
                    continue
                material = m.group(1)
                if material not in matDict:
                    material = ""
                    tof.write(line)
                    continue
                colorMap = matDict[material]["colorMap"] if "colorMap" in matDict[material] else ""
                dispMap = matDict[material]["displacementMap"]if "displacementMap" in matDict[material] else ""
                if colorMap:
                    colorName = material + "_Color"
                    texPath = os.path.join("..", os.path.dirname(colorMap),meshName + ".ptx")
                    tof.write('Texture "%s" "color" "ptex"\n' % colorName)
                    tof.write('         "string filename" ["%s"]\n\n'%texPath)
                if dispMap:
                    dispName = material + "_Disp"
                    texPath = os.path.join("..", os.path.dirname(dispMap),meshName + ".ptx")
                    tof.write('Texture "%s" "float" "ptex"\n' % dispName)
                    tof.write('         "string filename" ["%s"]\n\n'%texPath)
                # write back the original NamedMaterial line
                tof.write(line)
                continue

            if line.startswith("Shape") and meshName and material:
                tof.write(line)
                m = re.search('^Shape "trianglemesh"$', line)
                if not m: continue
                if colorMap:
                    tof.write('  "texture color" "%s"\n' % colorName)
                    colorMap = ""
                if dispMap:
                    tof.write('  "texture bumpmap" "%s"\n' % dispName)
                    dispMap = ""
                continue

            # everything else just goes into the tempfile
            tof.write(line)

    tof.close()

    # copy temp file over old one
    print "Replacing ", pbrtGeomFile
    shutil.copy2(tempPbrtFile, os.path.abspath(pbrtGeomFile))