コード例 #1
0
ファイル: texture.py プロジェクト: armory3d/armory
def make_texture(image_node, tex_name):
    wrd = bpy.data.worlds["Arm"]
    tex = {}
    tex["name"] = tex_name
    tex["file"] = ""
    image = image_node.image
    matname = mat_state.material.name

    if image == None:
        return None

    if image.filepath == "":
        log.warn(matname + "/" + image.name + " - file path not found")
        return None

    # Reference image name
    tex["file"] = armutils.extract_filename(image.filepath)
    tex["file"] = armutils.safefilename(tex["file"])
    s = tex["file"].rsplit(".", 1)

    if len(s) == 1:
        log.warn(matname + "/" + image.name + " - file extension required for image name")
        return None

    ext = s[1].lower()
    do_convert = ext != "jpg" and ext != "png" and ext != "hdr"  # Convert image
    if do_convert:
        tex["file"] = tex["file"].rsplit(".", 1)[0] + ".jpg"
        # log.warn(matname + '/' + image.name + ' - image format is not (jpg/png/hdr), converting to jpg.')

    if image.packed_file != None:
        # Extract packed data
        unpack_path = armutils.get_fp() + "/build/compiled/Assets/unpacked"
        if not os.path.exists(unpack_path):
            os.makedirs(unpack_path)
        unpack_filepath = unpack_path + "/" + tex["file"]

        if do_convert:
            if not os.path.isfile(unpack_filepath):
                armutils.write_image(image, unpack_filepath)

        # Write bytes if size is different or file does not exist yet
        elif os.path.isfile(unpack_filepath) == False or os.path.getsize(unpack_filepath) != image.packed_file.size:
            with open(unpack_filepath, "wb") as f:
                f.write(image.packed_file.data)

        assets.add(unpack_filepath)

    else:
        # if not os.path.isfile(image.filepath):
        # log.warn(matname + '/' + image.name + ' - file not found')
        # return tex

        if do_convert:
            converted_path = armutils.get_fp() + "/build/compiled/Assets/unpacked/" + tex["file"]
            # TODO: delete cache when file changes
            if not os.path.isfile(converted_path):
                armutils.write_image(image, converted_path)
            assets.add(converted_path)
        else:
            # Link image path to assets
            assets.add(armutils.safe_assetpath(image.filepath))

    # if image_format != 'RGBA32':
    # tex['format'] = image_format

    interpolation = image_node.interpolation
    aniso = wrd.anisotropic_filtering_state
    if aniso == "On":
        interpolation = "Smart"
    elif aniso == "Off" and interpolation == "Smart":
        interpolation = "Linear"

    # TODO: Blender seems to load full images on size request, cache size instead
    powimage = is_pow(image.size[0]) and is_pow(image.size[1])

    # Pow2 required to generate mipmaps
    if powimage == True:
        if interpolation == "Cubic":  # Mipmap linear
            tex["mipmap_filter"] = "linear"
            tex["generate_mipmaps"] = True
        elif interpolation == "Smart":  # Mipmap anisotropic
            tex["min_filter"] = "anisotropic"
            tex["mipmap_filter"] = "linear"
            tex["generate_mipmaps"] = True
    elif image_node.interpolation == "Cubic" or image_node.interpolation == "Smart":
        log.warn(
            matname
            + "/"
            + image.name
            + " - power of 2 texture required for "
            + image_node.interpolation
            + " interpolation"
        )

    if image_node.extension != "REPEAT":  # Extend or clip
        tex["u_addressing"] = "clamp"
        tex["v_addressing"] = "clamp"
    else:
        if state.target == "html5" and powimage == False:
            log.warn(matname + "/" + image.name + " - non power of 2 texture can not use repeat mode on HTML5 target")
            tex["u_addressing"] = "clamp"
            tex["v_addressing"] = "clamp"

    # if image.source == 'MOVIE': # Just append movie texture trait for now
    #     movie_trait = {}
    #     movie_trait['type'] = 'Script'
    #     movie_trait['class_name'] = 'armory.trait.internal.MovieTexture'
    #     movie_trait['parameters'] = [tex['file']]
    #     for o in self.materialToGameObjectDict[material]:
    #         o['traits'].append(movie_trait)
    #     tex['source'] = 'movie'
    #     tex['file'] = '' # MovieTexture will load the video

    return tex
