1
$\begingroup$

I'm trying to delete a Custom Transform Orientation using script, for a Custom Transform Orientation that I created using script, creating it is a breeze, no problems at all, deleting it on the other hand is a mystery, trying to execute the operation:

bpy.ops.transform.delete_orientation()

I'm getting a context is incorrect error, I tried to get the right context using the following hack, but it's not working for this specific operator, it worked for create_orientation() but not for delete_orientation():

import bpy
types = {'VIEW_3D', 'TIMELINE', 'GRAPH_EDITOR', 'DOPESHEET_EDITOR','NLA_EDITOR', 'IMAGE_EDITOR', 'SEQUENCE_EDITOR', 'CLIP_EDITOR', 'TEXT_EDITOR', 'NODE_EDITOR', 'LOGIC_EDITOR', 'PROPERTIES', 'OUTLINER', 'USER_PREFERENCES', 'INFO', 'FILE_BROWSER', 'CONSOLE'}

area = bpy.context.area.type

for type in types:

    bpy.context.area.type = type

    if bpy.ops.transform.delete_orientation.poll():
        print(type)

bpy.context.area.type = area

Looking at the System Console after running the script above, is printing nothing, as if this operator is not available in any context in Blender! Even when I delete the Custom Transform Orientation from the Properties Shelf > Transform Orientations in 3D view, by clicking the X, nothing appears as executed in the Info Window!! Is it even possible to delete a Custom Transform Orientation using Python? I wonder why it's so easy to create one, and so hard to delete, if any one knows how to escape this incorrect context error, or how to delete a Custom Transform Orientation using a different method, would be appreciated.
Check the screen shots:
enter image description here
enter image description here

$\endgroup$
1
$\begingroup$

You need to override the context of the operator:

import bpy

name = 'Edge.001' #The orientation to remove

views = [area.spaces.active for area in bpy.context.screen.areas if area.type == 'VIEW_3D']
if views: #Assign the orientation in the view so that it is active and can be removed
    views[0].transform_orientation = name

areas = [area for area in bpy.context.window.screen.areas if area.type == 'VIEW_3D']

if areas: #Give the good override context
    override = {'area' : areas[0]}
    bpy.ops.transform.delete_orientation( override )

Edit: following the comment, some 'novice' explanations

Operator's context can be override. An override is a dictionary with some keywords. Among them you have the 'area' (because we have bpy.context.area). And if you look in the N panel where a custom orientation can be deleted, you see you are in the view3D area.

So... we just need to give a view 3D area to the override context.

Edit again:

What you did in your code is changing your current context (the view itself)... but that does not change the execution context itself (where delete_orientation is called), that is changing only the view.

More information here


Update:


In order not to get a ( Pycontext '' not found ) error in the system console, it's better to write the code this way:

...
if areas: #Give the good override context
    override = bpy.context.copy()
    override['area'] = areas[0]
    bpy.ops.transform.delete_orientation( override )


Reference: here

$\endgroup$
  • $\begingroup$ Works like a charm, I still don't get it though, but it works, I'll leave understanding research for another day, many thanks! $\endgroup$ – Georges Mar 19 '17 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.