oxihuman-cli 0.1.2

Command-line interface for OxiHuman body generation and export
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0

//! Export subcommands: stl, collada, gltf-sep, svg, lod-export, variant-pack, report.

use anyhow::{bail, Context, Result};
use std::path::PathBuf;

use oxihuman_core::policy::{Policy, PolicyProfile};
use oxihuman_export::collada::{export_collada, ColladaExportOptions};
use oxihuman_export::gltf_sep::export_gltf_sep;
use oxihuman_export::lod_export::export_default_lod_pack;
use oxihuman_export::params_json::import_params;
use oxihuman_export::report_html::{
    export_html_report, mesh_report_from_buffers, PipelineReportData,
};
use oxihuman_export::stl::{export_stl_ascii, export_stl_binary};
use oxihuman_export::svg::{export_svg, export_uv_svg, SvgExportOptions, SvgProjection};
use oxihuman_export::variant_pack::{variant_entry, write_variant_pack};
use oxihuman_mesh::MeshBuffers;
use oxihuman_morph::params::ParamState;
use oxihuman_morph::presets::BodyPreset;

use crate::utils::{build_mesh_from_base, load_params};

// ── stl ───────────────────────────────────────────────────────────────────────

#[allow(clippy::too_many_arguments)]
pub fn cmd_stl(args: &[String]) -> Result<()> {
    let mut base: Option<PathBuf> = None;
    let mut output: Option<PathBuf> = None;
    let mut targets: Option<PathBuf> = None;
    let mut params_src: Option<String> = None;
    let mut preset_name: Option<String> = None;
    let mut binary = false;

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--base" => {
                i += 1;
                base = Some(PathBuf::from(&args[i]));
            }
            "--output" => {
                i += 1;
                output = Some(PathBuf::from(&args[i]));
            }
            "--targets" => {
                i += 1;
                targets = Some(PathBuf::from(&args[i]));
            }
            "--params" => {
                i += 1;
                params_src = Some(args[i].clone());
            }
            "--preset" => {
                i += 1;
                preset_name = Some(args[i].clone());
            }
            "--binary" => {
                binary = true;
            }
            other => bail!("unknown option: {}", other),
        }
        i += 1;
    }

    let base = base.context("--base is required for stl")?;
    let output = output.context("--output is required for stl")?;

    if !base.exists() {
        bail!("base mesh not found: {}", base.display());
    }

    let params = if let Some(name) = &preset_name {
        BodyPreset::from_name(name)
            .ok_or_else(|| anyhow::anyhow!("unknown preset '{}'", name))?
            .params()
    } else if let Some(src) = &params_src {
        load_params(src)?
    } else {
        ParamState::default()
    };

    let policy = Policy::new(PolicyProfile::Standard);
    let mesh = build_mesh_from_base(&base, targets.as_deref(), params, policy)?;

    let tri_count = mesh.indices.len() / 3;
    if binary {
        export_stl_binary(&mesh, &output)
            .with_context(|| format!("writing binary STL to {}", output.display()))?;
        println!(
            "Written STL (binary): {} triangles → {}",
            tri_count,
            output.display()
        );
    } else {
        let stem = output
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or_default();
        export_stl_ascii(&mesh, &output, stem)
            .with_context(|| format!("writing ASCII STL to {}", output.display()))?;
        println!(
            "Written STL (ascii): {} triangles → {}",
            tri_count,
            output.display()
        );
    }

    Ok(())
}

// ── collada ───────────────────────────────────────────────────────────────────

#[allow(clippy::too_many_arguments)]
pub fn cmd_collada(args: &[String]) -> Result<()> {
    let mut base: Option<PathBuf> = None;
    let mut output: Option<PathBuf> = None;
    let mut targets: Option<PathBuf> = None;
    let mut params_src: Option<String> = None;
    let mut preset_name: Option<String> = None;
    let mut author: Option<String> = None;

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--base" => {
                i += 1;
                base = Some(PathBuf::from(&args[i]));
            }
            "--output" => {
                i += 1;
                output = Some(PathBuf::from(&args[i]));
            }
            "--targets" => {
                i += 1;
                targets = Some(PathBuf::from(&args[i]));
            }
            "--params" => {
                i += 1;
                params_src = Some(args[i].clone());
            }
            "--preset" => {
                i += 1;
                preset_name = Some(args[i].clone());
            }
            "--author" => {
                i += 1;
                author = Some(args[i].clone());
            }
            other => bail!("unknown option: {}", other),
        }
        i += 1;
    }

    let base = base.context("--base is required for collada")?;
    let output = output.context("--output is required for collada")?;

    if !base.exists() {
        bail!("base mesh not found: {}", base.display());
    }

    let params = if let Some(name) = &preset_name {
        BodyPreset::from_name(name)
            .ok_or_else(|| anyhow::anyhow!("unknown preset '{}'", name))?
            .params()
    } else if let Some(src) = &params_src {
        load_params(src)?
    } else {
        ParamState::default()
    };

    let policy = Policy::new(PolicyProfile::Standard);
    let mesh = build_mesh_from_base(&base, targets.as_deref(), params, policy)?;

    let mut opts = ColladaExportOptions::default();
    if let Some(a) = author {
        opts.author = a;
    }

    let stats = export_collada(&mesh, &output, &opts)
        .with_context(|| format!("writing COLLADA to {}", output.display()))?;

    println!(
        "Written COLLADA: {} vertices, {} triangles → {}",
        stats.vertex_count,
        stats.face_count,
        output.display()
    );

    Ok(())
}

