console.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # <pep8-80 compliant>
  19. import bpy
  20. from bpy.types import Operator
  21. from bpy.props import (
  22. BoolProperty,
  23. StringProperty,
  24. )
  25. def _lang_module_get(sc):
  26. return __import__("console_" + sc.language,
  27. # for python 3.3, maybe a bug???
  28. level=0)
  29. class ConsoleExec(Operator):
  30. """Execute the current console line as a python expression"""
  31. bl_idname = "console.execute"
  32. bl_label = "Console Execute"
  33. interactive: BoolProperty(
  34. options={'SKIP_SAVE'},
  35. )
  36. @classmethod
  37. def poll(cls, context):
  38. return (context.area and context.area.type == 'CONSOLE')
  39. def execute(self, context):
  40. sc = context.space_data
  41. module = _lang_module_get(sc)
  42. execute = getattr(module, "execute", None)
  43. if execute is not None:
  44. return execute(context, self.interactive)
  45. else:
  46. print("Error: bpy.ops.console.execute_%s - not found" %
  47. sc.language)
  48. return {'FINISHED'}
  49. class ConsoleAutocomplete(Operator):
  50. """Evaluate the namespace up until the cursor and give a list of """ \
  51. """options or complete the name if there is only one"""
  52. bl_idname = "console.autocomplete"
  53. bl_label = "Console Autocomplete"
  54. @classmethod
  55. def poll(cls, context):
  56. return (context.area and context.area.type == 'CONSOLE')
  57. def execute(self, context):
  58. sc = context.space_data
  59. module = _lang_module_get(sc)
  60. autocomplete = getattr(module, "autocomplete", None)
  61. if autocomplete:
  62. return autocomplete(context)
  63. else:
  64. print("Error: bpy.ops.console.autocomplete_%s - not found" %
  65. sc.language)
  66. return {'FINISHED'}
  67. class ConsoleCopyAsScript(Operator):
  68. """Copy the console contents for use in a script"""
  69. bl_idname = "console.copy_as_script"
  70. bl_label = "Copy to Clipboard (as script)"
  71. @classmethod
  72. def poll(cls, context):
  73. return (context.area and context.area.type == 'CONSOLE')
  74. def execute(self, context):
  75. sc = context.space_data
  76. module = _lang_module_get(sc)
  77. copy_as_script = getattr(module, "copy_as_script", None)
  78. if copy_as_script:
  79. return copy_as_script(context)
  80. else:
  81. print("Error: copy_as_script - not found for %r" %
  82. sc.language)
  83. return {'FINISHED'}
  84. class ConsoleBanner(Operator):
  85. """Print a message when the terminal initializes"""
  86. bl_idname = "console.banner"
  87. bl_label = "Console Banner"
  88. @classmethod
  89. def poll(cls, context):
  90. return (context.area and context.area.type == 'CONSOLE')
  91. def execute(self, context):
  92. sc = context.space_data
  93. # default to python
  94. if not sc.language:
  95. sc.language = "python"
  96. module = _lang_module_get(sc)
  97. banner = getattr(module, "banner", None)
  98. if banner:
  99. return banner(context)
  100. else:
  101. print("Error: bpy.ops.console.banner_%s - not found" %
  102. sc.language)
  103. return {'FINISHED'}
  104. class ConsoleLanguage(Operator):
  105. """Set the current language for this console"""
  106. bl_idname = "console.language"
  107. bl_label = "Console Language"
  108. language: StringProperty(
  109. name="Language",
  110. maxlen=32,
  111. )
  112. @classmethod
  113. def poll(cls, context):
  114. return (context.area and context.area.type == 'CONSOLE')
  115. def execute(self, context):
  116. sc = context.space_data
  117. # default to python
  118. sc.language = self.language
  119. bpy.ops.console.banner()
  120. # insert a new blank line
  121. bpy.ops.console.history_append(text="", current_character=0,
  122. remove_duplicates=True)
  123. return {'FINISHED'}
  124. classes = (
  125. ConsoleAutocomplete,
  126. ConsoleBanner,
  127. ConsoleCopyAsScript,
  128. ConsoleExec,
  129. ConsoleLanguage,
  130. )