pythonwavefrontpyvista

How to set group name when exporting pyvista mesh to wavefront obj


I am generating some cylinder meshes from some csv data and adding them to a PyVista plotter, using this I then export them to an '.obj' file using the export_obj method. The problem is I want to associate a group name with each mesh inside the obj file so I can use that information later, however the exporter seems to assign a random name like grp123 to each mesh.

IDS = df[ID_FIELD].values
p = pv.Plotter()
length = len(IDS)
for index, id in enumerate(IDS[: NUM_TO_GENERATE]):
    print(f"Making Column {index}/{length}", end=", ")
    data = dfa.loc[dfa[ID_FIELD] == id]
    print(f"NUM ROWS: {len(data)}")
    print(id)

    x = float(df.loc[df[ID_FIELD] == id][C1])
    z = float(df.loc[df[ID_FIELD] == id][C2])
    for row in data.values:
        height = float(row[C3]) - float(row[C4])
        cyl = pv.Cylinder(height=height, center=(
        x, float(row[FROM_COLUMN]) + height/2, z), direction=(0, 1.0, 0))
        p.add_mesh(cyl, name=id, label=id)

p.add_axes()
p.export_obj("data.obj")

So yeah basically help associating a name with each group inside the obj file would be helpful.


Solution

  • PyVista uses vtkOBJExporter() under the hood to print out OBJ files. The VTK class has suspiciously few public methods, and none that seem relevant for naming groups.

    The smoking gun is in the implementation:

      // write out a group name and material
      fpObj << "\ng grp" << idStart << "\n";
      fpObj << "usemtl mtl" << idStart << "\n";
    

    This looks very much like something that is hard-coded in VTK itself (and using integer idStart indices to label everything in the output file), in other words the vtkOBJExporter() doesn't support defining custom group labels, I'm afraid. Since PyVista uses this exporter, you won't be able to work around this within the library.