View Full Version : Qt and PyQt in same application? Or how to connect Qt with 3D software Blender

14th May 2016, 10:11

Some of you may know the open source 3d software Blender. https://www.blender.org/

It has its own unique homebrew UI solution. It is imho not a really nice UI solution. Too limited, too hard to modify, and everything but intuitive. And so i want to change it.

What i want to achieve is to integrate Qt in the one or another way to overcome the limitation of the homebrew Blender UI solution. The problem is, the Blender UI itself is made in a mixture of C and Python. Additionally, all Blender addons relies at the Python code. So i have to stay with Python and the proper structure, or i kill all addons. Which is a problem since even some core functionality comes as an addon.

But PyQt does not give me the needed access to the vital parts from what i can see so far. I may be able to access the functionality*, i have seen some other folks doing this with a floating PyQt panel. But the Blender UI is divided into several OpenGl Editors and subpanels. And i have no idea how to integrate PyQt into this single editors. Floating is definitely no option here. I cannot bother the users to sort the UI elements by hand ^^

* I personally am still an absolute beginner with Qt, but willing to learn. I go through some PyQt5 tutorials at the moment. I was so far able to connect PyQt5 with Blender. A Qt panel is showing. But it has no connection to the rest yet, besides, when you click close then you close Blender too. And it is modal, and makes Blender unresponsible.

In short, i am lost how to proceed from here :)

What would be the proper way to integrate Qt/PyQt into Blender? Am i even at the right track with PyQt5? Do i need both, Qt and PyQt? Is a proper Qt integration even possible without to rewrite Blender in big parts?

Questions over questions. Could you be so kind to bring an absolute beginner at the right track? :)

14th May 2016, 17:24
My understanding is that PyQt is mostly a Python wrapper around Qt, that allows you to use Qt classes and functions from Python. So it will depend on having the Qt run-time libraries installed. It probably does not depend on having the full Qt development environment installed (compilers, debuggers, Qt Creator, etc.) unless you want to develop your own Qt-based C++ classes and expose the to Python through whatever mechanism PyQt uses for that.

If you are able to get Blender to display a Qt panel using PyQt, then you have proven that it is possible to integrate the two. I assume you have seen the Blender / Python documentation (https://www.blender.org/api/blender_python_api_2_77_0/)? This looks pretty comprehensive to me.

The problem of Blender quitting when you close your panel could be that you have the QGuiApplication::quitOnLastWindowClosed() flag set to true (the default). If your panel is the only Qt-based top-level window showing, then Qt is doing what it is supposed to do. (And maybe Blender is crashing rather than simply exiting because qApp has gone away). The modality problem is likely because you show your panel using exec() instead of show().

15th May 2016, 10:03
Thanks for your feedback and the tips with the modal problem. Will try it out :)

Yes, PyQt is a Python wrapper around Qt. And PyQt includes already Qt from what i have understood. And as told, i was able to show a first window. But i am still far away from an integration. Blender is not only Python, which makes things so complicated here. It is also C. And parts of the graphical UI is done in C too, not only Python. That's where my problem is. I can show a single independant window at the top. But how could i integrate for example a Qt button into one of the editor panels? They are C and OpenGl, and on top of it Python. That's where i am currently stuck.

Can/must i go with PyQt here? Shall i go with native Qt here? Both, means parts with native Qt, and parts with PyQt? is it even possible without to rewrite Blender in big parts? I lack at the moment of the needed knowledge to decide which path to go, and if what i want to achieve is even possible. As told, every hint is welcome :)

15th May 2016, 12:06
Hm, i got this advice elsewhere:

I would suggest just adding the qapplication layer and wrap a qmainwindow around each blender window.

What he didn't say though is how to do that. Is this native Qt? PyQt? And how can this be achieved?

15th May 2016, 17:02
What he didn't say though is how to do that.

Not entirely sure why that would help.

In any case, -if- you can get the native id for the Blender window (in Windows, it's the window handle or HWND), you can wrap that with a Qt QWindow using the QWindow::fromWinId() static member function. I don't know how you call a static function in Python; maybe it is as simple as QWindow.fromWinId(). Once you have a QWindow pointer, you can then wrap that with a QWidget using QWidget::createWindowContainer(), another static member function. Now you can create a QMainWindow and set that widget pointer as the central widget QMainWindow::setCentralWidget(). That will give you a main window instance with a Blender window hosted inside it. I can't tell you if the Blender window would be "active" in this case or whether the QMainWindow, QWidget, or QWindow would eat the keyboard or mouse events before they got to Blender, or the other way around - Blender eats the events and you never see them.

I have no idea what you want to do with Blender. It seems like the Blender API gives you the ability to add menus, buttons, and other things to existing Blender UI element. The Add-on tutorial (https://www.blender.org/api/blender_python_api_2_77_0/info_tutorial_addon.html) goes into that.

15th May 2016, 18:10
Thanks a bunch. This gives me a few more things to invest :)

I have no idea what you want to do with Blender. It seems like the Blender API gives you the ability to add menus, buttons, and other things to existing Blender UI element. The Add-on tutorial goes into that.

In short: i want to create a intuitive and easy to use graphical UI with which you can really work. And i have started a fork for that :)

Project page: http://www.bforartists.de/

The UI design document with a description of what i want to achieve can be found here: http://www.bforartists.de/data/Bforartists_UI_redesign_Designdoc_Part%201_-_general.pdf

While at it, every help is welcome ;)

The current Blender UI solution with Python might look fine at the first look. But it is simply too limited and unhandy when you try to modify it. This already starts with such simple things like making 32 x 32 icon buttons. You cannot do that. The icon size is hardcoded in C to 16x16 pixels, appwide. Same goes for button appearance and shape. Size is hardcoded. Height can be defined by per cent. But not the width. The elements are either as long as the text, or drag as wide as you drag the panel. Buttons and edit fields have rounded corners, and looks very similar, which is unchangeable. You cannot create a customizable icon toolbar that you can place where you want and need it. Tooltips are one line of text. And so on. There is simply a ton of limits regarding appearance and functionality.

And that's why i want to try out if it is possible to implement Qt / PyQt. It can of course happen that this try turns out to be unmanageable. But Qt would bring so much improvement to the graphical UI. I simply want to try this route out first before i try to modify the super complicated Blender C code with my very limited knowledge. I simply want to have a look how far i can push it :)