Raster Prop Monitor#

Raster Prop Monitor fills a pod’s IVA with working screens, and adds external cameras that the screens show. The scripts below draw a menu on a screen, and show a camera’s view as video in a window. They need a crewed pod with a Raster Prop Monitor IVA, such as the Mk1 Lander Can, and an external camera on the vessel.

A Screen Menu#

The following script turns a screen into a menu. The screen’s arrow buttons move a cursor, and its ENTER button toggles the lights, gear, brakes or SAS:

import queue

import krpc

conn = krpc.connect(name="Screen Menu")
vessel = conn.space_center.active_vessel
control = vessel.control
rpm = conn.raster_prop_monitor

# The first screen in the vessel's IVA, and the page the script draws on it
monitor = rpm.monitors(vessel)[0]
display = monitor.display

# The controls the menu toggles, and the item the cursor is on
items = ["lights", "gear", "brakes", "sas"]
cursor = 0


def draw():
    lines = ["[#00ff00]kRPC MENU", ""]
    for i, item in enumerate(items):
        mark = "[#ffff00]>" if i == cursor else " "
        state = "[#00ff00]ON" if getattr(control, item) else "[#ff4040]OFF"
        lines.append("%s %-8s %s[#ffffff]" % (mark, item.upper(), state))
    # Raster Prop Monitor formats this line, and keeps it up to date
    lines += ["", "ALT {0:SIP6.1}m $&$ ALTITUDE"]
    display.text = "\n".join(lines)


# Button presses arrive on the client's stream thread, so the callbacks
# queue them for the loop below
presses = queue.Queue()
buttons = monitor.buttons
up, down, enter = (
    buttons.index(name)
    for name in ("button_UP", "button_DOWN", "button_ENTER")
)
for button in (up, down, enter):
    event = display.button_pressed(button)
    event.add_callback(lambda button=button: presses.put(button))

# Show the menu, and make the NAV button show it too, in place of
# Raster Prop Monitor's navigation page
draw()
monitor.active_page = "krpc"
monitor.capture_button("buttonR1", "krpc")

try:
    while True:
        button = presses.get()
        if button == up:
            cursor = (cursor - 1) % len(items)
        elif button == down:
            cursor = (cursor + 1) % len(items)
        else:
            item = items[cursor]
            setattr(control, item, not getattr(control, item))
        draw()
finally:
    monitor.release_capture("buttonR1")
    monitor.active_page = "home"

Enter the IVA to see the menu, and click the buttons on the screen. The screen’s other page buttons go to Raster Prop Monitor’s own pages. The NAV button brings back the menu.

kRPC adds a page named krpc to every Raster Prop Monitor screen. The page shows the monitor’s Display, and the script draws the menu on it by setting Display.text. The text uses Raster Prop Monitor’s page format: [#00ff00] sets a color, and the altitude line names a variable that Raster Prop Monitor formats and updates by itself.

The script answers the screen’s buttons with the Display.button_pressed() events, which fire only while the page is shown. Monitor.capture_button() takes the NAV button from Raster Prop Monitor, so that a press shows the krpc page. On the screens of the Mk1 Lander Can, NAV is buttonR1, and Monitor.page_buttons lists the names on other screens.

Camera Video#

The following script shows the view of the vessel’s first external camera in a window. It uses Pillow to decode the frames, and tkinter to show them:

import io
import tkinter as tk

from PIL import Image
from PIL.ImageTk import PhotoImage

import krpc
from krpc.services.stdlib import ImageFormat

conn = krpc.connect(name="Camera Video")
vessel = conn.space_center.active_vessel
camera = conn.raster_prop_monitor.cameras(vessel)[0]

# The camera's view as JPEG frames of 640 by 480 pixels, 30 times a second
frames = conn.add_stream(camera.image, 640, 480, ImageFormat.jpeg)
frames.rate = 30

window = tk.Tk()
window.title(camera.name)
label = tk.Label(window)
label.pack()
# Tk keeps no reference to the image it shows
shown = []


def show():
    image = PhotoImage(Image.open(io.BytesIO(frames())))
    label.configure(image=image)
    shown[:] = [image]
    window.after(30, show)


show()
window.mainloop()

ExternalCamera.image() renders the camera’s view, here as a JPEG image. JPEG is lossy, and is smaller and faster to encode than PNG. A stream of it is a video, and the stream’s rate is the frame rate. The server renders a frame on each update the rate allows, up to one per physics tick, which is 50 a second. A 640 by 480 frame is about 25 KB, so a 30 frame a second video takes under 1 MB/s.

The window shows the newest frame each time it redraws. To capture every frame of a recording instead, call ExternalCamera.image() in a loop that holds each tick. The game then waits for the script on each tick.

See also

ExternalCamera for the camera’s field of view, and Monitor for reading a screen’s text and image.