To fill in the blanks in Blender Advent Calendar 2020 Publish the script used at work
A script that creates an object with an animated trajectory of the object
Record the movement of the rendering range of the active object It is such a specification that a mesh is stretched with the selected object as a cross section.
make_trailobject.py
import bpy
import os
import math
from mathutils import Vector, Euler, Matrix, Quaternion
#Creating a mesh object from data
def add_mesh_from_data(name,vertices,faces):
mesh = bpy.data.meshes.new(name)
obj = bpy.data.objects.new(name, mesh)
bpy.context.scene.collection.objects.link(obj)
mesh.from_pydata(vertices, [], faces)
mesh.update()
return obj
#Get the movement of objects in the range of the rendered frame in a matrix
def get_matrix_all_flame(obj):
#Get frame range
frame_start = scene.frame_start
frame_end = scene.frame_end
matrix_list = []
for t in range(frame_start,frame_end):
#Current frame settings
bpy.context.scene.frame_set( t )
#Conversion matrix to global coordinates
# Matrix()If you don't do it, matrix_Enter the list with a reference to world
matrix_world = Matrix(active_obj.matrix_world)
matrix_list.append(matrix_world)
return (matrix_list)
#Sweep objects from orbital data
def create_trail(matrix_list, target, name):
vertices = target.data.vertices
point_num = len(vertices)
line_num = len(matrix_list)-1
edges = [list(e.vertices) for e in target.data.edges]
edges_num = len(edges)
new_verts = []
new_faces = []
UV_list = []
#Calculation of data to create a mesh
for i, matrix in enumerate(matrix_list):
#Add vertices
for j in range(point_num):
p2 = matrix @ vertices[j].co
new_verts.append(p2)
#Add face
for k,l in enumerate(edges):
if i != 0:
#Create face ID
p1 = l[0] +(i-1)*point_num
p2 = l[1] +(i-1)*point_num
p3 = l[0] +i*point_num
p4 = l[1] +i*point_num
new_faces.append([p1,p2,p4,p3])
#UV creation
u1 = k/edges_num
u2 = (k +1)/edges_num
##UV starts at the bottom left, so correct it to start at the top left
v1 = 1 -(i-1)/line_num
v2 = 1 -i/line_num
#Coordinate setting
UV_list.append(Vector([u1, v1]))
UV_list.append(Vector([u2, v1]))
UV_list.append(Vector([u2, v2]))
UV_list.append(Vector([u1, v2]))
#Creating an object from data
obj = add_mesh_from_data(name,new_verts,new_faces)
#UV settings
uv = obj.data.uv_layers.new(name="grid_uv")
for i,p in enumerate(uv.data):
p.uv = UV_list[i]
scene = bpy.context.scene
#Get the selected object
selected_objects = bpy.context.selected_objects
active_obj = bpy.context.active_object
#Get the trajectory of an object
mat_list = get_matrix_all_flame(active_obj)
#Sweep objects other than the active object
for target in selected_objects:
if target == active_obj: continue
create_trail(mat_list, target, "trail")
After selecting the plane to be the cross section, select the object you want to use the movement and execute.
In the script, UVs are arranged in a grid in the order of vertex IDs. Since the vertex IDs are often not in the order of the mesh The UVs created may not always be manageable This needs improvement.
Recommended Posts