FileObj.Write(Mesh[]) ignores MapZtoY/Xform and modifies input meshes

Tested in Rhino 7.38.24338.17001, Rhino 8.35.26251.13001 and Rhino 9.0.26272.12303 (WIP), Windows x64. The input is a mesh box (0,0,0)..(10,6,3) made with Mesh.CreateFromBox (quads, with texture coordinates): one quad per box face in the transform tests (T), and a 2×2×2 subdivision (24 quads) in the input-mesh tests (M), so that triangulation and ngon creation have a visible effect. The attached script runs unmodified in all three versions (IronPython 2.7 in Rhino 7, Python 3 in Rhino 8 and 9) and judges every result against the geometrically correct answer, never against another Rhino version.

What happens

The Mesh[] overload has two problems. Both return WriteFileResult.Success and give no warning:

  • It ignores the transform. MapZtoY and FileWriteOptions.Xform have no effect; the overload writes the untransformed input. The RhinoDoc overload, given the same options object and the same mesh, applies the transform correctly.
  • It modifies the meshes passed in. With ExportAsTriangles, NgonMode.Create and, in Rhino 8 and 9, ExportVcs, the caller’s mesh is different after the call. The RhinoDoc overload leaves the object in the document unchanged.

Both are present in all three versions; the ExportVcs case only in Rhino 8 and 9. RH-85213 reported the first problem for MapZtoY and is marked fixed in 8.16. It is still present in 8.35 and 9.0 WIP, and FileWriteOptions.Xform is affected in the same way. I could not test an 8.16/8.17 build, so I cannot say whether this is a regression or whether the fix never reached this code path.

Reproduction

  1. Build the mesh box. Create fwo = FileWriteOptions() and opts = FileObjWriteOptions(fwo), and set opts.MapZtoY = True (or fwo.Xform = Transform.Translation(0,0,6)).
  2. Write the box with the Mesh[] overload: FileObj.Write(path, System.Array[Mesh]([box]), opts).
  3. Write the same box with the RhinoDoc overload, using the same opts, from a headless document that contains only the box.
  4. Read both files back with MapYtoZ = False and compare the vertices with box × opts.GetTransform().
  5. For the second problem, set opts.ExportAsTriangles = True (or one of the other two options), call the Mesh[] overload and compare the box before and after the call.

The attached script builds all the geometry itself, runs every case below and prints CORRECT / WRONG for the RhinoDoc overload, the Mesh[] overload and the workaround. It uses temporary files only, leaves inputs (blue) and results (orange) in the document for inspection, and on a re-run removes only the objects it created itself.

Note: pass the meshes as System.Array[Mesh]. A plain Python list does not bind to this overload: IronPython rejects it (expected Array[Mesh], got list), and under CPython the call resolves to the RhinoDoc overload and fails.

Measurement

Matrices returned by opts.GetTransform() in the three transform tests, identical in all three versions:

T01 MapZtoY = True       T02 Xform = Translation(0,0,6)    T03 Xform = Rotation 90 deg about Z
 1   0   0   0            1   0   0   0                     0  -1   0   0
 0   0   1   0            0   1   0   0                     1   0   0   0
 0  -1   0   0            0   0   1   6                     0   0   1   0

In each case the RhinoDoc overload writes exactly EXPECTED, every vertex with its own texture coordinate. The Mesh[] overload writes exactly the input.

Results across versions (C = correct, W = wrong). Doc = RhinoDoc overload, M[] = Mesh[] overload, Wa = workaround. In the T rows, correct means the OBJ read back contains exactly EXPECTED; in the M rows, it means the mesh passed in (for Doc: the document object) is unchanged after the call.

case                                 Rhino 7.38      Rhino 8.35      Rhino 9 WIP
                                     Doc  M[]  Wa    Doc  M[]  Wa    Doc  M[]  Wa
