Read/write mesh file

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Mohammadreza Rezaie
Posts: 418
Joined: Fri Nov 24, 2017 12:48 am

Read/write mesh file

Post by Mohammadreza Rezaie » Fri Jan 17, 2025 9:35 pm

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

Tags:

User avatar
Thomas Uchida
Posts: 1803
Joined: Wed May 16, 2012 11:40 am

Re: Read/write mesh file

Post by Thomas Uchida » Sun Jan 19, 2025 10:08 am

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.

User avatar
Mohammadreza Rezaie
Posts: 418
Joined: Fri Nov 24, 2017 12:48 am

Re: Read/write mesh file

Post by Mohammadreza Rezaie » Sun Jan 26, 2025 12:32 am

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.

User avatar
Thomas Uchida
Posts: 1803
Joined: Wed May 16, 2012 11:40 am

Re: Read/write mesh file

Post by Thomas Uchida » Thu Jan 30, 2025 7:06 am

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.

User avatar
Mohammadreza Rezaie
Posts: 418
Joined: Fri Nov 24, 2017 12:48 am

Re: Read/write mesh file

Post by Mohammadreza Rezaie » Fri Jan 31, 2025 1:32 am

Nice, thanks for your help.

POST REPLY