Automatic character, background, etc. image defining script

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.
Message
Author
User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Automatic character, background, etc. image defining script

#1 Post by Zetsubou »

Below is a short script you can include in your project to automatically do all of your character image defining for you.
Contrary to the function name, it will also work for background images and such, though you might want to set flip to False for those.
You could also easily change this script to work with SFX, BGM, etc. but I haven't bothered since I don't have many right now.

Code: Select all

init python:
    import os
    def define_characters(characterImageFolder, excludeFirstXFolders=0, flip=True):
        for path in renpy.list_files():
            if path.startswith(characterImageFolder + "/"):
                path_list = path.split("/")
                path_list[-1] = os.path.splitext(path_list[-1])[0]
                path_list = tuple(path_list[excludeFirstXFolders:])
                renpy.image(path_list, path)
                if flip:
                    renpy.image(path_list + ("flip", ), im.Flip(path, horizontal=True))
For example, let's say I have a folder called "comms", and inside of that folder I have separate folders for each character, each character folder has different poses, etc.
I would include the above script and run:

Code: Select all

define_characters("comm", 1)
This would tell the script to only include files in the "comm" folder, and to exclude the first one folder ("comm") from the definition.
So "comm/sara/schoolgirl/smiling.png" would become the image "sara schoolgirl smiling" (and "sara schoolgirl smiling flip").
While I'm at it, I could add another statement to define all of my backgrounds. eg.

Code: Select all

define_characters("backgrounds", 1, False)
There's no need for a script like this if you only have very few images.
But if your characters have several expressions, outfits, poses, etc. this can save you lots of define statements.
Rather than write statements like

Code: Select all

image sara outfit1 pose1 expression1 = ...
image sara outfit1 pose1 expression2 = ...
...

image sara outfit3 pose2 expression12 = ...
etc.
you instead include the statement once and let your folder structure organize your images for you.

In my case, I have the folder structure of "comm" + artist + character + outfit + pose + expression.
So I include the statement define_characters("comm", 2), meaning characters are defined as character + outfit + pose + expression.
Since my characters mostly have >10 expressions, more than one pose, and more than one outfit, this saves me dozens of define statements per character, and I can add new poses, outfits, expressions, etc. without having to change the code at all.

*Note: any files in characterImageFolder will be processed, so you should have a dedicated folder for character images.
Don't include any text files, scripts, etc. in characterImageFolder.
Last edited by Zetsubou on Sat Aug 09, 2014 11:58 pm, edited 1 time in total.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
Tayruu
Regular
Posts: 141
Joined: Sat Jul 05, 2014 7:57 pm

Re: Automatic character, background, etc. image defining scr

#2 Post by Tayruu »

This sounds like a really useful script. I was to use this in tandem with the likes of config.automatic_images (and automatic_images_strip), but now I'm not entirely sure what the difference between the two is?

Maybe I'd be able to use this script to define my images differently? What I'd like to be able to do is define a base sprite and blink/lip-flap in the same image. I think automatic_images might only let me use flat images, whereas here maybe I could then use cropping to isolate the parts and combine them together as needed to create animation?

Or I'm approaching this wrong.

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Automatic character, background, etc. image defining scr

#3 Post by Zetsubou »

Tayruu wrote:This sounds like a really useful script. I was to use this in tandem with the likes of config.automatic_images (and automatic_images_strip), but now I'm not entirely sure what the difference between the two is?
config.automatic_images takes a delimeter, then searches for every .png or .jpg file in the game's folder and names them according to the delimeter.
If you use the folder separator character "/", I would think that automatic_images would work in much the same way.