T01  MapZtoY = True                   C    W   C      C    W   C      C    W   C
T02  Xform = Translation(0,0,6)       C    W   C      C    W   C      C    W   C
T03  Xform = Rotation 90 deg about Z  C    W   C      C    W   C      C    W   C
M01  ExportAsTriangles = True         C    W   C      C    W   C      C    W   C
M02  NgonMode = Create                C    W   C      C    W   C      C    W   C
M03  ExportVcs = True                 C    C   C      C    W   C      C    W   C
M04  default options (control)        C    C   C      C    C   C      C    C   C

7 tests × 3 calls = 21 results per version. The logs report 16 of 21 results correct in Rhino 7.38 and 15 of 21 in Rhino 8.35 and Rhino 9 WIP.

State of the caller’s mesh after the Mesh[] overload (box with 2×2×2 subdivision):

                             faces   quads   triangles   ngons   vertex colours
before the call                24      24        0          0         0
ExportAsTriangles = True       48       0       48          0         0
NgonMode = Create              24      24        0          6         0
ExportVcs = True (R8, R9)      24      24        0          0        54, all #000000

Vertex coordinates, normals and texture coordinates of the caller’s mesh are unchanged in every case, and the mesh stays valid. In Rhino 8 and 9 the caller’s mesh is displayed black in the Rendered view after the export (observed in the viewport), because the added vertex colours are drawn instead of its material. In Rhino 7, ExportVcs = True writes no colours for a mesh without vertex colours; Rhino 8 and 9 write a colour on every v line. That new output is fine; the problem is that it is produced by modifying the caller’s mesh. Existing vertex colours are not overwritten.

Every mesh in the array is affected, not only the first. If the mesh passed in is RhinoObject.Geometry of a document object, the script’s reference is modified but the document object is not.

Additional checks (scripts available on request) give the same results for an irregular asymmetric mesh, for two meshes in one call, and for a 37° rotation, a uniform scale of 2.5 and a rotation combined with MapZtoY. In each of these, all transformed Mesh[] outputs are byte-identical to each other (MD5). Setting every boolean and enum property of FileObjWriteOptions one at a time (except UI- and macOS-specific ones) shows that only the three options above modify the caller’s mesh. The results are deterministic.

Consequence: exporting with the Mesh[] overload and MapZtoY = True, then importing with MapYtoZ = True, does not return the original coordinates. The same round trip through the RhinoDoc overload does.

Possible lead

  • The transform is available. opts.GetTransform() returns the correct matrix at the call site, and both overloads receive the same options object. The Mesh[] path does not apply it.
  • The RhinoDoc path writes copies. It applies the transform and leaves the source geometry untouched.
  • The Mesh[] path writes the caller’s meshes. It does not apply the transform, and the writer’s own processing (triangulation, ngon creation, vertex colours) ends up on the caller’s meshes.

This suggests that the Mesh[] path skips the “duplicate the mesh and apply the transform” step that the RhinoDoc path performs. Duplicating each mesh, transforming the copy by opts.GetTransform() and writing the copies would fix both problems. This is an inference from measurements only.

Expected

The Mesh[] overload should apply opts.GetTransform() like the RhinoDoc overload, and it should not modify the meshes passed to it.

Workaround

Duplicate and transform the meshes yourself, then call the Mesh[] overload with neutral options. Verified in all three versions: the output equals the RhinoDoc overload, texture coordinates included, and the caller’s meshes stay unchanged.

xf = opts.GetTransform()
copies = []
for m in meshes:
    c = m.DuplicateMesh()
    c.Transform(xf)
    copies.append(c)
fwo.Xform = rg.Transform.Identity   # fwo = the FileWriteOptions inside opts
opts.MapZtoY = False                # neutral options: no double transform once this is fixed
Rhino.FileIO.FileObj.Write(path, System.Array[rg.Mesh](copies), opts)

Related

RH-85213 (FileObj.Write does not pay attention to FileObjWriteOptions.MapZtoY), from this thread.

Python script: obj_mesharray_repro.py (21.3 KB)

Thanks @Teo - I’ve logged a ticket to a developer can review.

– Dale

RH-99276 is fixed in Rhino 8 Service Release 37 Release Candidate