Skip to content

Commit

Permalink
Added ability to index Scenes to reference child primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
klarh committed Feb 21, 2020
1 parent ef5ad7b commit 22916e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions plato/draw/Scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class Scene:
for prim in scene:
# (do something with prim)
Primitives can also be accessed in the order they were added to
the scene using list-like syntax::
first_prim = scene[:3]
last_prim = scene[-1]
Optional rendering arguments are enabled as *features*, which are
name-value pairs identifying a feature by name and any
configuration of the feature in the value.
Expand Down Expand Up @@ -83,6 +89,10 @@ def __init__(self, primitives=[], features={}, size=(40, 30),
for name in kwargs:
setattr(self, name, kwargs[name])

def __getitem__(self, key):
"""Returns the primitive(s) given an integer index or slice."""
return self._primitives[key]

def __iter__(self):
for prim in self._primitives:
yield prim
Expand Down
13 changes: 13 additions & 0 deletions test/test_Scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,18 @@ def test_features(self):
scene.get_feature_config('test3'),
{'value': 'auto_value', 'another_value': None})

def test_indexing(self):
prim0 = draw.Spheres()
prim1 = draw.ConvexPolyhedra()

scene = draw.Scene([prim0, prim1])

self.assertIs(scene[0], prim0)
self.assertIs(scene[-1], prim1)
self.assertEqual(scene[:3], [prim0, prim1])

with self.assertRaises(IndexError):
scene[5]

if __name__ == '__main__':
unittest.main()

0 comments on commit 22916e7

Please sign in to comment.