参考:Basics of Adding Background Images And Textures To Your 3-D Models
blender 版本:3.1.2
1、添加相机,并且相机配置上背图片:x
filepath = "G:/git_alg/dengta/digital_man/data/backgroud.jpg" img = bpy.data.images.load(filepath) # cam is a camera object. cam.show_background_images = True bg = cam.background_images.new() bg.image =img bg.alpha=1 bg.display_depth='BACK' bg.frame_method="STRETCH" bg.source="IMAGE" bpy.context.scene.render.film_transparent = True
2、添加背景图片并且可渲染:渲染时,动态将展示内容和背景合并,对应代码如下:
### Compositing
# scene = bpy.context.scene
# nodetree = scene.node_tree
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
for every_node in tree.nodes:
tree.nodes.remove(every_node)
#渲染层
RenderLayers_node = tree.nodes.new('CompositorNodeRLayers')
RenderLayers_node.location = -300, 300
#合成
comp_node = tree.nodes.new('CompositorNodeComposite')
comp_node.location = 400, 300
#alpha上叠
AplhaOver_node = tree.nodes.new(type="CompositorNodeAlphaOver")
AplhaOver_node.location = 150, 450
#scale_node
Scale_node = tree.nodes.new(type="CompositorNodeScale")
Scale_node.space = 'RENDER_SIZE'
Scale_node.location = -150, 500
#添加图像层
Image_node = tree.nodes.new(type="CompositorNodeImage")
Image_node.image = img
Image_node.location = -550, 500
links = tree.links
link1 = links.new(RenderLayers_node.outputs[0], AplhaOver_node.inputs[2])
link2 = links.new(AplhaOver_node.outputs[0], comp_node.inputs[0])
link3 = links.new(Scale_node.outputs[0], AplhaOver_node.inputs[1])
link4 = links.new(Image_node.outputs[0], Scale_node.inputs[0])
3、灯光布置:
light_data = bpy.data.lights.new(name="light001", type='SUN') light_data.energy = 30 # create new object with our light datablock light_object = bpy.data.objects.new(name="light001", object_data=light_data) # link light object bpy.context.collection.objects.link(light_object) # make it active bpy.context.view_layer.objects.active = light_object # change location light_object.location = (-2, -6, 5) light_object.rotation_euler=(49.1,-20,37) light_object.scale=(20, 20, 20)
亲测有效