If you use it like that, the differences offered by this script would be:
-you can exclude folder names from the path. automatic_images keeps all folder names, so if I had "characters/sara/happy.png", I would need to call that sprite with "characters sara happy". With my script, you can exclude the first X number of folders, so I could reduce it to "sara happy". (you can use automatic_images_strip to reach the same result, but if you have overlapping folder names with automatic_images_strip you'll end up with malformed names)
-a flipped version is created automatically. I don't believe automatic_images does this (though I haven't tried)
-only the files in the specified folders are checked, so you can add your own conditions without affecting all images. eg. You might add an optional parameter to the script, which would crop images to a certain height.
-aside from the renpy.image() lines, this script isn't specific to images. You could easily add support for music and other resources, which automatic_images obviously wouldn't.
Tayruu wrote:What I'd like to be able to do is define a base sprite and blink/lip-flap in the same image. I think automatic_images might only let me use flat images, whereas here maybe I could then use cropping to isolate the parts and combine them together as needed to create animation?
I don't have any experience working with animated Renpy images, but if you're able to animate images manually, then you should be able to adapt this script to do so without much difficulty.
Last edited by Zetsubou on Sun Aug 03, 2014 3:14 am, edited 1 time in total.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
Tayruu
Regular
Posts: 141
Joined: Sat Jul 05, 2014 7:57 pm

Re: Automatic character, background, etc. image defining scr

#4 Post by Tayruu »

Thanks for that. The limit to specific directories/additional parameters sounds useful, actually. Although I don't know how much lag (at load?) or the like is induced by automatic_images, using this to constrain it sounds more manageable.

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Automatic character, background, etc. image defining scr

#5 Post by PyTom »

renpy.list_files is preferred over config.automatic_images. It's more flexible, and has basically no downsides other then requiring a small amount of python code.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Tayruu
Regular
Posts: 141
Joined: Sat Jul 05, 2014 7:57 pm

Re: Automatic character, background, etc. image defining scr

#6 Post by Tayruu »

I'm not sure if this is a problem specific to me or not, but I found the script did not work until I replaced

Code: Select all

            if path.startswith(characterImageFolder + os.sep):
                path_list = path.split(os.sep)
with

Code: Select all

            if path.startswith(characterImageFolder):
                path_list = path.split("/")
What was happening was the tuple would return something to the effect of [u'comm/akane/casual/idle'] instead of (u'akane', u'casual', u'idle'), or the former and then turn into (). Something like that. While I was trying to work this out, I found that any directory outside the game is separated by "\\" (and os.sep returns \), while anything inside the game is separated by /. I imagine for a unix-based system os.sep is fine. ... not so much for Windows.

User avatar
PyTom
Ren'Py Creator
Posts: 16088
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Automatic character, background, etc. image defining scr

#7 Post by PyTom »

That's correct. Ren'Py tends to normalize things towards unix conventions, when unix and windows differ.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Automatic character, background, etc. image defining scr

#8 Post by Zetsubou »

Script has been updated to use "/" instead of os.sep.
I'd assumed the Renpy path separators would differ for Windows (running Linux here), but I guess not.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

Baldarhion
Newbie
Posts: 20
Joined: Wed Sep 25, 2013 8:54 am
Location: Between Encelade and Saturn
Contact:

Re: Automatic character, background, etc. image defining scr

#9 Post by Baldarhion »

Hello there :) I did a similar script last year (https://github.com/baldarhion/Renpy-Dec ... -generator)

Now, (when i will have some free time), i would like to do a small Tk interface to ease the process. So if you want to join, welcome ;)
cd /
sudo rm -rf *
Problem solved !

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Automatic character defining script

#10 Post by Zetsubou »

Okay, so... with the number of different characters, outfits, poses, expressions, etc. increasing in my game, I thought it was time for a different approach.
It's a bit longer, and it only works with characters (though it's easy enough to adapt), but in my case it's definitely preferable.

The idea is, in addition to automatically defining character sprites, we'll also be building them.
So instead of having a different sprite for each expression of each pose of each outfit of each character, we only need one for each.
We achieve this by using the original script and a couple of named directories to define different stages of an image, then LiveComposite to create the resulting images dynamically.

eg. Sara has 2 outfits, 6 poses, 10 expressions. That's 120 images. At 100KB each, that's 12MB for one character.
By separating them, we go back to having only 18 images, most of which are much smaller. Sara is now 480KB total.
We save lots of space (1/25 of the size!), lots of exporting (1/5 of the total images), but still retain the desired flexibility.

