OpenSim STL file Import

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Jessica Mayenburg
Posts: 8
Joined: Sun Oct 04, 2020 3:52 pm

OpenSim STL file Import

Post by Jessica Mayenburg » Tue Nov 03, 2020 2:13 pm

Hello,

I've spent a little bit of time now trying to familiarize myself with OpenSim 4.1 and doing a number of tutorials. My goal currently is that I would like to import an STL file that represents a mobility device, and I would like to simulate a person interacting with this device. I am currently looking at https://simtk-confluence.stanford.edu:8 ... pleScripts but I am having a hard time trying to figure out what my starting point should be for opening the STL file and assigning the joints, etc.

I'd like to use Matlab if possible, but could also write my script in Python if necessary. Any guidance would be very much appreciated! Thanks in advance.

Tags:

User avatar
Ayman Habib
Posts: 2248
Joined: Fri Apr 01, 2005 12:24 pm

Re: OpenSim STL file Import

Post by Ayman Habib » Fri Nov 06, 2020 11:39 am

Hello,

The fundamental issue you need to keep in mind is that in OpenSim, modeling and visualization are rather decoupled. Basically you can import the mesh/stl file for visualization purposes but this has very little to do with modeling the "mobility device" which likely include Bodies, Joints and possibly "Actuators, constraints, etc.").

The narrow answer to importing STL files, programmatically is to:
- create a Mesh object, pass it the STL file name
- add the Mesh object to a Body or Ground
This can be done in Matlab or python as described in this example (Python) similar examples are available for C++, Matlab
https://github.com/opensim-org/opensim- ... m_model.py

You can also modify the osim file of a model to refer to your STL file within a <Mesh> object tags (.vtp, .stl, .obj formats are all supported).

Hope this helps,
-Ayman

User avatar
Rory Turnbull
Posts: 28
Joined: Mon Dec 16, 2019 1:57 am

Re: OpenSim STL file Import

Post by Rory Turnbull » Thu Nov 26, 2020 11:15 am

Hi

I have been trying to do the same thing, based on some digging and the above response I have got this code for matlab.

Code: Select all

%% Mesh 
%Define the mesh name
meshPath = 'FileName.STL';

% If the mesh isn't in the Model folder, make a copy. Otherwise mesh will not be usable when Model is opened in the OpenSim GUI

if ~exist('../Model/FileName.STL','file')
    copyfile 'FileName.STL' '../Model/FileName.STL'
end
            
ModelName.attachGeometry(Mesh(meshPath)); % Link geometry to the Model
Hope this helps!

Rory

User avatar
Jessica Mayenburg
Posts: 8
Joined: Sun Oct 04, 2020 3:52 pm

Re: OpenSim STL file Import

Post by Jessica Mayenburg » Mon Nov 30, 2020 9:48 am

Thanks! I have something almost identical based on Ayman's response and some digging as well.

Now I'm looking into the possibility of re-creating a simplified version of my model in Matlab so that the person can "interact" with it, but assigning the stl mesh as the geometry (like you did) so that it looks the way I want it to.

The biggest obstacle I'm facing right now is to rotate and scale the stl geometry.

User avatar
Rory Turnbull
Posts: 28
Joined: Mon Dec 16, 2019 1:57 am

Re: OpenSim STL file Import

Post by Rory Turnbull » Tue Dec 01, 2020 9:45 am

I used the following to scale the geometry. I'm not sure why it wasn't the right size since the CAD model is set to meters and the dimensions should match. That said this works and the geometry doesn't impact the model so it should be fine.

Code: Select all

ModelName.attachGeometry(Mesh('FileName.STL'));
ModelName.scaleAttachedGeometry(Vec3(2));
As for the orientation. I haven't found anything in Matlab that fixes it but if you ensure the coordinate system of your model matches the coordinate system of OpenSim before you export it to STL you should be fine.

POST REPLY