Help Need with addDirToGeometrySearchPaths()

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Vinay Kumar
Posts: 34
Joined: Thu Nov 08, 2018 6:50 am

Help Need with addDirToGeometrySearchPaths()

Post by Vinay Kumar » Tue Jan 21, 2020 5:46 am

Hello

addDirToGeometrySearchPath() is not working with relative or absolute paths for me. Can you please help me locate the mistake. I am using the following code after initializing the system.

Code: Select all

auto &modelVisualizer = osimModel.updVisualizer();
const std::string geometryDir = "../geometry/";
//const std::string geometryDir = "/home/vinay/Repos/OpenSimExample/OptimizationExample/build/geometry";
modelVisualizer.addDirToGeometrySearchPaths(geometryDir);
The full code is available at https://github.com/vinaym815/OpenSimExa ... n.cpp#L177
Last edited by Vinay Kumar on Wed Jan 22, 2020 6:49 pm, edited 1 time in total.

Tags:

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

Re: Trouble: addDirToGeometrySearchPaths() not working

Post by Ayman Habib » Tue Jan 21, 2020 10:04 am

Hi Vinay,

Using relative path makes sense only when you know exactly your working directory (relative to what) otherwise I'd recommend you stay with absolute paths so your code doesn't fail randomly when you run from a different working directory/folder.

Looking at your code, the call to addDirToGeometrySearchPaths must be made before you initialize the model (using initSystem) otherwise it's too late for the paths to be searched and Geometry/mesh filesto be found.

Hope this helps,
-Ayman

User avatar
Vinay Kumar
Posts: 34
Joined: Thu Nov 08, 2018 6:50 am

Re: Trouble: addDirToGeometrySearchPaths() not working

Post by Vinay Kumar » Tue Jan 21, 2020 8:45 pm

aymanh wrote:
Tue Jan 21, 2020 10:04 am
Using relative path makes sense only when you know exactly your working directory (relative to what) otherwise I'd recommend you stay with absolute paths so your code doesn't fail randomly when you run from a different working directory/folder.
Thank you for the advice. I work on different systems which makes it a necessity to use relative paths.
aymanh wrote:
Tue Jan 21, 2020 10:04 am
Looking at your code, the call to addDirToGeometrySearchPaths must be made before you initialize the model (using initSystem) otherwise it's too late for the paths to be searched and Geometry/mesh filesto be found.
Calling osimModel.updVisualizer() before osimModel.initSystem() is not possible https://github.com/opensim-org/opensim- ... del.h#L357.

If I initialize the model once more after adding the directory, the program can find the geometry files, however annoyingly I end up with two visualization windows.

Kind regards
Vinay

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

Re: Trouble: addDirToGeometrySearchPaths() not working

Post by Ayman Habib » Wed Jan 22, 2020 10:16 am

Hi Vinay,

The method to

Code: Select all

addDirToGeometrySearchPaths
is static so you don't need an instance to invoke it.

The order of events would be to addDirToGeometrySearchPaths then instantiate a model and call initSystem on it so that mesh files are found. The visualizer is created within initSystem so if you try to access it before initSystem call, it will throw as expected.

Hope this helps,
-Ayman

User avatar
Vinay Kumar
Posts: 34
Joined: Thu Nov 08, 2018 6:50 am

Re: Trouble: addDirToGeometrySearchPaths() not working

Post by Vinay Kumar » Wed Jan 22, 2020 6:44 pm

aymanh wrote:
Wed Jan 22, 2020 10:16 am
The order of events would be to addDirToGeometrySearchPaths then instantiate a model and call initSystem on it so that mesh files are found. The visualizer is created within initSystem so if you try to access it before initSystem call, it will throw as expected.
Thank you. That was very very very helpful.

Kind Regards
Vinay

User avatar
Ibraheem Al-Dhamari
Posts: 35
Joined: Thu Mar 16, 2017 2:44 am

Re: Help Need with addDirToGeometrySearchPaths()

Post by Ibraheem Al-Dhamari » Wed Jan 29, 2020 3:03 am

Here is a complete example to load a model from a file and visualize the simulation, it would be nice if someone add visualizing the motion part

Code: Select all

#include <OpenSim/OpenSim.h>
#include "OpenSim/Common/STOFileAdapter.h"
#include <time.h>
#include <string>
using namespace OpenSim;
using namespace std;
int main(int argc, char* argv[]) {
   std::cout<<"=============================================="<<std::endl;
   std::cout<<"=                           OpenSim Visualizer                                  ="<<std::endl;
   std::cout<<"=============================================="<<std::endl;
    try {
        double finalTime    = 1 ;  //simulation duration
        // change these two lones to be your model path and its geometry folder
        string    inputModelPath               =  "../data/ss.osim"  ;
        string    inputModelGeometryPath =  "../data/geom";

        // create a model from an input file
        Model inputModel(inputModelPath) ;
        inputModel.setUseVisualizer(true);
        inputModel.finalizeConnections();

        // this is a static function, should be called before initializing the system
        ModelVisualizer::addDirToGeometrySearchPaths(inputModelGeometryPath);

        // Initialize the system and get the default state
        SimTK::State& si = inputModel.initSystem();

        // create  visualiser from simody
        SimTK::Visualizer& viz = inputModel.updVisualizer().updSimbodyVisualizer();
        viz.setWindowTitle("OpenSim Visualizer: "+inputModel.getInputFileName());
        viz.setShowSimTime(true);
        viz.setShowFrameNumber(true);
        viz.setBackgroundType(SimTK::Visualizer::GroundAndSky);

        Manager manager(inputModel);
        inputModel.printDetailedInfo(si, std::cout);
        std::cout << "integrating for " << finalTime << " seconds" << std::endl;
        manager.initialize(si);
        manager.integrate(finalTime);
    }catch (OpenSim::Exception ex) {
        std::cout << ex.getMessage() << std::endl;
        return 1;
    }
    time_t my_time = time(NULL);

    // ctime() used to give the present time
    printf(" All OpenSIm tasks are completed on: %s", ctime(&my_time));
}

POST REPLY