博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Blender下用Python实现模型文件导入、渲染和动画生成
阅读量:6093 次
发布时间:2019-06-20

本文共 3516 字,大约阅读时间需要 11 分钟。

为了让数值计算的结果能够有更好的渲染效果,这段时间一直在用Blender这个开源软件来处理计算结果。

因为是处理大量数据的计算结果,所以不得不考虑用Python编写脚本来实现批量处理,编写过程中,Google帮我解决了大部分实现过程中的障碍,下面是完整的实现过程:

  1. 将计算结果的mesh文件导入到Blender中;

    import bpy import os # folder path to save rendered imagein_dir = "E:\\SPH\\Paper 01\\D18\\"lst = os.listdir(in_dir)# folder path for importing data filesin_dir_ply = "E:\\SPH\\Paper 01\\D18\\Ply_batch\\"lst_ply = os.listdir(in_dir_ply)# folder path to save rendered animationout_dir = "E:\\SPH\\Paper 01\\D18\\test.avi"# Filter file list by valid file types.candidates = []candidates_name = []c = 0for item in lst_ply:    fileName, fileExtension = os.path.splitext(lst_ply[c])    if fileExtension == ".ply":        candidates.append(item)        candidates_name.append(fileName)    c = c + 1file = [{"name":i} for i in candidates]   n = len(file)print(n) # To import mesh.ply in batchesfor 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
  2. 在Blender中进行批量渲染并保存成图片文件;(这里用到了提前制作好的材质文件“Material.001”)

    # Set file_format for render imagesbpy.data.scenes["Scene"].render.image_settings.file_format = 'PNG'# To render and save rendered imagesfor 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
  3. 在Blender的Video Sequence Editor (VSE)中插入上一步生成的渲染后的图片文件;

    # Active VSE to generate rendering animationbpy.data.scenes["Scene"].render.use_sequencer = True# Filter file list by valid file types.re_image = []re_image_name = []c = 0for 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 databpy.context.scene.sequence_editor_create()# Add strip into VSE by importing new imagefor 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)
  4. 在Blender中生成并保存动画文件。

    # Resolution settings for animationresx = 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_dirbpy.ops.render.render( animation=True )

转载地址:http://dswza.baihongyu.com/

你可能感兴趣的文章
Go语言学习(五)----- 数组
查看>>
Android源码学习之观察者模式应用
查看>>
Content Provider的权限
查看>>
416. Partition Equal Subset Sum
查看>>
centos7.0 64位系统安装 nginx
查看>>
数据库运维平台~自动化上线审核需求
查看>>
注解开发
查看>>
如何用 Robotframework 来编写优秀的测试用例
查看>>
Django之FBV与CBV
查看>>
Vue之项目搭建
查看>>
app内部H5测试点总结
查看>>
Docker - 创建支持SSH服务的容器镜像
查看>>
[TC13761]Mutalisk
查看>>
三级菜单
查看>>
Data Wrangling文摘:Non-tidy-data
查看>>
加解密算法、消息摘要、消息认证技术、数字签名与公钥证书
查看>>
while()
查看>>
常用限制input的方法
查看>>
Ext Js简单事件处理和对象作用域
查看>>
IIS7下使用urlrewriter.dll配置
查看>>