# How to set transform orientation in code

In the 3d view:
On the bottom panel, on the right of the transform manipulator icon: Can change to: VIEW, NORMAL, GIMBAL, LOCAL, GLOBAL

How to do this in code though? According to the manual, it has to be done like this:
bpy.types.SpaceView3D.transform_orientation = ‘LOCAL’
But it has no effect.

What is the proper way to do this?

Edit:
Same for
bpy.types.SpaceView3D.use_pivot_point_align = False
It has no effect

I figured it out. It should be:

context.space_data.use_pivot_point_align = False
and
context.space_data.transform_orientation = ‘LOCAL’

I also want to know about transform orientations via python. But the lines you wrote don’t work. Specifically, i would access the degrees of the transform orientations. It could be useful if you can manipoulate the from some text box or similar, or have a better list of the actual transfomr orientations.
If someone founded something useful could say here? thankyou!

Really strange discoverings. With this code you can create a new transform orientation, and you can assign really strange values to its matrix. For example you can create a shorter Z axis, or orientation with inverted axis.

import bpy

bpy.ops.transform.create_orientation(name = “Personal2”, overwrite = True)

bpy.context.scene.orientations[-1].matrix = (((1.0, 0.0, 0.0),
(0.0, -1.0, 0.0),
(0.0, 0.0, -0.5)))

How can i get the current orientation?
If I remember exactly, there was an add-on that does something with the transform orientations. Does anybody remember it?

I would like to create an orientation manager like this, but I need some help

The things I need are to get the current custom orientation (if it there is) and to convert a 3x3 matrix in 3 degree values for x y and z, and viceversa.

Hope you can help me. I really need a thing like this in architectural modeling.

Thankyou! This is what i needed!

I found this, about the current orientation. http://www.blender.org/documentation/blender_python_api_2_63_13/bpy.types.SpaceView3D.html?highlight=current_orientation#bpy.types.SpaceView3D.current_orientation
But if i write it in the code it says that SpaceView3D has no current_orientation attribute. What is the problem?

I write

print(bpy.types.SpaceView3D.current_orientation)

what’s wrong?

Hi Lell,

You are looking at the class with bpy.types.ClassName.property, whereas you want to change the instance of that class
Eg context.space_data or loop thru the spaces of context.area etc

Below is some code that loops thru the current screen and shows each area and then each space and if it’s a 3d view the current_orientation

``````
import bpy

def show_screen():
#f = repr  #uncomment and use repr to show repr of instance
f = type  #uncomment and use type to show the type

context = bpy.context
print("------------------")
print("Screen: %s" % context.screen.name)
print("------------------")
for area_index, area in context.screen.areas.items():
print("	[%s] Area[%d] %s" % (area.type, area_index, f(area)))

#print("		area.spaces.active %s" % repr(area.spaces.active))
for space_index, space in area.spaces.items():
print("		[%s] space[%d] %s" % (space.type, space_index, f(space)))
if space.type == 'VIEW_3D':
print("		 current_orientation = %s" % repr(space.current_orientation))

bpy.app.driver_namespace["showscreen"] = show_screen

``````

Output to console showing class names

``````
&gt;&gt;&gt; SS = bpy.app.driver_namespace['showscreen']
&gt;&gt;&gt; SS()
------------------
Screen: Scripting
------------------
[INFO] Area[0] &lt;class 'bpy.types.Area'&gt;
[INFO] space[0] &lt;class 'bpy.types.SpaceInfo'&gt;
[OUTLINER] Area[1] &lt;class 'bpy.types.Area'&gt;
[OUTLINER] space[0] &lt;class 'bpy.types.SpaceOutliner'&gt;
[PROPERTIES] space[1] &lt;class 'bpy.types.SpaceProperties'&gt;
[VIEW_3D] Area[2] &lt;class 'bpy.types.Area'&gt;
[VIEW_3D] space[0] &lt;class 'bpy.types.SpaceView3D'&gt;
current_orientation = None
[CONSOLE] space[1] &lt;class 'bpy.types.SpaceConsole'&gt;
[PROPERTIES] Area[3] &lt;class 'bpy.types.Area'&gt;
[PROPERTIES] space[0] &lt;class 'bpy.types.SpaceProperties'&gt;
[VIEW_3D] space[1] &lt;class 'bpy.types.SpaceView3D'&gt;
current_orientation = None
[OUTLINER] space[2] &lt;class 'bpy.types.SpaceOutliner'&gt;
[TEXT_EDITOR] Area[4] &lt;class 'bpy.types.Area'&gt;
[TEXT_EDITOR] space[0] &lt;class 'bpy.types.SpaceTextEditor'&gt;
[VIEW_3D] space[1] &lt;class 'bpy.types.SpaceView3D'&gt;
current_orientation = None
[CONSOLE] Area[5] &lt;class 'bpy.types.Area'&gt;
[CONSOLE] space[0] &lt;class 'bpy.types.SpaceConsole'&gt;

