[PYTHON] Create a shape on the trajectory of an object

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 20123001.gif

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 image.png The UVs created may not always be manageable This needs improvement.

Recommended Posts

Create a shape on the trajectory of an object
Get the attributes of an object
The story of the escape probability of a random walk on an integer grid
Calculate the probability of outliers on a boxplot
Create a GUI on the terminal using curses
Create a QR code for the URL on Linux
Generate that shape of the bottom of a PET bottle
Python: Get a list of methods for an object
A Study on Visualization of the Scope of Prediction Models
Roughly estimate the total memory usage of an object
Make the initial directory of JupyterLab a Google Drive mounted on an external HDD
Create a classroom on Jupyterhub
Create a new csv with pandas based on the local csv
A note on the default behavior of collate_fn in PyTorch
An introduction to object orientation-let's change the internal state of an object
Create a correlation diagram from the conversation history of twitter
Create a bot that only returns the result of morphological analysis with MeCab on Discord
A memo that reproduces the slide show (gadget) of Windows 7 on Windows 10.
A method of converting the style of an image while preserving the color
[MariaDB] Install MariaDB on Linux and create a DB and an operating user.
I tried to create an environment of MkDocs on Amazon Linux
How to know the internal structure of an object in Python
Create a Python environment on Mac (2017/4)
On Linux, the time stamp of a file is a little past.
[Go] Create a CLI command to change the extension of the image
Create a SlackBot service on Pepper
Distinguishing the agari shape of mahjong
Create a Linux environment on Windows 10
Create a python environment on centos
Find the rank of a matrix in the XOR world (rank of a matrix on F2)
A command to easily check the speed of the network on the console
Create an app that notifies LINE of the weather every morning
The meaning of ".object" in Django
Dig the directory and create a list of directory paths + file names
Create a function to get the contents of the database in Go
The story of writing a program
Get the number of readers of a treatise on Mendeley in Python
Create a compatibility judgment program with the random module of python.
I made a POST script to create an issue on Github and register it in the Project
Approximation of distance between two points on the surface of a spheroid (on the surface of the earth)
Randomly play the movie on ChromeCast for a certain period of time
Various methods to numerically create the inverse function of a certain function Introduction
Semi-automatically generate a description of the package to be registered on PyPI
[Python] Create a linebot to write a name and age on an image
Test & Debug Tips: Create a file of the specified size in Python
I measured the run queue wait time of a process on Linux
How to create a wrapper that preserves the signature of the function to wrap
The process of repeatedly extracting an array differs slightly depending on the language ...
The story of an error in PyOCR
Investigate the effect of outliers on correlation
The story of making Python an exe
Measure the relevance strength of a crosstab
Create a JSON object mapper in Python
Create a graph using the Sympy module
[Python3] Rewrite the code object of the function
The story of making an immutable mold
Display the graph of tensorBoard on jupyter
[python] [meta] Is the type of python a type?
Create an application using the Spotify API
Create an OpenCV3 + python3 environment on OSX
Change the order of PostgreSQL on Heroku