snap_cursor_to_selected Fails in Python #89862

Closed
opened 2021-07-14 22:46:42 +02:00 by Leul Mulugeta · 9 comments

System Information
Operating system: Windows-10-10.0.19041-SP0 64 Bits
Graphics card: GeForce RTX 2070/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 461.92

Blender Version
Broken: version: 2.93.1, branch: master, commit date: 2021-06-22 05:57, hash: 1b8d33b18c
Worked: it Failed in 2.83 too

Short description of error
The snap_cursor_to_selected works with the shortcut but Fails in Python with an Error Message:

RuntimeError: Operator bpy.ops.view3d.snap_cursor_to_selected.poll() failed, context is incorrect
Error: Python script failed, check the message in the system console

Exact steps for others to reproduce the error

  • open the Blend File
  • Select the cube
  • Run the Script
import bpy

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.view3d.snap_cursor_to_selected()

Snap_Cursor_Bug.blend

**System Information** Operating system: Windows-10-10.0.19041-SP0 64 Bits Graphics card: GeForce RTX 2070/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 461.92 **Blender Version** Broken: version: 2.93.1, branch: master, commit date: 2021-06-22 05:57, hash: `1b8d33b18c` Worked: it Failed in 2.83 too **Short description of error** The snap_cursor_to_selected works with the shortcut but Fails in Python with an Error Message: ``` RuntimeError: Operator bpy.ops.view3d.snap_cursor_to_selected.poll() failed, context is incorrect Error: Python script failed, check the message in the system console ``` **Exact steps for others to reproduce the error** - open the Blend File - Select the cube - Run the Script ``` import bpy bpy.ops.object.editmode_toggle() bpy.ops.mesh.select_all(action='SELECT') bpy.ops.view3d.snap_cursor_to_selected() ``` [Snap_Cursor_Bug.blend](https://archive.blender.org/developer/F10224398/Snap_Cursor_Bug.blend)
Author

Added subscriber: @Leul

Added subscriber: @Leul
Member

Added subscriber: @kursadk

Added subscriber: @kursadk
Member

Unfortunately the context is incorrect message means that you are writing and using this piece of code incorrectly. Most likely you are running the line from a wrong place, or trying to use it with a wrong object. This is a pretty common scripting mistake. Basically this is not a bug rather it is a feature of the Python API.

Turn your script into a real add-on or look into context overriding.

Unfortunately the **context is incorrect** message means that you are writing and using this piece of code incorrectly. Most likely you are running the line from a wrong place, or trying to use it with a wrong object. This is a pretty common scripting mistake. Basically this is not a bug rather it is a feature of the Python API. Turn your script into a real add-on or look into context overriding.
Author

But the problem is if you just use the shortcuts:

  • Select a Cube
  • Tab to go to Edit
  • A to select all
  • Shift S to snap_cursor_to_selected
    everything works as Expected and in the codes shown in the info Viewer will be
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.view3d.snap_cursor_to_selected()

i just cant use them in Python, hopefully am not missing anything :/

But the problem is if you just use the shortcuts: - Select a Cube - Tab to go to Edit - A to select all - Shift S to snap_cursor_to_selected everything works as Expected and in the codes shown in the **info Viewer** will be ``` bpy.ops.object.editmode_toggle() bpy.ops.mesh.select_all(action='SELECT') bpy.ops.view3d.snap_cursor_to_selected() ``` i just cant use them in Python, hopefully am not missing anything :/

Added subscriber: @mano-wii

Added subscriber: @mano-wii

Changed status from 'Needs Triage' to: 'Archived'

Changed status from 'Needs Triage' to: 'Archived'

bpy.ops.view3d.snap_cursor_to_selected needs to be called in the correct context.
It is possible to have more than one view3d open and the code cannot predict which one to use.
Try overriding the context --> https://docs.blender.org/api/current/bpy.ops.html#overriding-context

In fact this does not seem to be a bug.
For help using Blender, please try one of the community websites: https://www.blender.org/community/

`bpy.ops.view3d.snap_cursor_to_selected` needs to be called in the correct context. It is possible to have more than one view3d open and the code cannot predict which one to use. Try overriding the context --> https://docs.blender.org/api/current/bpy.ops.html#overriding-context In fact this does not seem to be a bug. For help using Blender, please try one of the community websites: https://www.blender.org/community/
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

The thing is: you are running the code in the context of the text editor.

Please check
https://docs.blender.org/api/2.93/bpy.ops.html#execution-context
https://docs.blender.org/api/2.93/bpy.ops.html#overriding-context

So you should be able to override context with something like this:

import bpy

for window in bpy.context.window_manager.windows:
    screen = window.screen

    for area in screen.areas:
        if area.type == 'VIEW_3D':
            override = {'window': window, 'screen': screen, 'area': area}
            bpy.ops.object.editmode_toggle()
            bpy.ops.mesh.select_all(action='SELECT')
            bpy.ops.view3d.snap_cursor_to_selected(override)
            break

This seems to work

The thing is: you are running the code in the context of the text editor. Please check https://docs.blender.org/api/2.93/bpy.ops.html#execution-context https://docs.blender.org/api/2.93/bpy.ops.html#overriding-context So you should be able to override context with something like this: ``` import bpy for window in bpy.context.window_manager.windows: screen = window.screen for area in screen.areas: if area.type == 'VIEW_3D': override = {'window': window, 'screen': screen, 'area': area} bpy.ops.object.editmode_toggle() bpy.ops.mesh.select_all(action='SELECT') bpy.ops.view3d.snap_cursor_to_selected(override) break ``` This seems to work
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#89862
No description provided.