A Text Datablock named "Text" exists. Within a 3D View operator, I'm attempting to generate the dialog for writing this Datablock to a file, where the user can select a folder and choose a filename. In my 3D View operator, I have:

def execute(self, context):
    area_type = bpy.context.area.type
    bpy.context.area.type = 'TEXT_EDITOR'
    context.space_data.text = bpy.data.texts["Text"]
    bpy.context.area.type = area_type

However, when executing the line bpy.ops.text.save(), the following error occurs:

File "/usr/share/blender/scripts/modules/bpy/ops.py", line 188, in \_\_call\_\_

ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.text.save.poll() failed, context is incorrect

I expect I must better specify the context for bpy.ops.text.save. Any hints, advice, or a solution would be very welcomed. Thank you.


About 'INVOKE_DEFAULT' and context override

While you can often override a context not all operators support it.

Examples of context overrides, in particular this one: context override in text editor, see also a more extensive explanation of context overriding. Avoid doing this whenever possible, there is in this case an alternative.

A more sane alternative.

I recommend that you spawn your own file save dialog - a template for that code exists. See Text Editor > Templates > Python > Operator File Export

To save a Text Datablock to disk is easy.

destination = '/home/zeffii/Desktop/file.txt'

text_reference = bpy.data.texts['Text']
with open(destination, 'w') as outfile:
  • $\begingroup$ Text Editor > Templates > Python > Operator File Export => Perfect Answer! Thank you. $\endgroup$ Apr 16 '15 at 8: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.