blender版本是3.0,下载链接:https://www.blender.org/download/
官方API文档: https://docs.blender.org/api/current/index.html
BlenderPython API中文版:BlenderPython API
官方API说明:
https://docs.blender.org/api/current/bpy.ops.import_mesh.html
# 导入teethup.stl模型
bpy.ops.import_mesh.stl(filepath="/home/zzy/blender-3.0.0-linux-x64/scripts/teethup.stl")
blender中直接插入图片是作为参考图,不能渲染,因此需要先加入一张平面,然后设置图片材质
参考博文:blender python为物体添加材质
# 新建一个平面
bpy.ops.mesh.primitive_cube_add()
# 添加颜色材质
bpy.data.materials.new('mat')
mat = bpy.data.materials['mat']
bpy.data.objects["Plane"].data.materials.append(bpy.data.materials['mat'])
mat.use_nodes = True
r = 1
g = 0
b = 0
a = 1
bpy.data.materials['mat'].node_tree.nodes["Principled BSDF"].inputs[0].default_value=(r, g, b, a)
# 添加图片材质
bpy.data.materials.new('mat')
mat = bpy.data.materials['mat']
bpy.data.objects["Plane"].data.materials.append(bpy.data.materials['mat'])
mat.use_nodes = True
matnodes = mat.node_tree.nodes
#新建图片节点
tex = matnodes.new('ShaderNodeTexImage')
tex.image = bpy.data.images.load(filepath = "/home/zzy/blender-3.0.0-linux-x64/scripts/material/img1.jpg")
disp=bpy.data.materials['mat'].node_tree.nodes["Principled BSDF"].inputs['Base Color']
mat.node_tree.links.new(disp, tex.outputs[0])
官方API说明:
https://docs.blender.org/api/current/bpy.ops.object.html
#为了选定某个物体操作,所以先不要选择所有物体
bpy.ops.object.select_all(action='DESELECT')
#通过select_pattern命令来按照物体命名查找选定物体,支持通配符*模糊匹配
bpy.ops.object.select_pattern(pattern="Cube", case_sensitive=False, extend=True)
参考博客:blender 2.8 python bpy 编写脚本操作物体
官方API说明:
https://docs.blender.org/api/current/bpy.ops.transform.html
# set location
bpy.data.objects["teethup"].location = (0,0,0)
# set scale
bpy.data.objects["teethup"].scale = (1,1,1)
# set rotation
bpy.data.objects["teethup"].rotation_euler = (-1.57, 0, 3.14)
# or
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects["teethup"].select_set(True)
bpy.ops.transform.rotate(value=0, orient_axis='X', orient_type='GLOBAL')
bpy.ops.transform.rotate(value=1.57, orient_axis='Y', orient_type='GLOBAL')
bpy.ops.transform.rotate(value=3.14, orient_axis='Z', orient_type='GLOBAL')
一步到位(使用旋转矩阵设置rotation):
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_pattern(pattern="teethup", case_sensitive=False, extend=True)
bpy.ops.transform.translate(value=(0, 0, 0), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)),
orient_matrix_type='GLOBAL', constraint_axis=(False, True, False), mirror=True,
use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1,
use_proportional_connected=False, use_proportional_projected=False, release_confirm=True)
选中摄像头,设置location
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects["Camera"].select_set(True)
bpy.data.objects["Camera"].location = (0,0,30)
官方API说明:
https://docs.blender.org/api/current/bpy.ops.render.html
bpy.data.scenes["Scene"].render.image_settings.file_format = 'PNG'
bpy.data.scenes["Scene"].render.filepath = "/home/zzy/blender-3.0.0-linux-x64/scripts/output/output1.png"
bpy.data.scenes["Scene"].render.film_transparent = True
bpy.ops.render.render( write_still=True )
blender.exe -b -P blendertest.py -o blenderout/ -f 0 (命令区分大小写)
上述命令的含义是:
(-b)后台运行blender并在结束后关闭
(-P blendertest.py)运行脚本blendertest.py,效果和带界面启动后运行脚本相同
(-o blenderout/)输出到blenderout文件夹
(-f 0)取第一帧,输出默认是png格式
实际只需要输入 ./blender -b -P stl_render.py 即可,渲染的具体实现在脚本中完成