armory/blender/arm/make_world.py

220 lines
8.2 KiB
Python
Raw Normal View History

2016-02-08 14:58:55 +01:00
import bpy
2017-12-13 00:10:30 +01:00
import os
2016-02-08 14:58:55 +01:00
from bpy.types import NodeTree, Node, NodeSocket
from bpy.props import *
2017-03-15 12:30:14 +01:00
import arm.write_probes as write_probes
import arm.assets as assets
import arm.utils
2017-11-20 14:32:36 +01:00
import arm.node_utils as node_utils
2017-03-15 12:30:14 +01:00
import arm.log as log
2017-12-05 23:06:24 +01:00
import arm.make_state as state
2016-02-08 14:58:55 +01:00
2017-12-13 00:10:30 +01:00
def build():
worlds = []
for scene in bpy.data.scenes:
if scene.arm_export and scene.world != None and scene.world not in worlds:
worlds.append(scene.world)
build_node_tree(scene.world)
2016-02-08 14:58:55 +01:00
2016-10-19 13:28:06 +02:00
def build_node_tree(world):
2017-05-13 17:17:43 +02:00
wname = arm.utils.safestr(world.name)
2017-08-30 09:19:10 +02:00
wrd = bpy.data.worlds['Arm']
wrd.world_defs = ''
rpdat = arm.utils.get_rp()
# Traverse world node tree
2017-08-30 09:19:10 +02:00
parsed = False
if world.node_tree != None:
2017-11-20 14:32:36 +01:00
output_node = node_utils.get_node_by_type(world.node_tree, 'OUTPUT_WORLD')
2017-08-30 09:19:10 +02:00
if output_node != None:
2017-12-13 00:10:30 +01:00
parse_world_output(world, output_node)
2017-08-30 09:19:10 +02:00
parsed = True
if parsed == False:
2017-11-13 10:19:07 +01:00
solid_mat = rpdat.arm_material_model == 'Solid'
if wrd.arm_irradiance and not solid_mat:
2017-08-30 09:19:10 +02:00
wrd.world_defs += '_Irr'
2017-11-16 10:43:34 +01:00
c = world.horizon_color
world.arm_envtex_color = [c[0], c[1], c[2], 1.0]
2017-12-13 00:10:30 +01:00
world.arm_envtex_strength = 1.0
# Clear to color if no texture or sky is provided
if '_EnvSky' not in wrd.world_defs and '_EnvTex' not in wrd.world_defs:
2016-11-03 19:07:16 +01:00
if '_EnvImg' not in wrd.world_defs:
wrd.world_defs += '_EnvCol'
# Irradiance json file name
2017-08-21 12:17:55 +02:00
world.arm_envtex_name = wname
world.arm_envtex_irr_name = wname
write_probes.write_color_irradiance(wname, world.arm_envtex_color)
2017-11-10 15:46:05 +01:00
# film_transparent
if bpy.context.scene != None and bpy.context.scene.cycles != None and bpy.context.scene.cycles.film_transparent:
wrd.world_defs += '_EnvTransp'
wrd.world_defs += '_EnvCol'
# Clouds enabled
2017-08-21 20:16:06 +02:00
if rpdat.arm_clouds:
wrd.world_defs += '_EnvClouds'
2017-12-20 22:56:22 +01:00
if '_EnvSky' in wrd.world_defs or '_EnvTex' in wrd.world_defs or '_EnvImg' in wrd.world_defs or '_EnvClouds' in wrd.world_defs:
wrd.world_defs += '_EnvStr'
2016-11-08 15:14:56 +01:00
2017-12-13 00:10:30 +01:00
def parse_world_output(world, node):
if node.inputs[0].is_linked:
2017-11-20 14:32:36 +01:00
surface_node = node_utils.find_node_by_link(world.node_tree, node, node.inputs[0])
2017-12-13 00:10:30 +01:00
parse_surface(world, surface_node)
2017-12-13 00:10:30 +01:00
def parse_surface(world, node):
2017-01-28 20:00:04 +01:00
wrd = bpy.data.worlds['Arm']
rpdat = arm.utils.get_rp()
2017-11-13 10:19:07 +01:00
solid_mat = rpdat.arm_material_model == 'Solid'
2017-01-28 20:00:04 +01:00
# Extract environment strength
if node.type == 'BACKGROUND':
2017-01-28 20:00:04 +01:00
# Append irradiance define
2017-11-13 10:19:07 +01:00
if wrd.arm_irradiance and not solid_mat:
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_Irr'
2017-01-28 20:00:04 +01:00
2017-12-13 00:10:30 +01:00
world.arm_envtex_color = node.inputs[0].default_value
world.arm_envtex_strength = node.inputs[1].default_value
# Strength
if node.inputs[0].is_linked:
2017-11-20 14:32:36 +01:00
color_node = node_utils.find_node_by_link(world.node_tree, node, node.inputs[0])
2017-12-13 00:10:30 +01:00
parse_color(world, color_node)
2017-12-13 00:10:30 +01:00
def parse_color(world, node):
2016-11-05 20:57:04 +01:00
wrd = bpy.data.worlds['Arm']
rpdat = arm.utils.get_rp()
2017-10-01 19:42:47 +02:00
mobile_mat = rpdat.arm_material_model == 'Mobile' or rpdat.arm_material_model == 'Solid'
2016-11-05 20:57:04 +01:00
# Env map included
2016-11-24 23:24:55 +01:00
if node.type == 'TEX_ENVIRONMENT' and node.image != None:
2017-01-04 00:13:52 +01:00
image = node.image
filepath = image.filepath
2017-05-13 17:17:43 +02:00
if image.packed_file == None and not os.path.isfile(arm.utils.asset_path(filepath)):
2017-01-04 00:13:52 +01:00
log.warn(world.name + ' - unable to open ' + image.filepath)
return
# Reference image name
2017-12-13 00:10:30 +01:00
tex_file = arm.utils.extract_filename(image.filepath)
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':
2017-12-13 00:10:30 +01:00
tex_file = base[0] + '.hdr'
target_format = 'HDR'
else:
2017-12-13 00:10:30 +01:00
tex_file = base[0] + '.jpg'
target_format = 'JPEG'
if image.packed_file != None:
# Extract packed data
2017-05-23 01:03:44 +02:00
unpack_path = arm.utils.get_fp_build() + '/compiled/Assets/unpacked'
if not os.path.exists(unpack_path):
os.makedirs(unpack_path)
2017-12-13 00:10:30 +01:00
unpack_filepath = unpack_path + '/' + tex_file
filepath = unpack_filepath
if do_convert:
if not os.path.isfile(unpack_filepath):
2017-03-15 12:30:14 +01:00
arm.utils.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:
2017-12-13 00:10:30 +01:00
converted_path = arm.utils.get_fp_build() + '/compiled/Assets/unpacked/' + tex_file
filepath = converted_path
# TODO: delete cache when file changes
if not os.path.isfile(converted_path):
2017-03-15 12:30:14 +01:00
arm.utils.write_image(image, converted_path, file_format=target_format)
assets.add(converted_path)
else:
# Link image path to assets
2017-05-13 17:17:43 +02:00
assets.add(arm.utils.asset_path(image.filepath))
# Generate prefiltered envmaps
2017-12-13 00:10:30 +01:00
world.arm_envtex_name = tex_file
world.arm_envtex_irr_name = tex_file.rsplit('.', 1)[0]
disable_hdr = target_format == 'JPEG'
2017-08-21 12:17:55 +02:00
mip_count = world.arm_envtex_num_mips
mip_count = write_probes.write_probes(filepath, disable_hdr, mip_count, arm_radiance=wrd.arm_radiance)
2017-08-21 12:17:55 +02:00
world.arm_envtex_num_mips = mip_count
# Append envtex define
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_EnvTex'
# Append LDR define
if disable_hdr:
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_EnvLDR'
# Append radiance define
2017-10-01 19:09:09 +02:00
if wrd.arm_irradiance and wrd.arm_radiance and not mobile_mat:
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_Rad'
2016-11-03 19:07:16 +01:00
# Static image background
2017-12-13 00:10:30 +01:00
elif node.type == 'TEX_IMAGE':
2016-11-03 19:07:16 +01:00
image = node.image
filepath = image.filepath
if image.packed_file != None:
# Extract packed data
2017-05-23 01:03:44 +02:00
filepath = arm.utils.build_dir() + '/compiled/Assets/unpacked'
2017-03-15 12:30:14 +01:00
unpack_path = arm.utils.get_fp() + filepath
2016-11-03 19:07:16 +01:00
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
2017-05-13 17:17:43 +02:00
assets.add(arm.utils.asset_path(image.filepath))
2016-11-03 19:07:16 +01:00
# Reference image name
2017-12-13 00:10:30 +01:00
tex_file = arm.utils.extract_filename(image.filepath)
world.arm_envtex_name = tex_file
2016-11-03 19:07:16 +01:00
# Append sky define
elif node.type == 'TEX_SKY':
2016-12-21 00:51:04 +01:00
# Match to cycles
2017-12-13 00:10:30 +01:00
world.arm_envtex_strength *= 0.1
2016-10-12 17:52:27 +02:00
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_EnvSky'
2017-11-27 14:29:21 +01:00
assets.add_khafile_def('arm_hosek')
2017-12-13 00:10:30 +01:00
world.arm_envtex_sun_direction = [node.sun_direction[0], node.sun_direction[1], node.sun_direction[2]]
2017-08-21 12:17:55 +02:00
world.arm_envtex_turbidity = node.turbidity
world.arm_envtex_ground_albedo = node.ground_albedo
# Irradiance json file name
2017-05-13 17:17:43 +02:00
wname = arm.utils.safestr(world.name)
2017-08-21 12:17:55 +02:00
world.arm_envtex_irr_name = wname
2017-05-13 17:17:43 +02:00
write_probes.write_sky_irradiance(wname)
# Radiance
2017-10-01 19:09:09 +02:00
if wrd.arm_radiance_sky and wrd.arm_radiance and wrd.arm_irradiance and not mobile_mat:
2017-09-06 13:28:59 +02:00
wrd.world_defs += '_Rad'
2017-10-25 19:48:45 +02:00
hosek_path = 'armory/Assets/hosek/'
2017-03-15 12:30:14 +01:00
sdk_path = arm.utils.get_sdk_path()
2016-10-12 17:52:27 +02:00
# Use fake maps for now
2016-11-05 20:57:04 +01:00
assets.add(sdk_path + hosek_path + 'hosek_radiance.hdr')
for i in range(0, 8):
2016-11-05 20:57:04 +01:00
assets.add(sdk_path + hosek_path + 'hosek_radiance_' + str(i) + '.hdr')
2017-08-21 12:17:55 +02:00
world.arm_envtex_name = 'hosek'
world.arm_envtex_num_mips = 8