Home › Forum › SOFA › Using SOFA › SofaPython3 – Compatibility with SofaPython and other plugins
Tagged: 64_bits, Linux_ubuntu, Plugin_SoftRobots, python, SOFA_1812
- This topic has 2 replies, 3 voices, and was last updated 3 years, 10 months ago by Hugo.
-
AuthorPosts
-
2 April 2020 at 22:02 #15636stonkensBlocked
Hi everyone,
I have been working with SOFA for soft robotic control for the last couple of months and have made extensive use of the PythonScriptController, expanding upon the examples in the Soft Robots plugin.
However, I recently noticed a sofapython3 distribution (https://sofapython3.readthedocs.io/en/latest/menu/install.html) and was wondering whether this could be used with the soft robots plugin? If so, would it be possible to give me a quick run-down of what the differences are and how it works?
Specifically, I interface with SOFA as follows:
controller(Sofa.PythonScriptController)
– def initGraph(self, node)
– def onBeginAnimationStep(self, dt)
– def onEndAnimationStep(self, dt)
– def onKeyPressed(self, key)To access data from the scene I use e.g. np.fromstring(str(finger.getObject(‘tetras’).findData(‘position’)), sep=’ ‘)
To change the value of actuators I use e.g.:
self.model.getObject(actuator_name).value =Thanks so much!
3 April 2020 at 23:02 #15642Damien MarchalBlockedHi @stokens,
SofaPython3 can be used without problem with SoftRobots but the examples as well as the STLIB have not been updated so they will not work anymore.
The big difference are in how to implement the ScriptController (because we have extended it a lot) and how to access the data fields. There is no initGraph anymore, in general the work done in this function is not either in the python constructor or in createScene function (or an utility function).
Here is a quick and dirty example (I d’ont have python3 right now so I didn’t test, I hope it will work, otherwise tell me so that I made fresh install to fix it ;))
import Sofa.Core class EmptyController(Sofa.Core.Controller): """ custom %EmptyController% component for SOFA """ def __init__(self, *args, **kwargs): Sofa.Core.Controller.__init__(self, *args, **kwargs) #needed self.finger=kwargs.get("target", None) def init(): pass # DEFAULT EVENTS: def onAnimateBeginEvent(self, event): """ called at the beginning of each time step """ print("AnimatedBegin", event) def onAnimateEndEvent(self, event): """ called at the end of each time step """ print("AnimatedEnd", event) def onKeypressedEvent(self, event): print("Key pressed") a = self.finger.tetra.position.value ### a is a numpy array with a read only access print(a) b = a * 2 print(b) self.finger.tetra.position.value = b ### the value is now squared print(self.finger.tetra.position.value) print(a) with self.finger.tetra.position.writeableArray() as wa: wa += a wa *= 3 print(wa) def onEvent(self, name): print("Event received with name:", name ) def createScene(root): root.addObject("MechanicalObject", name="dofs", positions=[0,0,0,1,1,1,2,2,2]) root.addObject(EmptyController(target=root.dofs))
I’m sorry the documentation does not auto-regenerate on readthedocs (for reason I don’t understand..I need to find time to investigate that).
Hope this helped.
5 February 2021 at 11:54 #18531HugoKeymasterHey @stonkens @damien-marchaluniv-lille1-fr
The new release v20.12 is out!
Plugins SoftRobot, STLIB and so on are being (or were) updated to be compatible with. It should therefore be all fine!Let us know if you have any issue, otherwise we’ll close the topic.
Best,
Hugo
-
AuthorPosts
- You must be logged in to reply to this topic.