// ── gltf-sep ──────────────────────────────────────────────────────────────────

pub fn cmd_gltf_sep(args: &[String]) -> Result<()> {
    let mut base: Option<PathBuf> = None;
    let mut output: Option<PathBuf> = None;
    let mut bin_path: Option<PathBuf> = None;
    let mut targets: Option<PathBuf> = None;
    let mut params_src: Option<String> = None;

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--base" => {
                i += 1;
                base = Some(PathBuf::from(&args[i]));
            }
            "--output" => {
                i += 1;
                output = Some(PathBuf::from(&args[i]));
            }
            "--bin" => {
                i += 1;
                bin_path = Some(PathBuf::from(&args[i]));
            }
            "--targets" => {
                i += 1;
                targets = Some(PathBuf::from(&args[i]));
            }
            "--params" => {
                i += 1;
                params_src = Some(args[i].clone());
            }
            other => bail!("unknown option: {}", other),
        }
        i += 1;
    }

    let base = base.context("--base is required for gltf-sep")?;
    let output = output.context("--output is required for gltf-sep")?;

    if !base.exists() {
        bail!("base mesh not found: {}", base.display());
    }

    let params = if let Some(src) = &params_src {
        load_params(src)?
    } else {
        ParamState::default()
    };

    let policy = Policy::new(PolicyProfile::Standard);
    let mesh = build_mesh_from_base(&base, targets.as_deref(), params, policy)?;

    // Derive .bin path from .gltf stem if not provided
    let bin = bin_path.unwrap_or_else(|| output.with_extension("bin"));

    export_gltf_sep(&mesh, &output, &bin)
        .with_context(|| format!("writing glTF+BIN to {}", output.display()))?;

    println!("Written glTF+BIN: {} + {}", output.display(), bin.display());

    Ok(())
}

// ── svg ───────────────────────────────────────────────────────────────────────

pub fn cmd_svg(args: &[String]) -> Result<()> {
    let mut base: Option<PathBuf> = None;
    let mut output: Option<PathBuf> = None;
    let mut projection_str: Option<String> = None;
    let mut uv_mode = false;
    let mut width: u32 = 800;
    let mut height: u32 = 600;

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--base" => {
                i += 1;
                base = Some(PathBuf::from(&args[i]));
            }
            "--output" => {
                i += 1;
                output = Some(PathBuf::from(&args[i]));
            }
            "--projection" => {
                i += 1;
                projection_str = Some(args[i].clone());
            }
            "--uv" => {
                uv_mode = true;
            }
            "--width" => {
                i += 1;
                width = args[i].parse().context("--width must be an integer")?;
            }
            "--height" => {
                i += 1;
                height = args[i].parse().context("--height must be an integer")?;
            }
            other => bail!("unknown option: {}", other),
        }
        i += 1;
    }

    let base = base.context("--base is required for svg")?;
    let output = output.context("--output is required for svg")?;

    if !base.exists() {
        bail!("base mesh not found: {}", base.display());
    }

    let policy = Policy::new(PolicyProfile::Standard);
    let mesh = build_mesh_from_base(&base, None, ParamState::default(), policy)?;

    if uv_mode {
        export_uv_svg(&mesh, &output)
            .with_context(|| format!("writing UV SVG to {}", output.display()))?;
    } else {
        let projection = match projection_str.as_deref().unwrap_or("front") {
            "front" => SvgProjection::Front,
            "side" => SvgProjection::Side,
            "top" => SvgProjection::Top,
            other => bail!("unknown projection '{}'; choose front, side, or top", other),
        };
        let opts = SvgExportOptions {
            projection,
            width,
            height,
            ..Default::default()
        };
        export_svg(&mesh, &output, &opts)
            .with_context(|| format!("writing SVG to {}", output.display()))?;
    }

    println!("Written SVG: {}", output.display());

    Ok(())
}

