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!.

5 comments:

  1. That tutorial was fantastic! If it worked would be great. Pasted the code into a new blank project with a cube and getting the following error after I run it:
    Python: Traceback (most recent call last):
    File "\Text", line 37, in execute
    ValueError: bpy_struct: item.attr = val: sequence expected at dimension 1, not '_PropertyDeferred'

    Any ideas? Thanks!

    ReplyDelete
    Replies
    1. When defining a property in the newer versions of blender we no longer use = We now define them with :

      Delete
    2. Glad you knew right away:
      What exactly does this do, specifically what value does it give it when we say self.col? (I was never an expert at classes)

      Another reason I'm asking is look at these below...
      #Doesn't work: rgb_node.outputs[0].default_value = self.col
      #Does work: rgb_node.outputs[0].default_value : self.col
      the first option above errors.

      The first option below doesn't assign my new color...
      #Doesn't work: rgb_node.outputs[0].default_value : [0.33,1,1,1]
      #Does work: rgb_node.outputs[0].default_value = [0.33,1,1,1]

      Confused :-/
      Thanks again!

      Delete
    3. self.col is saying >> use the Property col (which you should have defined in the operator before the Execution) If you have not defined a property called col it will not work.

      Delete
    4. Oh! That's right col is a variable assigned above inside the class itself.
      I see it in the code now. I was testing the nice paste.bin code you supplied above. Very nice. Does it make sense the second part where I wouldn't use the : colon symbol when I'm assigning it a static array value [0.33, 1, 1, 1]

      Delete