As a practice of Blender 2.8 script, we will create a stage that looks like a ** example game **. (Image video)
(This is a real High Trestle Trail Bridge (Iowa))
We are planning to do something like this.
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.
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)
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
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
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
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.
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
The black material has already been created, so you can reuse it by simply calling it with mat = bpy.data.materials ['black']
.
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
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,
It looks like this.
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