Page MenuHome

development_external_text_edit.py

development_external_text_edit.py

# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
bl_info = {
'name': "Edit text files using External Text Editor",
'author': 'Sinsoft Entertainment Inc.',
'version': (1, 0, 3),
'blender': (2, 6, 3),
'api': 46400,
'location': "Text Editor > Edit",
'description': "Edit text files using External Text Editor",
'warning': "",
'wiki_url': "http://www.sinsoft.com/p/contact-us.html",
'tracker_url': "http://www.sinsoft.com/p/contact-us.html",
'category': 'Development'}
import os
import subprocess
import os.path
import bpy
from bpy.props import *
class TEXT_HT_header_external_edit(bpy.types.Header):
bl_space_type = 'TEXT_EDITOR'
def draw(self, context):
layout = self.layout
st = context.space_data
text = st.text
if text:
row = layout.row()
row.operator("text.external_edit", text="Edit")
#row.operator("text.external_updatefrom", text="Update")
row.prop(text, "text_file_extension", text="")
class TEXT_OT_external_edit(bpy.types.Operator):
bl_idname = "text.external_edit"
bl_label = "Text External Edit"
bl_description = "Edit External"
def execute(self, context):
st = context.space_data
text = st.text
filecontent = text.as_string()
filename = text.name
if filename.endswith(text.text_file_extension):
pass
else:
filename = filename + text.text_file_extension
fullfilename = bpy.context.user_preferences.filepaths.temporary_directory + filename
with open(fullfilename, 'w') as f:
f.write(filecontent)
try:
subproc = subprocess.Popen(fullfilename, shell=True)
subproc.communicate()
returncode = subproc.returncode
#if returncode != 0:
# self.report('ERROR',"Error while returning from external file")
# return {'CANCELLED'}
except OSError as err:
self.report('ERROR', "Could not open file: " + fullfilename + " in external editor." + str(err))
return {'CANCELLED'}
bpy.ops.text.external_updatefrom()
return {'FINISHED'}
def invoke(self, context, event):
self.execute(context)
return {'FINISHED'}
class TEXT_OT_external_updatefrom(bpy.types.Operator):
bl_idname = "text.external_updatefrom"
bl_label = "Text External Update"
bl_description = "Update From External"
def execute(self, context):
st = context.space_data
text = st.text
filename = text.name
if filename.endswith(text.text_file_extension):
pass
else:
filename = filename + text.text_file_extension
fullfilename = bpy.context.user_preferences.filepaths.temporary_directory + filename
if not os.path.isfile(fullfilename):
return {'CANCELLED'}
content_back = ""
with open(fullfilename, 'r') as f:
content_back = f.read()
text.clear()
text.write(content_back)
if text.filepath:
with open(text.filepath, 'w') as f:
f.write(content_back)
else:
bpy.ops.text.make_internal()
if not os.path.isfile(fullfilename):
return {'CANCELLED'}
try:
os.remove(fullfilename)
except OSError as err:
self.report("ERROR", "Could not remove temp file: " + fullfilename + ", error: " + str(err))
return {'CANCELLED'}
return {'FINISHED'}
def invoke(self, context, event):
self.execute(context)
return {'FINISHED'}
def register():
bpy.types.Text.text_file_extension = bpy.props.EnumProperty(
name='External File Extension',
items= (
('.py', ' .py', 'py'),
('.xml', ' .xml', 'xml'),
('.glsl', ' .glsl', 'glsl'),
('.vert', ' .vert', 'vert'),
('.frag', ' .frag', 'frag'),
('.fp', ' .fp', 'fp'),
('.vp', ' .vp', 'vp'),
('.fx', ' .fx', 'fx'),
('.fxh', ' .fxh', 'fxh'),
('.txt', ' .txt', 'txt')
),
description = 'file extension to assign external file',
default = '.py')
bpy.utils.register_class(TEXT_OT_external_edit)
bpy.utils.register_class(TEXT_OT_external_updatefrom)
bpy.utils.register_class(TEXT_HT_header_external_edit)
def unregister():
bpy.utils.unregister_class(TEXT_OT_external_edit)
bpy.utils.unregister_class(TEXT_OT_external_updatefrom)
bpy.utils.unregister_class(TEXT_HT_header_external_edit)
if __name__ == "__main__":
register()

File Metadata

Mime Type
text/x-python
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
5e/93/6a043eb2c2663d80194e5178a7d1

Event Timeline