[PYTHON] Create an example game-like stage with just the Blender 2.80 script

As a practice of Blender 2.8 script, we will create a stage that looks like a ** example game **. image.png (Image video)

image.png (This is a real High Trestle Trail Bridge (Iowa))

Completed video

We are planning to do something like this.

image.png

Source code

import bpy
import numpy as np

#Reset the status quo
for mat in bpy.data.materials:
    bpy.data.materials.remove(mat)
for cam in bpy.data.cameras:
    bpy.data.cameras.remove(cam)
for obj in bpy.data.objects:
    bpy.data.objects.remove(obj)
for obj in bpy.context.scene.objects:
    bpy.data.objects.remove(obj)

Import modules and reset the status quo, if nothing else.

scaffold

I will make a scaffold for the time being. First of all, the cube.

#Make a scaffold
bpy.ops.mesh.primitive_cube_add(location=(0,0,-50))
bpy.context.object.scale = (2,2,50)

image.png

Create a material and apply it.

#Set black material
bpy.data.materials.new(name = 'black')
mat = bpy.data.materials['black']
mat.use_nodes = True
node_tree = mat.node_tree
mat_node = node_tree.nodes['Principled BSDF']
mat_node.inputs['Base Color'].default_value = (0,0,0,1)
mat_node.inputs['Metallic'].default_value = 1
mat_node.inputs['Specular'].default_value = 1

#Apply black material to scaffolding
bpy.ops.object.material_slot_add()
bpy.context.object.active_material=mat

image.png

Scaffolding (frame)

Make a frame. Use the WIREFRAME modifier.

#Make a frame
bpy.ops.mesh.primitive_cube_add(location=(0,0,-50))
bpy.data.objects['Cube.001'].scale=(2,2,50)
bpy.ops.object.transform_apply(scale=True)
bpy.ops.object.modifier_add(type='WIREFRAME')
bpy.context.object.modifiers['Wireframe'].thickness = 0.05

image.png

Create a material that glows blue and apply it to the frame.

#Set luminescent material (blue)
bpy.data.materials.new(name = 'blue')
mat = bpy.data.materials['blue']
mat.use_nodes = True
node_tree = mat.node_tree
mat_node = node_tree.nodes.new('ShaderNodeEmission')
mat_node.inputs['Color'].default_value = (0,0,1,1)
mat_node.inputs['Strength'].default_value = 500
matout = node_tree.nodes['Material Output']
node_tree.links.new(mat_node.outputs[0], matout.inputs[0])

#Apply luminescent material (blue) to the frame
mat = bpy.data.materials['blue']
bpy.ops.object.material_slot_add()
bpy.context.object.active_material=mat

image.png

