armory/blender/nodes_world.py

196 lines
6 KiB
Python
Raw Normal View History

2016-02-08 14:58:55 +01:00
import bpy
from bpy.types import NodeTree, Node, NodeSocket
from bpy.props import *
import os
import json
2016-06-07 09:38:49 +02:00
import write_probes
2016-07-19 19:42:46 +02:00
import assets
2016-07-20 17:33:17 +02:00
import utils
2016-02-08 14:58:55 +01:00
def register():
pass
#bpy.utils.register_module(__name__)
def unregister():
pass
#bpy.utils.unregister_module(__name__)
2016-06-30 13:22:05 +02:00
def find_node(node_group, to_node, target_socket):
for link in node_group.links:
if link.to_node == to_node and link.to_socket == target_socket:
return link.from_node
def get_output_node(tree):
for n in tree.nodes:
if n.type == 'OUTPUT_WORLD':
return n
2016-07-12 12:03:43 +02:00
def buildNodeTrees():
2016-02-08 14:58:55 +01:00
s = bpy.data.filepath.split(os.path.sep)
s.pop()
fp = os.path.sep.join(s)
os.chdir(fp)
# Make sure Assets dir exists
2016-08-22 21:56:28 +02:00
if not os.path.exists('build/compiled/Assets/materials'):
os.makedirs('build/compiled/Assets/materials')
2016-02-08 14:58:55 +01:00
# Export world nodes
2016-07-12 12:03:43 +02:00
world_outputs = []
2016-02-08 14:58:55 +01:00
for world in bpy.data.worlds:
2016-07-12 12:03:43 +02:00
output = buildNodeTree(world.name, world.node_tree)
world_outputs.append(output)
return world_outputs
2016-02-08 14:58:55 +01:00
2016-07-12 12:03:43 +02:00
def buildNodeTree(world_name, node_group):
2016-07-20 17:33:17 +02:00
output = {}
2016-08-25 00:26:01 +02:00
dat = {}
output['material_datas'] = [dat]
dat['name'] = world_name.replace('.', '_') + '_material'
2016-07-20 17:33:17 +02:00
context = {}
2016-08-25 00:26:01 +02:00
dat['contexts'] = [context]
context['name'] = 'env'
2016-07-20 17:33:17 +02:00
context['bind_constants'] = []
context['bind_textures'] = []
2016-06-03 17:18:38 +02:00
bpy.data.worlds[0].world_defs = ''
2016-06-30 13:22:05 +02:00
# Traverse world node tree
output_node = get_output_node(node_group)
if output_node != None:
parse_world_output(node_group, output_node, context)
2016-06-07 09:38:49 +02:00
2016-07-10 00:51:39 +02:00
# Clear to color if no texture or sky is provided
2016-08-23 22:55:46 +02:00
wrd = bpy.data.worlds[0]
if '_EnvSky' not in wrd.world_defs and '_EnvTex' not in wrd.world_defs:
wrd.world_defs += '_EnvCol'
2016-07-12 00:09:02 +02:00
# Irradiance json file name
2016-08-23 22:55:46 +02:00
base_name = wrd.name
2016-07-12 00:09:02 +02:00
bpy.data.cameras[0].world_envtex_name = base_name
write_probes.write_color_irradiance(base_name, bpy.data.cameras[0].world_envtex_color)
2016-07-10 00:51:39 +02:00
2016-07-17 20:29:53 +02:00
# Clouds enabled
2016-08-23 22:55:46 +02:00
if wrd.generate_clouds:
wrd.world_defs += '_EnvClouds'
2016-07-17 20:29:53 +02:00
# Shadows disabled
2016-08-23 22:55:46 +02:00
if wrd.generate_shadows == False:
wrd.world_defs += '_NoShadows'
2016-07-17 20:29:53 +02:00
2016-06-07 09:38:49 +02:00
# Enable probes
for cam in bpy.data.cameras:
if cam.is_probe:
2016-08-23 22:55:46 +02:00
wrd.world_defs += '_Probes'
2016-02-08 14:58:55 +01:00
2016-07-12 12:03:43 +02:00
# Data will be written after pipeline has been processed to gather all defines
return output
def write_output(output, asset_references, shader_references):
2016-08-25 00:26:01 +02:00
# Add datas to khafie
dir_name = 'env'
2016-06-30 13:22:05 +02:00
# Append world defs
2016-08-23 22:55:46 +02:00
wrd = bpy.data.worlds[0]
2016-08-25 00:26:01 +02:00
data_name = 'env' + wrd.world_defs
2016-08-23 22:55:46 +02:00
2016-06-30 13:22:05 +02:00
# Reference correct shader context
2016-08-25 00:26:01 +02:00
dat = output['material_datas'][0]
dat['shader'] = data_name + '/' + data_name
asset_references.append('build/compiled/ShaderDatas/' + dir_name + '/' + data_name + '.arm')
shader_references.append('build/compiled/Shaders/' + dir_name + '/' + data_name)
2016-06-30 13:22:05 +02:00
# Write material json
2016-08-22 21:56:28 +02:00
path = 'build/compiled/Assets/materials/'
2016-08-25 00:26:01 +02:00
asset_path = path + dat['name'] + '.arm'
2016-07-20 17:33:17 +02:00
utils.write_arm(asset_path, output)
2016-07-19 19:42:46 +02:00
assets.add(asset_path)
2016-06-30 13:22:05 +02:00
def parse_world_output(node_group, node, context):
if node.inputs[0].is_linked:
surface_node = find_node(node_group, node, node.inputs[0])
parse_surface(node_group, surface_node, context)
def parse_surface(node_group, node, context):
# Extract environment strength
if node.type == 'BACKGROUND':
# Strength
2016-08-11 22:24:45 +02:00
envmap_strength_const = {}
2016-08-25 00:26:01 +02:00
envmap_strength_const['name'] = 'envmapStrength'
2016-08-11 22:24:45 +02:00
envmap_strength_const['float'] = node.inputs[1].default_value
context['bind_constants'].append(envmap_strength_const)
2016-06-30 13:22:05 +02:00
if node.inputs[0].is_linked:
color_node = find_node(node_group, node, node.inputs[0])
2016-08-11 22:24:45 +02:00
parse_color(node_group, color_node, context, envmap_strength_const)
# Cache results
bpy.data.cameras[0].world_envtex_color = node.inputs[0].default_value
bpy.data.cameras[0].world_envtex_strength = envmap_strength_const['float']
2016-06-30 13:22:05 +02:00
2016-08-11 22:24:45 +02:00
def parse_color(node_group, node, context, envmap_strength_const):
2016-06-30 13:22:05 +02:00
# Env map included
if node.type == 'TEX_ENVIRONMENT':
2016-07-20 17:33:17 +02:00
texture = {}
context['bind_textures'].append(texture)
2016-08-25 00:26:01 +02:00
texture['name'] = 'envmap'
2016-08-23 22:55:46 +02:00
image = node.image
if image.packed_file != None:
# Extract packed data
unpack_path = utils.get_fp() + '/build/compiled/Assets/unpacked'
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(utils.safe_assetpath(image.filepath))
# Reference image name
2016-08-25 00:26:01 +02:00
texture['file'] = utils.extract_filename_noext(image.filepath)
texture['file'] = utils.safe_filename(texture['file'])
2016-08-23 22:55:46 +02:00
2016-06-30 13:22:05 +02:00
# Generate prefiltered envmaps
generate_radiance = bpy.data.worlds[0].generate_radiance
2016-08-25 00:26:01 +02:00
bpy.data.cameras[0].world_envtex_name = texture['file']
2016-08-23 22:55:46 +02:00
disable_hdr = image.filepath.endswith('.jpg')
2016-06-30 13:22:05 +02:00
mip_count = bpy.data.cameras[0].world_envtex_num_mips
2016-07-17 20:29:53 +02:00
mip_count = write_probes.write_probes(node.image.filepath, disable_hdr, mip_count, generate_radiance=generate_radiance)
2016-06-30 13:22:05 +02:00
bpy.data.cameras[0].world_envtex_num_mips = mip_count
# Append envtex define
bpy.data.worlds[0].world_defs += '_EnvTex'
# Append LDR define
if disable_hdr:
2016-07-12 12:03:43 +02:00
bpy.data.worlds[0].world_defs += '_EnvLDR'
2016-06-30 13:22:05 +02:00
# Append radiance degine
if generate_radiance:
bpy.data.worlds[0].world_defs += '_Rad'
# Append sky define
elif node.type == 'TEX_SKY':
bpy.data.worlds[0].world_defs += '_EnvSky'
# Append sky properties to material
2016-07-20 17:33:17 +02:00
const = {}
2016-08-25 00:26:01 +02:00
const['name'] = 'sunDirection'
2016-07-12 00:09:02 +02:00
sun_direction = [node.sun_direction[0], node.sun_direction[1], node.sun_direction[2]]
2016-06-30 13:22:05 +02:00
sun_direction[1] *= -1 # Fix Y orientation
2016-07-20 17:33:17 +02:00
const['vec3'] = list(sun_direction)
context['bind_constants'].append(const)
2016-06-30 13:22:05 +02:00
bpy.data.cameras[0].world_envtex_sun_direction = sun_direction
bpy.data.cameras[0].world_envtex_turbidity = node.turbidity
bpy.data.cameras[0].world_envtex_ground_albedo = node.ground_albedo
# Irradiance json file name
base_name = bpy.data.worlds[0].name
bpy.data.cameras[0].world_envtex_name = base_name
write_probes.write_sky_irradiance(base_name)
2016-08-11 22:24:45 +02:00
# Adjust strength to match Cycles
envmap_strength_const['float'] *= 0.25