Skip to content
Snippets Groups Projects
render.py 277 KiB
Newer Older
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# #**** END GPL LICENSE BLOCK #****
Luca Bonavita's avatar
Luca Bonavita committed

Luca Bonavita's avatar
Luca Bonavita committed
import bpy
import subprocess
import os
import sys
import time
from math import atan, pi, degrees, sqrt, cos, sin
Maurice Raybaud's avatar
Maurice Raybaud committed
import random
import platform  #
import subprocess  #
import tempfile  # generate temporary files with random names
from bpy.types import Operator
from imghdr import what  # imghdr is a python lib to identify image file types
from bpy.utils import register_class
from . import df3  # for smoke rendering
from . import shading  # for BI POV shaders emulation
from . import primitives  # for import and export of POV specific primitives
from . import nodes  # for POV specific nodes

Maurice Raybaud's avatar
Maurice Raybaud committed
##############################SF###########################
##############find image texture
Maurice Raybaud's avatar
Maurice Raybaud committed
def imageFormat(imgF):
Maurice Raybaud's avatar
Maurice Raybaud committed
    """Identify input image filetypes to transmit to POV."""
    # First use the below explicit extensions to identify image file prospects
    ext = {
        'JPG': "jpeg",
        'JPEG': "jpeg",
        'GIF': "gif",
        'TGA': "tga",
        'IFF': "iff",
        'PPM': "ppm",
        'PNG': "png",
        'SYS': "sys",
        'TIFF': "tiff",
        'TIF': "tiff",
        'EXR': "exr",
        'HDR': "hdr",
    }.get(os.path.splitext(imgF)[-1].upper(), "")
Maurice Raybaud's avatar
Maurice Raybaud committed
    # Then, use imghdr to really identify the filetype as it can be different
        # maybe add a check for if path exists here?
        print(" WARNING: texture image has no extension")  # too verbose
        ext = what(imgF)  # imghdr is a python lib to identify image file types
Maurice Raybaud's avatar
Maurice Raybaud committed
    return ext

Maurice Raybaud's avatar
Maurice Raybaud committed
def imgMap(ts):
Maurice Raybaud's avatar
Maurice Raybaud committed
    """Translate mapping type from Blender UI to POV syntax and return that string."""
    image_map = ""
    if ts.mapping == 'FLAT':
        image_map = "map_type 0 "
    elif ts.mapping == 'SPHERE':
        image_map = "map_type 1 "
    elif ts.mapping == 'TUBE':
        image_map = "map_type 2 "
Maurice Raybaud's avatar
Maurice Raybaud committed
    ## map_type 3 and 4 in development (?) (ENV in pov 3.8)
    ## for POV-Ray, currently they just seem to default back to Flat (type 0)
    # elif ts.mapping=="?":
    #    image_map = " map_type 3 "
    # elif ts.mapping=="?":
    #    image_map = " map_type 4 "
    if ts.texture.use_interpolation:
        image_map += " interpolate 2 "
    if ts.texture.extension == 'CLIP':
        image_map += " once "
    # image_map += "}"
    # if ts.mapping=='CUBE':
    #    image_map+= "warp { cubic } rotate <-90,0,180>"
    # no direct cube type mapping. Though this should work in POV 3.7
    # it doesn't give that good results(best suited to environment maps?)
    # if image_map == "":
    #    print(" No texture image  found ")
Maurice Raybaud's avatar
Maurice Raybaud committed
    return image_map

