Spaces:
Sleeping
Sleeping
File size: 2,898 Bytes
8554568 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
.. _scene_guide:
Creating Scenes
===============
Before you render anything, you need to put all of your lights, cameras,
and meshes into a scene. The :class:`.Scene` object keeps track of the relative
poses of these primitives by inserting them into :class:`.Node` objects and
keeping them in a directed acyclic graph.
Adding Objects
--------------
To create a :class:`.Scene`, simply call the constructor. You can optionally
specify an ambient light color and a background color:
>>> scene = pyrender.Scene(ambient_light=[0.02, 0.02, 0.02],
... bg_color=[1.0, 1.0, 1.0])
You can add objects to a scene by first creating a :class:`.Node` object
and adding the object and its pose to the :class:`.Node`. Poses are specified
as 4x4 homogenous transformation matrices that are stored in the node's
:attr:`.Node.matrix` attribute. Note that the :class:`.Node`
constructor requires you to specify whether you're adding a mesh, light,
or camera.
>>> mesh = pyrender.Mesh.from_trimesh(tm)
>>> light = pyrender.PointLight(color=[1.0, 1.0, 1.0], intensity=2.0)
>>> cam = pyrender.PerspectiveCamera(yfov=np.pi / 3.0, aspectRatio=1.414)
>>> nm = pyrender.Node(mesh=mesh, matrix=np.eye(4))
>>> nl = pyrender.Node(light=light, matrix=np.eye(4))
>>> nc = pyrender.Node(camera=cam, matrix=np.eye(4))
>>> scene.add_node(nm)
>>> scene.add_node(nl)
>>> scene.add_node(nc)
You can also add objects directly to a scene with the :meth:`.Scene.add` function,
which takes care of creating a :class:`.Node` for you.
>>> scene.add(mesh, pose=np.eye(4))
>>> scene.add(light, pose=np.eye(4))
>>> scene.add(cam, pose=np.eye(4))
Nodes can be hierarchical, in which case the node's :attr:`.Node.matrix`
specifies that node's pose relative to its parent frame. You can add nodes to
a scene hierarchically by specifying a parent node in your calls to
:meth:`.Scene.add` or :meth:`.Scene.add_node`:
>>> scene.add_node(nl, parent_node=nc)
>>> scene.add(cam, parent_node=nm)
If you add multiple cameras to a scene, you can specify which one to render from
by setting the :attr:`.Scene.main_camera_node` attribute.
Updating Objects
----------------
You can update the poses of existing nodes with the :meth:`.Scene.set_pose`
function. Simply call it with a :class:`.Node` that is already in the scene
and the new pose of that node with respect to its parent as a 4x4 homogenous
transformation matrix:
>>> scene.set_pose(nl, pose=np.eye(4))
If you want to get the local pose of a node, you can just access its
:attr:`.Node.matrix` attribute. However, if you want to the get
the pose of a node *with respect to the world frame*, you can call the
:meth:`.Scene.get_pose` method.
>>> tf = scene.get_pose(nl)
Removing Objects
----------------
Finally, you can remove a :class:`.Node` and all of its children from the
scene with the :meth:`.Scene.remove_node` function:
>>> scene.remove_node(nl)
|