Skip to content

Commit 09da93a

Browse files
committed
Effect class docs
1 parent 1253a3d commit 09da93a

File tree

6 files changed

+170
-54
lines changed

6 files changed

+170
-54
lines changed

demosys/effects/effect.py

Lines changed: 115 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@
1212

1313
class Effect:
1414
"""
15-
Effect base class.
15+
Effect base class that should be extended when making an effect
1616
17-
The following attributes are injected by demosys before initialization:
17+
Example::
1818
19-
* ``window`` (demosys.context.Context): Window
20-
* ``ctx`` (moderngl.Context): The moderngl context
21-
* ``sys_camera`` (demosys.scene.camera.Camera): The system camera responding to inputs
19+
from demosys.effects import Effect
20+
21+
class MyEffect(Effect):
22+
def __init__(self):
23+
# Initalization
24+
25+
def draw(self, time, frametime, target):
26+
# Draw stuff
2227
"""
23-
# By default effects are runnable with ``runeffect``
28+
#: The runnable status of the effect instance.
29+
#: A runnable effect should be able to run with the ``runeffect`` command
30+
#: or run in a project
2431
runnable = True
2532

2633
# Full python path to the effect (set per instance)
@@ -34,9 +41,35 @@ class Effect:
3441
_ctx = None # type: moderngl.Context
3542
_sys_camera = None # type: camera.SystemCamera
3643

44+
def __init__(self, *args, **kwargs):
45+
"""
46+
Implement the initialize when extending the class.
47+
This method is responsible for fetching resources
48+
and doing genereal initalization of the effect.
49+
50+
The effect initializer is called when all resources are loaded
51+
with the exception of resources loaded by other effects in
52+
the initializer.
53+
54+
The siganture of this method is entirely up to you.
55+
56+
You do not have to call the superclass initializer though ``super()``
57+
58+
Example::
59+
60+
def __init__(self):
61+
# Fetch reference to resource by their label
62+
self.program = self.get_program('simple_textured')
63+
self.texture = self.get_texture('bricks')
64+
# .. create a cube etc ..
65+
"""
66+
pass
67+
3768
def post_load(self):
3869
"""
39-
Called when all effects are initialized before effects start running.
70+
Called after all effects are initialized before drawing starts.
71+
Some initialization may be neccessary to do here such as
72+
interaction with other effects.
4073
"""
4174
pass
4275

@@ -52,11 +85,12 @@ def label(self) -> str:
5285

5386
@property
5487
def window(self) -> BaseWindow:
88+
"""The :py:class:`Window`"""
5589
return self._window
5690

5791
@property
5892
def ctx(self) -> moderngl.Context:
59-
"""ModernGL context"""
93+
"""The ModernGL context"""
6094
return self._ctx
6195

