Auto switch transform orientation in edit / object mode

Hi,
I’d love to have an automatic switch between two transform orientations when entering and exiting edit mode, and so that such switch would include a custom orientation that I created manually for a given scene.
To be exact, I want the transform orientation to be switched to LOCAL when entering edit mode and then to a specific custom orientation <your_custom_orientation_name_hehe> when exiting back to object mode.
That itself would cover most of my needs, provided that I could turn this feature on and off easily - with a hotkey or a dedicated button.
There could even be a dedicated menu with two settings, like:

1) choose auto transform orientation for edit mode,
2) choose auto transform orientation for object mode.

That, of course, would be a further step after figuring out the code. I mean, it’s basically triggering one simple action to follow another, but if I only knew Python :smiley:
I guess the code would have to be run in the beginning of each Blender session (before being turned into an add-on, which I’m considering).

I just often catch myself forgetting to change transform orientation, and ruining my perfectly planar geometry (tough hard-surface requirements, you know).

If it’s not too complicated, I would appreciate any help with this riddle.

Btw. I’m not sure whether I chose the right post category. Maybe it should go to python support.

I’m just working with Transform Orientations at low level, but for a separate project (mechanical blender).

What you say is factible as a simple addon, just store the transform orientation on object mode, and the transform orientation on editmode (on value change). When switching between them change to the value stored.

On mblender project they are implemented the operator handlers, which make simple to implement this kind of things.

Also, the value of transform orientation on edit mode, could be stored linked to the object being editing. This way, when switching to edit mode you will have the transform orientation you previously set for this object.

I don’t necessarily need different orientations for specific objects, just a specific orientation for edit mode in general, with any object. Though, this is a nice idea, too.

Thing is…
…I have no idea how to do this since I can’t code. I may have sounded smarter than I actually am.
If someone helps with coming up with the code, I may be able to design UI for it and I will learn how to code it.

This may work, just execute with CTRL+P

import bpy

def mode_callback(object, data):
    #data should be always "mode"
    if bpy.context.object.mode == 'OBJECT':
        # bpy.context.scene.transform_orientation_slots[0].type = 'Cube'
        bpy.context.scene.transform_orientation_slots[0].type = 'GLOBAL'
        
    if bpy.context.object.mode == 'EDIT':
        bpy.context.scene.transform_orientation_slots[0].type = 'LOCAL'

def subscribe_to(owner, object,data_path,callback):
    bpy.msgbus.subscribe_rna(
        key=object.path_resolve(data_path, False),
        owner=owner,
        args=(object,data_path,),
        notify=callback,
    )

bpy.msgbus.clear_by_owner(bpy.context.object)
subscribe_to(bpy.context.object, bpy.context.object,"mode", mode_callback)
2 Likes

Wow, thank you very much, Mauge!
It works super smooth and will be very useful. I first had a problem with custom transform, but then realized that I need to respect capital letters, which doesn’t matter with default orientations. So when I created a custom transform called ‘Face’, it wouldn’t work if I swapped ‘OBJECT’ for ‘FACE’ (all caps). It has to be ‘Face’.

:wink:

If you store the value of transform orientation per mode, will work without any settings / changes.

Just one more question:
How do I deactivate this, other than by reverting / reopening my blend?
I sometimes may need to suspend this functionality.

Just execute this line. You can enhance the base code, setting the disable / enable option as operator, and assing a shorcut.

I get python error when executing this.
I added “import bpy” before this line, but now nothing happens. The switch is still active.

Um…

Maybe that because using bpy.context.object as owner, and when you execute the clear function you have changed the object ?

In fact the callback should be registered for each object.