コード例 #2
0
def parse_color(world, node, context, envmap_strength_const):
    wrd = bpy.data.worlds['Arm']

    # Env map included
    if node.type == 'TEX_ENVIRONMENT' and node.image != None:

        image = node.image
        filepath = image.filepath

        if image.packed_file == None and not os.path.isfile(
                armutils.safe_assetpath(filepath)):
            log.warn(world.name + ' - unable to open ' + image.filepath)
            return

        tex = {}
        context['bind_textures'].append(tex)
        tex['name'] = 'envmap'
        tex['u_addressing'] = 'clamp'
        tex['v_addressing'] = 'clamp'

        # Reference image name
        tex['file'] = armutils.extract_filename(image.filepath)
        tex['file'] = armutils.safe_filename(tex['file'])
        base = tex['file'].rsplit('.', 1)
        ext = base[1].lower()

        if ext == 'hdr':
            target_format = 'HDR'
        else:
            target_format = 'JPEG'
        do_convert = ext != 'hdr' and ext != 'jpg'
        if do_convert:
            if ext == 'exr':
                tex['file'] = base[0] + '.hdr'
                target_format = 'HDR'
            else:
                tex['file'] = base[0] + '.jpg'
                target_format = 'JPEG'

        if image.packed_file != None:
            # Extract packed data
            unpack_path = armutils.get_fp() + '/build/compiled/Assets/unpacked'
            if not os.path.exists(unpack_path):
                os.makedirs(unpack_path)
            unpack_filepath = unpack_path + '/' + tex['file']
            filepath = unpack_filepath

            if do_convert:
                if not os.path.isfile(unpack_filepath):
                    armutils.write_image(image,
                                         unpack_filepath,
                                         file_format=target_format)

            elif os.path.isfile(unpack_filepath) == False or os.path.getsize(
                    unpack_filepath) != image.packed_file.size:
                with open(unpack_filepath, 'wb') as f:
                    f.write(image.packed_file.data)

            assets.add(unpack_filepath)
        else:
            if do_convert:
                converted_path = armutils.get_fp(
                ) + '/build/compiled/Assets/unpacked/' + tex['file']
                filepath = converted_path
                # TODO: delete cache when file changes
                if not os.path.isfile(converted_path):
                    armutils.write_image(image,
                                         converted_path,
                                         file_format=target_format)
                assets.add(converted_path)
            else:
                # Link image path to assets
                assets.add(armutils.safe_assetpath(image.filepath))

        # Generate prefiltered envmaps
        world.world_envtex_name = tex['file']
        world.world_envtex_irr_name = tex['file'].rsplit('.', 1)[0]
        disable_hdr = target_format == 'JPEG'

        mip_count = world.world_envtex_num_mips
        mip_count = write_probes.write_probes(
            filepath,
            disable_hdr,
            mip_count,
            generate_radiance=wrd.generate_radiance)

        world.world_envtex_num_mips = mip_count

        # Append envtex define
        bpy.data.worlds['Arm'].world_defs += '_EnvTex'
        # Append LDR define
        if disable_hdr:
            bpy.data.worlds['Arm'].world_defs += '_EnvLDR'
        # Append radiance define
        if wrd.generate_irradiance and wrd.generate_radiance:
            bpy.data.worlds['Arm'].world_defs += '_Rad'

    # Static image background
    elif node.type == 'TEX_IMAGE':
        bpy.data.worlds['Arm'].world_defs += '_EnvImg'
        tex = {}
        context['bind_textures'].append(tex)
        tex['name'] = 'envmap'
        # No repeat for now
        tex['u_addressing'] = 'clamp'
        tex['v_addressing'] = 'clamp'

        image = node.image
        filepath = image.filepath

        if image.packed_file != None:
            # Extract packed data
            filepath = '/build/compiled/Assets/unpacked'
            unpack_path = armutils.get_fp() + filepath
            if not os.path.exists(unpack_path):
                os.makedirs(unpack_path)
            unpack_filepath = unpack_path + '/' + image.name
            if os.path.isfile(unpack_filepath) == False or os.path.getsize(
                    unpack_filepath) != image.packed_file.size:
                with open(unpack_filepath, 'wb') as f:
                    f.write(image.packed_file.data)
            assets.add(unpack_filepath)
        else:
            # Link image path to assets
            assets.add(armutils.safe_assetpath(image.filepath))

        # Reference image name
        tex['file'] = armutils.extract_filename(image.filepath)
        tex['file'] = armutils.safe_filename(tex['file'])

    # Append sky define
    elif node.type == 'TEX_SKY':
        # Match to cycles
        envmap_strength_const['float'] *= 0.1

        bpy.data.worlds['Arm'].world_defs += '_EnvSky'
        # Append sky properties to material
        const = {}
        const['name'] = 'sunDirection'
        sun_direction = [
            node.sun_direction[0], node.sun_direction[1], node.sun_direction[2]
        ]
        sun_direction[1] *= -1  # Fix Y orientation
        const['vec3'] = list(sun_direction)
        context['bind_constants'].append(const)

        world.world_envtex_sun_direction = sun_direction
        world.world_envtex_turbidity = node.turbidity
        world.world_envtex_ground_albedo = node.ground_albedo

        # Irradiance json file name
        world.world_envtex_irr_name = world.name
        write_probes.write_sky_irradiance(world.name)

        # Radiance
        if wrd.generate_radiance_sky and wrd.generate_radiance and wrd.generate_irradiance:
            bpy.data.worlds['Arm'].world_defs += '_Rad'

            if wrd.generate_radiance_sky_type == 'Hosek':
                hosek_path = 'armory/Assets/hosek/'
            else:
                hosek_path = 'armory/Assets/hosek_fake/'

            sdk_path = armutils.get_sdk_path()
            # Use fake maps for now
            assets.add(sdk_path + hosek_path + 'hosek_radiance.hdr')
            for i in range(0, 8):
                assets.add(sdk_path + hosek_path + 'hosek_radiance_' + str(i) +
                           '.hdr')

            world.world_envtex_name = 'hosek'
            world.world_envtex_num_mips = 8
