12 January, 2020

An Introduction to Scripting (Blender Python - bpy)




In this video,we will be taking a look at Scripting with Blender and Python.

Before we begin, I should mention that I am in no way an expert and in fact, I have only been learning Python (and scripting in general) for a short time. That being said, I went from knowing absolutely nothing - to developing the Darkfall VFX Nodes Add-on, and would like to share with you some tips and maybe a basic introduction into Scripting. 




  We first start by arranging our layout. There are three important windows we need in order to start scripting and they can be placed anywhere in your layout. It's a good idea to save a template file (so you can use a fresh scripting start up file each time). 

Learning Python and Scripting may seem daunting at first but with a little time and patience you can create something useful. 


If you do get stuck and need some help you can always check out the Blender API, there's a stockpile of really useful information along with some nice examples. You can also check out The Blender Stack Exchange, I have been saved many times by helpful people over at the Stack Exchange and certainly recommend using it. 






Blender also comes with many Python Templates for you to check out. I find it a good idea to have a look at how other scripts are structured and many times they can be slightly adapted to fit our needs.

Now, I assume that anyone reading this blog post will have their own idea for an add-on and many things in this introduction may not apply to what you need but I think having a basic understanding of creating a simple script will go a long way when it comes to tackling your own idea. 



In this Introduction, we will be creating a very simple add-on that I dubbed the "Object Adder"... With just a few lines of code we have created our first add-on!. Congratulations!.. 







We created a Panel in the 3D View that has a bunch of buttons, that when pressed add objects. So yeah, it's obviously not that impressive but if (like me) you thought you wouldn't ever be able to code something, you have just made your first steps into the new world of Python.



There are some different ways we can layout the panel and making it look "pretty" can always come later. I like to make sure everything is working and then we can go back and make some adjustments. The first version of the Darkfall VFX Nodes add-on was a mess. Not only did it look bad but it also was not as simple to use as it is now. 

Thanks to those who have downloaded and used the Add-on, I took their feedback and started to develop newer versions. So with that being said, If you find yourself stuck or need help improving your add-on, be sure to take all the feedback you can get. 

Since we could not cover everything in a single Video, I will be creating more videos on scripting and hopefully you guys will find them helpful. If you have any suggestions on what you want to see, make sure you leave a comment in the video and let us know!.

If you guys want to check out the code that we wrote in the video, you can find it here:


import bpy

class TestPanel(bpy.types.Panel):
    bl_label = "Object Adder"
    bl_idname = "PT_TestPanel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'My 1st Addon'
   
    def draw(self, context):
        layout = self.layout
       
        row = layout.row()
        row.label(text= "Add an object", icon= 'OBJECT_ORIGIN')
        row = layout.row()
        row.operator("mesh.primitive_cube_add", icon= 'CUBE')
       
        row.operator("mesh.primitive_uv_sphere_add", icon= 'SPHERE')
        row = layout.row()
        row.operator("object.text_add", icon= 'FILE_FONT', text= "Font Button")
       
       
       
       
def register():
    bpy.utils.register_class(TestPanel)
   
def unregister():
    bpy.utils.unregister_class(TestPanel)
   
if __name__ == "__main__":
    register()



5 comments: