0
Traceback (most recent call last):
  File "\Text", line 8, in <module>
  File "C:\Program Files\Blender Foundation\Blender 2.90\2.90\scripts\modules\bpy\ops.py", line 201, in __call__
    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.view3d.snap_selected_to_cursor.poll() failed, context is incorrect
Error: Python script failed, check the message in the system console

I am typing in this command, because I already got selected object before snapping:

bpy.ops.view3d.snap_selected_to_cursor(use_offset=False)

The question is: why I can not snap selected object to 3d cursor using bpy python while being able to do it by using mouse and keyboard?

brockmann
  • 12,613
  • 4
  • 50
  • 93
Robert
  • 35
  • 1
  • 7

1 Answers1

2

as @lemon says the answer is override the context

import bpy

for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        override = bpy.context.copy()
        override['area'] = area
        override['region'] = area.regions[4]
        bpy.ops.view3d.snap_selected_to_cursor( override, use_offset=False)
yhoyo
  • 2,285
  • 13
  • 32