Resetting cursor position

Hi

I’m trying to save the state of the cursor and reset it back to the original state once I’ve moved it. Yet it doesn’t seem to have an effect.

See code below:

What do I need to do to get this to work?

cur_cursor = bpy.context.scene.cursor.location
# Move cursor
bpy.context.scene.cursor.location = (0.0, 10, 0.0)

# Do some stuff with the cursor position

# Put your toys back where you found them
bpy.context.scene.cursor.location = cur_cursor  #This doesn't work

I’m using Blender 2.82a.

hi, you have to make your “cur_cursor” to a Vector

import bpy
from mathutils import Vector

cur_cursor = Vector(bpy.context.scene.cursor.location)

Thanks that works but it’s not apparent why. :thinking:

If I just call bpy.context.scene.cursor.location it returns a type Vector, see below. So it’s strange why I need to typecast it again.

Secondly if I just assign a tuple of floats to cursor.location that also works.

>>> cur_cursor = bpy.context.scene.cursor.location
>>> type(cur_cursor)
<class 'Vector'>

>>> cur_cursor_as_vec = Vector(bpy.context.scene.cursor.location)
>>> type(cur_cursor_as_vec)
<class 'Vector'>

Doh, I just realised my noob mistake.

>>> bpy.context.scene.cursor.location = (0, 0, 0)
>>> cur_cursor = bpy.context.scene.cursor.location
>>> cur_cursor
Vector((0.0, 0.0, 0.0))

>>> bpy.context.scene.cursor.location = (0, 10, 0)
>>> cur_cursor
Vector((0.0, 10.0, 0.0)) # The value has updated!!

cur_cursor is pointing to cursor.location, not a copy of the values.

Typecasting works because it returns a copy of the value and not the reference.

2 Likes

in case anyone following this trail needs it. here’s how to reset the cursor rotation.

bpy.context.scene.cursor.rotation_euler = (0, 0, 0)

1 Like