File: modify_parser.py

package info (click to toggle)
python-moderngl-window 3.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 69,096 kB
  • sloc: python: 12,076; makefile: 21
file content (71 lines) | stat: -rw-r--r-- 2,054 bytes parent folder | download
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
"""
Example showing how we can modify the default cli argument parser.

Imagine we're making a small utility displaying a 3D object
from some file. We require a path to the file and and optional
argument if the model should be rendered as wireframe or not.
In addition we have an optional argument for changing the
window title.

Possible arguments should be:

python script.py path/to/file
python script.py path/to/file --wireframe
python script.py path/to/file --wireframe --title "Custom Window Title"
python script.py path/to/file --title "Custom Window Title"

"""

import math

import moderngl_window


class ModifyParser(moderngl_window.WindowConfig):
    title = "Modify Parser"

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        print("all arguments :", self.argv)
        # Print our custom argument
        print("path      :", self.argv.path)
        print("wireframe :", self.argv.wireframe)
        print("title     :", self.argv.title)

        if self.argv.title:
            self.wnd.title = self.argv.title

    @classmethod
    def add_arguments(cls, parser):
        # Mandatory positional argument for the file to load
        parser.add_argument(
            "path",
            help="Path to the model to display",
        )
        # Optional flag for rendering the model in wireframe
        # This is simply enabled by adding "--wireframe" with no arguments
        parser.add_argument(
            "--wireframe",
            action="store_true",
            default=False,
            help="Display the model as a wireframe",
        )
        # Optional argument for window title
        parser.add_argument(
            "--title",
            type=str,
            help="Override the window title",
        )

    def on_render(self, time, frame_time):
        # Placeholder content
        self.ctx.clear(
            (math.sin(time) + 1.0) / 2,
            (math.sin(time + 2) + 1.0) / 2,
            (math.sin(time + 3) + 1.0) / 2,
        )


if __name__ == "__main__":
    ModifyParser.run()