做流体模拟的时候,想要复现别人的成果,但是别人的代码都是每帧输出 ply 格式的文件,渲染部分需要自己完成
看了一下,似乎用 blender 是最简单的,于是记录一下过程中用到的代码
Blender 版本 4.0
批量导入 ply
假设所有 ply 文件都和 blend 文件位于同一目录
ply 文件的文件名格式是 00001.ply, 00002.ply, 000123.ply 之类,编号表示帧数
希望导入所有 ply 文件,都放在一个 collection 里面,并且只连接到这个 collection
import bpy
import os
in_dir = bpy.path.abspath("//")
filters = [] # files to ignore
files_number = 0
def only_link_to_one_collection(obj, collection):
for other_col in obj.users_collection:
other_col.objects.unlink(obj)
if obj.name not in collection.objects:
collection.objects.link(obj)
def import_ply(path, filters):
need_file_items = []
need_file_names = []
filterDict = {
}
for item in filters:
filterDict[item] = True;
file_lst = os.listdir(path)
for item in file_lst:
fileName, fileExtension = os.path.splitext(item)
if fileExtension == ".ply" and (not item in filterDict):
need_file_items.append(item)
need_file_names.append(fileName)
fluid_mesh_collection = bpy.data.collections.new(name='FluidMesh')
bpy.context.scene.collection.children.link(fluid_mesh_collection)
files_number = len(need_file_items)
for i in range(files_number):
item = need_file_items