5
$\begingroup$

Some scripts use the 3D cursor (for adding new objects or for a point of reference).

A common way to do this is using bpy.context.scene.cursor_location.

However in Local View each view has its own cursor, making tools behave strangely when local-view is enabled.


So the question is:

What's a good way to access the 3D cursor which will use the cursor as seen by the user?

$\endgroup$
4
$\begingroup$

Here's an example of 2 different ways to access the cursor.

Checking the space type and using only 3D views.

def context_cursor(context):
    scene = context.scene
    space = context.space_data

    cursor = (space if space and space.type == 'VIEW_3D'
              else scene).cursor_location
    return cursor

Using getattr with a fallback, however this isn't so reliable since it may return a 2D cursor for the image space (which probably isn't what you want).

# don't use this example!
def context_cursor(context):
    cursor = getattr(context.space_data, "cursor_location",
                     context.scene.cursor_location)
    return cursor

Note that when the 3D view isn't a Local View. context.space_data.cursor_location will use the scene's cursor.

$\endgroup$
3
$\begingroup$

It appears that the local view's cursor is the VIEW_3D space's cursor (rather than the context scene's cursor):

import bpy
for area in bpy.context.screen.areas:
    if area.type == 'VIEW_3D':
        for space in area.spaces:
            if space.type == 'VIEW_3D':
                print( space.cursor_location )
                break

enter image description here

$\endgroup$
  • $\begingroup$ Right, pasted the wrong version for some reason, thanks @batFINGER. $\endgroup$ – TLousky Feb 25 '16 at 15:00
  • 1
    $\begingroup$ Wonder what voodoo magic ideasman42 has for this one. I feel context.space_data.cursor_location always gives you the "as seen by the user" cursor loc. when it's the 3D_VIEW type space. Could set up a bpy.types.Context.cursor_location property that returns such or... $\endgroup$ – batFINGER Feb 25 '16 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.