Files
armorpaint/Sources/arm/io/ExportArm.hx
T

137 lines
3.8 KiB
Haxe
Raw Normal View History

2019-06-19 09:40:14 +02:00
package arm.io;
2019-06-20 11:23:01 +02:00
import haxe.Json;
import zui.Nodes;
2019-06-19 09:40:14 +02:00
import iron.data.SceneFormat;
2019-06-20 11:23:01 +02:00
import iron.system.ArmPack;
2019-06-19 09:40:14 +02:00
import arm.ui.UITrait;
2019-06-19 18:45:36 +02:00
import arm.ui.UINodes;
import arm.util.Lz4;
import arm.util.Path;
2019-06-19 22:42:17 +02:00
import arm.Project;
2019-06-20 12:30:31 +02:00
using StringTools;
2019-06-19 09:40:14 +02:00
class ExportArm {
public static function run(path:String) {
2019-06-23 14:50:41 +02:00
var raw:TSceneFormat = { mesh_datas: [ Context.paintObject.data.raw ] };
2019-06-20 11:23:01 +02:00
var b = ArmPack.encode(raw);
2019-06-20 12:30:31 +02:00
if (!path.endsWith(".arm")) path += ".arm";
2019-06-19 09:40:14 +02:00
Krom.fileSaveBytes(path, b.getData());
}
2019-06-19 18:45:36 +02:00
public static function runProject() {
2019-06-20 11:23:01 +02:00
var mnodes:Array<TNodeCanvas> = [];
var bnodes:Array<TNodeCanvas> = [];
2019-06-19 18:45:36 +02:00
2019-06-23 14:50:41 +02:00
for (m in Project.materials) {
2019-06-20 11:23:01 +02:00
var c:TNodeCanvas = Json.parse(Json.stringify(UINodes.inst.canvasMap.get(m)));
2019-06-19 18:45:36 +02:00
for (n in c.nodes) {
if (n.type == "TEX_IMAGE") { // Convert image path from absolute to relative
2019-06-23 14:50:41 +02:00
var sameDrive = Project.filepath.charAt(0) == n.buttons[0].data.charAt(0);
2019-06-19 18:45:36 +02:00
if (sameDrive) {
2019-06-23 14:50:41 +02:00
n.buttons[0].data = Path.toRelative(Project.filepath, n.buttons[0].data);
2019-06-19 18:45:36 +02:00
}
}
}
mnodes.push(c);
}
2019-06-23 14:50:41 +02:00
for (b in Project.brushes) bnodes.push(UINodes.inst.canvasBrushMap.get(b));
2019-06-19 18:45:36 +02:00
var md:Array<TMeshData> = [];
2019-06-24 22:35:17 +02:00
for (p in Project.paintObjects) md.push(p.data.raw);
2019-06-19 18:45:36 +02:00
2019-09-17 22:28:15 +02:00
var texture_files:Array<String> = [];
2019-06-23 14:50:41 +02:00
for (a in Project.assets) {
2019-06-19 18:45:36 +02:00
// Convert image path from absolute to relative
2019-06-23 14:50:41 +02:00
var sameDrive = Project.filepath.charAt(0) == a.file.charAt(0);
2019-06-19 18:45:36 +02:00
if (sameDrive) {
2019-09-17 22:28:15 +02:00
texture_files.push(Path.toRelative(Project.filepath, a.file));
2019-06-19 18:45:36 +02:00
}
else {
2019-09-17 22:28:15 +02:00
texture_files.push(a.file);
}
}
var mesh_files:Array<String> = [];
for (file in Project.meshAssets) {
// Convert mesh path from absolute to relative
var sameDrive = Project.filepath.charAt(0) == file.charAt(0);
if (sameDrive) {
mesh_files.push(Path.toRelative(Project.filepath, file));
}
else {
mesh_files.push(file);
2019-06-19 18:45:36 +02:00
}
}
2019-07-10 07:59:58 +02:00
var bitsPos = UITrait.inst.bitsHandle.position;
var bpp = bitsPos == 0 ? 8 : bitsPos == 1 ? 16 : 32;
2019-06-19 18:45:36 +02:00
var ld:Array<TLayerData> = [];
2019-06-23 14:50:41 +02:00
for (l in Project.layers) {
2019-06-19 18:45:36 +02:00
ld.push({
res: l.texpaint.width,
2019-07-10 07:59:58 +02:00
bpp: bpp,
2019-06-19 18:45:36 +02:00
texpaint: Lz4.encode(l.texpaint.getPixels()),
texpaint_nor: Lz4.encode(l.texpaint_nor.getPixels()),
texpaint_pack: Lz4.encode(l.texpaint_pack.getPixels()),
texpaint_mask: l.texpaint_mask != null ? Lz4.encode(l.texpaint_mask.getPixels()) : null,
opacity_mask: l.maskOpacity,
2019-06-23 14:50:41 +02:00
material_mask: l.material_mask != null ? Project.materials.indexOf(l.material_mask) : -1,
2019-06-19 18:45:36 +02:00
object_mask: l.objectMask
});
}
2019-06-23 14:50:41 +02:00
Project.raw = {
2019-06-20 11:23:01 +02:00
version: App.version,
2019-06-19 18:45:36 +02:00
material_nodes: mnodes,
brush_nodes: bnodes,
mesh_datas: md,
layer_datas: ld,
2019-09-17 22:28:15 +02:00
assets: texture_files,
mesh_assets: mesh_files
2019-06-19 18:45:36 +02:00
};
2019-06-23 14:50:41 +02:00
var bytes = ArmPack.encode(Project.raw);
Krom.fileSaveBytes(Project.filepath, bytes.getData());
2019-06-19 18:45:36 +02:00
}
2019-09-01 09:28:54 +02:00
public static function runMaterial(path:String) {
var mnodes:Array<TNodeCanvas> = [];
var m = Context.material;
var c:TNodeCanvas = Json.parse(Json.stringify(UINodes.inst.canvasMap.get(m)));
for (n in c.nodes) {
if (n.type == "TEX_IMAGE") { // Convert image path from absolute to relative
var sameDrive = Project.filepath.charAt(0) == n.buttons[0].data.charAt(0);
if (sameDrive) {
n.buttons[0].data = Path.toRelative(Project.filepath, n.buttons[0].data);
}
}
}
mnodes.push(c);
2019-09-17 22:28:15 +02:00
var texture_files:Array<String> = [];
2019-09-01 09:28:54 +02:00
for (a in Project.assets) {
// Convert image path from absolute to relative
var sameDrive = Project.filepath.charAt(0) == a.file.charAt(0);
if (sameDrive) {
2019-09-17 22:28:15 +02:00
texture_files.push(Path.toRelative(Project.filepath, a.file));
2019-09-01 09:28:54 +02:00
}
else {
2019-09-17 22:28:15 +02:00
texture_files.push(a.file);
2019-09-01 09:28:54 +02:00
}
}
var raw = {
version: App.version,
material_nodes: mnodes,
2019-09-17 22:28:15 +02:00
assets: texture_files
2019-09-01 09:28:54 +02:00
};
var bytes = ArmPack.encode(raw);
if (!path.endsWith(".arm")) path += ".arm";
Krom.fileSaveBytes(path, bytes.getData());
}
2019-06-19 09:40:14 +02:00
}