JacquesLucke / blender_vscode

Visual Studio Code extension for Blender development.
MIT License
557 stars 76 forks source link

RuntimeError: Operator bpy.ops.wm.mouse_position.poll() Missing 'window' in context #36

Closed pilgrim333 closed 5 years ago

pilgrim333 commented 5 years ago

I'm trying to learn blender python API using this example:

import bpy
from mathutils import *
D = bpy.data
C = bpy.context

class SimpleMouseOperator(bpy.types.Operator):
    """ This operator shows the mouse location,
        this string is used for the tooltip and API docs
    """
    bl_idname = "wm.mouse_position"
    bl_label = "Invoke Mouse Operator"

    x = bpy.props.IntProperty()
    y = bpy.props.IntProperty()

    def execute(self, context):
        # rather than printing, use the report function,
        # this way the message appears in the header,
        self.report({'INFO'}, "Mouse coords are %d %d" % (self.x, self.y))
        return {'FINISHED'}

    def invoke(self, context, event):
        self.x = event.mouse_x
        self.y = event.mouse_y
        return self.execute(context)

bpy.utils.register_class(SimpleMouseOperator)
bpy.ops.wm.mouse_position('INVOKE_DEFAULT')
bpy.ops.wm.mouse_position('EXEC_DEFAULT', x=20, y=66)

When I do "Blender: Run Script", I get this in the console:

Debug client attached.
Got GET: {'type': 'ping'}
Got POST: {'type': 'script', 'path': 'c:\\msys64\\home\\phan\\python\\new3\\mouse.py'}
Error: Traceback (most recent call last):
  File "C:\Users\phan\.vscode\extensions\jacqueslucke.blender-development-0.0.12\pythonFiles\include\blender_vscode\operators\script_runner.py", line 16, in execute
    runpy.run_path(self.filepath, init_globals={"CTX" : ctx})
  File "c:\x\blender-2.80\2.80\python\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "c:\x\blender-2.80\2.80\python\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "c:\x\blender-2.80\2.80\python\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\msys64\home\phan\python\new3\mouse.py", line 32, in <module>
    bpy.ops.wm.mouse_position('INVOKE_DEFAULT')
  File "c:\x\blender-2.80\2.80\scripts\modules\bpy\ops.py", line 198, in __call__
    ret = op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
RuntimeError: Operator bpy.ops.wm.mouse_position.poll() Missing 'window' in context
location: c:\x\blender-2.80\2.8... truncated

Do you know what I did wrong?

Thank you.

pilgrim333 commented 5 years ago

Oh, I didn't realized that it still works when I set a breakpoint and then invoke in the blender window with F3. I am still curious why I got the error message and why it didn't work the first time when I run the script. Thank you for a great plugin!

JacquesLucke commented 5 years ago

The main issue is probably that the script is executed outside of any context in Blender. This extension tries to bring back some context, but can only do so in limited ways..

So this is probably not your fault, but a limitation of the current solution. Hopefully there will be a better solution in the future.