Skip to content

Commit 48d13a5

Browse files
authored
Merge pull request #372 from pariterre/python_wrapper
Proper python wrapper
2 parents bd96767 + 505fb4c commit 48d13a5

35 files changed

+3612
-227
lines changed

.vscode/launch.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@
55
"version": "0.2.0",
66
"configurations": [
77
{
8-
"name": "Forward dynamics Python example",
8+
"name": "Python: Current example file",
99
"type": "debugpy",
1010
"request": "launch",
11-
"program": "${workspaceFolder}/examples/python3/forward_dynamics.py",
11+
"program": "${file}",
1212
"cwd": "${workspaceFolder}/examples/python3",
1313
"console": "integratedTerminal",
14-
"justMyCode": false,
14+
"justMyCode": false
15+
},
16+
{
17+
"name": "Python: Current test file",
18+
"type": "debugpy",
19+
"request": "launch",
20+
"program": "${file}",
21+
"cwd": "${workspaceFolder}/test/binding/python3",
22+
"console": "integratedTerminal",
23+
"justMyCode": false
1524
}
1625
]
1726
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@
99
"[cpp]": {
1010
"editor.defaultFormatter": "ms-vscode.cpptools"
1111
},
12+
"[python]": {
13+
"editor.defaultFormatter": "ms-python.black-formatter",
14+
"editor.rulers": [
15+
120
16+
],
17+
}
1218
}

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.17)
22

3-
project(biorbd VERSION 1.11.3)
3+
project(biorbd VERSION 1.12.0)
44
set(BIORBD_ROOT_FOLDER ${PROJECT_SOURCE_DIR})
55
set (BIORBD_NAME_NO_SUFFIX ${PROJECT_NAME})
66
set (CMAKE_CXX_STANDARD 11)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,10 +649,10 @@ The tag `r` represented the width of the curve based of the torque-angle relatio
649649
...
650650

651651
## Convert from OpenSim models
652-
For users who have well-established models in OpenSim and wish to convert them to a `.bioMod`, there's a handy tool called Osim_to_biomod.
652+
For users who have well-established models in OpenSim and wish to convert them to a `.bioMod`, there's a handy tool called BioBuddy.
653653
This package facilitates the conversion process, ensuring that OpenSim models can be seamlessly integrated into our ecosystem.
654654

655-
To get started with the Osim_to_biomod package, please refer to its documentation and source code available at [Osim_to_biomod](https://github.com/pyomeca/osim_to_biomod) GitHub Repository.
655+
To get started with the BioBuddy package, please refer to its documentation and source code available at [BioBuddy](https://github.com/pyomeca/biobuddy) GitHub Repository.
656656

657657
Let us know if you'd like any further modifications!
658658

binding/python3/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ file(GLOB PYTHON_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.py")
108108
INSTALL(FILES ${PYTHON_FILES}
109109
DESTINATION "${Python3_SITELIB_INSTALL}/${BIORBD_PYTHON_DIR}"
110110
)
111+
file(GLOB PYTHON_FILES "${CMAKE_CURRENT_SOURCE_DIR}/wrapper/*.py")
112+
INSTALL(FILES ${PYTHON_FILES}
113+
DESTINATION "${Python3_SITELIB_INSTALL}/${BIORBD_PYTHON_DIR}/wrapper"
114+
)
111115

112116
# Check if swig has its version 3 or 4, to choose the right place to put the library
113117
if(${SWIG_VERSION} MATCHES "^3(\.[0-9]*)*$")

binding/python3/__init__.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
1-
import numpy as np
21
from . import biorbd # This is created while installing using CMake
32
from .biorbd import *
43
from ._version import __version__
54
from .surface_max_torque_actuator import *
65
from .rigid_body import *
76
from .utils import *
7+
from .wrapper import *
8+
from .wrapper import has_static_optimization, has_extended_kalman_filter
89

910

10-
if biorbd.currentLinearAlgebraBackend() == 1:
11-
from casadi import Function, MX, SX, horzcat
11+
if biorbd.currentLinearAlgebraBackend() == biorbd.CASADI:
12+
from casadi import MX, SX, Function, horzcat
1213

13-
def to_casadi_func(name, func, *all_param, expand=True):
14+
backend = biorbd.CASADI
15+
16+
def to_casadi_func(name, func, *all_param, expand=True, **kwargs):
1417
cx_param = []
1518
for p in all_param:
1619
if isinstance(p, (MX, SX)):
1720
cx_param.append(p)
21+
for value in kwargs.values():
22+
if isinstance(value, (MX, SX)):
23+
cx_param.append(value)
1824

1925
if isinstance(func, (MX, SX, Function)):
20-
func_evaluated = func
26+
func_evaluated = [func]
2127
else:
22-
func_evaluated = func(*all_param)
28+
func_evaluated = func(*all_param, **kwargs)
2329
if isinstance(func_evaluated, (list, tuple)):
24-
func_evaluated = horzcat(*[val if isinstance(val, MX) else val.to_mx() for val in func_evaluated])
30+
func_evaluated = [val if isinstance(val, MX) else val.to_mx() for val in func_evaluated]
2531
elif not isinstance(func_evaluated, MX):
26-
func_evaluated = func_evaluated.to_mx()
27-
func = Function(name, cx_param, [func_evaluated])
32+
func_evaluated = [func_evaluated.to_mx()]
33+
else:
34+
func_evaluated = [func_evaluated]
35+
func = Function(name, cx_param, func_evaluated)
2836
return func.expand() if expand else func
37+
38+
elif biorbd.currentLinearAlgebraBackend() == biorbd.EIGEN3:
39+
backend = biorbd.EIGEN3
40+
41+
def to_casadi_func(name, func, *all_param, expand=True):
42+
raise RuntimeError("to_casadi_func is only available with the CASADI backend")
43+
44+
else:
45+
raise RuntimeError("Unknown backend")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from .biorbd_model import Biorbd
2+
from .extended_kalman_filter import ExtendedKalmanFilterMarkers, has_extended_kalman_filter
3+
from .external_force_set import ExternalForceSet
4+
from .marker import Marker
5+
from .muscle import Muscle
6+
from .segment_frame import SegmentFrame
7+
from .segment import Segment
8+
from .static_optimization import StaticOptimization, has_static_optimization
9+
10+
__all__ = [
11+
Biorbd.__name__,
12+
ExtendedKalmanFilterMarkers.__name__,
13+
ExternalForceSet.__name__,
14+
Marker.__name__,
15+
Muscle.__name__,
16+
SegmentFrame.__name__,
17+
Segment.__name__,
18+
StaticOptimization.__name__,
19+
]

0 commit comments

Comments
 (0)