-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanda_render.py
More file actions
63 lines (50 loc) · 2.79 KB
/
panda_render.py
File metadata and controls
63 lines (50 loc) · 2.79 KB
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
from panda3d.core import WindowProperties
from direct.task import Task
from direct.showbase.DirectObject import DirectObject
from direct.showbase.Messenger import Messenger
RENDER_COMPLETE_MSG = 'pandarender-complete'
def __resize_window(w, h):
"""Method that resizes the ShowBase window.
:param w: The desired width of the window.
:param h: The desired height of the window.
"""
props = WindowProperties()
props.setSize(w, h)
base.win.requestProperties(props)
def resize_and_screenshot(w, h, screenshot_name="PandaRender.png"):
"""Method that changes the active ShowBase window's width and height to w and h respectively,
takes a screenshot, and saves it to screenshot_name, before returning to the previous size.
:param w: The desired width of the screenshot.
:param h: The desired height of the screenshot.
:param screenshot_name: The name (and, optionally, path) of the screenshot.
"""
prev_w, prev_h = base.win.getXSize(), base.win.getYSize()
do = DirectObject()
do.acceptOnce("window-event", __screenshot, extraArgs=[w, h, prev_w, prev_h, screenshot_name])
__resize_window(w, h)
def scale_and_screenshot(w, h, screenshot_name):
"""Method that gets the current size of the window, scales the width and height by w and h respectively,
takes a screenshot, and saves it to screenshot_name, before returning to the previous size.
:param w: The factor of which the width will be multiplied.
:param h: The factor of which the height will be multiplied.
:param screenshot_name: The name (and, optionally, path) of the screenshot.
"""
x, y = base.win.getXSize(), base.win.getYSize()
resize_and_screenshot(x * w, y * h, screenshot_name)
def __screenshot(w, h, prev_w, prev_h, screenshot_name, *args):
"""Method that checks if the ShowBase window's bounds are [w, h] before rendering the frame and taking a screenshot.
If this check fails, nothing is done. After taking a screenshot, the window is scaled to [prev_w, prev_h].
Broadcasts event 'pandarender-complete' with a boolean describing if the screenshot happened or not.
:param w: The desired width of the screenshot.
:param h: The desired height of the screenshot.
:param prev_w: The width to revert to when the screenshot has been taken.
:param prev_h: The height to revert to when the screenshot has been taken.
:param screenshot_name: The name (and, optionally, path) of the screenshot."""
curr_w, curr_h = base.win.getProperties().getXSize(), base.win.getProperties().getYSize()
if curr_w == w and curr_h == h:
base.graphicsEngine.renderFrame()
base.screenshot(screenshot_name, False)
messenger.send(RENDER_COMPLETE_MSG, [True])
else:
messenger.send(RENDER_COMPLETE_MSG, [False])
__resize_window(prev_w, prev_h)