Page 1 of 1

Python scripting issues - compile from source (linux)

Posted: Thu Jun 12, 2014 2:05 am
by sebastiank78
Hi there,

I just tried to compile OpenSim 3.2 with python scripting enabled.

Debian GCC 4.7.2
Python 2.7.3
SWIG 2.0.7
python-setuptools 0.6.24

Everything works fine, but when I try to import opensim I get an import error:
---------------------------------------------------------------------
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/opensim-3.2-py2.7.egg/opensim/__init__.py", line 1, in <module>
from opensim import *
File "/usr/local/lib/python2.7/dist-packages/opensim-3.2-py2.7.egg/opensim/opensim.py", line 26, in <module>
_opensim = swig_import_helper()
File "/usr/local/lib/python2.7/dist-packages/opensim-3.2-py2.7.egg/opensim/opensim.py", line 22, in swig_import_helper
_mod = imp.load_module('_opensim', fp, pathname, description)
ImportError: dynamic module does not define init function (init_opensim)
---------------------------------------------------------------------

I tried to solve this by brute force and just decided to try with python3:

Python 3.2.3mu
python3-setuptools 0.6.24

Now the import works fine, I can do "import opensim as osim" but as soon as try to load a model i get:
---------------------------------------------------------------------
>>> m = osim.Model()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Model'
>>>
---------------------------------------------------------------------

Could anyone please tell me what I am doing wrong?

Thank You

Re: Python scripting issues - compile from source (linux)

Posted: Wed Jun 18, 2014 5:27 pm
by chrisdembia
I have run into this error before, but I am currently unable to reproduce it. I just now created a fresh Ubuntu 13.10 machine (using VirtualBox) and successfully got the python scripting to work. Here's approximately what I did:

$ sudo apt-get install g++ git cmake liblapack-dev swig python2.7-dev python-setuptools
$ mkdir repos
$ cd repos
$ git clone https://github.com/simbody/simbody.git
$ cd simbody
$ git checkout Simbody-3.3.1
$ cd ../
$ mkdir simbody-build
$ cd simbody-build
$ cmake ../simbody -DCMAKE_INSTALL_PREFIX=/home/chris/simtk/simbody -DBUILD_VISUALIZER=off
$ make -j8 install
<Unzip OpenSim 3.2 source dist to ~/repos/OpenSim32-source
$ cd ~/repos
$ mkdir opensim32-build
$ cd opensim32-build
$ cmake ../OpenSim32-source -DCMAKE_INSTALL_PREFIX=/home/chris/simtk/opensim -DSimTK_INSTALL_DIR=/home/chris/simtk/simbody
$ make -j8
$ cmake ../OpenSim32-source -DBUILD_PYTHON_WRAPPING=on
$ make -j8
$ make _opensim
$ make -j8 install
$ cd ~/simtk/opensim/sdk/python
$ sudo python setup.py install
$ export LD_LIBRARY_PATH=/home/chris/simtk/opensim/lib
$ python
>>> import opensim
>>> m = opensim.Model()

EDIT I've made a change above. It seems that the following helps: build the project without python wrapping, then turn on BUILD_PYTHON_WRAPPING, and make the _opensim target

Re: Python scripting issues - compile from source (linux)

Posted: Thu Jun 19, 2014 12:27 am
by chrisdembia
I found out what the issue was! You can fix this by making the following change to OpenSim/Wrapping/Python/CMakeLists.txt, line 42-43:

Change

FILE(GLOB SOURCE_FILES...)
FILE(GLOB INCLUDE_FILES...)

to

SET(SOURCE_FILES OpenSimContext.cpp "${swig_output_cxx_file_fullname}")
SET(INCLUDE_FILES OpenSimContext.h "${swig_output_header_file_fullname}")

and rebuild with a fresh build directory (and running make install only once). Let me know if this works.

Re: Python scripting issues - compile from source (linux)

Posted: Wed Jul 02, 2014 6:57 am
by bradh
I am too having trouble with compiling from source with Python wrap. Ubuntu 12.04 LTS and installing fresh. The message I receive:

Code: Select all

