-
Notifications
You must be signed in to change notification settings - Fork 855
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
Python bindings for simulate #419
Python bindings for simulate #419
Conversation
aaa0540
to
47641f6
Compare
Thanks for doing this! We're just starting to look at your pybind11 code and will have more detailed comments in due course. I do have a few comments after a cursory glance at your code, though.
|
I agree on both points. Linking to GLFW from the bindings isn't desirable to begin with, however it gets even worse when you consider that the Python I like the idea of abstracting GLFW so it can be swapped with SDL, that would fix both problems. Another way, which I think is compatible with the abstraction layer idea, requires two steps:
Do those sound like good options? If a windowing abstraction layer appears later, I think it should be easy to switch over and the Python bindings won't be impacted. |
47641f6
to
1fd9e0e
Compare
Hi, I have updated the bindings so that the Python bindings no longer link to Instead of needing bindings for numerous members of the In order to create Setting up callbacks from C++ to Python outside of the There is one methodological hurdle still. Python creates the I've only tested on Ubuntu still, but I will work through any issues with Mac/Windows soon. |
I've literally just pushed out 3240596 to help you manage GLFW dependency 😅 Haven't had a chance to look into your latest round of code changes yet, but my main concern is to make sure that the simulate Python bindings work correctly with My idea was to ask you to do |
What timing! I think it all works out in the end. The latest bindings do not need to If a future user wanted to Incidentally, I think that suggests that Simulate's render loop should be modified to be a render function that can be called from a super loop responsible for multiple glfw windows (since all glfw calls need to be on the main thread in MacOS). |
I think leaving open the possibility of Python users ending up with two copies of GLFW is potentially asking for trouble. If we make Python simulate bindings depend on PyGLFW, and only have PyGLFW bring in GLFW library that would be the safest solution. WDYT? |
I agree. I did not mean to imply that the dispatch table wasn't needed, it definitely is for the reason you said. I just mean that the 2nd version of bindings is no longer using two copies of GLFW like the 1st version did. Rebasing to get the new dispatch table and then going through the process with |
Yes, I think we're on the same page :) BTW if you don't agree with my idea please do let me know! |
36d2a87
to
2169bbc
Compare
Ok the dynamic dispatch table works, the latest commit is sets the handle and mixes Python and C++ glfw calls. Very cool! :) However, I had to remove the There is an issue, and I think it might be related to running Still need to clean up the build system (right now I hardcoded dynamic dispatch to be on for now), get this to work on other systems, and fix some memory management bugs/bad practices. |
Hmm, where is the repeated |
Sorry just found the issue, it's something very basic, as usual :). The Also yes |
Should Previous versions of this PR built Additionally, Also, to build the python bindings the library has to be installed to the binary release. It feels like installing a shared library is "nicer" than installing a static one. I don't have a strong opinion either way (and I have very little experience with these things), it just seems like a shared library is easier than combining a bunch of static libraries with |
My thinking is:
Would that work? |
That will work! Thanks for the quick response. |
Hi, This should be ready for a detailed review. I've been able to test Ubuntu and MacOS but not Windows. For some reason Windows is taking excessively long to compile the Python module (I think it might take several hours on my machine). Is this a known issue? I noticed that the Python bindings are not built for Windows in the CI either. Either way, it would be great to get to the bottom of this because I'm not sure I got the dynamic library names in On MacOS the |
Yes, MSVC seems to have trouble compiling a couple of files in the |
I've tested on Windows. |
Could you please rebase to HEAD? |
bd88f82
to
bd8b4c1
Compare
No problem! It is rebased now. |
Hi, sorry, just pushed a small change to simulate.cc, could you rebase again? |
Re-rebasing, nevermind, all sorted. |
I can confirm that |
bd8b4c1
to
95a4b1c
Compare
After offline discussion the decision was made to reimplement the physics loop in Python as in the first commit of this PR instead of reusing the C++ version of the physics loop as in the subsequent commits. The reasoning was that the callbacks module was too difficult to use across translation units and that having the physics loop in Python would allow for more powerful features in the future without introducing too much code duplication. Those commits that re-used the C++ physics loop have been force pushed away, and all that is left is the fixes to glfw usage, build system, and updating the Python physics loop to use the new timing scheme introduced on I will go through this a few more times to check for small issues. There were comments before that the interface needs to be more Pythonic. I'm happy to make any requested changes towards that. I did not consider changing any part of the interface when going from C++ to Python, I just bound the various methods in the most straightforward way. |
Sorry for the delay in getting this merged. I've made some modifications to the API and added REPL functionality. Also removed some stuff that I don't see being used in this PR. If I removed anything that you are depending on externally please send a followup PR. Thanks again for your help on this! |
Fantastic! Glad we were able to get this through. REPL functionality looks very interesting. It looks like the on control callback registering/deregistering was what was removed (which was required for the double pendulum and fixation examples to work properly when the reload button was used). I'm not sure I was doing that in the best way anyway. I'll circle back soon. |
Hi, I have an error when using the double-pendulum.py. See the below message, I have mujoco 2.3.7 installed with pip. mjParseXML: could not open file './double_pendulum.xml' |
Try using the full path instead like, |
Is built-in 'simulate' viewer working only for Python? Can I use it with C? Thanks @aftersomemath the double pendulum is working now with Python. |
You can use it with C++. The main.cc which is compiled into the |
This PR is to get initial feedback about my Python bindings for
simulate
in order to determine if they are worth polishing until they are mergable. Right now they are at a "look it works" level of quality. I have been using the bindings for a few months on Linux, however some careful consideration of howglfw
linkage should work is needed before they will work on Windows or Mac.Here are some questions:
rollout
example in the python package and so are only loosely integrated with the rest of the MuJoCo Python bindings.If the answer to the first question is no, then we can close this and I will instead make a PR to add a CMake option that installs the
mjsimulate
library as discussed in #201. Then these bindings can be easily maintained in a fork.Here are two examples of what the bindings make easy:
Double Pendulum Stabilization
Fixation using Visual Feedback