If ʻuse_nodes is set to True, Principled BSDF is applied by default, but if you use other shaders, create a new node with node_tree.nodes.new` and link it with the output node. I have to let you. The processing around that is

mat_node = node_tree.nodes.new('ShaderNodeEmission')

When

node_tree.links.new(mat_node.outputs[0], matout.inputs[0])

Will be.

Twisted frame

I will make a twisted frame that extends far away. Generate while twisting the plane, and use WIREFRAME again.

#Make a twisted frame
for i in range(50):
    bpy.ops.mesh.primitive_plane_add(size=15,location=(0,i*5+10,0),rotation=(np.pi/2,i/5,0))
    bpy.ops.object.modifier_add(type='WIREFRAME')
    bpy.context.object.modifiers['Wireframe'].thickness = 0.8
    mat = bpy.data.materials['black']
    bpy.ops.object.material_slot_add()
    bpy.context.object.active_material=mat

image.png

The black material has already been created, so you can reuse it by simply calling it with mat = bpy.data.materials ['black'].

Make rails

Let's lay the red and blue rails. First create a red version of the luminous material.

#Set luminescent material (red)
bpy.data.materials.new(name = 'red')
mat = bpy.data.materials['red']
mat.use_nodes = True
node_tree = mat.node_tree
mat_node = node_tree.nodes.new('ShaderNodeEmission')
mat_node.inputs['Color'].default_value = (1,0,0,1)
mat_node.inputs['Strength'].default_value = 100
matout = node_tree.nodes['Material Output']
node_tree.links.new(mat_node.outputs[0], matout.inputs[0])

And rail creation again.

#Make rails
poslist = [
    [ 4,0,-2],
    [ 4,0, 2],
    [-4,0,-2],
    [-4,0, 2]
    ]

for pos in poslist:
    bpy.ops.mesh.primitive_cube_add(size=1,location=pos)
    bpy.context.object.scale = [0.2,800,0.2]
    if bpy.context.object.location[2] > 1:
        mat = bpy.data.materials['red']
        bpy.ops.object.material_slot_add()
        bpy.context.object.active_material=mat
    else:
        mat = bpy.data.materials['blue']
        bpy.ops.object.material_slot_add()
        bpy.context.object.active_material=mat

image.png

camera

Finally, turn on the camera.

#Set camera
bpy.ops.object.camera_add(location=(2,-10,2))
bpy.data.objects['Camera'].rotation_euler = (np.pi*1/2, 0, 0)
bpy.data.cameras[0].lens = 20

After that, if you render with a black background,

image.png

It looks like this.

All source code

Combine the previous ones into one source.

import bpy
import numpy as np

#Reset the status quo
for mat in bpy.data.materials:
    bpy.data.materials.remove(mat)
for cam in bpy.data.cameras:
    bpy.data.cameras.remove(cam)
for obj in bpy.data.objects:
    bpy.data.objects.remove(obj)
for obj in bpy.context.scene.objects:
    bpy.data.objects.remove(obj)

#Make a scaffold
bpy.ops.mesh.primitive_cube_add(location=(0,0,-50))
bpy.context.object.scale = (2,2,50)

#Set black material
bpy.data.materials.new(name = 'black')
mat = bpy.data.materials['black']
mat.use_nodes = True
node_tree = mat.node_tree
mat_node = node_tree.nodes['Principled BSDF']
mat_node.inputs['Base Color'].default_value = (0,0,0,1)
mat_node.inputs['Metallic'].default_value = 1
mat_node.inputs['Specular'].default_value = 1

#Apply black material to scaffolding
bpy.ops.object.material_slot_add()
bpy.context.object.active_material=mat

#Make a frame
bpy.ops.mesh.primitive_cube_add(location=(0,0,-50))
bpy.data.objects['Cube.001'].scale=(2,2,50)
bpy.ops.object.transform_apply(scale=True)
bpy.ops.object.modifier_add(type='WIREFRAME')
bpy.context.object.modifiers['Wireframe'].thickness = 0.050

#Set luminescent material (blue)
bpy.data.materials.new(name = 'blue')
mat = bpy.data.materials['blue']
mat.use_nodes = True
node_tree = mat.node_tree
mat_node = node_tree.nodes.new('ShaderNodeEmission')
mat_node.inputs['Color'].default_value = (0,0,1,1)
mat_node.inputs['Strength'].default_value = 500
matout = node_tree.nodes['Material Output']
node_tree.links.new(mat_node.outputs[0], matout.inputs[0])

#Apply luminescent material (blue) to the frame
mat = bpy.data.materials['blue']
bpy.ops.object.material_slot_add()
bpy.context.object.active_material=mat

#Make a twisted frame
for i in range(50):
    bpy.ops.mesh.primitive_plane_add(size=15,location=(0,i*5+10,0),rotation=(np.pi/2,i/5,0))
    bpy.ops.object.modifier_add(type='WIREFRAME')
    bpy.context.object.modifiers['Wireframe'].thickness = 0.8
    mat = bpy.data.materials['black']
    bpy.ops.object.material_slot_add()
    bpy.context.object.active_material=mat

#Set luminescent material (red)
bpy.data.materials.new(name = 'red')
mat = bpy.data.materials['red']
mat.use_nodes = True
node_tree = mat.node_tree
mat_node = node_tree.nodes.new('ShaderNodeEmission')
mat_node.inputs['Color'].default_value = (1,0,0,1)
mat_node.inputs['Strength'].default_value = 100
matout = node_tree.nodes['Material Output']
node_tree.links.new(mat_node.outputs[0], matout.inputs[0])

#Make rails
poslist = [
    [ 4,0,-2],
    [ 4,0, 2],
    [-4,0,-2],
    [-4,0, 2]
    ]

for pos in poslist:
    bpy.ops.mesh.primitive_cube_add(size=1,location=pos)
    bpy.context.object.scale = [0.2,800,0.2]
    if bpy.context.object.location[2] > 1:
        mat = bpy.data.materials['red']
        bpy.ops.object.material_slot_add()
        bpy.context.object.active_material=mat
    else:
        mat = bpy.data.materials['blue']
        bpy.ops.object.material_slot_add()
        bpy.context.object.active_material=mat

#Set camera
bpy.ops.object.camera_add(location=(2,-10,2))
bpy.data.objects['Camera'].rotation_euler = (np.pi*1/2, 0, 0)
bpy.data.cameras[0].lens = 20

Recommended Posts

Create an example game-like stage with just the Blender 2.80 script
Create a star system with Blender 2.80 script
Just add the driver to the shape key with blender
Create an environment with virtualenv
[Python] Get the script execution directory with an absolute path
Create an application that just searches using the Google Custom Search API with Python 3.3.1 in Bottle
Create an alias for Route53 to CloudFront with the AWS API
[Python] Explains how to use the format function with an example
Create an Excel file with Python3
Debug the script with Sakura Editor
Create an age group with pandas
Get the return value of an external shell script (ls) with python3
Create an application by classifying with Pygame
[Blender] How to set shape_key with script
Create an image processing viewer with PySimpleGUI
Just print the selected object in Blender
Quickly create an excel file with Python #python
[Python] Round up with just the operator
Script example to display BoundingBox with PIL
[Blender] Use OpenGL from inside the script
Create an update screen with Django Updateview
Create an application using the Spotify API
[Python] Quickly create an API with Flask
Create an add-in-enabled Excel instance with xlwings
Create an English word app with python
Create a devilish picture with Blender scripts
Create an upgradeable msi file with cx_Freeze
Let's explain the asset allocation by the Black-Litterman model (with an execution example by Python)
Create an app that works well with people's reports using the COTOHA API