[Blender x Python] Let's master the material !!

table of contents

  1. How to add a material
  2. Let's make a glass material !!
  3. Let's make metallic !!
  4. Add text !!
  5. English words

0. How to add a material

0-1. Procedure for adding materials

① Create a new material ② Make the node available ③ Add an object ④ Set the value of the node ⑤ Apply the material to the object

0-2. Contents of Principled BSDF

◯ Each item of the node is assigned a number in order from 0. This time, I will explain using Principled BSDF.

Base Color→0 Subsurface→1 : Metallic→4 : Roughness→7 : Transmission→15

It looks like this.

スクリーンショット 2020-11-13 19.19.02.png

◯ Use this to specify as nodes [" Principled BSDF"]. Inputs [0].

1. Let's make a glass material !!

◯ A program to make a simple glass ico_sphere

import bpy

#Create a new material
material_glass = bpy.data.materials.new('Green')
#Make the node available
material_glass.use_nodes = True
#Add an object
bpy.ops.mesh.primitive_ico_sphere_add(subdivisions=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))

#Set the value of the node
#Variable p_Create BSDF to reduce the amount of writing
p_BSDF = material_glass.node_tree.nodes["Principled BSDF"]
#0→BaseColor/7→roughness(=Roughness)/15→transmission(=propagation)
#default_value = (R, G, B, A)
p_BSDF.inputs[0].default_value = (0, 1, 0, 1)
p_BSDF.inputs[7].default_value = 0
p_BSDF.inputs[15].default_value = 1
#Add a material element to an object
bpy.context.object.data.materials.append(material_glass)

cube02.png


◯ A program for arranging glass cubes in three dimensions

import bpy

for i in range(0,5):
    for j in range(0,5):
        for k in range(0,5):
            #Create a new material
            material_glass = bpy.data.materials.new('Red')
            #Make the node available
            material_glass.use_nodes = True
            bpy.ops.mesh.primitive_cube_add(
                size=0.8,
                #Determine the length, width, and height of each
                location=(i, j, k),
                )
            p_BSDF = material_glass.node_tree.nodes["Principled BSDF"]
            p_BSDF.inputs[0].default_value = (1, 0, 0, 1)
            p_BSDF.inputs[7].default_value = 0
            p_BSDF.inputs[15].default_value = 1
            bpy.context.object.data.materials.append(material_glass)

cube01.png


2. Let's make metallic !!

◯ A program that arranges metallic ico_spehre

import bpy

#Repeat processing 6 times
for i in range(0,6):
    #Create a new material
    material_glass = bpy.data.materials.new('Blue')
    #Make the node available
    material_glass.use_nodes = True
    #ico_Function to add sphere
    bpy.ops.mesh.primitive_ico_sphere_add(
        #Make it smoother and smoother
        subdivisions=i+1,
        location=(i, 0, 0),
        scale=(0.7, 0.7, 0.7)
        )
    p_BSDF = material_glass.node_tree.nodes["Principled BSDF"]
    #0→BaseColor/4→Metallic(=metal)/7→roughness(=Roughness)
    p_BSDF.inputs[0].default_value = (0, 0, 1, 0.5)
    p_BSDF.inputs[4].default_value = 1
    p_BSDF.inputs[7].default_value = 0
    #Add a material element to an object
    bpy.context.object.data.materials.append(material_glass)

cube03.png


◯ A program that changes colors little by little

You can change the color little by little by substituting i, which changes each time the process is repeated, into the color.

import bpy
import math
import random

for i in range(0,10):
    #Assign a random value from 0 to 1 to the variable num
    num = random.random()
    
    material_glass = bpy.data.materials.new('Blue')
    material_glass.use_nodes = True
    bpy.ops.mesh.primitive_torus_add(
        #180(Every time)×0~Random value of 1
        rotation=(math.pi * num, math.pi * num, 0),
        major_segments=64,
        minor_segments=3,
        major_radius=10-i,
        minor_radius=0.1,
         )
    p_BSDF = material_glass.node_tree.nodes["Principled BSDF"]
    p_BSDF.inputs[4].default_value = 1
    p_BSDF.inputs[7].default_value = 0
    p_BSDF.inputs[0].default_value = (0, 0, 1 - i/10, 1)
    bpy.context.object.data.materials.append(material_glass)