コード例 #3
0
ファイル: write_probes.py プロジェクト: armory3d/armory
def write_probes(image_filepath, disable_hdr, cached_num_mips, generate_radiance=True):
    if not os.path.exists('build/compiled/Assets/envmaps'):
        os.makedirs('build/compiled/Assets/envmaps')

    base_name = image_filepath.rsplit(os.path.sep, 1)[1].rsplit('.', 1)[0] # Extract file name without extension
    
    # Assets to be generated
    output_file_irr = 'build/compiled/Assets/envmaps/' + base_name + '_irradiance'
    if generate_radiance:
        output_file_rad = 'build/compiled/Assets/envmaps/' + base_name + '_radiance'
        rad_format = 'jpg' if disable_hdr else 'hdr'

    # Irradiance exists, keep cache
    if os.path.exists('build/compiled/Assets/envmaps/' + base_name + '_irradiance.arm'):
        add_irr_assets(output_file_irr)
        if generate_radiance:
            add_rad_assets(output_file_rad, rad_format, cached_num_mips)
        return cached_num_mips
    
    # Get paths
    sdk_path = armutils.get_sdk_path()

    if armutils.get_os() == 'win':
        cmft_path = sdk_path + '/armory/tools/cmft/cmft.exe'
        kraffiti_path = sdk_path + '/win32/resources/app/extensions/kha/Kha/Kore/Tools/kraffiti/kraffiti.exe'
    elif armutils.get_os() == 'mac':
        cmft_path = sdk_path + '/armory/tools/cmft/cmft-osx'
        kraffiti_path = sdk_path + '/"Kode Studio.app"/Contents/Resources/app/extensions/kha/Kha/Kore/Tools/kraffiti/kraffiti-osx'
    else:
        cmft_path = sdk_path + '/armory/tools/cmft/cmft-linux64'
        kraffiti_path = sdk_path + '/linux64/resources/app/extensions/kha/Kha/Kore/Tools/kraffiti/kraffiti-linux64'
    
    output_gama_numerator = '1.0' if disable_hdr else '2.2'
    input_file = armutils.safe_assetpath(image_filepath)
    
    # Scale map
    wrd = bpy.data.worlds['Arm']
    target_w = int(wrd.generate_radiance_size)
    target_h = int(target_w / 2)
    scaled_file = output_file_rad + '.' + rad_format

    if armutils.get_os() == 'win':
        output = subprocess.check_output([ \
            kraffiti_path,
            'from=' + input_file,
            'to=' + scaled_file,
            'format=' + rad_format,
            'width=' + str(target_w),
            'height=' + str(target_h)])
    else:
        output = subprocess.check_output([ \
            kraffiti_path + \
            ' from=' + input_file + \
            ' to=' + scaled_file + \
            ' format=' + rad_format + \
            ' width=' + str(target_w) + \
            ' height=' + str(target_h)], shell=True)

    # Generate irradiance
    # gama_options = ''
    # if disable_hdr:
        # gama_options = \
        # ' --inputGammaNumerator 2.2' + \
        # ' --inputGammaDenominator 1.0' + \
        # ' --outputGammaNumerator 1.0' + \
        # ' --outputGammaDenominator ' + output_gama_numerator
    
    # Irradiance spherical harmonics
    if armutils.get_os() == 'win':
        subprocess.call([ \
            cmft_path,
            '--input', scaled_file,
            '--filter', 'shcoeffs',
            #gama_options + \
            '--outputNum', '1',
            '--output0', output_file_irr])
    else:
        subprocess.call([ \
            cmft_path + \
            ' --input ' + scaled_file + \
            ' --filter shcoeffs' + \
            #gama_options + \
            ' --outputNum 1' + \
            ' --output0 ' + output_file_irr], shell=True)

    sh_to_json(output_file_irr)
    add_irr_assets(output_file_irr)
    
    # Mip-mapped radiance
    if generate_radiance == False:
        return cached_num_mips

    # 4096 = 256 face
    # 2048 = 128 face
    # 1024 = 64 face
    face_size = target_w / 8
    if target_w == 2048:
        mip_count = 9
    elif target_w == 1024:
        mip_count = 8
    else:
        mip_count = 7
    
    if armutils.get_os() == 'win':
        subprocess.call([ \
            cmft_path,
            '--input', input_file,
            '--filter radiance',
            '--dstFaceSize', str(face_size),
            '--srcFaceSize', str(face_size),
            '--excludeBase', 'false',
            # '--mipCount', str(mip_count),
            '--glossScale', '7',
            '--glossBias', '3',
            '--lightingModel', 'blinnbrdf',
            '--edgeFixup', 'none',
            '--numCpuProcessingThreads', '4',
            '--useOpenCL', 'true',
            '--clVendor', 'anyGpuVendor',
            '--deviceType', 'gpu',
            '--deviceIndex', '0',
            '--generateMipChain', 'true',
            '--inputGammaNumerator', '2.2',
            '--inputGammaDenominator', '1.0',
            '--outputGammaNumerator', '1.0',
            '--outputGammaDenominator', output_gama_numerator,
            '--outputNum', '1',
            '--output0', output_file_rad,
            '--output0params', 'hdr,rgbe,latlong'])
    else:
        subprocess.call([ \
            cmft_path + \
            ' --input ' + input_file + \
            ' --filter radiance' + \
            ' --dstFaceSize ' + str(face_size) + \
            ' --srcFaceSize ' + str(face_size) + \
            ' --excludeBase false' + \
            #' --mipCount ' + str(mip_count) + \
            ' --glossScale 7' + \
            ' --glossBias 3' + \
            ' --lightingModel blinnbrdf' + \
            ' --edgeFixup none' + \
            ' --numCpuProcessingThreads 4' + \
            ' --useOpenCL true' + \
            ' --clVendor anyGpuVendor' + \
            ' --deviceType gpu' + \
            ' --deviceIndex 0' + \
            ' --generateMipChain true' + \
            ' --inputGammaNumerator 2.2' + \
            ' --inputGammaDenominator 1.0' + \
            ' --outputGammaNumerator 1.0' + \
            ' --outputGammaDenominator ' + output_gama_numerator + \
            ' --outputNum 1' + \
            ' --output0 ' + output_file_rad + \
            ' --output0params hdr,rgbe,latlong'], shell=True)

    # Remove size extensions in file name
    mip_w = int(face_size * 4)
    mip_base = output_file_rad + '_'
    mip_num = 0
    while mip_w >= 4:
        mip_name = mip_base + str(mip_num)
        os.rename(
            mip_name + '_' + str(mip_w) + 'x' + str(int(mip_w / 2)) + '.hdr',
            mip_name + '.hdr')
        mip_w = int(mip_w / 2)
        mip_num += 1

    # Append mips       
    generated_files = []
    for i in range(0, mip_count):
        generated_files.append(output_file_rad + '_' + str(i))
    
    # Convert to jpgs
    if disable_hdr is True:
        for f in generated_files:
            if armutils.get_os() == 'win':
                subprocess.call([ \
                    kraffiti_path,
                    'from=' + f + '.hdr',
                    'to=' + f + '.jpg',
                    'format=jpg'])
            else:
                subprocess.call([ \
                    kraffiti_path + \
                    ' from=' + f + '.hdr' + \
                    ' to=' + f + '.jpg' + \
                    ' format=jpg'], shell=True)
            os.remove(f + '.hdr')
    
    # Scale from (4x2 to 1x1>
    for i in range (0, 2):
        last = generated_files[-1]
        out = output_file_rad + '_' + str(mip_count + i)
        if armutils.get_os() == 'win':
            subprocess.call([ \
                kraffiti_path,
                'from=' + last + '.' + rad_format,
                'to=' + out + '.' + rad_format,
                'scale=0.5',
                'format=' + rad_format], shell=True)
        else:
            subprocess.call([ \
                kraffiti_path + \
                ' from=' + last + '.' + rad_format + \
                ' to=' + out + '.' + rad_format + \
                ' scale=0.5' + \
                ' format=' + rad_format], shell=True)
        generated_files.append(out)
    
    mip_count += 2

    add_rad_assets(output_file_rad, rad_format, mip_count)

    return mip_count
