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 : sequence of 3 floats (default=(0.0, 0.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.Object
"""
if collection is None:
collection = bpy.context.scene.collection
cam = bpy.data.cameras.new(name)
cam.type = type_
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(bobj, point, world_up=(0.0, 0.0, 1.0), distance=None):
"""
Rotates camera such that it looks at the given point.
See ``ddg.visualization.blender.object.look_at_point`` for full documentation.
Parameters
----------
bobj: bpy.types.Object
Blender object to align.
point : sequence of 3 floats
Point of reference to turn object.
world_up : sequence of 3 floats (default=(0.0, 0.0, 1.0))
Where the up direction should end up.
distance : float (default=None)
Distance the object should have from the point.
See Also
--------
ddg.visualization.blender.object.look_at_point
"""
bobj_front = (0, 0, -1)
bobj_up = (0, 1, 0)
ddg.visualization.blender.object.look_at_point(
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.
"""
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