Page MenuHome

bpy.ops.screen.userpref_show() not Working
Closed, ResolvedPublic

Description

System Information
Operating system: Win 10
Graphics card: Nivida GForce RTX 2060

Blender Version
Broken: (version: 2.82a (sub 7), branch: master, commit date: 2020-03-12 05:06, hash: 375c7dc4caf4, type: Release
build date: 2020-03-12, 15:41:08)
Worked:: ?

Short description of error
Using "bpy.ops.screen.userpref_show()" in a script or add-on button doesn't display the Preference window at all.

I know the button works because I used another command and the command executed fine.
It also doesn't work in script editor either

Exact steps for others to reproduce the error
Got command from right-clicking and copying command off of preferences menu.
add "bpy.ops.screen.userpref_show()" to script or operator button

Event Timeline

Can confirm, will wait for others to confirm it is a bug and not intended behavior.

Julian Eisel (Severin) changed the task status from Needs Triage to Needs Information from User.EditedSat, May 23, 11:34 PM

I see why the operator wouldn't execute from the Python console. But it works fine if I execute it from a script or button.

Could you upload the failing code here? I have a fix ready but would like to know why it doesn't work from a button for you.

(For triagers: Please don't close this without checking with me first.)

This is the code to the operator.
As I stated earlier, I know the button works because I put different commands in. Eliminated if it was my coding in the panel of my add on.

_INIT_

import bpy
from . filefuncs import PREF_OT_Operator

classes = (PREF_OT_Operator, QTASK_PT_Panel)

def register():
    for cls in classes:
        bpy.utils.register_class(cls)
        bpy.types.Scene.my_tool = bpy.props.PointerProperty(type=My_Settings)

def unregister():
    del bpy.types.Scene.my_tool
    for cls in reversed(classes):
        bpy.utils.unregister_class(cls)

PANEL (qtpanel.py)
row.operator('viewed3d.file_preferences', text="", icon="PREFERENCES")

OPERATOR (filefunc.py)

class PREF_OT_Operator(bpy.types.Operator):
    bl_idname = "viewed3d.file_preferences"
    bl_label = "Preferences"
    bl_description = "Preferences"

    def execute(self, context):
        bpy.ops.screen.userpref_show()
        return {'FINISHED'}

Here is a link to the entire addon if you want to download the zip
However, the version on the website has that button commented out

QuickTasks Addon

BTW... this is the error I get in the console...

ERROR (wm.operator): c:\b\win64_cmake_vs2017\win64_cmake_vs2017\blender.git\source\blender\windowmanager\intern\wm_event_system.c:1486 wm_operator_invoke: invalid operator call 'SCREEN_OT_userpref_show'

The reason this did not work is because the operator uses the invoke() function, so that there's an event to take the cursor position from. This cursor position is used to determine the window position.
So you were actually able to call the operator, but you had to call it in the invoke context: bpy.ops.screen.userpref_show('INVOKE_DEFAULT').

We can however get the cursor position differently and always use the execute() function then, which is what I've done now.

Also, since I've seen this mistake a few times recently: You don't need to create an operator in your example. screen.userpref_show() already is an operator and it can assigned to a button directly, e.g. layout.operator("screen.userpref_show").

Okay... I will give it a try... Thanks

Cool, it fixes info_log_show() and others as well, was about to ask.

@Julian Eisel (Severin) - cursor position is used to determine the window position.

Any idea why?

I can understand why we position our little "OK?" alerts close to the mouse pointer, but I can't see why the positioning of new windows would want to be influenced by the position of the mouse when you invoked it. You'd think we'd just want to initially center a new window using the bounds of the previous one. And then you wouldn't need an xy for WM_window_open_temp() and so don't need to use invoke.

If you look at that positioning logic as it is now it seems jankey anyway and would generally get you a left position that is off the screen and would just be corrected by the following wm_window_check_position(). Which is why Preferences is almost always hugging the left side of the monitor. The File Browser only centers nicely because it doesn't actually use the mouse position at all but just pretends the mouse is at screen center instead.

I'm fine with discussing and possibly changing that. I thought about it as well but didn't want to do design changes like that without discussing first. The only reason I changed code was that there was a simple solution, so it was quick to address.

Thinking about it though, I actually find it quite "smart" that Blender places the window under your cursor. If you open the Preferences, it's usually with the intend to interact with it immediately. If it opened at the center you have to move the mouse to it first. But Blender knows where your mouse is, it can just place the window under your cursor and you can skip that part.
Maybe having a predictable window position is more important than this supposed, tiny quality of life improvement.

IIRC for the File Browser we decided to center it always, because you'd often trigger it from a more centered position and it would just look slightly out of place then. For some reason its default size or aspect ratio also made it feel odd if placed under the cursor.

Anyway, these are just my current thoughts on this. It's all up for debate.