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?
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?
See Adhi's answer here as to why this won't work the way you are doing it.
Using
ContextIt'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, indexXmust 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)
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)
In Blender 2.80 this should be:
bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
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)
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.