2

My goal is to make an addon that makes a new tab in the sidebar with sliders and buttons and stuff that do nothing (it’s my first addon, so I’m just experimenting).

I stole some code from the cursor array addon tutorial found on the Blender documentation just to get the first button made (which I could only figure out how to put in the VIEW3D_PT_view3d_properties part of the side panel). Here is my code thus far.

I’m pretty sure I could figure out how to make all the buttons if I could figure out how to register and unregister multiple classes (as you can see, class pan is unregistered).

I am a beginner with Python so try to answer in simplest terms.

Peter Mortensen
  • 681
  • 4
  • 11
desperrrr
  • 634
  • 4
  • 17

2 Answers2

4

Here is a very trivial very small very complete add-on. It draws one button in one panel that shows up as the "TLA" tab in the 3D Viewport when the Viewport is in Object Mode. It only has two classes, but it uses a common technique to register multiple classes by putting their names in an array and processing the array.

For each button you want, add an operator Class with a different name and a new col.operator.

# Very small add on to start from

bl_info = { "name" : "draw colors", "description" : "A skeleton addon", "author" : "Marty", "version" : (0, 0, 1), "blender" : (2, 80, 0), "location" : "View3D", "warning" : "", "support" : "COMMUNITY", "doc_url" : "", "category" : "3D View" }

import bpy from bpy.types import Operator from bpy.types import Panel

class TLA_OT_operator(Operator): """ tooltip goes here """ bl_idname = "demo.operator" bl_label = "I'm a Skeleton Operator" bl_options = {"REGISTER", "UNDO"}

@classmethod
def poll(cls, context):
    return context.mode == "OBJECT"

def execute(self, context):

    self.report({'INFO'},
        f"execute()")

    return {'FINISHED'}


class TLA_PT_sidebar(Panel): """Display test button""" bl_label = "TLA" bl_space_type = "VIEW_3D" bl_region_type = "UI" bl_category = "TLA"

def draw(self, context):
    col = self.layout.column(align=True)
    prop = col.operator(TLA_OT_operator.bl_idname, text="Say Something")


classes = [ TLA_OT_operator, TLA_PT_sidebar, ]

def register(): for c in classes: bpy.utils.register_class(c)

def unregister(): for c in classes: bpy.utils.unregister_class(c)

if name == 'main': register()

Marty Fouts
  • 33,070
  • 10
  • 35
  • 79
1

Here I have extended Marty's answer with bool and float properties:

import bpy

bl_info = { "name" : "whatever", "description" : "addon", "author" : "Marty", "version" : (0, 0, 1), "blender" : (2, 80, 0), "location" : "View3D", "warning" : "", "support" : "COMMUNITY", "doc_url" : "", "category" : "3D View" }

import bpy from bpy.types import Operator from bpy.types import Panel

class TLA_OT_operator(Operator): """ tooltip goes here """ bl_idname = "demo.operator" bl_label = "I'm a Skeleton Operator" bl_options = {"REGISTER", "UNDO"}

@classmethod
def poll(cls, context):
    return context.mode == "OBJECT"

def execute(self, context):

    self.report({'INFO'},
        f"execute()")

    return {'FINISHED'}


class TLA_PT_sidebar(Panel): """Display test button""" bl_label = "TLA" bl_space_type = "VIEW_3D" bl_region_type = "UI" bl_category = "TLA"

def draw(self, context):
    col = self.layout.column(align=True)
    prop = col.operator(TLA_OT_operator.bl_idname, text="Say Something")

    col.prop(context.scene, "my_tool")
    col.prop(context.scene, "my_float")


classes = [ TLA_OT_operator, TLA_PT_sidebar, ]

def register():

for c in classes:
    bpy.utils.register_class(c)
bpy.types.Scene.my_tool = bpy.props.BoolProperty(
    name='Bool',
    default=False
)
bpy.types.Scene.my_float = bpy.props.FloatProperty(
    name='Float',
    default=0.0 
)

def unregister(): del bpy.types.Scene.my_float del bpy.types.Scene.my_bool for c in classes: bpy.utils.unregister_class(c)

if name == 'main': register()

result:

The new TLA side panel open in the N Panel

Marty Fouts
  • 33,070
  • 10
  • 35
  • 79
Chris
  • 59,454
  • 6
  • 30
  • 84
  • You do realize you owe me all of the reputation you earn from this answer. ;) I'll settle for you doing me the favor of adding a comment to your answer reinforcing my point that this is a 'tip of the iceberg' addition and the OP should follow the "Scripting for Artists" Tutorial to find out the bits they're missing. Also, I edited your answer so that the code would be consistent with what I would have done and I removed the property group, which you didn't actually use, to make the code clearer. (Always better for additions to match original style.) Hope you don't mind. – Marty Fouts Jan 29 '22 at 15:59