Home › Forum › SOFA › Using SOFA › How can I use python to create a scene?
Tagged: 64_bits, Linux_ubuntu, SOFA_1912
- This topic has 9 replies, 3 voices, and was last updated 5 years, 6 months ago by
Hugo.
-
AuthorPosts
-
26 April 2020 at 07:19 #15941
PengYu
BlockedI just start to use Sofa. The document just shows creating scene using xml and c++?
I wonder how can I creating a scene using python?26 April 2020 at 11:43 #15948Hugo
KeymasterHi @rainvector
Welcome to the SOFA forum!
You should consider reading through the documentation for your first steps it would help.
For your question, a dedicated article is available here. Good start with SOFA!Best wishes,
Hugo
27 April 2020 at 13:22 #15957PengYu
Blocked@Hugo Thanks for your reply.
I tried to follow the steps.
When I openfountain.scn, the scene is loaded correctly (the particles is generated). However, I got the errors as below.
The
fountain.pyis in the same directory withfountain.scn.27 April 2020 at 21:21 #15962Hugo
KeymasterHi @rainvector
This is (unfortunately) normal. So no worry.
The fountain scene is buggy. It supposed to spray new particles when some get out from the bowl. But the code was not that robust! I will try to find some time and fix it.Best wishes,
Hugo
28 April 2020 at 06:29 #15964PengYu
Blocked@Hugo I’m still confused about creating scene using python scripts.
I have two questions.
1. Is it necessary to create a.scnfile for each scene?
2. Do I have to setRequiredPluginin a.scnfile?
3. In a python scripts likehouseofcards.py, it starts fromcreateScene(node)other thancreateScene(sofa.PythonScriptController). Is it ok?
According to the document, I should create a.scnfile and usePythonScriptControllerto reference the corresponding python scripts.
I still realize that.scnfile is not really necessary, for instance I can directly openhouseofCrad.pyand don’t need ahouseofCards.scn. However, when animating the scene, the cards and floor don’t have collision detection. And I got the following warning

How can I solve this kind of problems to handle the collisions? @Hugo I’m still confused about creating scene usin python scripts.
I have two questions.
1. Is it necessary to create a.scnfile for each scene?
2. Do I have to setRequiredPluginin a.scnfile?
3. In a python scripts likehouseofcards.py, it starts fromcreateScene(node)other thancreateScene(sofa.PythonScriptController). Is it ok?
According to the document, I should create a.scnfile and usePythonScriptControllerto reference the corresponding python scripts.
I still realize that.scnfile is not really necessary, for instance I can directly openhouseofCrad.pyand don’t need ahouseofCards.scn. However, when animating the scene, the cards and floor don’t have collision detection. And I got the following warning

How can I solve this kind of problems to handle the collisions?28 April 2020 at 08:24 #15966PengYu
Blocked@Hugo I’m still confused about creating scene using python scripts.
I have three questions.
1. Is it necessary to create a.scnfile for each scene?
2. Do I have to setRequiredPluginin a.scnfile?
3. In a python scripts likehouseofcards.py, it starts fromcreateScene(node)other thancreateScene(sofa.PythonScriptController). Is it ok?According to the document, I should create a
.scnfile and usePythonScriptControllerto reference the corresponding python scripts.
I still realize that.scnfile is not really necessary, for instance I can directly openhouseofCrad.pyand don’t need ahouseofCards.scn. However, when animating the scene, the cards and floor don’t have collision detection. And I got the following warning

How can I solve this kind of problems to handle the collisions?29 April 2020 at 10:28 #15973Sarah
BlockedI’m not sure if I can answer all your questions, as I’m not a SOFA expert. But in my experience
1. I don’t think it is not necessary to use a.scnfile.2. I don’t know if you would need to set
RequiredPluginin a.scnfile, but from the error message it seems as though you should. And similarly for.pyfile but than in pythonrootNode.createObject('RegquiredPlugin', pluginName='...')3. It is fine to start .py files with createScene(node) and you don’t need to use a PythonScriptController for the initial build up of the scene but can use it to control various parametrs during the simulation.
I’m not sure why the collision in
.pywas not detected. But maybe you could check that is referring to the correct plugin in python and that it can find it, you could also add the SofaPython Plugin but I don’t think it is necessary. I’m not an expert, but I hope this helps a little.29 April 2020 at 18:02 #15976PengYu
BlockedHi @Sarah, thanks for your reply.
1. I can load a scene which is only defined in a python file like
houseOfCards.pyin directoryapplications/plugin/SofaPlugin/examples/.2. I add the plugins in the warning log using
rootNode.createObject('RegquiredPlugin', pluginName='...'), but the collision is still not work. And I want to know How can I find a detailed SofaPython API. Te functions in SofaPython plugin are not clear for me.3. What’s the entry point of python scripts? I just find some entry points in PythonScriptController components according to the documents in SofaPython directory.
Thanks again. I am new to Sofa. It often crash for no reason and generating many strange errors. Do you have some advices for learning it?
30 April 2020 at 22:31 #15991Hugo
KeymasterHi @rainvector and @sarah
Well done @sarah for your answers.
1. yes, .py scenes can be directly loaded
2. RequiredPlugin (be careful in the typos, you wrote it wrong: ReGquiredPlugin) must be added when using codes which are not in the core of SOFA, or which were modularized. Follow the error messages for this.
3. See the SofaPython.pdf in the plugin applications/plugins/SofaPython repository. Take a look also to the examples/ folder in this repository.NOTE: this plugin will be deprecated starting from v20.06 since Python 2.7 is deprecated. A new plugin is now available: SofaPython3. More information and documentation on the transition will soon be made available.
Best wishes,
Hugo
-
AuthorPosts
- You must be logged in to reply to this topic.
