3
$\begingroup$

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.

$\endgroup$

1 Answer 1

5
$\begingroup$

Result:

You can see the operator button added to the header enter image description here

Answer:

You were on the right path. The link you shared shows how to add extra items in headers. You don't need to create new header class for that. you can append draw function to the header.

it said that TEXT_EDITOR_HT_header is not a valid attribute of bpy.types

Yes if you type bpy.types.TEXT in console, autocomplete shows that headerin text editor can be accessed now via bpy.types.TEXT_HT_footer

enter image description here

You can achieve what you are trying with this code. Important code blocks are these 2:

def draw_item(self, context):
    layout = self.layout
    layout.operator('console.run_file', icon='CONSOLE')
bpy.types.TEXT_HT_header.append(draw_item)

Full Code:

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)
        
def draw_item(self, context):
    layout = self.layout
    layout.operator('console.run_file', icon='CONSOLE')


def register():
    bpy.utils.register_class(TEXT_EDITOR_OT_run_file)
    bpy.types.TEXT_HT_header.append(draw_item)

def unregister():
    bpy.types.TEXT_HT_header.remove(draw_item)
    bpy.utils.unregister_class(TEXT_EDITOR_OT_run_file)

if __name__ == "__main__":
    register()

Note:

In the Code you have posted, You are not registering the operator.

$\endgroup$
2
  • $\begingroup$ I thought bpy.utils.register_class() registered the operator? What do you mean that I'm not registering the operator $\endgroup$
    – Fez_Master
    Apr 4 at 0:55
  • 1
    $\begingroup$ bpy.utils.register_class(TEXT_EDITOR_OT_run_file) This line is missing in the code snippet $\endgroup$
    – Reigen
    Apr 4 at 2:29

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.