为了让数值计算的结果能够有更好的渲染效果,这段时间一直在用Blender这个开源软件来处理计算结果。
因为是处理大量数据的计算结果,所以不得不考虑用Python编写脚本来实现批量处理,编写过程中,Google帮我解决了大部分实现过程中的障碍,下面是完整的实现过程:
-
将计算结果的mesh文件导入到Blender中;
import bpy import os # folder path to save rendered image in_dir = "E:\\SPH\\Paper 01\\D18\\" lst = os.listdir(in_dir) # folder path for importing data files in_dir_ply = "E:\\SPH\\Paper 01\\D18\\Ply_batch\\" lst_ply = os.listdir(in_dir_ply) # folder path to save rendered animation out_dir = "E:\\SPH\\Paper 01\\D18\\test.avi" # Filter file list by valid file types. candidates = [] candidates_name = [] c = 0 for item in lst_ply: fileName, fileExtension = os.path.splitext(lst_ply[c]) if fileExtension == ".ply": candidates.append(item) candidates_name.append(fileName) c = c + 1 file = [{"name":i} for i in candidates] n = len(file) print(n) # To import mesh.ply in batches for i in range (0,n): bpy.ops.import_mesh.ply(filepath=candidates[n], files=[file[n]], directory=in_dir_ply, filter_glob="*.ply") bpy.data.objects[candidates_name[i]].hide = True bpy.data.objects[candidates_name[i]].hide_render = True
-
在Blender中进行批量渲染并保存成图片文件;(这里用到了提前制作好的材质文件“Material.001”)
# Set file_format for render images bpy.data.scenes["Scene"].render.image_settings.file_format = 'PNG' # To render and save rendered images for i in range (0,n): bpy.data.objects[candidates_name[i]].hide = False #objects must be visible to use modifier bpy.data.objects[candidates_name[i]].hide_render = False #objects must be renderable to export render image bpy.context.scene.objects.active = bpy.data.objects[candidates_name[i]] #get object bpy.ops.object.modifier_add(type='PARTICLE_SYSTEM') #add modifier as particle_system bpy.data.objects[candidates_name[i]].particle_systems['ParticleSystem'].settings = bpy.data.particles['ParticleSettings'] #assign particle settings to object's particle system bpy.data.objects[candidates_name[i]].data.materials.append(bpy.data.materials["Material.001"]) #assign existed material to active object. bpy.data.scenes["Scene"].render.filepath = '//%02d'%(i) #set save filepath bpy.ops.render.render( write_still=True ) #render and save bpy.data.objects[candidates_name[i]].hide = True #hide again for next image rendering bpy.data.objects[candidates_name[i]].hide_render = True #hide again for next image rendering
-
在Blender的Video Sequence Editor (VSE)中插入上一步生成的渲染后的图片文件;
# Active VSE to generate rendering animation bpy.data.scenes["Scene"].render.use_sequencer = True # Filter file list by valid file types. re_image = [] re_image_name = [] c = 0 for item in lst: fileName, fileExtension = os.path.splitext(lst[c]) if fileExtension == ".png": re_image.append(item) re_image_name.append(fileName) c = c + 1 # Create the sequencer data bpy.context.scene.sequence_editor_create() # Add strip into VSE by importing new image for i in range (0,n): bpy.context.scene.sequence_editor.sequences.new_image( name=re_image[i], filepath=os.path.join(in_dir, re_image[i]), channel=1, frame_start=i)
-
在Blender中生成并保存动画文件。
# Resolution settings for animation resx = 720; #1920 resy = 480; #1080 bpy.data.scenes["Scene"].render.resolution_x = resx bpy.data.scenes["Scene"].render.resolution_y = resy bpy.data.scenes["Scene"].render.resolution_percentage = 100 bpy.data.scenes["Scene"].frame_end = n bpy.data.scenes["Scene"].render.image_settings.file_format = 'AVI_JPEG' bpy.data.scenes["Scene"].render.filepath = out_dir bpy.ops.render.render( animation=True )
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。