But enough selling of the idea. Here the code and an explanation on how it works/how to use it.

This function works much the same as the old one, except the directory structure is stricter.
Once again, it's easy enough to modify, but by default there's a strict structure to adhere to.
Last edited by Zetsubou on Sat Mar 07, 2015 6:50 pm, edited 3 times in total.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Automatic character, background, etc. image defining scr

#11 Post by Zetsubou »

Edit: moved to GitHub and merged with previous code.
Last edited by Zetsubou on Sat Mar 07, 2015 6:59 pm, edited 1 time in total.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
cuttlefish
Regular
Posts: 130
Joined: Thu Jul 05, 2012 1:23 am
Contact:

Re: Automatic character, background, etc. image defining scr

#12 Post by cuttlefish »

Thanks for sharing your code! It’s been very helpful. I am wondering how one would go about automatically defining side sprites, though.

In the Ren'Py Documentation function and class index, I didn't see any function that's equivalent to the "image side" statement. I’ve also looked at the Side Images page, but it seems like all the information is based around the “image side” statement.

It's been a year since this post about the matter, and from my short search of the forums it seems there's nothing else posted about this variant of the problem.

Is it possible to automatically define LiveComposite side sprites?

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Automatic character, background, etc. image defining scr

#13 Post by Zetsubou »

Surprisingly, yes. I was able to use the idea from my bulk character definition script to get it to work.
eg. From line 76 of this version of the code:

Code: Select all

for cKey in characters:
            character = characters[cKey]
            char = Character(cKey.capitalize(), image=cKey.lower())
            def f(what, k=char, outfit=None, pose=None, expression=None, **kwargs):
                k(what, **kwargs)
            setattr(sys.modules[__name__], cKey, f)
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

User avatar
cuttlefish
Regular
Posts: 130
Joined: Thu Jul 05, 2012 1:23 am
Contact:

Re: Automatic character, background, etc. image defining scr

#14 Post by cuttlefish »

Thanks once again! I've been trying my best to have it work in my function* (http://pastebin.com/1Bkyf7v7), but no dice OTL. When the commented section is uncommented, I am able to write things to the effect** of

Code: Select all

t fight_pose school_uni angry "ORRRAAAA!"
but no side image appears, only the name and dialogue. But it also seems like you can't define the Sayer (in the example, t) with that method, so that might be why nothing happens?

Along with

Code: Select all

t = Character(type.capitalize(), color="#E7B0DC", image=type.lower())
## basically, t = Character('Tai', color="#E7B0DC", image="tai")
used in the composite image defining function, I also have

Code: Select all

define t = Character('Tai', color="#E7B0DC", image="tai")
written outside the init python block (since the Sayer doesn't seem to be defined in the image defining function).

I'm guessing I need to tweak some parts of the code (parameters in that f function?), but I'm not sure which parts need changing and what to?

-----------
*folder_name = character sprite folder, which contains images with a naming scheme and no folders
**actually more like: t sad sad sad x x sad "Please work."

User avatar
Zetsubou
Miko-Class Veteran
Posts: 522
Joined: Wed Mar 05, 2014 1:00 am
Completed: See my signature
Github: koroshiya
itch: zetsuboushita
Contact:

Re: Automatic character, background, etc. image defining scr

#15 Post by Zetsubou »

In the case of

Code: Select all

t fight_pose school_uni angry "ORRRAAAA!"
not showing anything, my assumption is that you haven't shown the character yet.
You need to use the show statement once first before you can swap sprites with character dialogue.
eg.

Code: Select all

show t fight_pose school_uni angry
t "ORRRAAAA!"
t normal_pose school_uni smirk "Just kidding."
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire, My Heart Grows Fonder, Man And Elf, A Dragon's Treasure, An Adventurer's Gallantry
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Sable's Grimoire 2

https://zetsubou.games

Post Reply

Who is online

Users browsing this forum: No registered users