What's the right way to snap cursor to selected with Python? I'm getting a failed context error with this:
bpy.ops.view3d.snap_cursor_to_selected()
I've seen a solution using an operator but this is not what I want (at least I don't think?).
Set the location directly
There is no need to use the operator.
What context are we looking at here? In object mode snaps to the average of selected objects origins, in edit mode the average of selected verts for mesh, bones for bones etc etc. When the global location vector (x, y, z) is known it's simply a matter of scene.cursor.location = (x, y, z) for 2.8x or scene.cursor_location in prior
eg 2.8x object mode example, moves cursor to average global location of selected objects.
import bpy
from mathutils import Vector
context = bpy.context
scene = context.scene
obs = context.selected_objects
n = len(obs)
assert(n)
scene.cursor.location = sum([o.matrix_world.translation for o in obs], Vector()) / n
(x, y, z)is known it's simply a matter ofscene.cursor.location = (x, y, z)for 2.8x orscene.cursor_locationin prior. $\endgroup$