17 January, 2020

Scripting Continued - Blender Python - bpy



In this video, we will be continuing scripting our "Object Adder" Add-on.

We will add some more functions such as the Smooth Shading option, Add a Modifier and more.

We also look at creating two more panels (for simplicity we call them Panel A and Panel B). After creating the new Panels we can add them to the Main Panel as "Sub Panels". 





It's really easy to add a panel to another panel and it only requires one property. By adding the, bl_parent_id = 'PT_MainPanel' 
we are telling 'Panel A' to be a child of the Main Panel.



By adding the, bl_options = {'DEFAULT_CLOSED'} 
we are telling the Panel to be minimized by default. If you have a whole bunch of panels open by default it can be a pain for the user to navigate to the section they intend to use.




 Once we are happy with the Add-on we need to make sure we add the bl-info at the beginning of the script (you can see the full script below). 

Without this Information, we can not install the Add-on. 







With our add-on now installed, we can use it without needing to run the script nor mess around with any settings.


  

Of course this Add-on is super simple and we can always add, change and improve this add-on but I wanted to create something easy to follow and I hope I have managed to inspire you to take this further and start to make awesome add-ons!..

If you want to use this script you can copy and paste the text below or you can click here.

In the Next Tutorial, we will be creating an Add-on that's a little more advanced but with the knowledge we have learned over the last two videos, it should be easier to pick up.

Again, I hope you found this Tutorial helpful! be sure to let us know what you think of this series or if you want more videos like this? 



Script result : 

__________________________________________________________________________
 


bl_info = {
    "name": "Object Adder",
    "author": "Darkfall",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "View3D > Toolbar > Object Adder",
    "description": "Adds objects and other functions to help our workflow (Tutorial Result)",
    "warning": "",
    "wiki_url": "",
    "category": "Add Mesh",
}

import bpy

    #This is the Main Panel (Parent of Panel A and B)
class MainPanel(bpy.types.Panel):
    bl_label = "Object Adder"
    bl_idname = "PT_MainPanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Object Adder'
   
    def draw(self, context):
        layout = self.layout
        layout.scale_y = 1.2
       
        row = layout.row()
        row.label(text= "Add an object", icon= 'OBJECT_ORIGIN')
        row = layout.row()
        row.operator("mesh.primitive_cube_add", icon= 'CUBE', text= "Cube")
        row.operator("mesh.primitive_uv_sphere_add", icon= 'SPHERE', text= "Sphere")
        row.operator("mesh.primitive_monkey_add", icon= 'MESH_MONKEY', text= "Suzanne")
        row = layout.row()
        row.operator("curve.primitive_bezier_curve_add", icon= 'CURVE_BEZCURVE', text= "Bezier Curve")
        row.operator("curve.primitive_bezier_circle_add", icon= 'CURVE_BEZCIRCLE', text= "Bezier Circle")
       
       
        row = layout.row()
        row.operator("object.text_add", icon= 'FILE_FONT', text= "Add Font")
        row = layout.row()
       


    #This is Panel A - The Scale Sub Panel (Child of MainPanel)
class PanelA(bpy.types.Panel):
    bl_label = "Scale"
    bl_idname = "PT_PanelA"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Object Adder'
    bl_parent_id = 'PT_MainPanel'
    bl_options = {'DEFAULT_CLOSED'}
   
    def draw(self, context):
        layout = self.layout
        obj = context.object
       
        row = layout.row()
        row.label(text= "Select an option to scale your", icon= 'FONT_DATA')
        row = layout.row()
        row.label(text= "      object.")
        row = layout.row()
        row.operator("transform.resize")
        row = layout.row()
        layout.scale_y = 1.2
       
        col = layout.column()
        col.prop(obj, "scale")
       


    #This is Panel B - The Specials Sub Panel (Child of MainPanel)
class PanelB(bpy.types.Panel):
    bl_label = "Specials"
    bl_idname = "PT_PanelB"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Object Adder'
    bl_parent_id = 'PT_MainPanel'
    bl_options = {'DEFAULT_CLOSED'}
   
    def draw(self, context):
        layout = self.layout
       
        row = layout.row()
        row.label(text= "Select a Special Option", icon= 'COLOR_BLUE')
        row = layout.row()
        row.operator("object.shade_smooth", icon= 'MOD_SMOOTH', text= "Set Smooth Shading")
        row.operator("object.subdivision_set", icon= 'MOD_SUBSURF', text= "Add Subsurf")
        row = layout.row()
        row.operator("object.modifier_add", icon= 'MODIFIER')
       
  
       
    #Here we are Registering the Classes       
def register():
    bpy.utils.register_class(MainPanel)
    bpy.utils.register_class(PanelA)
    bpy.utils.register_class(PanelB)

    #Here we are UnRegistering the Classes   
def unregister():
    bpy.utils.unregister_class(MainPanel)
    bpy.utils.unregister_class(PanelA)
    bpy.utils.unregister_class(PanelB)

    #This is required in order for the script to run in the text editor   
if __name__ == "__main__":
    register() 
                         






1 comment:

  1. Hey John! I really appreciate your effort to teach add-ons in this simplified way thanks a lot. kindly teach how to add lights controls to a curve ( Just like Char light Rig in Maya add attributes to any mesh or curve ) it really helps us. thanks bro

    ReplyDelete