6296
@property
@@ -70,9 +104,10 @@ def draw(self, time, frametime, target):
70104
Draw function called by the system every frame when the effect is active.
71105
You are supposed to override this method.
72106
73-
:param time: The current time in seconds (float)
74-
:param frametime: The time the previous frame used to render in seconds (float)
75-
:param target: The target FBO for the effect
107+
Args:
108+
time (float): The current time in seconds.
109+
frametime (float): The time the previous frame used to render in seconds.
110+
target (``moderngl.Framebuffer``): The target FBO for the effect.
76111
"""
77112
raise NotImplementedError("draw() is not implemented")
78113

@@ -82,89 +117,127 @@ def get_program(self, label) -> moderngl.Program:
82117
"""
83118
Get a program by its label
84119
85-
:param label: The label for the program
86-
:return: moderngl.Program instance
120+
Args:
121+
label (str): The label for the program
122+
123+
Returns: py:class:`moderngl.Program` instance
87124
"""
88125
return self._project.get_program(label)
89126

90127
def get_texture(self, label) -> moderngl.Texture:
91128
"""
92129
Get a texture by its label
93130
94-
:param label: Label for the texture
95-
:return: The moderngl texture instance
131+
Args:
132+
label (str): The Label for the texture
133+
134+
Returns:
135+
The py:class:`moderngl.Texture` instance
96136
"""
97137
return self._project.get_texture(label)
98138

99139
def get_track(self, name) -> Track:
100140
"""
141+
This is only avaiable when using a Rocket timer.
142+
101143
Get or create a rocket track. This only makes sense when using rocket timers.
102144
If the resource is not loaded yet, an empty track object
103145
is returned that will be populated later.
104146
105-
:param name: The rocket track name
106-
:param local: Auto-prepend the effect package name to the path
107-
:return: Track object
147+
Args:
148+
name (str): The rocket track name
149+
150+
Returns:
151+
The :py:class:`rocket.Track` instance
108152
"""
109153
return resources.tracks.get(name)
110154

111155
def get_scene(self, label) -> Scene:
112156
"""
113157
Get a scene by its label
114158
115-
:param label: The label for the scene
116-
:return: Scene object
159+
Args:
160+
label (str): The label for the scene
161+
162+
Returns: The :py:class:`Scene` instance
117163
"""
118164
return self._project.get_scene(label)
119165

120166
def get_data(self, label) -> Any:
121167
"""
122-
Get a data file by its label
168+
Get a data instance by its label
123169
124-
:param label: Label for the data file
125-
:return: Contents of the data file
170+
Args:
171+
label (str): Label for the data instance
172+
173+
Returns:
174+
Contents of the data file
126175
"""
127176
return self._project.get_data(label)
128177

129178
def get_effect(self, label) -> 'Effect':
130179
"""
131-
Get an effect instance by label
180+
Get an effect instance by label.
181+
This is only possible when you have your own Project
182+
183+
Args:
184+
label (str): Label for the data file
132185
133-
:param label: Label for the data file
134-
:return: The requested effect instance
186+
Returns: The :py:class:`Effect` instance
135187
"""
136188
return self._project.get_effect(label)
137189

138190
def get_effect_class(self, effect_name, package_name=None) -> Type['Effect']:
139191
"""
140192
Get an effect class.
141193
142-
:param effect_name: Name of the effect class
143-
:param package_name: (optional) The package this effect belongs to
194+
Args:
195+
effect_name (str): Name of the effect class
196+
197+
Keyword Args:
198+
package_name (str): The package the effect belongs to
199+
200+
Returns:
201+
:py:class:`Effect` class
144202
"""
145203
return self._project.get_effect_class(effect_name, package_name=package_name)
146204

147205
# Utility methods for matrices
148206

149-
def create_projection(self, fov=75.0, near=1.0, far=100.0, ratio=None):
207+
def create_projection(self, fov=75.0, near=1.0, far=100.0, aspect_ratio=None):
150208
"""
151209
Create a projection matrix with the following parameters.
210+
When ``aspect_ratio`` is not provided the configured aspect
211+
ratio for the window will be used.
212+
213+
Args:
214+
:param float fov: Field of view (float)
215+
:param float near: Camera near value
216+
:param float far: Camrea far value
152217
153-
:param fov: Field of view (float)
154-
:param near: Camera near value
155-
:param far: Camrea far value
156-
:param ratio: Aspect ratio of the window
157-
:return: The projection matrix
218+
:param float ratio: Aspect ratio of the window
219+
220+
Returns: The projection matrix as a float32 :py:class:`numpy.array`
158221
"""
159222
return matrix44.create_perspective_projection_matrix(
160223
fov,
161-
ratio or self.window.aspect_ratio,
224+
aspect_ratio or self.window.aspect_ratio,
162225
near,
163226
far,
227+
dtype='f4',
164228
)
165229

166230
def create_transformation(self, rotation=None, translation=None):
167-
"""Convenient transformation method doing rotations and translation"""
231+
"""
232+
Creates a transformation matrix woth rotations and translation.
233+
234+
Args:
235+
rotation: 3 component vector as a list, tuple, or :py:class:`pyrr.Vector3`
236+
translation: 3 component vector as a list, tuple, or :py:class:`pyrr.Vector3`
237+
238+
Returns:
239+
A 4x4 matrix as a :py:class:`numpy.array`
240+
"""
168241
mat = None
169242
if rotation is not None:
170243
mat = Matrix44.from_eulers(Vector3(rotation))
@@ -180,11 +253,13 @@ def create_transformation(self, rotation=None, translation=None):
180253

181254
def create_normal_matrix(self, modelview):
182255
"""
183-
Convert to mat3 and return inverse transpose.
184-
These are normally needed when dealing with normals in shaders.
256+
Creates a normal matrix from modelview matrix
257+
258+
Args:
259+
modelview: The modelview matrix
185260
186-
:param modelview: The modelview matrix
187-
:return: Normal matrix
261+
:return:
262+
A 3x3 Normal matrix as a :py:class:`numpy.array`
188263
"""
189264
normal_m = Matrix33.from_matrix44(modelview)
190265
normal_m = normal_m.inverse

docs/conf.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@
3333
demosys.setup()
3434

3535

36-
class Mock(MagicMock):
37-
@classmethod
38-
def __getattr__(cls, name):
39-
return MagicMock()
40-
41-
MOCK_MODULES = ['glfw',]
42-
sys.modules.update((mod_name, Mock()) for mod_name in MOCK_MODULES)
43-
4436
# Monkey patch sphinx to ignore: WARNING: nonlocal image URI
4537
def _warn_node(self, msg, node, **kwargs):
4638
if not msg.startswith('nonlocal image URI found:'):
@@ -57,7 +49,9 @@ def _warn_node(self, msg, node, **kwargs):
5749
# Add any Sphinx extension module names here, as strings. They can be
5850
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
5951
# ones.
60-
extensions = ['sphinx.ext.autodoc']
52+
# NOTE: sphinxcontrib.napoleon is for google style docstrings
53+
extensions = ['sphinx.ext.autodoc', 'sphinxcontrib.napoleon']
54+
autoclass_content = "class"
6155

6256
# Add any paths that contain templates here, relative to this directory.
6357
templates_path = ['ntemplates']
@@ -95,7 +89,7 @@ def _warn_node(self, msg, node, **kwargs):
9589
# List of patterns, relative to source directory, that match files and
9690
# directories to ignore when looking for source files.
9791
# This patterns also effect to html_static_path and html_extra_path
98-
exclude_patterns = []
92+
exclude_patterns = ['Thumbs.db', '.DS_Store']
9993

10094
# The name of the Pygments (syntax highlighting) style to use.
10195
pygments_style = 'sphinx'

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ A Python 3 cross platform OpenGL 3.3+ core framework based on `ModernGL <https:/
1313
:caption: Contents:
1414

1515
quickstart
16-
reference/index.rst
1716
settings.rst
1817
controls.rst
1918
guides/index.rst
19+
reference/index.rst
2020

2121

2222
Indices and tables

docs/reference/effects.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
.. py:module:: demosys.effects
3+
.. py:currentmodule:: demosys.effects
4+
5+
Effect
6+
======
7+
8+
.. autoclass:: Effect
9+
10+
Initialization
11+
--------------
12+
13+
.. automethod:: Effect.__init__
14+
.. automethod:: Effect.post_load
15+
16+
Draw Methods
17+
------------
18+
19+
.. automethod:: Effect.draw
20+
21+
Resource Methods
22+
----------------
23+
24+
.. automethod:: Effect.get_texture
25+
.. automethod:: Effect.get_program
26+
.. automethod:: Effect.get_scene
27+
.. automethod:: Effect.get_data
28+
.. automethod:: Effect.get_effect
29+
.. automethod:: Effect.get_effect_class
30+
.. automethod:: Effect.get_track
31+
32+
Utility Methods
33+
---------------
34+
35+
.. automethod:: Effect.create_projection
36+
.. automethod:: Effect.create_transformation
37+
.. automethod:: Effect.create_normal_matrix
38+
39+
Attributes
40+
----------
41+
42+
.. autoattribute:: Effect.runnable
43+
.. autoattribute:: Effect.ctx
44+
.. autoattribute:: Effect.window
45+
.. autoattribute:: Effect.sys_camera
46+
.. autoattribute:: Effect.name
47+
.. autoattribute:: Effect.label

docs/reference/index.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ Reference
44
.. toctree::
55
:maxdepth: 1
66

7-
effect.rst
8-
vao.rst
9-
geometry.rst
7+
effects.rst

docs/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Sphinx
2+
sphinx-rtd-theme

0 commit comments

Comments
 (0)