[Blender × Python] Create your own function & summary so far

In this article, I'll show you how to create your own functions to make it easier to do more versatile processing.

table of contents

  1. What is a function?
  2. Function type
  3. Sample code to create a function
  4. English words

0. What is a function?

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.

1. Function type

There are two types of functions.

** Functions prepared by someone ** and ** Functions you create yourself **.

1-0. A function prepared by someone

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.

1-1. Functions you create yourself

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.

1-2. Try to create a function

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.

① Definition

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

スクリーンショット 2020-11-15 17.25.32.png

2. Sample code to create a function

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.

Create a function that turns an object into a wireframe

import bpy

def cube():
    bpy.ops.mesh.primitive_cube_add()

def wireframe():
    bpy.ops.object.modifier_add(type='WIREFRAME')
    
cube()
wireframe()

スクリーンショット 2020-11-15 15.11.10.png

Use the wireframe function among other functions

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)

スクリーンショット 2020-11-15 15.24.31.png

Create a function that makes uv_spehre appear

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

スクリーンショット 2020-11-15 15.03.36.png


Create a function that arranges cubes in a row

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)

スクリーンショット 2020-11-15 14.23.07.png

Create a function to set the material (glass)

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

スクリーンショット 2020-11-15 16.48.48.png

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

Create a function to set the material (metal)

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

u.png

3. English words

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

[Blender × Python] Create your own function & summary so far
Create your own Linux commands in Python
[LLDB] Create your own command in Python
Create your own exception
Python function argument summary
Create your own Big Data in Python for validation
Create a function in Python
Create your own Django middleware
[Python] Make your own LINE bot
[Python] logging in your own module
How to create your own Transform
Create your own graph structure class and its drawing in python
Try docker: Create your own container image for your Python web app
Create your own name resolution service
[Django] Create your own 403, 404, 500 error pages
[OpenCV; Python] Summary of findcontours function
Create your own virtual camera with Python + OpenCV and apply original effects
Create a Python function decorator with Class
Create wordcloud from your tweet with python3
Easily use your own functions in Python
Create your own DNS server with Twisted
Create a python environment on your Mac
[Python] Package and distribute your own modules
Create your own Composite Value with SQLAlchemy
[Python] Register your own library on PyPI
Until you install your own Python library
Publish your own Python library with Homebrew
Get your own IP address in Python
Python Summary
python function ①
[Python] function
Python summary
python function ②
Python has come to be recognized so far.
[Python] Implement your own list-like class using collections.UserList
Make your own module quickly with setuptools (python)
Import your own modules in Grasshopper's Python development
Create a wheel of your own OpenCV module
python: Use your own class for numpy ndarray
Create a Python (Django) learning environment with Docker so that you can debug with VS Code (almost your own procedure memo)