コード例 #4
0
ファイル: texture.py プロジェクト: daela/armory
def make_texture(image_node, tex_name, matname=None):
    wrd = bpy.data.worlds['Arm']
    tex = {}
    tex['name'] = tex_name
    tex['file'] = ''
    image = image_node.image
    if matname == None:
        matname = mat_state.material.name

    if image == None:
        return None

    if image.filepath == '':
        log.warn(matname + '/' + image.name + ' - file path not found')
        return None

    # Reference image name
    tex['file'] = armutils.extract_filename(image.filepath)
    tex['file'] = armutils.safefilename(tex['file'])
    s = tex['file'].rsplit('.', 1)

    if len(s) == 1:
        log.warn(matname + '/' + image.name +
                 ' - file extension required for image name')
        return None

    ext = s[1].lower()
    do_convert = ext != 'jpg' and ext != 'png' and ext != 'hdr' and ext != 'mp4'  # Convert image
    if do_convert:
        tex['file'] = tex['file'].rsplit('.', 1)[0] + '.jpg'
        # log.warn(matname + '/' + image.name + ' - image format is not (jpg/png/hdr), converting to jpg.')

    if image.packed_file != None:
        # Extract packed data
        unpack_path = armutils.get_fp() + '/build/compiled/Assets/unpacked'
        if not os.path.exists(unpack_path):
            os.makedirs(unpack_path)
        unpack_filepath = unpack_path + '/' + tex['file']

        if do_convert:
            if not os.path.isfile(unpack_filepath):
                armutils.write_image(image, unpack_filepath)

        # Write bytes if size is different or file does not exist yet
        elif os.path.isfile(unpack_filepath) == False or os.path.getsize(
                unpack_filepath) != image.packed_file.size:
            with open(unpack_filepath, 'wb') as f:
                f.write(image.packed_file.data)

        assets.add(unpack_filepath)

    else:
        if not os.path.isfile(armutils.safe_assetpath(image.filepath)):
            log.warn('Material ' + matname + '/' + image.name +
                     ' - file not found(' + image.filepath + ')')
            return None

        if do_convert:
            converted_path = armutils.get_fp(
            ) + '/build/compiled/Assets/unpacked/' + tex['file']
            # TODO: delete cache when file changes
            if not os.path.isfile(converted_path):
                armutils.write_image(image, converted_path)
            assets.add(converted_path)
        else:
            # Link image path to assets
            # TODO: Khamake converts .PNG to .jpg? Convert ext to lowercase on windows
            if armutils.get_os() == 'win':
                s = image.filepath.rsplit('.', 1)
                assets.add(armutils.safe_assetpath(s[0] + '.' + s[1].lower()))
            else:
                assets.add(armutils.safe_assetpath(image.filepath))

    # if image_format != 'RGBA32':
    # tex['format'] = image_format

    interpolation = image_node.interpolation
    aniso = wrd.anisotropic_filtering_state
    if aniso == 'On':
        interpolation = 'Smart'
    elif aniso == 'Off' and interpolation == 'Smart':
        interpolation = 'Linear'

    # TODO: Blender seems to load full images on size request, cache size instead
    powimage = is_pow(image.size[0]) and is_pow(image.size[1])

    # Pow2 required to generate mipmaps
    if powimage == True:
        if interpolation == 'Cubic':  # Mipmap linear
            tex['mipmap_filter'] = 'linear'
            tex['generate_mipmaps'] = True
        elif interpolation == 'Smart':  # Mipmap anisotropic
            tex['min_filter'] = 'anisotropic'
            tex['mipmap_filter'] = 'linear'
            tex['generate_mipmaps'] = True
    elif (image_node.interpolation == 'Cubic'
          or image_node.interpolation == 'Smart'):
        log.warn(matname + '/' + image.name +
                 ' - power of 2 texture required for ' +
                 image_node.interpolation + ' interpolation')

    if image_node.extension != 'REPEAT':  # Extend or clip
        tex['u_addressing'] = 'clamp'
        tex['v_addressing'] = 'clamp'
    else:
        if state.target == 'html5' and powimage == False:
            log.warn(
                matname + '/' + image.name +
                ' - non power of 2 texture can not use repeat mode on HTML5 target'
            )
            tex['u_addressing'] = 'clamp'
            tex['v_addressing'] = 'clamp'

    if image.source == 'MOVIE':  # Just append movie texture trait for now
        movie_trait = {}
        movie_trait['type'] = 'Script'
        movie_trait['class_name'] = 'armory.trait.internal.MovieTexture'
        movie_trait['parameters'] = [tex['file']]
        for o in mat_state.mat_armusers[mat_state.material]:
            o['traits'].append(movie_trait)
        tex['source'] = 'movie'
        tex['file'] = ''  # MovieTexture will load the video

    return tex