Scanning dependencies of target _opensim
[100%] [100%] Building CXX object OpenSim/Wrapping/Python/CMakeFiles/_opensim.dir/pyOpenSim_wrap.cxx.o
Building CXX object OpenSim/Wrapping/Python/CMakeFiles/_opensim.dir/OpenSimContext.cpp.o
/home/humphreysb/simtk/OpenSim32-build/OpenSim/Wrapping/Python/pyOpenSim_wrap.cxx: In function ‘PyObject* _wrap_new_DecorativeGeometryImplementation(PyObject*, PyObject*)’:
/home/humphreysb/simtk/OpenSim32-build/OpenSim/Wrapping/Python/pyOpenSim_wrap.cxx:26004:111: error: cannot allocate an object of abstract type ‘SwigDirector_DecorativeGeometryImplementation’
/home/humphreysb/simtk/OpenSim32-build/OpenSim/Wrapping/Python/pyOpenSim_wrap.h:18:7: note:   because the following virtual functions are pure within ‘SwigDirector_DecorativeGeometryImplementation’:
/home/humphreysb/simtk/simbody/include/simbody/SimTKcommon/internal/DecorativeGeometry.h:661:18: note: 	virtual void SimTK::DecorativeGeometryImplementation::implementMeshFileGeometry(const SimTK::DecorativeMeshFile&)
make[2]: *** [OpenSim/Wrapping/Python/CMakeFiles/_opensim.dir/pyOpenSim_wrap.cxx.o] Error 1
make[1]: *** [OpenSim/Wrapping/Python/CMakeFiles/_opensim.dir/all] Error 2
make: *** [all] Error 2
My build notes (which follow the posts in this thread, the simbody directions in the GitHub readme, and http://simtk-confluence.stanford.edu:80 ... structions):


INSTALL SIMBODY

Verify I have everything I need:
$ sudo apt-get install g++ git cmake liblapack-dev swig python2.7-dev python-setuptools

Get the simbody source code (3.3.1) from the OpenSim Download page and put it into ~/simtk/simbody-source:


Make directories
$ mkdir ~/simtk/simbody-build

Create make files
$cd ~/simtk/simbody-build
$cmake ~/simtk/simbody-source -DCMAKE_INSTALL_PREFIX=~/simtk/simbody -DCMAKE_BUILD_TYPE=RelWithDebInfo -DSIMBODY_STANDARD_11=off

build dOxygen
$ make doxygen

Compile:
$make -j8

Run the tests:
$ ctest -j8

Install:
$ make -j8 install

Set environment variables:
gedit ~/.bashrc

add lines:
export SIMBODY_HOME=~/simtk/simbody (note this matches the MAKE_INSTALL_PREFIX above)
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/simtk/simbody/lib (note that this path should be where the libSimTKcommom.so files are located)

save, close, and start a new terminal.
Verify this works by:
$ echo $SIMBODY_HOME
$ echo $LD_LIBRARY_PATH

Run compiled examples:
cd ~/simtk/simbody-build
./ExamplePendulum

Test the installation:
$cd ~/simtk/simbody/share/doc/simbody/examples/bin
$./SimbodyInstallTest # or ./SimbodyInstallTestNoViz


INSTALL OPENSIM

Unzip openSim 3.2 source into ~/simtk/
Create directory: ~/simtk/OpenSim32-build
cd to the directory: cd ~/simtk/OpenSim32-source
cmake ~/simtk/OpenSim32-source -DCMAKE_INSTALL_PREFIX=~/simtk/opensim -DSimTK_INSTALL_DIR=~/simtk/simbody/include/simbody -DCMAKE_BUILD_TYPE=RelWithDebInfo

$cd ~/simtk/OpenSim32-source
Make the API docs:
$ make doxygen

compile OpenSim:
make -j8

test openSim:
$ ctest -j8

Make with Python Wrappers
$ cmake ~/simtk/OpenSim32-source -DBUILD_PYTHON_WRAPPING=on


compile OpenSim:
$ make -j8
$ make _opensim
$ make -j8 install

Setup Python
$ cd ~/simtk/opensim/sdk/python
$ sudo python setup.py install


Set environment variables:
gedit ~/.bashrc

add or modify lines:
export LD_LIBRARY_PATH=~/simtk/opensim/lib
save, close, and start a new terminal.

$ python
>>> import opensim
>>> m = opensim.Model()

(The above has been edited to show the successful process)

Re: Python scripting issues - compile from source (linux)

Posted: Wed Jul 02, 2014 8:05 am
by aymanh
Hi Brad,

OpenSim 3.2 is compatible with simbody 3.3.1, please redo with this version of simbody and let us know how it goes.
The source for simbody 3.3.1 is also available on the downloads page on simtk.org under OpenSim Source Code.

Best regards,
-Ayman

Re: Python scripting issues - compile from source (linux)

Posted: Thu Jul 03, 2014 7:31 am
by bradh
Ayman - once again, thanks for the help. When I went to get the simbody install directions I just grabbed the latest and greatest simbody. When I used 3.3.1 simbody with Opensim 3.2, it works. I updated my process above to show what worked for me.

I did have one small issue: when I ran ctest after building OpenSim, a couple of tests failed:

Code: Select all

The following tests FAILED:
33 - testOptimizationExampleRuns (Timeout)
34 - testOptimizationExample (Failed)
50 - testCMC (Timeout)
Errors while running CTest
I am working some optimization modeling, so should I be concerned?

Regards,

Brad

Re: Python scripting issues - compile from source (linux)

Posted: Mon Jul 07, 2014 2:07 pm
by chrisdembia
The source release has been updated with a fix for the original issue (ImportError: dynamic module does not define init function (init_opensim)). https://simtk.org/frs/download.php?file_id=4085