povray integration
authorCampbell Barton <ideasman42@gmail.com>
Sun, 9 Aug 2009 07:09:32 +0000 (07:09 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 9 Aug 2009 07:09:32 +0000 (07:09 +0000)
- declare materials and reference them later
- support ray-transp filter setting
- blobs pigment wasn't working right

release/io/engine_render_pov.py

index b1281e73c95eb5e6ae5358d786d6c57a13d4b791..ea419bb0ace6ac66a35dcb55f00c37371f597157 100644 (file)
@@ -22,50 +22,109 @@ def write_pov(filename, scene=None, info_callback = None):
        
        render = scene.render_data
        world = scene.world
-       materialTable = {}
        
-       def saneName(name):
-               name = name.lower()
-               for ch in ' /\\+=-[]{}().,<>\'":;~!@#$%^&*|?':
-                       name = name.replace(ch, '_')
+       # --- taken from fbx exporter 
+       ## This was used to make V, but faster not to do all that
+       ##valid = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_,.()[]{}'
+       ##v = range(255)
+       ##for c in valid: v.remove(ord(c))
+       v = [0,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,42,43,46,47,58,59,60,61,62,63,64,92,94,96,124,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]
+       invalid = ''.join([chr(i) for i in v])
+       def cleanName(name):
+               for ch in invalid:      name = name.replace(ch, '_')
                return name
+       del v
+       
+       # --- done with clean name.
+       
+       def uniqueName(name, nameSeq):
+               
+               if name not in nameSeq:
+                       return name
+               
+               name_orig = name
+               i = 1
+               while name in nameSeq:
+                       name = '%s_%.3d' % (name_orig, i)
+                       i+=1
+               
+               return name
+               
        
        def writeMatrix(matrix):
                file.write('\tmatrix <%.6f, %.6f, %.6f,  %.6f, %.6f, %.6f,  %.6f, %.6f, %.6f,  %.6f, %.6f, %.6f>\n' %\
                (matrix[0][0], matrix[0][1], matrix[0][2],  matrix[1][0], matrix[1][1], matrix[1][2],  matrix[2][0], matrix[2][1], matrix[2][2],  matrix[3][0], matrix[3][1], matrix[3][2]) )
        
-       def blenderMaterialToPovString(material):
-               povstring = 'finish {'
-               if world != None:
-                       povstring += 'ambient <%.6f, %.6f, %.6f> ' % tuple([c*material.ambient for c in world.ambient_color])
+       def writeObjectMaterial(material):
+               if material and material.raytrace_transparency.enabled:
+                       file.write('\tinterior { ior %.6f }\n' % material.raytrace_transparency.ior)
+                       
+                       # Other interior args
+                       # fade_distance 2
+                       # fade_power [Value]
+                       # fade_color
+                       
+                       # dispersion
+                       # dispersion_samples
+       
+       materialNames = {}
+       DEF_MAT_NAME = 'Default'
+       def writeMaterial(material):
+               # Assumes only called once on each material
                
-               povstring += 'diffuse %.6f ' % material.diffuse_reflection
-               povstring += 'specular %.6f ' % material.specular_reflection
+               if material:
+                       name_orig = material.name
+               else:
+                       name_orig = DEF_MAT_NAME
                
+               name = materialNames[name_orig] = uniqueName(cleanName(name_orig), materialNames)
                
-               if material.raytrace_mirror.enabled:
-                       #povstring += 'interior { ior %.6f } ' % material.IOR
-                       raytrace_mirror= material.raytrace_mirror
-                       if raytrace_mirror.reflect:
-                               povstring += 'reflection {'
-                               povstring += '<%.6f, %.6f, %.6f>' % tuple(material.mirror_color) # Should ask for ray mirror flag
-                               povstring += 'fresnel 1 falloff %.6f exponent %.6f metallic %.6f} ' % (raytrace_mirror.fresnel, raytrace_mirror.fresnel_fac, raytrace_mirror.reflect)
+               file.write('#declare %s = finish {\n' % name)
+               
+               if material:
+                       file.write('\tdiffuse %.3g\n' % material.diffuse_reflection)
+                       file.write('\tspecular %.3g\n' % material.specular_reflection)
                        
+                       file.write('\tambient %.3g\n' % material.ambient)
+                       #file.write('\tambient rgb <%.3g, %.3g, %.3g>\n' % tuple([c*material.ambient for c in world.ambient_color])) # povray blends the global value
                        
-                               
+                       # map hardness between 0.0 and 1.0
+                       roughness = ((1.0 - ((material.specular_hardness-1.0)/510.0)))
+                       # scale from 0.0 to 0.1
+                       roughness *= 0.1
+                       # add a small value because 0.0 is invalid
+                       roughness += (1/511.0)
+                       
+                       file.write('\troughness %.3g\n' % roughness)
+                       
+                       # 'phong 70.0 '
+                       
+                       if material.raytrace_mirror.enabled:
+                               raytrace_mirror= material.raytrace_mirror
+                               if raytrace_mirror.reflect:
+                                       file.write('\treflection {\n')
+                                       file.write('\t\trgb <%.3g, %.3g, %.3g>' % tuple(material.mirror_color))
+                                       file.write('\t\tfresnel 1 falloff %.3g exponent %.3g metallic %.3g} ' % (raytrace_mirror.fresnel, raytrace_mirror.fresnel_fac, raytrace_mirror.reflect))
+               
+               else:
+                       file.write('\tdiffuse 0.8\n')
+                       file.write('\tspecular 0.2\n')
+                       
+                       
+               
+               # This is written into the object
+               '''
                if material.raytrace_transparency.enabled:
-                       #povstring += 'interior { ior %.6f } ' % material.IOR
-                       pass
+                       'interior { ior %.3g} ' % material.raytrace_transparency.ior
+               '''
                
-               #file.write('\t\troughness %.6f\n' % (material.hard*0.5))
-               #file.write('\t\t\tcrand 0.0\n') # Sand granyness
+               #file.write('\t\t\tcrand 1.0\n') # Sand granyness
                #file.write('\t\t\tmetallic %.6f\n' % material.spec)
                #file.write('\t\t\tphong %.6f\n' % material.spec)
                #file.write('\t\t\tphong_size %.6f\n' % material.spec)
-               povstring += 'brilliance %.6f ' % (material.specular_hardness/256.0) # Like hardness
-               povstring += '}'
-               #file.write('\t}\n')
-               return povstring
+               #file.write('\t\t\tbrilliance %.6f ' % (material.specular_hardness/256.0) # Like hardness
+               
+               file.write('}\n')
        
        def exportCamera():
                camera = scene.camera
@@ -187,19 +246,20 @@ def write_pov(filename, scene=None, info_callback = None):
                                        file.write(     'scale <%.6g, %.6g, %.6g> ' % (elem.size_x, elem.size_y, elem.size_z))
                                
                                if material:
-                                       # materialString = materialTable[material.name]
                                        diffuse_color = material.diffuse_color
                                        
+                                       if material.raytrace_transparency.enabled:      trans = 1-material.raytrace_transparency.filter
+                                       else:                                                                           trans = 0.0
+                                       
                                        file.write(
-                                               'pigment {rgbf<%.3g, %.3g, %.3g, %.3g>} }\n' % \
-                                               (diffuse_color[0], diffuse_color[1], diffuse_color[2], 1-material.alpha)
+                                               'pigment {rgbft<%.3g, %.3g, %.3g, %.3g, %.3g>} finish {%s} }\n' % \
+                                               (diffuse_color[0], diffuse_color[1], diffuse_color[2], 1-material.alpha, trans, materialNames[material.name])
                                        )
+                                       
                                else:
-                                       file.write('}\n')
+                                       file.write('pigment {rgb<1 1 1>} finish {%s} }\n' % DEF_MAT_NAME)               # Write the finish last.
                        
-                       # Write the finish last.
-                       if material:
-                               file.write('\t%s\n' % materialTable[material.name])
+                       writeObjectMaterial(material)
 
                        writeMatrix(ob.matrix)
                        
@@ -363,13 +423,19 @@ def write_pov(filename, scene=None, info_callback = None):
                                
                                if me_materials:
                                        material = me_materials[col[3]]
-                                       materialString = materialTable[material.name]
+                                       material_finish = materialNames[material.name]
+                                       
+                                       if material.raytrace_transparency.enabled:      trans = 1-material.raytrace_transparency.filter
+                                       else:                                                                           trans = 0.0
+                                       
                                else:
-                                       materialString = '' # Dont write anything
+                                       material_finish = DEF_MAT_NAME # not working properly,
+                                       trans = 0.0
                                
-                               float_col = col[0], col[1], col[2], 1-material.alpha, materialString
                                #print material.apl
-                               file.write(',\n\t\ttexture { pigment {rgbf<%.3g, %.3g, %.3g, %.3g>}%s}' % float_col)
+                               file.write(     ',\n\t\ttexture { pigment {rgbft<%.3g, %.3g, %.3g, %.3g, %.3g>} finish {%s}}' %
+                                                       (col[0], col[1], col[2], 1-material.alpha, trans, material_finish) )
+                               
                                index[0] = idx
                                idx+=1
                        
@@ -463,8 +529,7 @@ def write_pov(filename, scene=None, info_callback = None):
                        
                        if me.materials:
                                material = me.materials[0] # dodgy
-                               if material and material.raytrace_transparency.enabled:
-                                       file.write('\tinterior { ior %.6f }\n' % material.raytrace_transparency.ior)
+                               writeObjectMaterial(material)
                        
                        writeMatrix(matrix)
                        file.write('}\n')
@@ -508,12 +573,17 @@ def write_pov(filename, scene=None, info_callback = None):
                        file.write("\t\trecursion_limit %d\n" % scene.pov_radio_recursion_limit)
                        file.write('\t}\n')
                
+               if world:
+                       file.write("\tambient_light rgb<%.3g, %.3g, %.3g>\n" % tuple(world.ambient_color))
+               
                file.write('}\n')
        
        
        # Convert all materials to strings we can access directly per vertex.
+       writeMaterial(None) # default material
+       
        for material in bpy.data.materials:
-               materialTable[material.name] = blenderMaterialToPovString(material)
+               writeMaterial(material)
        
        exportCamera()
        #exportMaterials()
@@ -595,8 +665,6 @@ class PovrayRender(bpy.types.RenderEngine):
                write_pov_ini(self.temp_file_ini, self.temp_file_in, self.temp_file_out)
                
                print ("***-STARTING-***")
-               # This works too but means we have to wait until its done
-               # os.system('povray %s' % self.temp_file_ini)
                
                pov_binary = "povray"
                
@@ -606,7 +674,11 @@ class PovrayRender(bpy.types.RenderEngine):
                        else:
                                pov_binary = "pvengine"
                        
-               self.process = subprocess.Popen([pov_binary, self.temp_file_ini]) # stdout=subprocess.PIPE, stderr=subprocess.PIPE
+               if 1:
+                       self.process = subprocess.Popen([pov_binary, self.temp_file_ini]) # stdout=subprocess.PIPE, stderr=subprocess.PIPE
+               else:
+                       # This works too but means we have to wait until its done
+                       os.system('%s %s' % (pov_binary, self.temp_file_ini))
                
                print ("***-DONE-***")