Maurice Raybaud's avatar
Maurice Raybaud committed
    """Translate mapping transformations from Blender UI to POV syntax and return that string."""
    # XXX TODO: unchecked textures give error of variable referenced before assignment XXX
    # POV-Ray "scale" is not a number of repetitions factor, but ,its
    # inverse, a standard scale factor.
    # 0.5 Offset is needed relatively to scale because center of the
    # scale is 0.5,0.5 in blender and 0,0 in POV
    # Strange that the translation factor for scale is not the same as for
    # translate.
    # TODO: verify both matches with blender internal.
    image_map_transforms = (
        "scale <%.4g,%.4g,%.4g> translate <%.4g,%.4g,%.4g>"
        % (
            1.0 / ts.scale.x,
            1.0 / ts.scale.y,
            1.0 / ts.scale.z,
            0.5 - (0.5 / ts.scale.x) - (ts.offset.x),
            0.5 - (0.5 / ts.scale.y) - (ts.offset.y),
            ts.offset.z,
        )
    )
    # image_map_transforms = (" translate <-0.5,-0.5,0.0> scale <%.4g,%.4g,%.4g> translate <%.4g,%.4g,%.4g>" % \
    # ( 1.0 / ts.scale.x,
    # 1.0 / ts.scale.y,
    # 1.0 / ts.scale.z,
    # (0.5 / ts.scale.x) + ts.offset.x,
    # (0.5 / ts.scale.y) + ts.offset.y,
    # ts.offset.z))
    # image_map_transforms = ("translate <-0.5,-0.5,0> scale <-1,-1,1> * <%.4g,%.4g,%.4g> translate <0.5,0.5,0> + <%.4g,%.4g,%.4g>" % \
    # (1.0 / ts.scale.x,
    # 1.0 / ts.scale.y,
    # 1.0 / ts.scale.z,
    # ts.offset.x,
    # ts.offset.y,
    # ts.offset.z))
Maurice Raybaud's avatar
Maurice Raybaud committed
def imgMapBG(wts):
Maurice Raybaud's avatar
Maurice Raybaud committed
    """Translate world mapping from Blender UI to POV syntax and return that string."""
    image_mapBG = ""
    # texture_coords refers to the mapping of world textures:
    if wts.texture_coords == 'VIEW' or wts.texture_coords == 'GLOBAL':
        image_mapBG = " map_type 0 "
    elif wts.texture_coords == 'ANGMAP':
        image_mapBG = " map_type 1 "
    elif wts.texture_coords == 'TUBE':
        image_mapBG = " map_type 2 "

    if wts.texture.use_interpolation:
        image_mapBG += " interpolate 2 "
    if wts.texture.extension == 'CLIP':
        image_mapBG += " once "
    # image_mapBG += "}"
    # if wts.mapping == 'CUBE':
    #   image_mapBG += "warp { cubic } rotate <-90,0,180>"
    # no direct cube type mapping. Though this should work in POV 3.7
    # it doesn't give that good results(best suited to environment maps?)
    # if image_mapBG == "":
    #    print(" No background texture image  found ")
Maurice Raybaud's avatar
Maurice Raybaud committed
    return image_mapBG
Maurice Raybaud's avatar
Maurice Raybaud committed
def path_image(image):
Maurice Raybaud's avatar
Maurice Raybaud committed
    """Conform a path string to POV syntax to avoid POV errors."""
    return bpy.path.abspath(image.filepath, library=image.library).replace(
        "\\", "/"
    )
    # .replace("\\","/") to get only forward slashes as it's what POV prefers,
Campbell Barton's avatar
Campbell Barton committed
# end find image texture
# -----------------------------------------------------------------------------
Campbell Barton's avatar
Campbell Barton committed
def string_strip_hyphen(name):
Maurice Raybaud's avatar
Maurice Raybaud committed
    """Remove hyphen characters from a string to avoid POV errors."""
Campbell Barton's avatar
Campbell Barton committed
    return name.replace("-", "")
def safety(name, Level):
Maurice Raybaud's avatar
Maurice Raybaud committed
    """append suffix characters to names of various material declinations.
Maurice Raybaud's avatar
Maurice Raybaud committed
    Material declinations are necessary to POV syntax and used in shading.py
    by the povHasnoSpecularMaps function to create the finish map trick and
    the suffixes avoid name collisions.
    Keyword arguments:
    name -- the initial material name as a string
    Level -- the enum number of the Level being written:
        Level=1 is for texture with No specular nor Mirror reflection
        Level=2 is for texture with translation of spec and mir levels
        for when no map influences them
Maurice Raybaud's avatar
Maurice Raybaud committed
        Level=3 is for texture with Maximum Spec and Mirror
Maurice Raybaud's avatar
Maurice Raybaud committed
    try:
Campbell Barton's avatar
Campbell Barton committed
        if int(name) > 0:
            prefix = "shader"
Campbell Barton's avatar
Campbell Barton committed
    except:
Loading
Loading full blame...