I'm trying to create a button in the Header of the TEXT_EDITOR but for some reason the button isn't actually appearing in the header when I run the code? I'm not sure why this is happening.
import bpy
def main(self, context):
text = bpy.data.texts.get(self.text)
if text is not None:
text = "exec(compile(" + repr(text) + ".as_string(), '" + text.name + "', 'exec'))"
bpy.ops.console.clear_line()
bpy.ops.console.insert(text=text)
bpy.ops.console.execute()
class TEXT_EDITOR_OT_run_file(bpy.types.Operator):
bl_idname = 'console.run_file'
bl_label = 'Run File in Console'
text = bpy.props.StringProperty()
@classmethod
def poll(cls, context):
return context.area.ui_type == 'CONSOLE'
def execute(self, context):
main(self, context)
return {'FINISHED'}
class TEheader(bpy.types.Header):
bl_idname = 'TEXT_EDITOR_HT_newheader'
bl_label = 'New Header'
bl_space_type = 'TEXT_EDITOR'
def draw(self, context):
print ('code ran')
self.layout.operator(TEXT_EDITOR_OT_run_file.bl_idname, icon='CONSOLE')
def register():
bpy.utils.register_class(TEheader)
def unregister():
bpy.utils.unregister_class(TEheader)
if name == "main":
register()
print ('registered')
I found another article here that seems to be exactly what I'm looking for, but when I attempted something similar with my code, it didn't work. It said that TEXT_EDITOR_HT_header is not a valid attribute of bpy.types so I assumed that blender has since been updated and you can no longer reference headers that way anymore.
I tried reformatting so that instead I was creating a new header and drawing the button, but nothing changes and the print('code ran') bit that I put in didn't run, and I can't figure out how to get it to run.


bpy.utils.register_class()registered the operator? What do you mean that I'm not registering the operator – Fez_Master Apr 04 '22 at 00:55bpy.utils.register_class(TEXT_EDITOR_OT_run_file)This line is missing in the code snippet – Reigen Apr 04 '22 at 02:29