Forum Archive

Screen recording in scene

DarthXander

It is possible to record a scene, or a Node and its descendants? I know you can access a Texture object of a Node, but this object appears to be uneditable, and un-transferable to either pixel data or to write to a file.

zipit

Hi,

recording a scene is quite a relative term, but if performance is not an issue you can use Node.render_to_texture(). The method returns a Texture representing the node and all its (grand)children.

Cheers
zipit

ccc

Record as a still image or as a video?

https://forum.omz-software.com/search/draw_snapshot?in=titlesposts

DarthXander

Ah thank you, this could work! As long as it's fast enough to work every frame... I could of course just slow it down.

omz

The draw_snapshot method is fine for static screenshots, but not nearly fast enough to record video.

You can use objc_util to bridge the native ReplayKit framework. Here's a demo of that. It includes a very simple scene with some rotating space ships, just to have something to record... In this example, the recording is started in the scene's setup method, and stopped in its stop method (which is called automatically when the scene is about to be closed).

from objc_util import *
from scene import *
import random

load_framework('ReplayKit')
RPScreenRecorder = ObjCClass('RPScreenRecorder')

def previewControllerDidFinish_(_self, _cmd, _vc):
    ObjCInstance(_vc).dismissViewControllerAnimated_completion_(True, None)

PreviewDelegate = create_objc_class('PreviewDelegate', methods=[previewControllerDidFinish_])

def stop_callback(_cmd, _vc):
    vc = ObjCInstance(_vc)
    delegate = PreviewDelegate.new().autorelease()
    vc.setPreviewControllerDelegate_(delegate)
    rootvc = UIApplication.sharedApplication().keyWindow().rootViewController()
    vc.popoverPresentationController().setSourceView_(rootvc.view())
    rootvc.presentViewController_animated_completion_(vc, True, None)

stop_handler = ObjCBlock(stop_callback, restype=None, argtypes=[c_void_p, c_void_p])
recorder = RPScreenRecorder.sharedRecorder()

@on_main_thread
def start_recording():
    recorder.startRecordingWithMicrophoneEnabled_handler_(False, None)

@on_main_thread
def stop_recording():
    recorder.stopRecordingWithHandler_(stop_handler)


class MyScene (Scene):
    def setup(self):
        start_recording()
        for i in range(25):
            s = SpriteNode('spc:PlayerShip2Orange', position=(random.uniform(0, self.size.w), random.uniform(0, self.size.h)), parent=self)
            s.run_action(Action.repeat(Action.rotate_by(1, random.uniform(0.2, 0.8)), 0))

    def stop(self):
        stop_recording()

run(MyScene())

jmv38

awsome! thanks for this example.

Yilia

Do you mean it can make a recording and revise some related parameters for the output video by edit its codec?
I am now just having experience in recording desktop video and audio activities with a screen recorder on Windows.

ciderpunk

I know some online desktop screen recorders can save the scene as gif or output thumbnails, such as Screencastify and Loom.