Code/AutoRig/Vast/ObjWriter.cs

Utility that serializes a RigMesh into a minimal Wavefront OBJ file as UTF-8 bytes. It writes vertex positions (v) and triangle faces (f) with 1-based indices and a comment header.

using System.Globalization;
using System.Text;
using AutoRig.Mesh;

namespace AutoRig.Vast;

/// <summary>Minimal OBJ export — the upload format for remote rigging
/// (every reference pipeline ingests OBJ).</summary>
public static class ObjWriter
{
    public static byte[] Write( RigMesh mesh )
    {
        ArgumentNullException.ThrowIfNull( mesh );
        var sb = new StringBuilder( mesh.Positions.Length * 32 );
        sb.Append( "# auto-rig cloud upload\n" );
        foreach ( var p in mesh.Positions )
            sb.Append( "v " )
                .Append( p.X.ToString( CultureInfo.InvariantCulture ) ).Append( ' ' )
                .Append( p.Y.ToString( CultureInfo.InvariantCulture ) ).Append( ' ' )
                .Append( p.Z.ToString( CultureInfo.InvariantCulture ) ).Append( '\n' );
        for ( var t = 0; t < mesh.TriangleCount; t++ )
            sb.Append( "f " )
                .Append( mesh.Triangles[t * 3] + 1 ).Append( ' ' )
                .Append( mesh.Triangles[t * 3 + 1] + 1 ).Append( ' ' )
                .Append( mesh.Triangles[t * 3 + 2] + 1 ).Append( '\n' );
        return Encoding.UTF8.GetBytes( sb.ToString() );
    }
}