import bpy
import ddg.visualization.blender.object
[docs]def camera(name="Camera", type="PERSP", location=(0.0, 0.0, 0.0), collection=None):
"""
Add a camera.
Parameters
----------
name : str, default='Camera'
Name of the created camera
type : str, default='PERSP'
Type of the camera, 'PERSP', 'ORTHO' or 'PANO'
location : Iterable of three floats, default=(0., 0., 0.)
location of the camera
collection : bpy.types.collection, default=None
Collection to link camera to,
If the parameter is not given or set to None the camera
gets linked to bpy.context.scene.collection
Returns
-------
bpy.types.Camera
"""
cam = bpy.data.cameras.new(name)
cam.type = type
if collection is None:
collection = bpy.context.scene.collection
cam_ob = bpy.data.objects.new(name, cam)
collection.objects.link(cam_ob)
cam_ob.location = location
return cam_ob
[docs]def look_at_point(camera_bobj, point, world_up=(0, 0, 1), distance=None):
"""
Rotates camera such that it looks at the given point.
See ddg.visualization.blender.object.look_at_point for full documentation.
"""
bobj_front = (0, 0, -1)
bobj_up = (0, 1, 0)
ddg.visualization.blender.object.look_at_point(
camera_bobj,
point,
bobj_front=bobj_front,
bobj_up=bobj_up,
world_up=world_up,
distance=distance,
)
[docs]def clear(cameras=None, do_unlink=True):
"""Delete all given cameras.
Parameters
----------
cameras : Iterable of bpy.type.Camera (default=None)
Cameras to be deleted. If the argument is not provided or None all cameras
will be deleted.
do_unlink : bool (default=True)
Unlink cameras from their scenes, if needed, before deleting them.
Returns
-------
None
"""
if cameras is None:
cameras = bpy.data.cameras
cameras = list(cameras)
while cameras:
bpy.data.cameras.remove(cameras.pop(), do_unlink=do_unlink)
[docs]def set_background_image(camera_bobj, image_path):
"""Add a background image to the camera.
The background image can be used as reference
in the camera view but it won't appear in the renders.
Parameters
----------
camera_bobj : bpy.types.Object with a bpy.types.Camera as data
Camera object to which the image will be added.
image_path : str
Path to the image.
"""
image = bpy.data.images.load(image_path)
camera_data = camera_bobj.data
camera_data.show_background_images = True
camera_background = camera_data.background_images.new()
camera_background.image = image
[docs]def set_background_movieclip(camera_bobj, movieclip_path):
"""Add a background movieclip to the camera.
The background movieclip can be used as reference
in the camera view but it won't appear in the renders.
Parameters
----------
camera_bobj : bpy.types.Object with a bpy.types.Camera as data
Camera object to which the movieclip will be added.
image_path : str
Path to the movieclip.
"""
movieclip = bpy.data.movieclips.load(movieclip_path)
camera_data = camera_bobj.data
camera_data.show_background_images = True
camera_background = camera_data.background_images.new()
camera_background.source = "MOVIE_CLIP"
camera_background.clip = movieclip