Home › Forum › SofaPython3 › Using SofaPython3 › Running example with pygame
Tagged: Linux_ubuntu, pygame, SOFA_2106, SofaPython3, SoftRobots Plugin
- This topic has 14 replies, 3 voices, and was last updated 3 years, 1 month ago by Hugo.
-
AuthorPosts
-
2 August 2021 at 05:23 #20109AbdelrahmanBlocked
Hello everyone
I have installed sofa on linux ubuntu 20.04 using t cmake and added the path of sofa python packages to .bashrc as folows
export PYTHONPATH=/home/abdelrahman/sofa/build/v21.06/lib/python3/site-packages
export SOFA_ROOT=/home/abdelrahman/sofa/build/v21.06
to be able to import sofa modules from conda environment. Now I am trying to run one of the softrobots examples plugin specifically the trunk example to work with pygame to render the picture of the softrobot. The following is the code that I used to run my example from conda envfrom myTrunk import Trunk from math import cos from math import sin from splib3.numerics import Vec3, Quat from splib3.animation import animate, AnimationManager from myController import TrunkController import Sofa import pygame from OpenGL.GL import * from OpenGL.GLU import * import Sofa.SofaGL import SofaRuntime import Sofa.Simulation import os import time display_size = (800, 600) def main(): root = Sofa.Core.Node("root") createScene(root) Sofa.Simulation.init(root) init_display(root) try: while True: Sofa.Simulation.animate(root, root.getDt()) Sofa.Simulation.updateVisual(root) simple_render(root) time.sleep(root.getDt()) except KeyboardInterrupt: pass def createScene(rootNode): # Choose your resolution mode # 1- inverseMode=True, solve the inverse problem and control the end effectors # 2- inverseMode=False, solve the direct problem and set the cable displacements by hand inverseMode = False rootNode.addObject('RequiredPlugin', pluginName=['SoftRobots','SofaSparseSolver','SofaPreconditioner','SofaPython3','SofaConstraint', 'SofaImplicitOdeSolver','SofaLoader','SofaSimpleFem','SofaBoundaryCondition','SofaEngine', 'SofaOpenglVisual', 'SofaGeneralLoader']) #AnimationManager(rootNode) #rootNode.addObject('VisualStyle', displayFlags='showBehavior') rootNode.addObject("VisualStyle", displayFlags="showAll") rootNode.gravity = [0., -9810., 0.] #rootNode.addObject('FreeMotionAnimationLoop') simulation = rootNode.addChild('Simulation') simulation.addObject('EulerImplicitSolver', name='odesolver', firstOrder=False, rayleighMass=0.1, rayleighStiffness=0.1) simulation.addObject('ShewchukPCGLinearSolver', name='linearSolver', iterations=500, tolerance=1.0e-18, preconditioners='precond') simulation.addObject('SparseLDLSolver', name='precond') simulation.addObject('GenericConstraintCorrection', solverName='precond') trunk = Trunk(simulation, inverseMode=inverseMode) trunk.addVisualModel(color=[1., 1., 1., 0.8]) trunk.fixExtremity() # to add the controller script rootNode.addObject(TrunkController(node=rootNode)) #rootNode.createObject('PythonScriptController', filename="pythonControllers/TrunkController.py", classname="controller") def init_display(node): pygame.display.init() pygame.display.set_mode(display_size, pygame.DOUBLEBUF | pygame.OPENGL) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glEnable(GL_LIGHTING) glEnable(GL_DEPTH_TEST) Sofa.SofaGL.glewInit() Sofa.Simulation.initVisual(node) Sofa.Simulation.initTextures(node) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45, (display_size[0] / display_size[1]), 0.1, 50.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def simple_render(rootNode): """ Get the OpenGL Context to render an image (snapshot) of the simulation state """ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glEnable(GL_LIGHTING) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45, (display_size[0] / display_size[1]), 0.1, 50.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() rootNode.addObject("Camera", name = "camera") cameraMVM = rootNode.camera.getOpenGLModelViewMatrix() glMultMatrixd(cameraMVM) Sofa.SofaGL.draw(rootNode) pygame.display.get_surface().fill((0,0,0)) pygame.display.flip() if __name__ == '__main__': main()
myTrunk file just has the class for the trunk example that can be found in the softrobot plugin. Everytime I run the code it gives me a black screen. Can anyone tell me what’s wrong with the code that I produced to be able to fix it.
Also one more thing I saw on the sofa framework youtube channel that is there’s a sofaGym library but I can’t fine the code, have the authors made it public or not yet. Thanks and sorry for the long question.2 August 2021 at 15:29 #20111psomersBlockedI think you’ll need to update to the latest master branch version of Sofa so that you can use the version of SofaPython3 that has Sofa.GL. And then you will likely run into the problem that SoftRobotics is not quite updated to work with these versions yet… It is just a guess, but I think you may need to wait a bit before a python GUI will work for the SoftRobotics package.
3 August 2021 at 10:45 #20112AbdelrahmanBlockedHello
Thanks for your reply. I’m using the latest sofa version v21.06, for the soft robotics package I think it should work well with python 3 although after installing sofa I was not able to import splib3 in the begging so I had to copy past the files of the splib3 from the latest binary version to the lib file inside the build directory but I don’t think this would be the source of the problem here.
3 August 2021 at 11:06 #20116psomersBlockedHave you been able to run the GUI examples in SofaPython3?
https://github.com/sofa-framework/SofaPython3/blob/master/examples/additional-examples3 August 2021 at 11:25 #20117AbdelrahmanBlockedyes I can run it from the conda environment, although it should close when I press on the keyboard due the try except condition but that never happens I have to force quit it to close it
3 August 2021 at 11:33 #20118psomersBlockedyou are adding a new camera to your scene everytime simple_render is called and then using it without initializing it. This is likely your problem 😉
To clarify – you should add the camera where you create your scene. Then it will be initialized when you initialize everything else.
3 August 2021 at 15:41 #20119AbdelrahmanBlockedbut isn’t this different from the example which I followed
3 August 2021 at 16:43 #20120psomersBlockedI’m not sure which example you’re following, but what I said still stands as it makes no sense to create a new camera everytime the scene is drawn. Have you tried it?
If you’re not getting errors and just a black screen, I assume the code is rendering a camera view that isn’t pointing at your model.Try moving the addObject(“Camera”…) line to your createScene function.
3 August 2021 at 17:07 #20121AbdelrahmanBlockedyes I have tried and still gave the same black screen but one more thing I keep getting this message in the terminal I don’t know if it’s related
[WARNING] [SparseLDLSolver(precond)] Invalid Linear System to solve. Please insure that there is enough constraints (not rank deficient).
3 August 2021 at 17:16 #20122psomersBlockedI’m pretty sure your camera just isn’t pointing in the right direction. I’m not sure what your model is, so I can’t say what numbers to use, but try setting the the position and lookAt values for the camera such that it is outside your model, but pointing at it. Check the pygame example from the link I shared and see how it is done for the “InteractiveCamera” there.
Unfortunately, if there is a way to get Sofa to automatically best place the camera, I don’t know what it is.3 August 2021 at 17:18 #20123psomersBlockedI personally use pyqt for viewing and have written things to control the camera with the keyboard (so you can look around). If that is something you want to try, here is an example:
https://github.com/psomers3/QSofaGLViewTools/blob/main/examples/example1.py16 August 2021 at 09:47 #20114psomersBlockedHave you been able to run the Python3 GUI examples? For example, the pygame one:
https://github.com/sofa-framework/SofaPython3/blob/master/examples/additional-examples/pygame_example.py23 August 2021 at 18:36 #20212HugoKeymaster15 September 2021 at 17:26 #20360AbdelrahmanBlockedHello @hugo
I took @psomers advice and started working with GUI instead of the pygame but I was away for a while by the way @psomers thanks for the advice.
Now I launch the GUI from conda env. Also @hugo is it possible to have access for the sofaGym that was mentioned on one of the videos on the youtube channel of Sofa?
best regards
7 October 2021 at 22:46 #20557HugoKeymasterHey @abdelrahman2010
SofaGym is not yet released, but should be very soon! The release was a bit postponed (due to review time)
Stay tuned!Best
Hugo
-
AuthorPosts
- You must be logged in to reply to this topic.