Convert Python(.py) files to Ren'Py(.rpy)

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
Human Bolt Diary
Regular
Posts: 111
Joined: Fri Oct 11, 2013 12:46 am
Contact:

Convert Python(.py) files to Ren'Py(.rpy)

#1 Post by Human Bolt Diary »

If you happen to want to convert files from pure Python to Ren'Py Python, I've written a script to do that.

This may be of use to people who want to write .py files while developing, documenting, and testing, but ultimately want the finished product to be available in Ren'Py without the use of imports.

Download at: https://github.com/jsfehler/py_to_rpy

.rpy files generated by this script are identical to the .py files, except for all the code being placed inside a Ren'Py "init python" block.

Usage

From the command line

When called from the command line, multiple files can be specified at once, each separated by a space. The file extension should be omitted.

Code: Select all

python py_to_rpy.py file1 file2 file3
Into a specific destination

By default, new .rpy files are created next to the .py files. However, the --dest argument can be used to place them into a new folder.

Code: Select all

python py_to_rpy.py file1 file2 file3 --dest=my_folder
Strict Mode

By default, new .rpy files will preserve renpy imports that were in the .py file. However, these imports are normally unnecessary in .rpy files. To strip them out, use the --strict flag.

Code: Select all

python py_to_rpy.py file1 file2 file3 --strict
The following imports are stripped:

import renpy.exports as renpy

from renpy.{module} import {class}

User avatar
ComputerArt.Club
Veteran
Posts: 427
Joined: Mon May 22, 2017 8:12 am
Completed: Famous Fables, BoPoMoFo: Learn Chinese, Santa's workshop, Cat's Bath, Computer Art Club
Location: Taiwan
Contact:

Re: Convert Python(.py) files to Ren'Py(.rpy)

#2 Post by ComputerArt.Club »

Thanks for this! I probably won't be able to use anything like this for awhile, but it sounds really useful!

picobyte
Newbie
Posts: 15
Joined: Mon Mar 14, 2016 2:15 pm
Contact:

Re: Convert Python(.py) files to Ren'Py(.rpy)

#3 Post by picobyte »

You can probably extend your script with more features, but for this task I believe this may do the same.

sed -r '1s/^/init python:\n /;2,$s/^/ /' source.py > dest.rpy
sed -r -n '/^(import renpy.exports as renpy|from renpy\.)/b;1s/^/init python:\n /;2,$s/^/ /;p' source.py > dest_strict.rpy

Post Reply

Who is online

Users browsing this forum: No registered users