// ── lod-export ────────────────────────────────────────────────────────────────

#[allow(clippy::too_many_arguments)]
pub fn cmd_lod_export(args: &[String]) -> Result<()> {
    let mut base: Option<PathBuf> = None;
    let mut output_dir: Option<PathBuf> = None;
    let mut targets: Option<PathBuf> = None;
    let mut params_src: Option<String> = None;
    let mut preset_name: Option<String> = None;
    let mut _levels: usize = 3;

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--base" => {
                i += 1;
                base = Some(PathBuf::from(&args[i]));
            }
            "--output-dir" => {
                i += 1;
                output_dir = Some(PathBuf::from(&args[i]));
            }
            "--targets" => {
                i += 1;
                targets = Some(PathBuf::from(&args[i]));
            }
            "--params" => {
                i += 1;
                params_src = Some(args[i].clone());
            }
            "--preset" => {
                i += 1;
                preset_name = Some(args[i].clone());
            }
            "--levels" => {
                i += 1;
                _levels = args[i].parse().context("--levels must be an integer")?;
            }
            other => bail!("unknown option: {}", other),
        }
        i += 1;
    }

    let base = base.context("--base is required for lod-export")?;
    let output_dir = output_dir.context("--output-dir is required for lod-export")?;

    if !base.exists() {
        bail!("base mesh not found: {}", base.display());
    }

    let params = if let Some(name) = &preset_name {
        BodyPreset::from_name(name)
            .ok_or_else(|| anyhow::anyhow!("unknown preset '{}'", name))?
            .params()
    } else if let Some(src) = &params_src {
        load_params(src)?
    } else {
        ParamState::default()
    };

    let policy = Policy::new(PolicyProfile::Standard);
    let mesh = build_mesh_from_base(&base, targets.as_deref(), params, policy)?;

    let stem = base
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or_default();

    let paths = export_default_lod_pack(&mesh, stem, &output_dir)
        .with_context(|| format!("writing LOD pack to {}", output_dir.display()))?;

    println!(
        "Written LOD pack: {} levels → {}",
        paths.len(),
        output_dir.display()
    );

    Ok(())
}

// ── variant-pack ──────────────────────────────────────────────────────────────

#[allow(clippy::too_many_arguments)]
pub fn cmd_variant_pack(args: &[String]) -> Result<()> {
    let mut params_list_path: Option<PathBuf> = None;
    let mut base: Option<PathBuf> = None;
    let mut targets: Option<PathBuf> = None;
    let mut output_dir: Option<PathBuf> = None;
    let mut pack_name = "oxihuman_variants".to_string();

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--params-list" => {
                i += 1;
                params_list_path = Some(PathBuf::from(&args[i]));
            }
            "--base" => {
                i += 1;
                base = Some(PathBuf::from(&args[i]));
            }
            "--targets" => {
                i += 1;
                targets = Some(PathBuf::from(&args[i]));
            }
            "--output-dir" => {
                i += 1;
                output_dir = Some(PathBuf::from(&args[i]));
            }
            "--pack-name" => {
                i += 1;
                pack_name = args[i].clone();
            }
            other => bail!("unknown option: {}", other),
        }
        i += 1;
    }

    let params_list_path =
        params_list_path.context("--params-list is required for variant-pack")?;
    let base = base.context("--base is required for variant-pack")?;
    let output_dir = output_dir.context("--output-dir is required for variant-pack")?;

    if !params_list_path.exists() {
        bail!("params-list file not found: {}", params_list_path.display());
    }
    if !base.exists() {
        bail!("base mesh not found: {}", base.display());
    }

    // Parse the JSON array of param sets
    let list_src = std::fs::read_to_string(&params_list_path)
        .with_context(|| format!("reading params-list: {}", params_list_path.display()))?;
    let param_values: Vec<serde_json::Value> =
        serde_json::from_str(&list_src).context("params-list must be a JSON array")?;

    std::fs::create_dir_all(&output_dir)
        .with_context(|| format!("creating output dir: {}", output_dir.display()))?;

    let policy = Policy::new(PolicyProfile::Standard);

    // Build each variant
    let mut entries_and_meshes: Vec<(oxihuman_export::variant_pack::VariantEntry, MeshBuffers)> =
        Vec::new();

    for (idx, val) in param_values.iter().enumerate() {
        let params = import_params(val)?;

        // Build a params HashMap for the VariantEntry
        let mut params_map = std::collections::HashMap::new();
        params_map.insert("height".to_string(), params.height);
        params_map.insert("weight".to_string(), params.weight);
        params_map.insert("muscle".to_string(), params.muscle);
        params_map.insert("age".to_string(), params.age);

        let id = format!("variant_{:03}", idx);
        let glb_filename = format!("{}.glb", id);
        let entry = variant_entry(&id, &format!("Variant {}", idx), &glb_filename, params_map);

        let mesh = build_mesh_from_base(&base, targets.as_deref(), params, policy.clone())?;
        entries_and_meshes.push((entry, mesh));
    }

    // Build slices for write_variant_pack
    let pairs: Vec<(oxihuman_export::variant_pack::VariantEntry, &MeshBuffers)> =
        entries_and_meshes
            .iter()
            .map(|(e, m)| (e.clone(), m))
            .collect();

    let result = write_variant_pack(&pairs, &output_dir, &pack_name)
        .with_context(|| format!("writing variant pack to {}", output_dir.display()))?;

    println!(
        "Written variant pack: {} variants → {}",
        result.glb_paths.len(),
        output_dir.display()
    );

    Ok(())
}

