armory/blender/write_data.py

84 lines
2.9 KiB
Python
Raw Normal View History

2015-12-07 21:05:27 +01:00
import bpy
import os
# Write khafile.js
2016-01-11 13:07:44 +01:00
def write_khafilejs(shader_references):
2016-01-17 22:38:46 +01:00
# Merge duplicates and sort
shader_references = sorted(list(set(shader_references)))
2015-12-07 21:05:27 +01:00
with open('khafile.js', 'w') as f:
f.write(
"""// Auto-generated
var project = new Project('""" + bpy.data.worlds[0]['CGProjectName'] + """');
project.addSources('Sources');
project.addShaders('Sources/Shaders/**');
project.addAssets('Assets/**');
2016-01-11 13:07:44 +01:00
project.addLibrary('cyclesgame');
project.addAssets('Libraries/cyclesgame/Assets/**');
2015-12-07 21:05:27 +01:00
""")
2016-01-11 13:07:44 +01:00
for ref in shader_references:
2016-01-28 01:35:43 +01:00
# ArmoryExporter.pipeline_pass instead of split
2016-01-28 14:47:46 +01:00
base_name = ref.split('_', 1)[0] + "/"
2016-02-07 23:03:52 +01:00
f.write("project.addAssets('compiled/ShaderResources/" + base_name + "" + ref + ".json');\n")
f.write("project.addShaders('compiled/Shaders/" + base_name + "" + ref + ".frag.glsl');\n")
f.write("project.addShaders('compiled/Shaders/" + base_name + "" + ref + ".vert.glsl');\n")
# TODO: properly include all shader contexts
defsarr = ref.split('_', 1) # Get shader defs and append to shadowmap shader name
defs = ''
if len(defsarr) > 1:
defs = '_' + defsarr[1]
f.write("project.addShaders('compiled/Shaders/" + base_name + "" + 'shadowmap' + defs + ".frag.glsl');\n")
f.write("project.addShaders('compiled/Shaders/" + base_name + "" + 'shadowmap' + defs + ".vert.glsl');\n")
2016-01-11 13:07:44 +01:00
2016-01-11 13:50:54 +01:00
if bpy.data.worlds[0]['CGPhysics'] != 0:
f.write("\nproject.addDefine('WITH_PHYSICS')\n")
f.write("project.addLibrary('haxebullet')\n")
f.write("\nreturn project;\n")
2016-01-11 13:07:44 +01:00
2015-12-07 21:05:27 +01:00
# Write Main.hx
def write_main():
#if not os.path.isfile('Sources/Main.hx'):
with open('Sources/Main.hx', 'w') as f:
f.write(
"""// Auto-generated
package ;
class Main {
2016-01-11 21:10:33 +01:00
public static inline var projectName = '""" + bpy.data.worlds[0]['CGProjectName'] + """';
public static inline var projectPackage = '""" + bpy.data.worlds[0]['CGProjectPackage'] + """';
2015-12-07 21:05:27 +01:00
static inline var projectWidth = """ + str(bpy.data.worlds[0]['CGProjectWidth']) + """;
static inline var projectHeight = """ + str(bpy.data.worlds[0]['CGProjectHeight']) + """;
2016-01-25 16:46:13 +01:00
public static inline var projectScene = '""" + str(bpy.data.worlds[0]['CGProjectScene']) + """';
2015-12-07 21:05:27 +01:00
public static function main() {
lue.sys.CompileTime.importPackage('lue.trait');
lue.sys.CompileTime.importPackage('cycles.trait');
2015-12-08 23:57:00 +01:00
lue.sys.CompileTime.importPackage('""" + bpy.data.worlds[0]['CGProjectPackage'] + """');
2016-01-11 13:50:54 +01:00
#if (js && WITH_PHYSICS)
2015-12-07 21:05:27 +01:00
untyped __js__("
function loadScript(url, callback) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onreadystatechange = callback;
script.onload = callback;
head.appendChild(script);
}
");
untyped loadScript('ammo.js', start);
#else
start();
#end
}
static function start() {
kha.System.init(projectName, projectWidth, projectHeight, function() {
new lue.App(cycles.Root);
});
}
}
""")