Showing posts with label blender python. Show all posts
Showing posts with label blender python. Show all posts

10 August, 2020

Blender Python Tutorial: How to Create and Assign a Shader Material




In this video, we will be taking a look at not only creating a Shader Material but also assigning that material to the currently selected object.

We can create something simple or something complex but both ways will be using an Operator. We first create a panel where our button will live. Then we can create a simple operator, that when pressed will create and assign a Shader Material to the Selected Object.

If you want the user to have any options, we can easily upgrade the simple operator and make it become a popup dialog box. We can then giver the user control over any setting or option. In this example we want the user to be able to set the color before adding the Shader.

We have covered popup dialog boxes in other posts and videos so be sure to check them out!. Though in this example we will be taking a look at the FloatVectorProperty.

In code that would be:

The Float Vector Property usually looks like this:
If we tried to use this in it's current state, it would kick up an error because as we know to change a Color Value we actually need four values. We need the Red, Green, Blue and Alpha Values.


We can just define the Size and set it to 4 to add another value:

Which would look like this:


Now we have this, it doesn't look great. In fact it looks terrible, we want the user to have a good and quick experience.

So let's change this FloatVector slightly by defining the Sub type.

And that would turn the Float vector into something much more appealing:

If you want to set the default color (here you can see I set the default to green), you just need to define the default like so:

If you want to download the template script you can download the script here.

I hope you found this video helpful and as always thanks for reading!.

30 July, 2020

Blender Python Tutorial: Class Naming Convention





In this video, we will be looking at the correct Naming Convention for our classes. The Class name should be as follows: ADDONNAME_PT_main_panel


ADDONNAME - Using Uppercase, we type the Name of the Add-on. An underscore can be used if the name is more than one word.

_PT_ - it is then separated by two letters which denotes the Class Type (from which the type is inherited).

main_panel - using lowercase, we can type the name of the Operator/Panel/Header ect.

Other Class Types:
  • HT – Header
  • MT – Menu
  • OT – Operator
  • PT – Panel
  • UL – UI list

Here are some examples of Valid Class Names and their Identifiers.













Notice how the Identifier for the Operator is different?.

We do not need to add the OT but instead we must add a full stop. We also do not need to add the '_operator' at the end but I prefer to name it as such.

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()