e.png


3. Add text !!

3-1. How to add text

◯ Enter simple text.

import bpy

#Add text so you can edit it in edit mode
bpy.ops.object.text_add(enter_editmode = True,location = (0,0,0))
#Delete the initial text
bpy.ops.font.delete(type = 'PREVIOUS_WORD')
#Enter text
bpy.ops.font.text_insert(text = 'Blender')
#Exit edit mode
bpy.ops.object.editmode_toggle()

スクリーンショット 2020-11-13 20.14.23.png


3-1. Create metallic text

◯ Combine metallic material with text.

import bpy

material_glass = bpy.data.materials.new('Blue')
#Make the node available
material_glass.use_nodes = True

#Add text so you can edit it in edit mode
bpy.ops.object.text_add(enter_editmode = True,location = (0,0,0))
#Delete the initial text
bpy.ops.font.delete(type = 'PREVIOUS_WORD')
#Enter text
bpy.ops.font.text_insert(text = 'Blender')
#Exit edit mode
bpy.ops.object.editmode_toggle()

#Play with nodes
p_BSDF = material_glass.node_tree.nodes["Principled BSDF"]
p_BSDF.inputs[4].default_value = 1
p_BSDF.inputs[7].default_value = 0
p_BSDF.inputs[0].default_value = (0, 0, 1, 1)
bpy.context.object.data.materials.append(material_glass)

e1.png


4. English words

English words Translation
material Material
default Default/default
value value
append Add
object Things, objects
edit Edit
type type
delete Erase
toggle switching
operation(ops) Operation
add add to
segment Division, division
insert insert
input Get, get
previous Previous, previous
principled Principle, principle
subdivision Subdivision

Recommended Posts

[Blender x Python] Let's master the material !!
[Blender x Python] Let's master random !!
[Blender x Python] Let's master rotation !!
[Blender x Python] Let's get started with Blender Python !!
[Blender x Python] Blender Python tips (11/100)
[Blender x Python] Let's arrange a lot of Susanne neatly !!
[Python] Let's master all and any
[Blender x Python] Particle Animation (Part 1)
Master the type with Python [Python 3.9 compatible]
Master the weakref module in Python
Python Master RTA for the time being
Let's read the RINEX file with Python ①
Let's summarize the Python coding standard PEP8 (1)
Let's observe the Python Logging cookbook SocketHandler
Let's summarize the Python coding standard PEP8 (2)
[Blender x Python] How to use modifiers
Introduction to Python Let's prepare the development environment
Let's parse the git commit log in Python!
[Python] Let's execute the module regularly using schedule
Blender 2.9 Python Extrude extrude
Get the X Window System window title in Python
Excel X Python The fastest way to work
[Algorithm x Python] How to use the list
[Blender x Python] How to make an animation
[Blender x Python] How to make vertex animation
[Blender x Python] Think of code with symbols
Download the file while viewing the progress in Python 3.x
Sort in Python. Next, let's think about the algorithm.
How to know the current directory in Python in Blender
[Blender x Python] How to create an original object
[Blender x Python] 3D Bouncing Ball and other animations
Let's break down the basics of TensorFlow Python code
I checked out the versions of Blender and Python
(◎◎) {Let's let Python do the boring things) ......... (Hey? Let's let Python do the homework} (゜) (゜)
Let's use the Python version of the Confluence API module.
The first step to getting Blender available from Python
Let's use the open data of "Mamebus" in Python
[Python] Let's remember the new writing style after pip10
[Python] Let's change the URL of the Django administrator site
Find the maximum Python
Master the inclusion notation
Python string manipulation master
the zen of Python
blender, python, spiral staircase
Run Blender with python
blender, python, sphere behavior
Blender 2.8, Python Cube Scaling
Operate Blender with Python
[Python] Split the date
Let's review the language specifications around Python iterators and generators
Master the type in Python? (When should type check be done)
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
I just wrote the original material for the python sample code