# How do I change viewport alignment of newly created objects with a python command?

I'm making a custom pie menu with a pie editor addon and wanted to have some buttons with options that will let me change the default alignment of newly created objects from "world", "view" to "3d cursor" as I tend to change these options sometimes and don't want to open preferences window all the time I do. Can somebody tell me if there's a specific python command that will allow me to do that? I can't seem to find one.

In Python, the command to set the alignment is  bpy.context.preferences.edit.object_align = 'VALUE' where VALUE can be one of WORLD, VIEW, or CURSOR.

## Background:

The way to find such things in preferences is to enable Developer Extras in the Interface panel. Then if you hover over the input field and right click you'll get a menu like this:

If you select Edit Source it will make the source for that part of the UI available as text object. You can go to the text editor and view the code. You may have to use the dropdown to select the right file. Here's the code for that field:

        flow.prop(edit, "object_align", text="Align To")


You have to do some decoding to get the proper name. First you start with the bpy.context.preferences prefix. The first argument of the prop field tells you that the field is in the edit properties. The second argument tells you the name of the field. Putting it all together you get bpy.context.preferences.edit.object_align.

If, as in this case, you don't know what values to assign, you can use the Python console to figure them out. Before each line in the below sample, I manually set the field to one of the three options. So the output below is the value you should use if you want that option.

>>> bpy.context.preferences.edit.object_align
'WORLD'

>>> bpy.context.preferences.edit.object_align
'VIEW'

>>> bpy.context.preferences.edit.object_align
'CURSOR'


## Bonus for reading this far:

Here's a very crude demonstration of a pie menu using this command:

import bpy

# bl_label is displayed at the center of the pie menu
bl_label = 'Align Mode'
bl_idname = 'MY_MT_Align'

def draw(self, context):
layout = self.layout
pie.operator("my.align_world", icon = 'EVENT_A')
pie.operator("my.align_view", icon = 'EVENT_B')
pie.operator("my.align_cursor", icon = 'EVENT_C')

class MY_OT_align_world(Operator):
bl_idname = 'my.align_world'
bl_label = 'Align World'
bl_description = "Set New object alignment to world"

def execute(self, context):
bpy.context.preferences.edit.object_align = "WORLD"
return {'FINISHED'}

class MY_OT_align_view(Operator):
bl_idname = 'my.align_view'
bl_label = 'Align View'
bl_description = "Set New object alignment to view"

def execute(self, context):
bpy.context.preferences.edit.object_align = "VIEW"
return {'FINISHED'}

class MY_OT_align_cursor(Operator):
bl_idname = 'my.align_cursor'
bl_label = 'Align Cursor'
bl_description = "Set New object alignment to 3D Cursor"

def execute(self, context):
bpy.context.preferences.edit.object_align = "CURSOR"
return {'FINISHED'}

classes = [
MY_OT_align_world,
MY_OT_align_view,
MY_OT_align_cursor,
MY_MT_align,
]

def register():
for cls in classes:
bpy.utils.register_class(cls)

def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)

if __name__ == '__main__':
register()

• You can also directly modify the enum prop by adding pie.prop(context.preferences.edit, "object_align") to the pie draw method Jan 15 at 10:03