22
$\begingroup$

I am trying to set a cursor location and pivot point with

bpy.types.SpaceView3D.pivot_point='CURSOR'
bpy.types.SpaceView3D.cursor_location = (0.0, 0.0, 0.0)

and I see I am not doing it right. How to do it correctly?

$\endgroup$

5 Answers 5

25
$\begingroup$

See Adhi's answer here as to why this won't work the way you are doing it.

Using Context

It's easier to read 3D View space's settings through bpy.context, e.g.:

  • Use bpy.context.space_data, if the 3D View area is active (i.e. accessed through an operator executed from the 3D View itself).
  • Use bpy.context.area.spaces[1], if accessed through a Console whose display type is directly switched from a 3D View.
  • Use bpy.context.screen.areas[X].spaces[0] if accessed through a Console in another area, index X must be searched beforehand.

What you want to use is

bpy.context.area.spaces[1].pivot_point='CURSOR'
bpy.context.area.spaces[1].cursor_location = (0.0, 0.0, 0.0)

For the cursor location, there is a shorter alternative. All 3D View's cursor location is linked to the scene's, so it can also be accessed from bpy.context.scene:

bpy.context.scene.cursor_location = (0.0, 0.0, 0.0)

V2.8+ :

bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
$\endgroup$
3
  • $\begingroup$ what worked for me was your third example $\endgroup$
    – dimus
    Dec 8, 2013 at 0:10
  • $\begingroup$ @dimus Do you mean that the others don't work or that way is better? $\endgroup$
    – iKlsR
    Dec 8, 2013 at 0:23
  • $\begingroup$ I ran it from text editor, and the third example worked -- I added a 'comment-answer' with the code: blender.stackexchange.com/a/5361/1311 $\endgroup$
    – dimus
    Dec 8, 2013 at 0:31
6
$\begingroup$

Here is a snippet of the code (forgive my bad python) which I ended up using after reading the answer

import bpy

def areas_tuple():
    res = {}                                                               
    count = 0
    for area in bpy.context.screen.areas:                                  
        res[area.type] = count                                             
        count += 1
    return res  

areas = areas_tuple()
view3d = bpy.context.screen.areas[areas['VIEW_3D']].spaces[0]
view3d.pivot_point='CURSOR'
view3d.cursor_location = (0.0, 0.0, 0.0)
$\endgroup$
0
5
$\begingroup$

Building on dimus' answer and the accepted one further:

def area_of_type(type_name):
    for area in bpy.context.screen.areas:
        if area.type == type_name:
            return area

def get_3d_view():
    return area_of_type('VIEW_3D').spaces[0]

view3d = get_3d_view()
view3d.pivot_point='CURSOR'
view3d.cursor_location = (0.0, 0.0, 0.0)
$\endgroup$
5
$\begingroup$

In Blender 2.80 this should be:

bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
$\endgroup$
1
$\begingroup$

If you have zero or multiple number of view3d, the way to process this case is

view3ds = [area.spaces[0] for area in bpy.context.screen.areas if (area.type=="VIEW_3D")]
for view3d in view3ds:
    view3d.pivot_point='CURSOR'
    view3d.cursor_location = (0.0, 0.0, 0.0)

If you exactly know there is only one VIEW_3D by your eye, the way to process this case is

view3d = [area.spaces[0] for area in bpy.context.screen.areas if (area.type=="VIEW_3D")][0]
view3d.pivot_point='CURSOR'
view3d.cursor_location = (0.0, 0.0, 0.0)

The idea to make this simple coding way is by python's magic.

[f(x) for x in sequence if condition]

This intuition of coding method can help you to process more complicated data with simple thinking and coding.

$\endgroup$

You must log in to answer this question.

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