Page 1 of 1

Read/write mesh file

Posted: Fri Jan 17, 2025 9:35 pm
by kernalnet
Hi, I need to read/write the points and normals from/to mesh files (e.g. vtp, stl), and am currently using XML parsing methods (I prefer not to use external libraries). I wonder if such method already exists in OpenSim API. I tested PolygonalMesh(), but the output is empty.

Code: Select all

>>> import opensim as osim
>>> mesh = osim.PolygonalMesh().loadFile("l_femur.vtp")
>>> type(mesh)
<class 'NoneType'>
Any help would be greatly appreciated.
Mohammadreza

Re: Read/write mesh file

Posted: Sun Jan 19, 2025 10:08 am
by tkuchida
The implementation for loading mesh files can be found in Simbody (PolygonalMesh.cpp L242: https://github.com/simbody/simbody/blob ... h.cpp#L242). The doxygen can be found here: https://simtk.org/api_docs/simbody/3.5/ ... lMesh.html.

Re: Read/write mesh file

Posted: Sun Jan 26, 2025 12:32 am
by kernalnet
Thanks Dr. Uchida for your response. I shouldn't have combined the commands; now it works:

Code: Select all

mesh = osim.PolygonalMesh()
mesh.loadFile("l_femur.vtp")

for i in range(mesh.getNumVertices()):
    print(i, mesh.getVertexPosition(i))
However, the PolygonalMesh only reads the points, and I couldn't find any method for updating the points, reading and updating the normals, and writing to a new mesh file. Here are all the available methods:

Code: Select all

'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__firstlineno__',
'__format__',
'__ge__',
'__getattribute__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__static_attributes__',
'__str__',
'__subclasshook__',
'__swig_destroy__',
'__weakref__',
'addFace',
'addVertex',
'clear',
'createBrickMesh',
'createCylinderMesh',
'createSphereMesh',
'getFaceVertex',
'getNumFaces',
'getNumVertices',
'getNumVerticesForFace',
'getVertexPosition',
'loadFile',
'scaleMesh',
'thisown',
'transformMesh'
I wonder if I can do these things using OpenSim API.

Thanks again.

Re: Read/write mesh file

Posted: Thu Jan 30, 2025 7:06 am
by tkuchida
OpenSim isn't designed for editing meshes; there are other tools for doing that (e.g., MeshLab, ParaView). OpenSim Creator's Mesh Warper (https://docs.opensimcreator.com/manual/ ... /tut5.html) might be of interest as well.

Re: Read/write mesh file

Posted: Fri Jan 31, 2025 1:32 am
by kernalnet
Nice, thanks for your help.