2
$\begingroup$

I am trying to add a panel to the sidebar in the image editor. I used the template "UI Panel Simple" provided by Blender. The template works just fine if I leave it as is, but if I change "bl_space_type" to 'IMAGE_EDITOR' and change "bl_region_type" to 'UI' then the script doesn't work anymore. If I copy the same script into the Console, I get the Error: "IndentationError: unexpected indent" in a ton of places in the Console. I don't know what I'm doing wrong, all tutorials I've seen on Youtube do basically the same thing. I've tried deleting portions and it seems that the problem is "bpy.utils.register_class(HelloWorldPanel)" but I can't find out why. What is happening? I'm using Blender 2.93.4

import bpy 
class HelloWorldPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
    bl_label = "dsnldfbnsln"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'IMAGE_EDITOR'
    bl_region_type = 'UI'
    bl_context = "object"
    def draw(self, context):
        layout = self.layout

        obj = context.object

        row = layout.row()
        row.label(text="Hello world!", icon='WORLD_DATA')

        row = layout.row()
        row.label(text="Active object is: " + obj.name)
        row = layout.row()
        row.prop(obj, "name")

        row = layout.row()
        row.operator("mesh.primitive_cube_add")
def register():    
    bpy.utils.register_class(HelloWorldPanel)
def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)
if __name__ == "__main__":
    register()
$\endgroup$
1
  • $\begingroup$ The python console hates blank lines, so if you remove those, you should be able to paste your code to the console. You would still need to indent the doc string though. But that won't get your panel attached to the existing panels. $\endgroup$ Commented Nov 26, 2021 at 0:46

1 Answer 1

1
$\begingroup$

You need to make some changes to switch to the image editor. Here is a version of the class that will work:

class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "label"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'IMAGE_EDITOR'
    bl_region_type = 'UI'
    bl_category = "hello"

    def draw(self, context):
        layout = self.layout
        obj = context.object
        row = layout.row()
        row.label(text="Hello world!", icon='WORLD_DATA')
        row = layout.row()
        row = layout.row()

Note that this will not append the panel to any of the existing image editor panels, but will instead add another tab to the image editor side bar called 'hello'.

Here are the changes:

  1. add bl_category to give the panel its own tab. The name you use for the category will appear in the tab on the sidebar.
  2. Remove any references to objects, since there are no objects in image context.

If you want to make the panel a subpanel, instead of bl_category you need to set bl_parent_id to the id of the panel that you want to make it a subpanel of.

If, instead, you want to make the panel sit in the same tab of one of the existing panels, set bl_category to the category of that tab. Your tab will be appended below the existing tab. If, for instance, you would want to append it to the image tab, use

    bl_category = "Image"
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .