1
$\begingroup$

So I'm not sure if this will be possible at all, but someone might have an idea for a workaround for this problem.

So if I have made a toolbar, e.g. (short version)

class add_item(Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

But I would like to have this toolbar placed in multiple toolbars so it'd become something like this (code below doesn't actually work, just an example of what I'd like):

class add_item(Panel):
    bl_space_type = ['VIEW_3D', 'IMAGE_EDITOR', 'NODE_EDITOR']
    bl_region_type = 'TOOLS'

Would something similar to this be possible to do without duplicating all the code for the toolbar multiple times, just to then change the bl_space_type?

$\endgroup$
2
$\begingroup$

You can do this using a loop to generate dynamically named panels, each with the desired bl_space_type (based on this nice answer):

import bpy

def panelFactory( spaceType ):
    class NewClass( bpy.types.Panel ):
        bl_space_type = spaceType
        bl_region_type = 'TOOLS'
        bl_label       = "add_item_%s" % spaceType

        def draw( self, context ):
            layout = self.layout
            layout.operator( "mesh.primitive_cube_add" )

    NewClass.__name__ = "add_item_%s" % spaceType
    return NewClass

spaceTypes = ['VIEW_3D', 'IMAGE_EDITOR', 'NODE_EDITOR']

for spaceType in spaceTypes:
    panelFactory( spaceType )

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()

This code will add the "Add Mesh Cube" operator in a new panel to the "Misc" tab in each of the desired windows (space types).

$\endgroup$
1
$\begingroup$

You can use inheritance to share code between panels while customising parts that are unique to each editor.

class MyBasePanel(bpy.types.Panel):
    """A Panel to show in many places"""
    bl_label = "My Tools Panel"
    bl_region_type = 'TOOLS'

    def mydraw(self, context):
        # do nothing here so each class can add it's own thing'
        pass

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

class My3DViewPanel(MyBasePanel):
    bl_space_type = 'VIEW_3D'

class MyNodesPanel(MyBasePanel):
    bl_label = "My Nodes Panel"
    bl_space_type = 'NODE_EDITOR'

class MyImagePanel(MyBasePanel):
    bl_space_type = 'IMAGE_EDITOR'

    def mydraw(self, context):
        row = self.layout.row()
        row.label('For the Image editor')
$\endgroup$

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.