How to set pivot point types from Python...

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

How to set pivot point types from Python...

Post by dvochin » Wed May 29, 2013 6:46 am

Hi, I'm trying to set the pivot_point from python to "CURSOR" but after many attempts I'm getting nowhere

From the post at (http://blenderartists.org/forum/archive ... 44453.html) It was said that the line below works but I get an error that ''Context' object has no attribute 'space''

bpy.ops.wm.context_set_enum(data_path="space.pivot_point",value="CURSOR")

How can I do this? (I also need to set the mesh selection mode to 'vertex select')

Thanks!

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Wed May 29, 2013 1:05 pm

try "space_data.pivot_point"
I'm sitting, waiting, wishing, building Blender in superstition...

dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

Post by dvochin » Wed May 29, 2013 7:34 pm

CoDEmanX wrote:try "space_data.pivot_point"
Hi CoDEmanX, thanks for taking the time again...

I've tried in Blender's Python console "bpy.context.space_data.pivot_point = 'CURSOR'" and I get "AttributeError: 'SpaceConsole' object has no attribute 'pivot_point'"... presumable because the SpaceView3D must be active?

How do I set a view active? So far the only (really bad) way I know of doing this is going through the tree of objects in bpy.context.screens[].areas[].regions[] to find the view3d in whatever screen layout I have at the time and find it this way (tried and it works... but is extremely brittle & messy!)

Is the method in my first post broken? It seemed to work for the folks at the previous post about a year ago...

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Thu May 30, 2013 1:16 am

the correct path is space_data, not space - not sure if this was renamed a while ago.

If you run an operator from view 3d, then just access context.space_data, otherwise there is no other way than to iterate over all areas in current screen. There is no cleaner solution, and it actually makes sense this way - how should it know the view3d you are interested into (if there's more than 1)? You need to handle that in your own code.
I'm sitting, waiting, wishing, building Blender in superstition...

dvochin
Posts: 0
Joined: Mon Aug 20, 2012 9:39 pm

Post by dvochin » Thu May 30, 2013 2:27 am

Hi CoDEmanX, thanks again... So if I read your answer correct then

1. if the user's mouse is hovering over a 3D view then the following python code will work:

bpy.ops.wm.context_set_enum(data_path="space_data.pivot_point",value="CURSOR")

2. If the user's mouse is NOT in the 3D view, then there is no other way then to fish out the 3D view from the tree of objects from bpy.context.screen.

Have I gotten that right?

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Thu May 30, 2013 8:18 am

That's right!

So if you wanna change the pivot no matter what, write your own operator which sets it for all 3d views regardless of context.
I'm sitting, waiting, wishing, building Blender in superstition...

Akihisa
Posts: 0
Joined: Sun Jun 23, 2013 12:57 pm

Re: How to set pivot point types from Python...

Post by Akihisa » Sun Jun 23, 2013 1:38 pm

dvochin wrote:Hi, I'm trying to set the pivot_point from python to "CURSOR" but after many attempts I'm getting nowhere

From the post at (http://blenderartists.org/forum/archive ... 44453.html) It was said that the line below works but I get an error that ''Context' object has no attribute 'space''

bpy.ops.wm.context_set_enum(data_path="space.pivot_point",value="CURSOR")

How can I do this? (I also need to set the mesh selection mode to 'vertex select')

Thanks!
Hi here is a code which enable you to set the pivot point to the cursor :

def center():

original_type = bpy.context.area.type
bpy.context.area.type = "VIEW_3D"
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action = 'DESELECT')
bpy.ops.mesh.select_all(action = 'SELECT')
#bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.editmode_toggle()
bpy.ops.object.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
bpy.context.area.type = original_type
curseur_location = bpy.context.scene.cursor_location.copy()
print(curseur_location)
return curseur_location

if you delete the "#" on line 6, the code will set the pivot point to the center of your object. I hope that's what you need

Post Reply