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


**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.

  • $\begingroup$ are you trying this from within an Add-on or from a simple script / the python console? $\endgroup$
    – aliasguru
    Apr 5 '17 at 6:48
  • $\begingroup$ Simple script python console basically. $\endgroup$
    – Chris
    Apr 5 '17 at 6:50
  • 1
    $\begingroup$ [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 $\endgroup$
    – 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'
  • $\begingroup$ for proper context. Could that be v = bpy.context.space_data? $\endgroup$
    – Chris
    Apr 5 '17 at 7:03
  • $\begingroup$ Answered my own question there. bpy.context.space_data does NOT work. Your solution with v3d worked perfectly. Thank you very much. $\endgroup$
    – Chris
    Apr 5 '17 at 7:08
  • $\begingroup$ @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. $\endgroup$
    – lemon
    Apr 5 '17 at 7:14
  • $\begingroup$ @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. $\endgroup$
    – Chris
    Apr 5 '17 at 7:18
  • $\begingroup$ @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. $\endgroup$
    – aliasguru
    Apr 5 '17 at 7:22

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.