0
$\begingroup$
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?

$\endgroup$
8

1 Answer 1

2
$\begingroup$

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)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .