① Create a new material ② Make the node available ③ Add an object ④ Set the value of the node ⑤ Apply the material to the object
◯ 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.
◯ Use this to specify as nodes [" Principled BSDF"]. Inputs [0]
.
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)
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)
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)
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)
◯ 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()
◯ 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)
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