executing context commands from a Qt Gui

Hello everyone, recently I have been spending a lot of time designing UI’s for blender,

Here is the latest UI, it is an in-app tutorial viewer, this is a customizeable window that currently works in Blender, Maya and Unity.


I will post a video tonight demoing it,

I do however have a small error, I am attempting to run some commands from this window, and although all bpy.data works, bpy.context and bpy.ops does not. (for obvious reasons)

I would like to run commands like:
bpy.ops.wm.link_append(directory=“E:/Desktop/temp/fileCompare/scaffolding.blend/Group/”, filename=“myGroup”, link=False)

Does anyone know of a solution to access these commands via a Qt GUI?

Thank you
-Alex Telford

UPDATE:
Video demo:

Alex,
I don’t know much about qt other than it exists and is used for building GUI’s. And I don’t know much about “proper” python coding. My experience has been I learn the bare minimum and sometimes hacky way of doing something just so my project works. In one of my projects, I did this little routine because I couldn’t figure out how to do it any other way:

def get_view3d_context(object):
for window in bpy.context.window_manager.windows:screen = window.screen
for area in screen.areas:

[INDENT=2]if area.type == ‘VIEW_3D’:
[/INDENT]
[INDENT=3]for region in area.regions:
[/INDENT]
[INDENT=4]if region.type == ‘WINDOW’:
[/INDENT]
[INDENT=5]bpy.context.scene.objects.active = object
e_object = bpy.context.object
[/INDENT]
[INDENT=5]override = {‘window’:window,‘screen’:screen,‘area’:area,‘region’:region,‘edit_object’:e_object}
return override
[/INDENT]
[INDENT=2]
[/INDENT]
I used that to force a context in commands like:

context_override = get_view3d_context(working_object)

bpy.ops.view3d.snap_cursor_to_selected(context_override)

I don’t know if that helps in your case but I’m sharing what I have. There might have been a piece of the context missing (my override might not have had all the proper pairs) as the console threw an error but it worked anyway.

Awesome thank you,
I’m working late tonight but will give that a go when I get home.
-Alex

Here someone called dvochin has made a similar function with much better notation and comments:
http://www.blender.org/forum/viewtopic.php?p=105783#105783

Also the thread talks a bit about how to use it: for operators with multiple args, pass the context override as the first arg and not keyworded. i.e. bpy.ops.some_operator(context_override, arg1 = “True”, arg2 = “LOCAL” etc…).

Both dvochin’s example and mine use the view3D window as our target context, so you have to figure out what the context_override dictionary variable needs to have in it for the context your going for then you should be able to pass the context to the operator along with any other args you need.

Hi Alex,
How did you combine PyQt and Blender?
Are you launching an external Python process? Or do you look for a Py3.3 PyQt installation and launch it inside Blender?

I am in the process of porting my HiveGUI from PyQt to Blender, so I am very interested in this.

cheers

Sjoerd

Whew, worked late tonight so just took a look,
I couldn’t get this working, I have a temporary workaround for now where I just pass the context to the GUI on startup, Here is my code if you want to take a crack at it:
http://www.pasteall.org/45859/python

Also here is my get window function():

def get3DWindow():
    for win in bpy.data.window_managers[0].windows:
        for area in [area for area in win.screen.areas if area.type=="VIEW_3D"]:
            return [region for region in area.regions if region.type=="WINDOW"][0]



And yes I built PyQt into the blender python path. I also built PySide which I prefer, but it was unstable in blender.
Thank you for all your help everyone :slight_smile:
-Alex Telford

I see… unfortunately this is too fragile for me. I could get it running on my own machine, but the installation instructions would be evil…
On the other hand, if someone (you?) would maintain Blender-compatible PyQt binaries (just copy into the Blender Python folder) for a variety of platforms, I would happily try out your approach.

Alternatively, would execnet be viable with PyQt, do you think?

I am thinking about integrating PyQt and PySide into blenders source code tomorrow as I have the day off work,
The main challenge is getting a stable event loop running by default so we don’t have to have such ridiculous work arounds.
I will post the odd version to graphicall but I would likely not create a new build for every version of blender.

And I don’t see why execnet would not be viable, PyQt is just a wrapper around C++ commands.
-Alex

Integrating with the source code :open_mouth: That would be great, but I don’t see that being accepted into trunk…

What I meant is, binary versions of PyQt/PySide compiled against Python3.3, with the same compile flags as Python3.3 in Blender’s release builds. Then, “import PyQt4 / PySide” is guaranteed to work from within Blender, if you copy the binaries to the right directory.

I’m not worried about things getting put into trunk or not, as long as things work for me that is fine.
Compiling Qt directly into blenders python is pretty much a one click process using the Qt installer.

The main reason I want Qt built into blender is so that I can more easily use C++ Qt Gui’s which would allow me to have further control over blender, effectively gaining the potential to redesign blender in a way that best suits my workflow.
-Alex

Ok so I have built Qt into blender and can access context commands (to an extent, launching tools proves challenging)
unfortunately my blender version is now linked to my system so posting my build would not be useful.
Thank you for your help everyone, the next step is embedding blender windows into Qt Widgets :wink:
-Alex

Good to hear! I believe Pyppet managed to embed Blender inside GTK, perhaps this can work for Qt too.