Welcome to mirror list, hosted at ThFree Co, Russian Federation.

texturing.py « render_povray - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b8aa8afb12eefc7cd334683a9d23356eb6cef779 (plain)
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
# SPDX-License-Identifier: GPL-2.0-or-later

"""Translate blender texture influences into POV."""
import os
import bpy

local_material_names = []
material_finish = None


def write_texture_influence(
    file,
    mater,
    material_names_dictionary,
    image_format,
    img_map,
    img_map_transforms,
    tab_write,
    comments,
    col,
    preview_dir,
    unpacked_images,
):
    """Translate Blender texture influences to various POV texture tricks and write to pov file."""

    from .scenography import path_image, exported_lights_count
    from .render import string_strip_hyphen, safety, using_uberpov

    material_finish = material_names_dictionary[mater.name]
    trans = 1.0 - mater.pov.alpha if mater.pov.use_transparency else 0.0
    if (mater.pov.specular_color.s == 0.0) or (mater.pov.diffuse_shader == "MINNAERT"):
        # No layered texture because of aoi pattern used for minnaert and pov can't layer patterned
        colored_specular_found = False
    else:
        colored_specular_found = True

    if mater.pov.use_transparency and mater.pov.transparency_method == "RAYTRACE":
        pov_filter = mater.pov_raytrace_transparency.filter * (1.0 - mater.pov.alpha)
        trans = (1.0 - mater.pov.alpha) - pov_filter
    else:
        pov_filter = 0.0

    texture_dif = ""
    texture_spec = ""
    texture_norm = ""
    texture_alpha = ""
    # procedural_flag=False
    tmpidx = -1
    used_texture_slots = (tesl for tesl in mater.pov_texture_slots if tesl.use)
    # and (bpy.data.textures[mater.pov_texture_slots[tmpidx-1].texture] is not None)
    for t in used_texture_slots:

        tmpidx += 1
        # index = mater.pov.active_texture_index
        slot = mater.pov_texture_slots[tmpidx]  # [index]
        povtex = slot.texture  # slot.name
        tex = bpy.data.textures[povtex]
        if tex is None:
            continue  # move to next slot
        # 'NONE' ('NONE' type texture is different from no texture covered above)
        if tex.type == "NONE" and tex.pov.tex_pattern_type == "emulator":
            continue  # move to next slot

        # Implicit else-if (as not skipped by previous "continue")
        if tex.type not in ("IMAGE","NONE"):
            # PROCEDURAL TEXTURE
            image_filename = "PAT_%s" % string_strip_hyphen(bpy.path.clean_name(tex.name))
            if image_filename:
                if t.use_map_color_diffuse:
                    texture_dif = image_filename
                    # colvalue = t.default_value  # UNUSED
                    t_dif = t
                    if t_dif.texture.pov.tex_gamma_enable:
                        img_gamma = " gamma %.3g " % t_dif.texture.pov.tex_gamma_value
                if t.use_map_specular or t.use_map_raymir:
                    texture_spec = image_filename
                    # colvalue = t.default_value  # UNUSED
                    t_spec = t
                if t.use_map_normal:
                    texture_norm = image_filename
                    # colvalue = t.normal_factor/10 # UNUSED
                    # textNormName=tex.image.name + ".normal"
                    # was the above used? --MR
                    t_nor = t
                if t.use_map_alpha:
                    texture_alpha = image_filename
                    # colvalue = t.alpha_factor * 10.0  # UNUSED
                    # textDispName=tex.image.name + ".displ"
                    # was the above used? --MR
                    t_alpha = t
        # RASTER IMAGE
        elif tex.type == "IMAGE" and tex.image and tex.pov.tex_pattern_type == "emulator":
            # NOT A PROCEDURAL TEXTURE
            # PACKED
            if tex.image.packed_file:
                orig_image_filename = tex.image.filepath_raw
                unpackedfilename = os.path.join(
                    preview_dir,
                    ("unpacked_img_" + (string_strip_hyphen(bpy.path.clean_name(tex.name)))),
                )
                if not os.path.exists(unpackedfilename):
                    # record which images that were newly copied and can be safely
                    # cleaned up
                    unpacked_images.append(unpackedfilename)
                tex.image.filepath_raw = unpackedfilename
                tex.image.save()
                image_filename = unpackedfilename.replace("\\", "/")
                # .replace("\\","/") to get only forward slashes as it's what POV prefers,
                # even on windows
                tex.image.filepath_raw = orig_image_filename
            # FILE
            else:
                image_filename = path_image(tex.image)
            # IMAGE SEQUENCE BEGINS
            if image_filename and bpy.data.images[tex.image.name].source == "SEQUENCE":
                korvaa = "." + str(tex.image_user.frame_offset + 1).zfill(3) + "."
                image_filename = image_filename.replace(".001.", korvaa)
                print(" seq debug ")
                print(image_filename)
            # IMAGE SEQUENCE ENDS
            img_gamma = ""
            if image_filename:
                texdata = bpy.data.textures[t.texture]
                if t.use_map_color_diffuse:
                    texture_dif = image_filename
                    # colvalue = t.default_value  # UNUSED
                    t_dif = t
                    print(texdata)
                    if texdata.pov.tex_gamma_enable:
                        img_gamma = " gamma %.3g " % t_dif.texture.pov.tex_gamma_value
                if t.use_map_specular or t.use_map_raymir:
                    texture_spec = image_filename
                    # colvalue = t.default_value  # UNUSED
                    t_spec = t
                if t.use_map_normal:
                    texture_norm = image_filename
                    # colvalue = t.normal_factor/10  # UNUSED
                    # textNormName=tex.image.name + ".normal"
                    # was the above used? --MR
                    t_nor = t
                if t.use_map_alpha:
                    texture_alpha = image_filename
                    # colvalue = t.alpha_factor * 10.0  # UNUSED
                    # textDispName=tex.image.name + ".displ"
                    # was the above used? --MR
                    t_alpha = t

    # -----------------------------------------------------------------------------

    tab_write(file, "\n")
    # THIS AREA NEEDS TO LEAVE THE TEXTURE OPEN UNTIL ALL MAPS ARE WRITTEN DOWN.

    current_material_name = string_strip_hyphen(material_names_dictionary[mater.name])
    global local_material_names
    local_material_names.append(current_material_name)
    tab_write(file, "\n#declare MAT_%s = \ntexture{\n" % current_material_name)
    # -----------------------------------------------------------------------------

    if mater.pov.replacement_text:
        tab_write(file, "%s\n" % mater.pov.replacement_text)
    # -----------------------------------------------------------------------------
    # XXX TODO: replace by new POV MINNAERT rather than aoi
    if mater.pov.diffuse_shader == "MINNAERT":
        tab_write(file, "\n")
        tab_write(file, "aoi\n")
        tab_write(file, "texture_map {\n")
        tab_write(
            file, "[%.3g finish {diffuse %.3g}]\n" % (mater.pov.darkness / 2.0,
                                                      2.0 - mater.pov.darkness)
        )
        tab_write(file, "[%.3g\n" % (1.0 - (mater.pov.darkness / 2.0)))

    if mater.pov.diffuse_shader == "FRESNEL":
        # For FRESNEL diffuse in POV, we'll layer slope patterned textures
        # with lamp vector as the slope vector and nest one slope per lamp
        # into each texture map's entry.

        c = 1
        while c <= exported_lights_count:
            tab_write(file, "slope { lampTarget%s }\n" % c)
            tab_write(file, "texture_map {\n")
            # Diffuse Fresnel value and factor go up to five,
            # other kind of values needed: used the number 5 below to remap
            tab_write(
                file,
                "[%.3g finish {diffuse %.3g}]\n"
                % (
                    (5.0 - mater.pov.diffuse_fresnel) / 5,
                    (mater.pov.diffuse_intensity * ((5.0 - mater.pov.diffuse_fresnel_factor) / 5)),
                ),
            )
            tab_write(
                file,
                "[%.3g\n" % ((mater.pov.diffuse_fresnel_factor / 5) * (mater.pov.diffuse_fresnel / 5.0)),
            )
            c += 1

    # if shader is a 'FRESNEL' or 'MINNAERT': slope pigment pattern or aoi
    # and texture map above, the rest below as one of its entry

    if texture_spec or texture_alpha:
        if texture_spec:
            # tab_write(file, "\n")
            tab_write(file, "pigment_pattern {\n")

            mapping_spec = img_map_transforms(t_spec)
            if texture_spec and texture_spec.startswith("PAT_"):
                tab_write(file, "function{f%s(x,y,z).grey}\n" % texture_spec)
            else:

                tab_write(
                    file,
                    'uv_mapping image_map{%s "%s" %s}\n'
                    % (image_format(texture_spec), texture_spec, img_map(t_spec)),
                )
            tab_write(file, "%s\n" % mapping_spec)
            tab_write(file, "}\n")
            tab_write(file, "texture_map {\n")
            tab_write(file, "[0 \n")

        if not texture_dif:
            if texture_alpha:
                tab_write(file, "\n")

                mapping_alpha = img_map_transforms(t_alpha)

                if texture_alpha and texture_alpha.startswith("PAT_"):
                    tab_write(
                        file, "function{f%s(x,y,z).transmit}%s\n" % (texture_alpha, mapping_alpha)
                    )
                else:

                    tab_write(
                        file,
                        "pigment {pigment_pattern {uv_mapping image_map"
                        '{%s "%s" %s}%s'
                        % (
                            image_format(texture_alpha),
                            texture_alpha,
                            img_map(t_alpha),
                            mapping_alpha,
                        ),
                    )
                tab_write(file, "}\n")
                tab_write(file, "pigment_map {\n")
                tab_write(file, "[0 color rgbft<0,0,0,1,1>]\n")
                tab_write(
                    file,
                    "[1 color rgbft<%.3g, %.3g, %.3g, %.3g, %.3g>]\n"
                    % (col[0], col[1], col[2], pov_filter, trans),
                )
                tab_write(file, "}\n")
                tab_write(file, "}\n")

            else:

                tab_write(
                    file,
                    "pigment {rgbft<%.3g, %.3g, %.3g, %.3g, %.3g>}\n"
                    % (col[0], col[1], col[2], pov_filter, trans),
                )

        else:
            mapping_dif = img_map_transforms(t_dif)

            if texture_alpha:
                mapping_alpha = img_map_transforms(t_alpha)

                tab_write(file, "pigment {\n")
                tab_write(file, "pigment_pattern {\n")
                if texture_alpha and texture_alpha.startswith("PAT_"):
                    tab_write(
                        file, "function{f%s(x,y,z).transmit}%s\n" % (texture_alpha, mapping_alpha)
                    )
                else:
                    tab_write(
                        file,
                        'uv_mapping image_map{%s "%s" %s}%s}\n'
                        % (
                            image_format(texture_alpha),
                            texture_alpha,
                            img_map(t_alpha),
                            mapping_alpha,
                        ),
                    )
                tab_write(file, "pigment_map {\n")
                tab_write(file, "[0 color rgbft<0,0,0,1,1>]\n")
                # if texture_alpha and texture_alpha.startswith("PAT_"):
                # tab_write(file, "[1 pigment{%s}]\n" %texture_dif)
                if texture_dif:
                    if not texture_dif.startswith("PAT_"):
                        tab_write(
                            file,
                            '[1 uv_mapping image_map {%s "%s" %s} %s]\n'
                            % (
                                image_format(texture_dif),
                                texture_dif,
                                (img_gamma + img_map(t_dif)),
                                mapping_dif,
                            ),
                        )
                    elif texture_dif.startswith("PAT_"):
                        tab_write(file, "[1 %s]\n" % texture_dif)
                tab_write(file, "}\n")
                tab_write(file, "}\n")
                if texture_alpha and texture_alpha.startswith("PAT_"):
                    tab_write(file, "}\n")

            else:
                if texture_dif and texture_dif.startswith("PAT_"):
                    tab_write(file, "pigment{%s}\n" % texture_dif)
                else:
                    tab_write(
                        file,
                        'pigment {uv_mapping image_map {%s "%s" %s}%s}\n'
                        % (
                            image_format(texture_dif),
                            texture_dif,
                            (img_gamma + img_map(t_dif)),
                            mapping_dif,
                        ),
                    )

                    # scale 1 rotate y*0
                    # imageMap = ("{image_map {%s \"%s\" %s }\n" % \
                    #            (image_format(textures),textures,img_map(t_dif)))
                    # tab_write(file, "uv_mapping pigment %s} %s finish {%s}\n" % \
                    #         (imageMap,mapping,safety(material_finish)))
                    # tab_write(file, "pigment {uv_mapping image_map {%s \"%s\" %s}%s} " \
                    #         "finish {%s}\n" % \
                    #         (image_format(texture_dif), texture_dif, img_map(t_dif),
                    #          mapping_dif, safety(material_finish)))
        if texture_spec:
            # ref_level_bound 1 is no specular
            tab_write(file, "finish {%s}\n" % (safety(material_finish, ref_level_bound=1)))

        else:
            # ref_level_bound 2 is translated spec
            tab_write(file, "finish {%s}\n" % (safety(material_finish, ref_level_bound=2)))

        if texture_norm:
            # scale 1 rotate y*0

            mapping_normal = img_map_transforms(t_nor)

            if texture_norm and texture_norm.startswith("PAT_"):
                tab_write(
                    file,
                    "normal{function{f%s(x,y,z).grey} bump_size %.4g %s}\n"
                    % (texture_norm, (-t_nor.normal_factor * 9.5), mapping_normal),
                )
            else:
                tab_write(file, "normal {\n")
                # XXX TODO: fix and propagate the micro normals reflection blur below
                #  to non textured materials
                if (
                    mater.pov_raytrace_mirror.use
                    and mater.pov_raytrace_mirror.gloss_factor < 1.0
                    and not using_uberpov
                ):
                    tab_write(file, "average\n")
                    tab_write(file, "normal_map{\n")
                    # 0.5 for entries below means a 50 percent mix
                    # between the micro normal and user bump map
                    # order seems indifferent as commutative
                    tab_write(
                        file,
                        "[0.025 bumps %.4g scale 0.1*%.4g]\n"
                        % (
                            (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                            (10 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                        ),
                    )  # micronormals blurring
                    tab_write(
                        file,
                        "[0.025 bumps %.4g scale 0.1*%.4g phase 0.1]\n"
                        % (
                            (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                            (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                        ),
                    )  # micronormals blurring
                    tab_write(
                        file,
                        "[0.025 bumps %.4g scale 0.1*%.4g phase 0.15]\n"
                        % (
                            (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                            (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                        ),
                    )  # micronormals blurring
                    tab_write(
                        file,
                        "[0.025 bumps %.4g scale 0.1*%.4g phase 0.2]\n"
                        % (
                            (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                            (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                        ),
                    )  # micronormals blurring
                    tab_write(
                        file,
                        "[0.025 bumps %.4g scale 0.1*%.4g phase 0.25]\n"
                        % (
                            (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                            (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                        ),
                    )  # micronormals blurring
                    tab_write(
                        file,
                        "[0.025 bumps %.4g scale 0.1*%.4g phase 0.3]\n"
                        % (
                            (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                            (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                        ),
                    )  # micronormals blurring
                    tab_write(
                        file,
                        "[0.025 bumps %.4g scale 0.1*%.4g phase 0.35]\n"
                        % (
                            (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                            (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                        ),
                    )  # micronormals blurring
                    tab_write(
                        file,
                        "[0.025 bumps %.4g scale 0.1*%.4g phase 0.4]\n"
                        % (
                            (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                            (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                        ),
                    )  # micronormals blurring
                    tab_write(
                        file,
                        "[0.025 bumps %.4g scale 0.1*%.4g phase 0.45]\n"
                        % (
                            (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                            (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                        ),
                    )  # micronormals blurring
                    tab_write(
                        file,
                        "[0.025 bumps %.4g scale 0.1*%.4g phase 0.5]\n"
                        % (
                            (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                            (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                        ),
                    )  # micronormals blurring
                    tab_write(file, "[1.0 ")  # Proceed with user bump...
                tab_write(
                    file,
                    "uv_mapping bump_map "
                    '{%s "%s" %s  bump_size %.4g }%s'
                    % (
                        image_format(texture_norm),
                        texture_norm,
                        img_map(t_nor),
                        (-t_nor.normal_factor * 9.5),
                        mapping_normal,
                    ),
                )
                # ...Then close its last entry and the the normal_map itself
                if (
                    mater.pov_raytrace_mirror.use
                    and mater.pov_raytrace_mirror.gloss_factor < 1.0
                    and not using_uberpov
                ):
                    tab_write(file, r"]}}" + "\n")
                else:
                    tab_write(file, r"}" + "\n")
    if texture_spec:
        tab_write(file, "]\n")
        # -------- Second index for mapping specular max value -------- #
        tab_write(file, "[1 \n")

    if not texture_dif and not mater.pov.replacement_text:
        if texture_alpha:
            mapping_alpha = img_map_transforms(t_alpha)

            if texture_alpha and texture_alpha.startswith("PAT_"):
                tab_write(
                    file, "function{f%s(x,y,z).transmit %s}\n" % (texture_alpha, mapping_alpha)
                )
            else:
                tab_write(
                    file,
                    "pigment {pigment_pattern {uv_mapping image_map"
                    '{%s "%s" %s}%s}\n'
                    % (image_format(texture_alpha), texture_alpha, img_map(t_alpha), mapping_alpha),
                )
            tab_write(file, "pigment_map {\n")
            tab_write(file, "[0 color rgbft<0,0,0,1,1>]\n")
            tab_write(
                file,
                "[1 color rgbft<%.3g, %.3g, %.3g, %.3g, %.3g>]\n"
                % (col[0], col[1], col[2], pov_filter, trans),
            )
            tab_write(file, "}\n")
            tab_write(file, "}\n")

        else:
            tab_write(
                file,
                "pigment {rgbft<%.3g, %.3g, %.3g, %.3g, %.3g>}\n"
                % (col[0], col[1], col[2], pov_filter, trans),
            )

        if texture_spec:
            # ref_level_bound 3 is full specular
            tab_write(file, "finish {%s}\n" % (safety(material_finish, ref_level_bound=3)))

            if (
                mater.pov_raytrace_mirror.use
                and mater.pov_raytrace_mirror.gloss_factor < 1
                and not using_uberpov
            ):
                tab_write(file, "normal {\n")
                tab_write(file, "average\n")
                tab_write(file, "normal_map{\n")
                # 0.5 for entries below means a 50 percent mix
                # between the micro normal and user bump map
                # order seems indifferent as commutative
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (10 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.1]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.15]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.2]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.25]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.3]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.35]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.4]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.45]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.5]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
            # XXX IF USER BUMP_MAP
            if texture_norm:
                tab_write(
                    file, "[1.0 "
                )  # Blurry reflection or not Proceed with user bump in either case...
                tab_write(
                    file,
                    "uv_mapping bump_map "
                    '{%s "%s" %s  bump_size %.4g }%s\n'
                    % (
                        image_format(texture_norm),
                        texture_norm,
                        img_map(t_nor),
                        (-t_nor.normal_factor * 9.5),
                        mapping_normal,
                    ),
                )
            # ...Then close the normal_map itself if blurry reflection
            if (
                mater.pov_raytrace_mirror.use
                and mater.pov_raytrace_mirror.gloss_factor < 1
                and not using_uberpov
            ):
                tab_write(file, "]\n}}\n")
            else:
                tab_write(file, "}\n")
        elif colored_specular_found:
            # ref_level_bound 1 is no specular
            tab_write(file, "finish {%s}\n" % (safety(material_finish, ref_level_bound=1)))

        else:
            # ref_level_bound 2 is translated specular
            tab_write(file, "finish {%s}\n" % (safety(material_finish, ref_level_bound=2)))

    elif not mater.pov.replacement_text:
        mapping_dif = img_map_transforms(t_dif)

        if texture_alpha:

            mapping_alpha = img_map_transforms(t_alpha)

            if texture_alpha and texture_alpha.startswith("PAT_"):
                tab_write(
                    file,
                    "pigment{pigment_pattern {function{f%s(x,y,z).transmit}%s}\n"
                    % (texture_alpha, mapping_alpha),
                )
            else:
                tab_write(
                    file,
                    "pigment {pigment_pattern {uv_mapping image_map"
                    '{%s "%s" %s}%s}\n'
                    % (image_format(texture_alpha), texture_alpha, img_map(t_alpha), mapping_alpha),
                )
            tab_write(file, "pigment_map {\n")
            tab_write(file, "[0 color rgbft<0,0,0,1,1>]\n")
            if texture_alpha and texture_alpha.startswith("PAT_"):
                tab_write(
                    file, "[1 function{f%s(x,y,z).transmit}%s]\n" % (texture_alpha, mapping_alpha)
                )
            elif texture_dif and not texture_dif.startswith("PAT_"):
                tab_write(
                    file,
                    '[1 uv_mapping image_map {%s "%s" %s} %s]\n'
                    % (
                        image_format(texture_dif),
                        texture_dif,
                        (img_map(t_dif) + img_gamma),
                        mapping_dif,
                    ),
                )
            elif texture_dif and texture_dif.startswith("PAT_"):
                tab_write(file, "[1 %s %s]\n" % (texture_dif, mapping_dif))
            tab_write(file, "}\n")
            tab_write(file, "}\n")

        else:
            if texture_dif and texture_dif.startswith("PAT_"):
                tab_write(file, "pigment{%s %s}\n" % (texture_dif, mapping_dif))
            else:
                tab_write(file, "pigment {\n")
                tab_write(file, "uv_mapping image_map {\n")
                # tab_write(file, "%s \"%s\" %s}%s\n" % \
                #         (image_format(texture_dif), texture_dif,
                #         (img_gamma + img_map(t_dif)),mapping_dif))
                tab_write(file, '%s "%s" \n' % (image_format(texture_dif), texture_dif))
                tab_write(file, "%s\n" % (img_gamma + img_map(t_dif)))
                tab_write(file, "}\n")
                tab_write(file, "%s\n" % mapping_dif)
                tab_write(file, "}\n")

        if texture_spec:
            # ref_level_bound 3 is full specular
            tab_write(file, "finish {%s}\n" % (safety(material_finish, ref_level_bound=3)))
        else:
            # ref_level_bound 2 is translated specular
            tab_write(file, "finish {%s}\n" % (safety(material_finish, ref_level_bound=2)))

        # scale 1 rotate y*0
        # imageMap = ("{image_map {%s \"%s\" %s }" % \
        #            (image_format(textures), textures,img_map(t_dif)))
        # tab_write(file, "\n\t\t\tuv_mapping pigment %s} %s finish {%s}" % \
        #           (imageMap, mapping, safety(material_finish)))
        # tab_write(file, "\n\t\t\tpigment {uv_mapping image_map " \
        #           "{%s \"%s\" %s}%s} finish {%s}" % \
        #           (image_format(texture_dif), texture_dif,img_map(t_dif),
        #            mapping_dif, safety(material_finish)))
    if texture_norm and not mater.pov.replacement_text:

        mapping_normal = img_map_transforms(t_nor)

        if texture_norm and texture_norm.startswith("PAT_"):
            tab_write(
                file,
                "normal{function{f%s(x,y,z).grey} bump_size %.4g %s}\n"
                % (texture_norm, (-t_nor.normal_factor * 9.5), mapping_normal),
            )
        else:
            tab_write(file, "normal {\n")
            # XXX TODO: fix and propagate the micro normals reflection blur below
            #  to non textured materials
            if (
                mater.pov_raytrace_mirror.use
                and mater.pov_raytrace_mirror.gloss_factor < 1.0
                and not using_uberpov
            ):
                tab_write(file, "average\n")
                tab_write(file, "normal_map{\n")
                # 0.5 for entries below means a 50 percent mix
                # between the micro normal and user bump map
                # order seems indifferent as commutative
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (10 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.1]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.15]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.2]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.25]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.3]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.35]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.4]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.45]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file,
                    "[0.025 bumps %.4g scale 0.1*%.4g phase 0.5]\n"
                    % (
                        (10 / (mater.pov_raytrace_mirror.gloss_factor + 0.01)),
                        (1 / (mater.pov_raytrace_mirror.gloss_samples + 0.001)),
                    ),
                )  # micronormals blurring
                tab_write(
                    file, "[1.0 "
                )  # Blurry reflection or not Proceed with user bump in either case...
            tab_write(
                file,
                "uv_mapping bump_map "
                '{%s "%s" %s  bump_size %.4g }%s\n'
                % (
                    image_format(texture_norm),
                    texture_norm,
                    img_map(t_nor),
                    (-t_nor.normal_factor * 9.5),
                    mapping_normal,
                ),
            )
            # ...Then close the normal_map itself if blurry reflection
            if (
                mater.pov_raytrace_mirror.use
                and mater.pov_raytrace_mirror.gloss_factor < 1
                and not using_uberpov
            ):
                tab_write(file, "]}}\n")
            else:
                tab_write(file, "}\n")
    if texture_spec and not mater.pov.replacement_text:
        tab_write(file, "]\n")

        tab_write(file, "}\n")

    # End of slope/ior texture_map
    if mater.pov.diffuse_shader == "MINNAERT" and not mater.pov.replacement_text:
        tab_write(file, "]\n")
        tab_write(file, "}\n")
    if mater.pov.diffuse_shader == "FRESNEL" and not mater.pov.replacement_text:
        c = 1
        while c <= exported_lights_count:
            tab_write(file, "]\n")
            tab_write(file, "}\n")
            c += 1

    # Close first layer of POV "texture" (Blender material)
    tab_write(file, "}\n")

    colored_specular_found = bool(
        (mater.pov.specular_color.s > 0) and (mater.pov.diffuse_shader != "MINNAERT")
    )

    # Write another layered texture using invisible diffuse and metallic trick
    # to emulate colored specular highlights
    special_texture_found = False
    tmpidx = -1
    for t in mater.pov_texture_slots:
        tmpidx += 1
        # index = mater.pov.active_texture_index
        slot = mater.pov_texture_slots[tmpidx]  # [index]
        povtex = slot.texture  # slot.name
        tex = bpy.data.textures[povtex]
        # Specular mapped textures would conflict with colored specular
        # because POV can't layer over or under pigment patterned textures
        special_texture_found = bool(
            t
            and t.use
            and ((tex.type == "IMAGE" and tex.image) or tex.type != "IMAGE")
            and (t.use_map_specular or t.use_map_raymir)
        )
    if colored_specular_found and not special_texture_found:
        if comments:
            tab_write(file, "  // colored highlights with a stransparent metallic layer\n")
        else:
            tab_write(file, "\n")

        tab_write(file, "texture {\n")
        tab_write(
            file,
            "pigment {rgbft<%.3g, %.3g, %.3g, 0, 1>}\n"
            % (
                mater.pov.specular_color[0],
                mater.pov.specular_color[1],
                mater.pov.specular_color[2],
            ),
        )
        tab_write(
            file, "finish {%s}\n" % (safety(material_finish, ref_level_bound=2))
        )  # ref_level_bound 2 is translated spec

        texture_norm = ""
        for t in mater.pov_texture_slots:

            if tex.pov.tex_pattern_type != "emulator":
                # PROCEDURAL TEXTURE
                image_filename = string_strip_hyphen(bpy.path.clean_name(tex.name))
            if (
                tex.type == "IMAGE"
                and t.use
                and tex.image
                and tex.pov.tex_pattern_type == "emulator"
            ):
                procedural_flag = False
                image_filename = path_image(tex.image)
                img_gamma = ""
                if image_filename and t.use_map_normal:
                    texture_norm = image_filename
                    # colvalue = t.normal_factor/10  # UNUSED   XXX *-9.5 !
                    # textNormName=tex.image.name + ".normal"
                    # was the above used? --MR
                    t_nor = t
                    if procedural_flag:
                        tab_write(
                            file,
                            "normal{function"
                            "{f%s(x,y,z).grey} bump_size %.4g}\n"
                            % (texture_norm, (-t_nor.normal_factor * 9.5)),
                        )
                    else:
                        tab_write(
                            file,
                            "normal {uv_mapping bump_map "
                            '{%s "%s" %s  bump_size %.4g }%s}\n'
                            % (
                                image_format(texture_norm),
                                texture_norm,
                                img_map(t_nor),
                                (-t_nor.normal_factor * 9.5),
                                mapping_normal,
                            ),
                        )

        tab_write(file, "}\n")  # THEN IT CAN CLOSE LAST LAYER OF TEXTURE