Files
armorpaint/base/Sources/Args.ts
T

193 lines
5.1 KiB
TypeScript
Raw Normal View History

2024-01-17 18:53:31 +01:00
class Args {
static useArgs = false;
static assetPath = "";
static background = false;
///if (is_paint || is_lab)
static exportTextures = false;
static exportTexturesType = "";
static exportTexturesPreset = "";
static exportTexturesPath = "";
///end
///if (is_paint || is_sculpt)
static reimportMesh = false;
static exportMesh = false;
static exportMeshPath = "";
///end
///if is_paint
static exportMaterial = false;
static exportMaterialPath = "";
///end
static parse = () => {
2024-02-10 19:26:53 +01:00
if (krom_get_arg_count() > 1) {
2024-01-17 18:53:31 +01:00
Args.useArgs = true;
let i = 0;
2024-02-10 19:26:53 +01:00
while (i < krom_get_arg_count()) {
2024-01-17 18:53:31 +01:00
// Process each arg
2024-02-10 19:26:53 +01:00
let currentArg = krom_get_arg(i);
2024-01-17 18:53:31 +01:00
if (Path.isProject(currentArg)) {
Project.filepath = currentArg;
}
else if (currentArg == "--b" || currentArg == "--background") {
Args.background = true;
}
///if (is_paint || is_lab)
else if (Path.isTexture(currentArg)) {
Args.assetPath = currentArg;
}
2024-02-10 19:26:53 +01:00
else if (currentArg == "--export-textures" && (i + 3) <= krom_get_arg_count()) {
2024-01-17 18:53:31 +01:00
Args.exportTextures = true;
++i;
2024-02-10 19:26:53 +01:00
Args.exportTexturesType = krom_get_arg(i);
2024-01-17 18:53:31 +01:00
++i;
2024-02-10 19:26:53 +01:00
Args.exportTexturesPreset = krom_get_arg(i);
2024-01-17 18:53:31 +01:00
++i;
2024-02-10 19:26:53 +01:00
Args.exportTexturesPath = krom_get_arg(i);
2024-01-17 18:53:31 +01:00
}
///end
///if (is_paint || is_sculpt)
else if (currentArg == "--reload-mesh") {
Args.reimportMesh = true;
}
2024-02-10 19:26:53 +01:00
else if (currentArg == "--export-mesh" && (i + 1) <= krom_get_arg_count()) {
2024-01-17 18:53:31 +01:00
Args.exportMesh = true;
++i;
2024-02-10 19:26:53 +01:00
Args.exportMeshPath = krom_get_arg(i);
2024-01-17 18:53:31 +01:00
}
else if (Path.isMesh(currentArg) ||
(i > 1 && !currentArg.startsWith("-") && Path.isFolder(currentArg))) {
Args.assetPath = currentArg;
}
///end
///if is_paint
2024-02-10 19:26:53 +01:00
else if (currentArg == "--export-material" && (i + 1) <= krom_get_arg_count()) {
2024-01-17 18:53:31 +01:00
Args.exportMaterial = true;
++i;
2024-02-10 19:26:53 +01:00
Args.exportMaterialPath = krom_get_arg(i);
2024-01-17 18:53:31 +01:00
}
///end
++i;
}
}
}
static run = () => {
if (Args.useArgs) {
2024-02-06 19:32:21 +01:00
app_notify_on_init(() => {
2024-01-17 18:53:31 +01:00
if (Project.filepath != "") {
ImportArm.runProject(Project.filepath);
}
else if (Args.assetPath != "") {
ImportAsset.run(Args.assetPath, -1, -1, false);
///if is_paint
if (Path.isTexture(Args.assetPath)) {
2024-01-21 15:18:44 +01:00
UIBase.show2DView(View2DType.View2DAsset);
2024-01-17 18:53:31 +01:00
}
///end
}
///if (is_paint || is_sculpt)
else if (Args.reimportMesh) {
Project.reimportMesh();
}
///end
///if (is_paint || is_lab)
if (Args.exportTextures) {
if (Args.exportTexturesType == "png" ||
Args.exportTexturesType == "jpg" ||
Args.exportTexturesType == "exr16" ||
Args.exportTexturesType == "exr32") {
if (Path.isFolder(Args.exportTexturesPath)) {
// Applying the correct format type from args
if (Args.exportTexturesType == "png") {
///if is_paint
Base.bitsHandle.position = TextureBits.Bits8;
///end
Context.raw.formatType = TextureLdrFormat.FormatPng;
}
else if (Args.exportTexturesType == "jpg") {
///if is_paint
Base.bitsHandle.position = TextureBits.Bits8;
///end
Context.raw.formatType = TextureLdrFormat.FormatJpg;
}
else if (Args.exportTexturesType == "exr16") {
///if is_paint
Base.bitsHandle.position = TextureBits.Bits16;
///end
}
else if (Args.exportTexturesType == "exr32") {
///if is_paint
Base.bitsHandle.position = TextureBits.Bits32;
///end
}
///if is_paint
Context.raw.layersExport = ExportMode.ExportVisible;
///end
// Get export preset and apply the correct one from args
BoxExport.files = File.readDirectory(Path.data() + Path.sep + "export_presets");
for (let i = 0; i < BoxExport.files.length; ++i) {
BoxExport.files[i] = BoxExport.files[i].substr(0, BoxExport.files[i].length - 5); // Strip .json
}
let file = "export_presets/" + BoxExport.files[0] + ".json";
for (let f of BoxExport.files) if (f == Args.exportTexturesPreset) {
file = "export_presets/" + BoxExport.files[BoxExport.files.indexOf(f)] + ".json";
}
2024-02-06 19:32:21 +01:00
data_get_blob(file, (blob: ArrayBuffer) => {
2024-02-05 20:57:20 +01:00
BoxExport.preset = JSON.parse(sys_buffer_to_string(blob));
2024-02-06 19:32:21 +01:00
data_delete_blob("export_presets/" + file);
2024-01-17 18:53:31 +01:00
});
// Export queue
2024-02-06 19:32:21 +01:00
app_notify_on_init(() => {
2024-01-17 18:53:31 +01:00
ExportTexture.run(Args.exportTexturesPath);
});
}
else {
2024-02-10 19:26:53 +01:00
krom_log(tr("Invalid export directory"));
2024-01-17 18:53:31 +01:00
}
}
else {
2024-02-10 19:26:53 +01:00
krom_log(tr("Invalid texture type"));
2024-01-17 18:53:31 +01:00
}
}
///end
///if (is_paint || is_sculpt)
else if (Args.exportMesh) {
if (Path.isFolder(Args.exportMeshPath)) {
let f = UIFiles.filename;
if (f == "") f = tr("untitled");
ExportMesh.run(Args.exportMeshPath + Path.sep + f, null, false);
}
else {
2024-02-10 19:26:53 +01:00
krom_log(tr("Invalid export directory"));
2024-01-17 18:53:31 +01:00
}
}
///end
///if is_paint
else if (Args.exportMaterial) {
Context.raw.writeIconOnExport = true;
ExportArm.runMaterial(Args.exportMaterialPath);
}
///end
2024-02-05 20:57:20 +01:00
if (Args.background) sys_stop();
2024-01-17 18:53:31 +01:00
});
}
}
}