In this article, I'll show you how to create your own functions to make it easier to do more versatile processing.
A function is like an output device. Receives the argument information and executes the process.
It looks like function name (argument 1, argument 2, ...)
.
The argument acts as a parameter.
In other words, if you mess with the arguments, the result of the process will change.
In that respect, it is similar to cooking. If you change the amount of salt (parameter), the taste (output) of the dish will naturally change.
There are two types of functions.
** Functions prepared by someone ** and ** Functions you create yourself **.
Functions such as primitive_cube_add ()
that add cubes that I have used frequently are ** functions provided by someone **.
You might think that it's a long function name, such as primitive_cube_add ()
, but I'm sure some uncles worked hard to talk to each other and created a function with such a long name. I will.
I am grateful to use the functions prepared by my uncles to create my own functions.
① ** Definition ** ② ** Use **
Proceed in the order of.
To be a little more specific,
def function():
processing
After ** defining ** the function like
function()
Just write.
Let's take a concrete look in the next chapter.
Let's create a function that makes one cube appear.
First, let's take a look at the function primitive_cube_add ()
that makes a cube appear, provided by someone somewhere.
import bpy
bpy.ops.mesh.primitive_cube_add()
Use this to create your own function.
In the example below, you define your own function called cube ().
import bpy
#Definition of original function
def cube():
#A function provided by someone
bpy.ops.mesh.primitive_cube_add()
In the code above, I'm ** defining ** my original function cube ()
using def
. The structure is as follows.
def The name of the function you create():
What you want to do when you call a function
② Use Let's call the function.
cube()
Only this. The whole picture is as follows.
import bpy
#Function definition
def cube():
#What you want to do when you call a function
bpy.ops.mesh.primitive_cube_add()
#Self-defined function cube()Call
cube()
I think it's easier to understand if you look at a concrete example, so I'll post sample code and a brief explanation from here.
import bpy
def cube():
bpy.ops.mesh.primitive_cube_add()
def wireframe():
bpy.ops.object.modifier_add(type='WIREFRAME')
cube()
wireframe()
import bpy
#A function that makes an object a wireframe
def wireframe():
bpy.ops.object.modifier_add(type='WIREFRAME')
#A function that makes a wireframe cube appear while changing its size
def wireframe_cube(_size = 2,num = 3):
for i in range(0,num):
bpy.ops.mesh.primitive_cube_add(
size = _size * i
)
#Make the appearing objects into wireframes one by one
wireframe()
#The first size is 1 and doubles it. The number of cubes is 10.
wireframe_cube(_size = 1,num = 10)
import bpy
#sphere()Define a function called
#Allow adjustment of smoothness and appearance position
def uv_sphere(_segments = 32,_location = (0,0,0)):
bpy.ops.mesh.primitive_uv_sphere_add(
segments=_segments,
location = _location
)
#Make 3 appear
uv_sphere()
uv_sphere(_segments = 16,_location = (3,0,0))
uv_sphere(_segments = 3,_location = (6,0,0))
import bpy
#Definition of a function that makes any number of cubes appear in a row from any place
#Substitute the default value for the argument
def cube_line_up(start_position = (0,0,0),num = 2,distance = 2):
for i in range(0,num * distance + 1,distance):
bpy.ops.mesh.primitive_cube_add(
#Arrange in the X-axis direction
location=(start_position[0] + i,start_position[1],start_position[2])
)
#Self-defined function cube()Call
cube_line_up()
cube_line_up(start_position = (0,-4,0),num = 10,distance = 5)
cube_line_up(start_position = (0,-8,0),num = 4,distance = 3)
import bpy
#Definition of a function that makes a cube appear
def cube():
bpy.ops.mesh.primitive_cube_add()
#Definition of function to set material
def material(name = 'material',color = (0, 0, 0, 1)):
material_glass = bpy.data.materials.new(name)
#Make the node available
material_glass.use_nodes = True
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 = color
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)
cube()
material(name = 'Red',color = (1, 0, 0, 1))
import bpy
#Definition of a function that makes a cube appear
def cube():
bpy.ops.mesh.primitive_cube_add()
#Definition of function to set material
def material(name = 'material',color = (0, 0, 0, 1)):
material_glass = bpy.data.materials.new(name)
#Make the node available
material_glass.use_nodes = True
p_BSDF = material_glass.node_tree.nodes["Principled BSDF"]
#0→BaseColor/4→Metallic(=metal)/7→Roughness(=Roughness)
#default_value = (R, G, B, A)
p_BSDF.inputs[0].default_value = color
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)
cube()
material(name = 'Blue',color = (0, 0, 1, 1))
English words | Translation |
---|---|
function | function |
context | context/environment |
append | to add |
default | Default |
add | to add |
distance | distance |
inputs | get |
principled | Principle, principle |
line up | Line up |
segments | part/Separation |
Recommended Posts