-
Notifications
You must be signed in to change notification settings - Fork 351
Accept paths as Path objects (not only strings) #4191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Python pathlib paths are much cleaner and easier to use than strings, and it is the recommended Python approach for manipulating paths since Python 3.6 (introduced in Python 3.4). When using OpenSim in other libraries that work with Path objects, it is a bit of a pain and a sourve of errors to have to convert the path into a string.
The PR lets the OpenSim Python bindings accept paths both as strings and Path objects.
Please note that I spent a full day trying to build it from source without success (I'm definitely lacking practice for this), and I feel like I need to let it go for now.
I'm fairly confident that it would work, but I did not test it. At the very least, someone should try to build it and test this, at the very least:
```python
import opensim as osim
from pathlib import Path
model = osim.Model("arm26.osim")
model = osim.Model(Path("arm26.osim"))
```
accept python path objects
|
OpenSim is successfully builtnow, but it still struggles to accept Path objects. Working on it now. |
|
I'm all for (I've never touched the bindings, so it'd be poor form for me to approve it, maybe @nickbianco / @aymanhab would know more about how/where type conversions should happen!) |
|
Support for |
|
I tried to change the order of the items in the file (eg, putting >>> from pathlib import Path
>>> import opensim as osim
>>> osim.Model(Path('Model_Pose2Sim_simple.osim'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\opensim-workspace\opensim-core-build\Bindings\Python\Release\opensim\simulation.py", line 26802, in __init__
_simulation.Model_swiginit(self, _simulation.new_Model(*args))
TypeError: Wrong number or type of arguments for overloaded function 'new_Model'.
Possible C/C++ prototypes are:
OpenSim::Model::Model()
OpenSim::Model::Model(std::string const &)
OpenSim::Model::Model(OpenSim::Model const &)I must say that I'm not sure what to look for next. |
|
Doing |
Do you know what C++ type SWIG is converting the |
|
Honestly, it is the first time I deal with SWIG so I know very little about it. From what I can see, though, SWIG does not have a built-in typemap for pathlib.Path, hence the typemap that I proposed (and that gets ignored, apparently) |
Brief summary of changes
Python pathlib paths are much cleaner and easier to use than strings, and it is the recommended Python approach for manipulating paths since Python 3.6 (introduced in Python 3.4). When using OpenSim in other libraries that work with Path objects, it is a bit of a pain and a source of errors to have to convert the path into a string.
The PR lets the OpenSim Python bindings accept paths both as strings and Path objects.
Testing I've completed
Please note that I spent a full day trying to build it from source without success (I'm definitely lacking practice for this), and I feel like I need to let it go for now.I built it successfully now, but OpenSim still struggles to accept Path objects. Working on it now.
I'm fairly confident that it would work, but I did not test it. At the very least, someone should try to build it and test this:
Fixes issue #4190
Looking for feedback on...
CHANGELOG.md (choose one)
Updated: https://github.com/opensim-org/opensim-core/blob/main/CHANGELOG.md
This change is