&gt;&gt;&gt;

``````

Output to console showing repr of instance names

``````
------------------
Screen: Scripting
------------------
[INFO] Area[0] bpy.data.screens['Scripting']...Area
[INFO] space[0] bpy.data.screens['Scripting']...SpaceInfo
[OUTLINER] Area[1] bpy.data.screens['Scripting']...Area
[OUTLINER] space[0] bpy.data.screens['Scripting']...SpaceOutliner
[PROPERTIES] space[1] bpy.data.screens['Scripting']...SpaceProperties
[VIEW_3D] Area[2] bpy.data.screens['Scripting']...Area
[VIEW_3D] space[0] bpy.data.screens['Scripting']...SpaceView3D
current_orientation = None
[CONSOLE] space[1] bpy.data.screens['Scripting']...SpaceConsole
[PROPERTIES] Area[3] bpy.data.screens['Scripting']...Area
[PROPERTIES] space[0] bpy.data.screens['Scripting']...SpaceProperties
[VIEW_3D] space[1] bpy.data.screens['Scripting']...SpaceView3D
current_orientation = None
[OUTLINER] space[2] bpy.data.screens['Scripting']...SpaceOutliner
[TEXT_EDITOR] Area[4] bpy.data.screens['Scripting']...Area
[TEXT_EDITOR] space[0] bpy.data.screens['Scripting']...SpaceTextEditor
[VIEW_3D] space[1] bpy.data.screens['Scripting']...SpaceView3D
current_orientation = None
[CONSOLE] Area[5] bpy.data.screens['Scripting']...Area
[CONSOLE] space[0] bpy.data.screens['Scripting']...SpaceConsole

``````

Thankyou for explanation! That seems logical!

anyone know the 2.8 code ? could’nt find out this one is tricky

bpy.context.scene.transform_orientation_slots

bpy.context.scene.transform_orientation_slots = ‘LOCAL’

not working

.type

not being recognize, altho its on the 2.8 api page

if we set orientation not with the header but with the set tranfrom orientation operator we can get this code in the info panel

bpy.ops.transform.select_orientation(orientation=‘GIMBAL’)

not working either, how strange

that massive upvote tho

transform_orientation_slots is an array, so:

bpy.context.scene.transform_orientation_slots[0].type = ‘LOCAL’

I’m not sure what the purpose of slots other than 0 are, but 0 seems to be the viewport.

Edit: 1 seems to be the transform gizmo orientation.

4 Likes

How strange, how did you knew that it was an array ?

I can’t remember how I figured it out. I came across it a while back. I think maybe I just noticed it was plural, and gave it a try.

1 Like

by the way do you know how to invoke the whole list of orientation types ?

print(list(bpy.context.scene.transform_orientation_slots[0].type))

this is the string, i want to print the whole list and not just the stringle letters
can’t get my hands on it, i thinks is related to the data branch, not sure

I didn’t know, but I was curious and did a bit of searching and found this: https://blender.stackexchange.com/questions/58030/how-to-get-all-available-entries-of-an-enumproperty

Just swap out the second to last line in the solution with:
`enum = enum_members_from_instance(bpy.context.scene.transform_orientation_slots[0], "type")`