4
$\begingroup$

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.

enter image description here

$\endgroup$

1 Answer 1

4
$\begingroup$

Answer:

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.

This will affect any objects added after you've made the change.

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:

right click menu

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
from bpy.types import Menu, Operator


class MY_MT_align(Menu):
    # 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 = layout.menu_pie()
        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()
    bpy.ops.wm.call_menu_pie(name = 'MY_MT_Align')
$\endgroup$
5
  • $\begingroup$ Thank you for your thorough answer, I am just starting to learn the API of blender after years of use. You saved me, I was looking for the method for quite a while and looked in the wrong direction. For some reason I thought it was somewhere in bpy.context.Preferences class, your answer cleared everything out for me and now the structure makes perfect sense! A new addition to my mega pie! $\endgroup$ Commented Jan 14, 2022 at 22:30
  • 1
    $\begingroup$ You're very welcome. I'm glad I was able to help. I look forward to seeing what you accomplish. The Preferences settings take some getting used to, but now you've seen the organization you should have no trouble figuring out the rest of them. $\endgroup$ Commented Jan 14, 2022 at 22:36
  • 1
    $\begingroup$ I should add that the script at the end is using a big hammer. You can do it with a single operator, if you give the operator a string property, set the string property differently in each operator entry in the menu and use it for the assignment. But I wanted the crude version to not introduce too many concepts all at once. $\endgroup$ Commented Jan 14, 2022 at 23:51
  • 1
    $\begingroup$ Ooh, yeah. I figured that 3 operators for 3 methods with one command might be an overkill. Thank you for your help, you saved me lots of headache! :) $\endgroup$ Commented Jan 15, 2022 at 9:00
  • 3
    $\begingroup$ You can also directly modify the enum prop by adding pie.prop(context.preferences.edit, "object_align") to the pie draw method $\endgroup$
    – Gorgious
    Commented Jan 15, 2022 at 10:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .