It appears that the custom_orientation
field of the slot is only valid if the slot type
is set to the name of the custom orientation. Also, somewhere I saw a post that said that slot 0 is the slot for the 3D view's Transform Orientation. If so, searching for the correct slot shouldn't be necessary.
I really needed this feature, so this is how I did it. Assume m
contains the matrix for the new orientation.
# Create view for manual alignment along baseline.
bpy.ops.transform.create_orientation(name="BASELINE", overwrite=True)
### Set baseline
slot = C.scene.transform_orientation_slots[0]
# Save current orientation setting
last_slot = slot.type
# Set new orientation (custom_orientation isn't available until we set the
# type to a custom orientation)
slot.type = 'BASELINE'
slot.custom_orientation.matrix = m
# Set orientation back to what it was
slot.type = last_slot
I haven't tested it a lot yet, but it appears to work.