# How to set screen transform_orientation

I have not been able to find how to set this property in any forum.

I am trying to set the transform orientation from 'GLOBAL' to 'GIMBAL' through python.

blender api has the transform_orientation property: https://docs.blender.org/api/blender_python_api_2_78c_release/bpy.types.SpaceView3D.html

I have not been able to set this property. When i hover over the property in blender i get this

bpy.data.screens["Default"]...transform_orientation


**I know of gimbal lock. Lets just assume i already know this and have accounted for it already in my rotations.

I have already manually set up all of this using the UI and it is working perfectly. I just need to know how to set this property using python script.

Thank you.

• are you trying this from within an Add-on or from a simple script / the python console? – aliasguru Apr 5 '17 at 6:48
• Simple script python console basically. – Chris Apr 5 '17 at 6:50
• [p for s in bpy.data.screens for a in s.areas if a.type == 'VIEW_3D' for p in a.spaces][0].transform_orientation – lemon Apr 5 '17 at 6:51

If you're using the script editor, you can do it like this:

import bpy

v3d = [x for x in bpy.context.screen.areas if x.type == 'VIEW_3D']

if v3d:
# you need to decide what to do if there are more than one 3D Views present
# you could either set the orientation on the first one, or on a specific one

# to set it on all views:
for v in v3d:
v.spaces[0].transform_orientation = 'GIMBAL'

# to set it only on the first one found
v3d[0].spaces[0].transform_orientation = 'GIMBAL'


From an add-on, it should be:

# from within an Add-on, it's a lot easier, as you have a proper context
def execute(self, context):
v = context.space_data
v.transform_orientation = 'GIMBAL'

• for proper context. Could that be v = bpy.context.space_data? – Chris Apr 5 '17 at 7:03
• Answered my own question there. bpy.context.space_data does NOT work. Your solution with v3d worked perfectly. Thank you very much. – Chris Apr 5 '17 at 7:08
• @ChrisOmlor, bpy.context.space_data works if in an operator and run from a 3D view. If not test space_data.type == 'VIEW_3D' before. – lemon Apr 5 '17 at 7:14
• @lemon Makes sense. That is where my mistake has been the entire time. I was not in an operator and trying to grab the context like i do for the scene objects with bpy.context.scene.objects which does not work here. Thank you for the clarification. – Chris Apr 5 '17 at 7:18
• @ChrisOmlor note that with the operator method I'm not using bpy.context, but context instead. This variable is passed on automatically to the execute() method, that's why you have it in hand, and you can be sure you're dealing with a 3D View context. – aliasguru Apr 5 '17 at 7:22