コード例 #5
0
def write_probes(image_filepath,
                 disable_hdr,
                 cached_num_mips,
                 generate_radiance=True):
    envpath = 'build/compiled/Assets/envmaps'

    if not os.path.exists(envpath):
        os.makedirs(envpath)

    base_name = armutils.extract_filename(image_filepath).rsplit('.', 1)[0]

    # Assets to be generated
    output_file_irr = envpath + '/' + base_name + '_irradiance'
    if generate_radiance:
        output_file_rad = envpath + '/' + base_name + '_radiance'
        rad_format = 'jpg' if disable_hdr else 'hdr'

    # Radiance & irradiance exists, keep cache
    basep = envpath + '/' + base_name
    if os.path.exists(basep + '_irradiance.arm'):
        if not generate_radiance or os.path.exists(basep + '_radiance_0.' +
                                                   rad_format):
            add_irr_assets(output_file_irr)
            if generate_radiance:
                add_rad_assets(output_file_rad, rad_format, cached_num_mips)
            return cached_num_mips

    # Get paths
    sdk_path = armutils.get_sdk_path()

    if armutils.get_os() == 'win':
        cmft_path = sdk_path + '/armory/tools/cmft/cmft.exe'
        kraffiti_path = sdk_path + '/win32/Kha/Kore/Tools/kraffiti/kraffiti.exe'
    elif armutils.get_os() == 'mac':
        cmft_path = '"' + sdk_path + '/armory/tools/cmft/cmft-osx"'
        kraffiti_path = '"' + sdk_path + '/Kode Studio.app/Contents/Kha/Kore/Tools/kraffiti/kraffiti-osx"'
    else:
        cmft_path = '"' + sdk_path + '/armory/tools/cmft/cmft-linux64"'
        kraffiti_path = '"' + sdk_path + '/linux64/Kha/Kore/Tools/kraffiti/kraffiti-linux64"'

    output_gama_numerator = '1.0' if disable_hdr else '2.2'
    input_file = armutils.safe_assetpath(image_filepath)

    # Scale map
    wrd = bpy.data.worlds['Arm']
    target_w = int(wrd.generate_radiance_size)
    target_h = int(target_w / 2)
    scaled_file = output_file_rad + '.' + rad_format

    if armutils.get_os() == 'win':
        output = subprocess.check_output([ \
            kraffiti_path,
            'from=' + input_file.replace(' ', '\ '),
            'to=' + scaled_file.replace(' ', '\ '),
            'format=' + rad_format,
            'width=' + str(target_w),
            'height=' + str(target_h)])
    else:
        output = subprocess.check_output([ \
            kraffiti_path + \
            ' from="' + input_file + '"' + \
            ' to="' + scaled_file + '"' + \
            ' format=' + rad_format + \
            ' width=' + str(target_w) + \
            ' height=' + str(target_h)], shell=True)

    # Generate irradiance
    # gama_options = ''
    # if disable_hdr:
    # gama_options = \
    # ' --inputGammaNumerator 2.2' + \
    # ' --inputGammaDenominator 1.0' + \
    # ' --outputGammaNumerator 1.0' + \
    # ' --outputGammaDenominator ' + output_gama_numerator

    # Irradiance spherical harmonics
    if armutils.get_os() == 'win':
        subprocess.call([ \
            cmft_path,
            '--input', scaled_file.replace(' ', '\ '),
            '--filter', 'shcoeffs',
            #gama_options + \
            '--outputNum', '1',
            '--output0', output_file_irr])
    else:
        subprocess.call([ \
            cmft_path + \
            ' --input ' + '"' + scaled_file + '"' + \
            ' --filter shcoeffs' + \
            #gama_options + \
            ' --outputNum 1' + \
            ' --output0 ' + output_file_irr], shell=True)

    sh_to_json(output_file_irr)
    add_irr_assets(output_file_irr)

    # Mip-mapped radiance
    if generate_radiance == False:
        return cached_num_mips

    # 4096 = 256 face
    # 2048 = 128 face
    # 1024 = 64 face
    face_size = target_w / 8
    if target_w == 2048:
        mip_count = 9
    elif target_w == 1024:
        mip_count = 8
    else:
        mip_count = 7

    use_opencl = 'true' if wrd.arm_gpu_processing else 'false'

    if armutils.get_os() == 'win':
        subprocess.call([ \
            cmft_path,
            '--input', input_file.replace(' ', '\ '),
            '--filter', 'radiance',
            '--dstFaceSize', str(face_size),
            '--srcFaceSize', str(face_size),
            '--excludeBase', 'false',
            # '--mipCount', str(mip_count),
            '--glossScale', '7',
            '--glossBias', '3',
            '--lightingModel', 'blinnbrdf',
            '--edgeFixup', 'none',
            '--numCpuProcessingThreads', '4',
            '--useOpenCL', use_opencl,
            '--clVendor', 'anyGpuVendor',
            '--deviceType', 'gpu',
            '--deviceIndex', '0',
            '--generateMipChain', 'true',
            '--inputGammaNumerator', '2.2',
            '--inputGammaDenominator', '1.0',
            '--outputGammaNumerator', '1.0',
            '--outputGammaDenominator', output_gama_numerator,
            '--outputNum', '1',
            '--output0', output_file_rad.replace(' ', '\ '),
            '--output0params', 'hdr,rgbe,latlong'])
    else:
        subprocess.call([ \
            cmft_path + \
            ' --input "' + input_file + '"' + \
            ' --filter radiance' + \
            ' --dstFaceSize ' + str(face_size) + \
            ' --srcFaceSize ' + str(face_size) + \
            ' --excludeBase false' + \
            #' --mipCount ' + str(mip_count) + \
            ' --glossScale 7' + \
            ' --glossBias 3' + \
            ' --lightingModel blinnbrdf' + \
            ' --edgeFixup none' + \
            ' --numCpuProcessingThreads 4' + \
            ' --useOpenCL ' + use_opencl + \
            ' --clVendor anyGpuVendor' + \
            ' --deviceType gpu' + \
            ' --deviceIndex 0' + \
            ' --generateMipChain true' + \
            ' --inputGammaNumerator 2.2' + \
            ' --inputGammaDenominator 1.0' + \
            ' --outputGammaNumerator 1.0' + \
            ' --outputGammaDenominator ' + output_gama_numerator + \
            ' --outputNum 1' + \
            ' --output0 "' + output_file_rad + '"' + \
            ' --output0params hdr,rgbe,latlong'], shell=True)

    # Remove size extensions in file name
    mip_w = int(face_size * 4)
    mip_base = output_file_rad + '_'
    mip_num = 0
    while mip_w >= 4:
        mip_name = mip_base + str(mip_num)
        os.rename(
            mip_name + '_' + str(mip_w) + 'x' + str(int(mip_w / 2)) + '.hdr',
            mip_name + '.hdr')
        mip_w = int(mip_w / 2)
        mip_num += 1

    # Append mips
    generated_files = []
    for i in range(0, mip_count):
        generated_files.append(output_file_rad + '_' + str(i))

    # Convert to jpgs
    if disable_hdr is True:
        for f in generated_files:
            if armutils.get_os() == 'win':
                subprocess.call([ \
                    kraffiti_path,
                    'from=' + f + '.hdr',
                    'to=' + f + '.jpg',
                    'format=jpg'])
            else:
                subprocess.call([ \
                    kraffiti_path + \
                    ' from=' + f + '.hdr' + \
                    ' to=' + f + '.jpg' + \
                    ' format=jpg'], shell=True)
            os.remove(f + '.hdr')

    # Scale from (4x2 to 1x1>
    for i in range(0, 2):
        last = generated_files[-1]
        out = output_file_rad + '_' + str(mip_count + i)
        if armutils.get_os() == 'win':
            subprocess.call([ \
                kraffiti_path,
                'from=' + last + '.' + rad_format,
                'to=' + out + '.' + rad_format,
                'scale=0.5',
                'format=' + rad_format], shell=True)
        else:
            subprocess.call([ \
                kraffiti_path + \
                ' from=' + last + '.' + rad_format + \
                ' to=' + out + '.' + rad_format + \
                ' scale=0.5' + \
                ' format=' + rad_format], shell=True)
        generated_files.append(out)

    mip_count += 2

    add_rad_assets(output_file_rad, rad_format, mip_count)

    return mip_count