Home › Forum › SofaPython3 › Using SofaPython3 › setBackgroundImage not working as expected
Tagged: GUI
- This topic has 6 replies, 2 voices, and was last updated 3 years, 9 months ago by Andrea.
-
AuthorPosts
-
29 January 2021 at 13:35 #18418AndreaBlocked
Hello everyone,
I am using quite recent versions of Sofa and SofaPython3 (December 2020) and the function setBackgroundImage is not working as before.
In python, when I run:
import Sofa.Gui Sofa.Gui.BaseGUI.setBackgroundImage("image.png")
I get the following error:
TypeError: setBackgroundImage(): incompatible function arguments. The following argument types are supported: 1. (self: Sofa.Gui.BaseGUI, arg0: str) -> None
Does anybody know how can I fix this?
Thanks
Andrea1 February 2021 at 10:26 #18454jnbrunetModeratorHey Andrea,
Looking at your output, it looks like the
setBackgroundImage()
is looking for a BaseGui as first argument. This is similar to the “self” argument you usually find inside class methods in python. Hence this is a method which should be called from an instance of BaseGui, and not a static function that you can call directly.Could you try the following:
# Get the current BaseGui instance current_gui = Sofa.Gui.GUIManager.GetGUI() # Change the background image current_gui.setBackgroundImage("image.png")
J-N
1 February 2021 at 11:08 #18456AndreaBlockedHey JN,
Thanks for your reply.
I also tried that, but
Sofa.Gui.GUIManager.GetGUI()
returns None. Yet I have initialized the GUIManager by doing:Sofa.Gui.GUIManager.Init("scene", "qglviewer") Sofa.Gui.GUIManager.createGUI(root) Sofa.Gui.GUIManager.MainLoop(root)
Andrea
1 February 2021 at 17:03 #18470jnbrunetModeratorHey Andrea,
You need to call this function after the GUI has been created:
Sofa.Gui.GUIManager.Init("scene", "qglviewer") Sofa.Gui.GUIManager.createGUI(root) # ---> HERE <---- # Get the current BaseGui instance current_gui = Sofa.Gui.GUIManager.GetGUI() # Change the background image current_gui.setBackgroundImage("image.png") # ---------------- Sofa.Gui.GUIManager.MainLoop(root)
Let me know if this is working or if you face other issues.
J-N
1 February 2021 at 17:56 #18471AndreaBlockedHey JN,
Still not working. If I run what you suggested I get the following warning:
[SofaViewer] Could not create file 'path_to_my_image/img.png' Valid extensions: dds<code>and there is no background image.
But this time, current_gui is not None 🙂
If I only run
Sofa.Gui.GUIManager.Init("scene", "qglviewer") Sofa.Gui.GUIManager.createGUI(root, __file__)
I get a similar warning:
[SofaViewer] Could not create file '/home/andrea/Projects/sofa/share/textures/SOFA_logo.bmp' Valid extensions: dds
Also note that if I use
Sofa.Gui.GUIManager.createGUI(root)
(without__file__
) I get the following error:
[SofaRuntime] RuntimeError: basic_string::_M_construct null not valid
`I both checked the existence and accessibility to
path_to_my_image/img.png
and to/home/andrea/Projects/sofa/share/textures/SOFA_logo.bmp
Moreover, I need to update the background image at every time step so I will also have to call setBackgroundImage() inside my functions…
Andrea
2 February 2021 at 09:24 #18473jnbrunetModeratorHey Andrea,
Well, we went a little bit further 🙂
Moreover, I need to update the background image at every time step so I will also have to call setBackgroundImage() inside my functions…
This is not a problem since the important thing is that
setBackgroundImage()
is called afterSofa.Gui.GUIManager.Init()
. Events such asonBeginAnimationStep()
are usually handled by your controller after the initialization of the GUI. You probably got thecurrentGui == None
if you tried to get it in yourcreateScene
function since it is called before the init GUI.Also note that if I use Sofa.Gui.GUIManager.createGUI(root) (without __file__) I get the following error:
[SofaRuntime] RuntimeError: basic_string::_M_construct null not validYeah, this is already known, the second argument is mandatory and we should handle this better.
Finally,
Still not working. If I run what you suggested I get the following warning:
[SofaViewer] Could not create file ‘path_to_my_image/img.png’
Valid extensions: ddsand there is no background image.
(…)
If I only run
Sofa.Gui.GUIManager.Init(“scene”, “qglviewer”)
Sofa.Gui.GUIManager.createGUI(root, __file__)I get a similar warning:
[SofaViewer] Could not create file ‘/home/andrea/Projects/sofa/share/textures/SOFA_logo.bmp’
Valid extensions: ddsThis is because extensions other than dds (jpeg, png, tiff and bmp) are handled by the CImg plugin. Simply add a
root.addObject('RequiredPlugin', name='CImgPlugin')
In your
createScene
function.Let me know how that worked out.
J-N2 February 2021 at 17:13 #18476AndreaBlockedIt works!!! Thank you so much JN!!
-
AuthorPosts
- The topic ‘setBackgroundImage not working as expected’ is closed to new replies.