blender学习笔记(一):python脚本的使用
blender版本是3.0,下载链接:https://www.blender.org/download/
常用API
官方API文档: https://docs.blender.org/api/current/index.html BlenderPython API中文版:BlenderPython API
STL模型导入
官方API说明: https://docs.blender.org/api/current/bpy.ops.import_mesh.html
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')
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
bpy.data.objects["teethup"].location = (0,0,0)
bpy.data.objects["teethup"].scale = (1,1,1)
bpy.data.objects["teethup"].rotation_euler = (-1.57, 0, 3.14)
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脚本开发
- 在Blender 2.8使用Python编程(入门级)
- Blender下用Python实现模型文件导入、渲染和动画生成
|