// ── report ────────────────────────────────────────────────────────────────────

pub fn cmd_report(args: &[String]) -> Result<()> {
    use oxihuman_core::parser::obj::parse_obj;

    let mut base: Option<PathBuf> = None;
    let mut output: Option<PathBuf> = None;
    let mut targets_dir: Option<PathBuf> = None;
    let mut pack: Option<PathBuf> = None;
    let mut title = String::from("OxiHuman Report");

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--base" => {
                i += 1;
                base = Some(PathBuf::from(&args[i]));
            }
            "--output" => {
                i += 1;
                output = Some(PathBuf::from(&args[i]));
            }
            "--targets" => {
                i += 1;
                targets_dir = Some(PathBuf::from(&args[i]));
            }
            "--pack" => {
                i += 1;
                pack = Some(PathBuf::from(&args[i]));
            }
            "--title" => {
                i += 1;
                title = args[i].clone();
            }
            other => bail!("unknown option: {}", other),
        }
        i += 1;
    }

    let base = base.context("--base is required for report")?;
    let output = output.context("--output is required for report")?;

    if !base.exists() {
        bail!("base mesh not found: {}", base.display());
    }

    // Parse the OBJ and compute mesh stats.
    let src = std::fs::read_to_string(&base)
        .with_context(|| format!("reading OBJ: {}", base.display()))?;
    let obj = parse_obj(&src).context("parsing OBJ")?;
    let morph_buf = oxihuman_morph::engine::MeshBuffers {
        positions: obj.positions,
        normals: obj.normals,
        uvs: obj.uvs,
        indices: obj.indices,
        has_suit: false,
    };
    let mut mesh = MeshBuffers::from_morph(morph_buf);
    oxihuman_mesh::normals::compute_normals(&mut mesh);
    oxihuman_mesh::suit::apply_suit_flag(&mut mesh);

    let base_name = base
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("base.obj")
        .to_string();
    let file_size = std::fs::metadata(&base).map(|m| m.len()).ok();
    let mesh_data = mesh_report_from_buffers(&mesh, &base_name, "obj");

    let mut report = PipelineReportData::new(&title);
    report.add_mesh({
        use oxihuman_export::report_html::MeshReportData;
        MeshReportData {
            name: mesh_data.name,
            vertex_count: mesh_data.vertex_count,
            face_count: mesh_data.face_count,
            has_normals: mesh_data.has_normals,
            has_uvs: mesh_data.has_uvs,
            has_colors: mesh_data.has_colors,
            bounding_box_min: mesh_data.bounding_box_min,
            bounding_box_max: mesh_data.bounding_box_max,
            file_size_bytes: file_size,
            format: mesh_data.format,
        }
    });

    // Count targets if directory provided.
    if let Some(ref td) = targets_dir {
        if !td.exists() {
            bail!("targets directory not found: {}", td.display());
        }
        let count = std::fs::read_dir(td)
            .with_context(|| format!("reading targets dir: {}", td.display()))?
            .flatten()
            .filter(|e| e.path().extension().map(|x| x == "target").unwrap_or(false))
            .count();
        report.add_param("target_count", count as f32);
    }

    // Load manifest if provided.
    if let Some(ref mp) = pack {
        if !mp.exists() {
            bail!("manifest file not found: {}", mp.display());
        }
        report.add_export_path(mp.to_string_lossy().into_owned());
    }

    export_html_report(&report, &output)
        .with_context(|| format!("writing HTML report: {}", output.display()))?;

    println!("Written report: {}", output.display());
    Ok(())
}