Page 1 of 1

Cannot get the python API to recognize the .dll files, despite using the instructions on the "Scripting in Python" page

Posted: Wed Mar 15, 2023 4:21 pm
by otherrealm
I am having trouble getting the python API to recognize the .dll files when I try to run a python script with the Open Sim API imported, despite using the instructions on the "Scripting in Python" (https://simtk-confluence.stanford.edu:8 ... onWindows:) page. I put the code locations in the path, but I don't think that is the problem because it is identifying the locations and running the python code, just not the .dll stuff. Is there something I'm missing? Thanks.

Language/OS specifics:
OpenSim Version 4.4
Python: conda-forge 3.10.8
OS Specifications:
Edition Windows 10 Enterprise
Version 22H2
Installed on 2021-04-29
OS build 19045.2673
Experience Windows Feature Experience Pack 120.2212.4190.0

I am using Visual Studio Code, but I also tried running the commands in an elevated Windows CMD prompt and got the same results.

Code: Select all

C:\WINDOWS\system32>cd c:\opensim\

c:\OpenSim>cd sdk/Python

c:\OpenSim\sdk\Python>python setup_win_python38.py
install path found as c:/OpenSim/bin

c:\OpenSim\sdk\Python>python -m pip install .
Processing c:\opensim\sdk\python
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: opensim
  Building wheel for opensim (setup.py) ... done
  Created wheel for opensim: filename=opensim-4.4-py3-none-any.whl size=9948799 sha256=9090c27a2c08017a9c156c5f1d9cbe61fc03ecbb0024f237cfbdf30c3bf2f0eb
  Stored in directory: C:\Users\Someone\AppData\Local\Temp\pip-ephem-wheel-cache-fal2vkjm\wheels\32\11\cd\de98af55e43a8d12d3e004492f5a67f398fe42e5b4e4800094
Successfully built opensim
Installing collected packages: opensim
  Attempting uninstall: opensim
    Found existing installation: opensim 4.4
    Uninstalling opensim-4.4:
      Successfully uninstalled opensim-4.4
  WARNING: The script opensim-moco-generate-report.exe is installed in 'C:\conda\envs\oshoe\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed opensim-4.4

Code: Select all

c:\OpenSim\sdk\Python>python
Python 3.10.8 | packaged by conda-forge | (main, Nov 22 2022, 08:16:33) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.add_dll_directory("C:/OpenSim/bin")
<AddedDllDirectory('C:/OpenSim/bin')>
>>> import opensim
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\OpenSim\sdk\Python\opensim\__init__.py", line 9, in <module>
    from .simbody import *
  File "c:\OpenSim\sdk\Python\opensim\simbody.py", line 12, in <module>
    from . import _simbody
ImportError: DLL load failed while importing _simbody: The specified module could not be found.
>>>
Image

Re: Cannot get the python API to recognize the .dll files, despite using the instructions on the "Scripting in Python" p

Posted: Wed Mar 15, 2023 4:33 pm
by aafox
Hi Aaron,

I'm pretty sure this is a python version issue. I've had similar difficulties using python 3.9 and 3.10 - but things run well for me with python 3.8, so perhaps try with that.

Aaron

Re: Cannot get the python API to recognize the .dll files, despite using the instructions on the "Scripting in Python" p

Posted: Fri Mar 17, 2023 7:53 am
by otherrealm
Thanks,
I setup a Micromamba instance running 3.8 and put

Code: Select all

import os
os.add_dll_directory("C:/OpenSim/bin")
in the beginning and it seemed to work
(well, at least I got something back from OpenSim, I still got an error but I think that is in my code)
I am getting

Code: Select all

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
c:\Develop\opensim\testingOpenSim.py in line 14
     12 for force in model.getForceSet():
     13     print(force.getName())
---> 14 for name, muscle in model.getMuscles():
     15     print(name, muscle.get_max_isometric_force())
TypeError: cannot unpack non-iterable Muscle object
When I run:

Code: Select all

import os
os.add_dll_directory("C:/OpenSim/bin")
import opensim
v = opensim.Vector([6, 7, 8, 9])
rv = opensim.RowVector([1, 2, 5])
v = opensim.Vec3(1, 2, 3)
v[0] = 1.5 + v[1]
w = opensim.Vector(5, 1.5)
w[0] = 3 * w[1]
model = opensim.Model("E:\Documents\Learning\Biomechanics\OpenSim\Models\Gait2354_Simbody\subject01_simbody.osim")
for force in model.getForceSet():
    print(force.getName())
for name, muscle in model.getMuscles():
    print(name, muscle.get_max_isometric_force()) 

Re: Cannot get the python API to recognize the .dll files, despite using the instructions on the "Scripting in Python" p

Posted: Fri Mar 17, 2023 11:44 am
by aymanh
Hi Aaron & Aaron,

Thanks Aaron for helping Aaron. Let me explain the issue in more details:
1. The distribution of the OpenSim application includes libraries built against a specific version of python (3.8) which was the prevailing version at the time of the release. If you try to set this up with a different python version it will fail. The simbody message is misleading, it just happens that simbody is the first library it's trying to load.
2. To work around this fact (the fixed python version) and avoid publishing distributions for all python versions, we distribute conda packages that are python version specific. As of now we distribute packages for python {3.7, 3.8, 3.9} with 3.10 coming up soon.
3. If you have a python environment other than conda, you can build from source, just make sure to choose your python distribution while building to generate the bindings and then follow the instructions as if using an opensim distribution.
4. As a note, for python versions 3.8 and later on windows, the path is not used to locate dll's, you need to call add_dll_directory.

Hope this clarifies the picture for you and for all our python users. If our documentation is not inline with this picture please let us know so we can clarify/update.

Other questions regarding specific errors should be posted to another thread so that the questions/answers are easily found and are helpful to the wider user community.

Best regards,
-Ayman

Re: Cannot get the python API to recognize the .dll files, despite using the instructions on the "Scripting in Python" p

Posted: Thu Apr 13, 2023 11:10 am
by carlosoleto
I just went through the same problem, and I think I got the fix.

I'm not able to use the conda_env.yml directly because (in the time that I tried) it didn't have some libraries that I use, especially pandas.

So my workaround was to create a Anaconda python 3.8.13 and run the steps in the documentation

Code: Select all

(opensim_scripting) C:\> python setup_win_python38.py 
(opensim_scripting) C:\> python -m pip install . 
Today I'm following the same steps that I did and getting the same error DLL error mentioned here. By the way, just adding

Code: Select all

import os 
os.add_dll_directory('c:/OpenSim 4.4/bin') 
Fix this error in any script. What I did to fix it permanently was to remove the if else statement in opensim/__init__.py inside de SDK folder for Python. It ended like this:

Code: Select all

import sys 
import os 

os.add_dll_directory('c:/OpenSim 4.4/bin') 

from .simbody import * 
from .common import * 
from .simulation import * 
from .actuators import * 
from .analyses import * 
from .tools import * 
from .examplecomponents import * 
from .moco import * 
from . import report 
from .version import __version__ 
Hope it helps.

Best regards.