Declare automatically images/sprites from your files names

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
User avatar
Ziassan
Veteran
Posts: 231
Joined: Thu Jun 09, 2011 8:14 pm

Declare automatically images/sprites from your files names

#1 Post by Ziassan »

edit : note that since 6.10.0 you have an automatic simple version of this by enabling this config http://renpy.org/wiki/renpy/doc/referen ... tic_images
The code below allows you to have control on the thing and to adapt it to your needs.

Starting with Ren'py itself but old in general coding, I thought it was a bit bothering sometimes to have to write exactly the same thing in the pictures names and then in the code. Like "image Satria embarassed = 'img/Satria/embarassed.png" : two time nearly the same thing, in coding, is bad... it needs to be improved.

Data path and variable names looking that similar... a lot of time can surely be earned.

Code: Select all

init python hide:

    for file in renpy.list_files():
        if file.startswith('img/'):
            if file.endswith('.png'):
                name = file.replace('img/','').replace('/', ' ').replace('.png','')
                renpy.image(name, Image(file, yanchor=400))
                continue
            continue
Replace img by your image directory (or the directory where you want Ren'py to use images to make variables). You have to use some conventions in naming files but it works for every picture (sprites, BG, CG, ...).

In short: it takes every files in the directory you want, and see if they are PNG files (you can change the extension too). Then it takes the path of the file and build the image variable from it. "img/Satria/upside_down.png" wille create a "Satria upside_down", "img/CG/black_night.png" will do a "CG black_night".


With that in your directory : http://puu.sh/MfYT http://puu.sh/MfV0
It will avoid you to write these lines :

Code: Select all

image Satria embarassed = 'img/Satria/embarassed.png"
image Satria upside_down = 'img/Satria/upside_down.png"
etc...

An other advantage is that you just have to add a picture to your directory and you can already use it in your code.
Last edited by Ziassan on Sat Sep 15, 2012 12:32 pm, edited 6 times in total.

User avatar
SusanTheCat
Miko-Class Veteran
Posts: 952
Joined: Mon Dec 13, 2010 9:30 am
Location: New Brunswick, Canada
Contact:

Re: Declare automatically images/sprites from your files nam

#2 Post by SusanTheCat »

Sweet!

Thank you!

Susan
" It's not at all important to get it right the first time. It's vitally important to get it right the last time. "
— Andrew Hunt and David Thomas

User avatar
Obscura
Eileen-Class Veteran
Posts: 1431
Joined: Fri Mar 09, 2012 6:58 pm
Projects: Coming Out On Top
Location: United States
Contact:

Re: Declare automatically images/sprites from your files nam

#3 Post by Obscura »

OMG, this is awesome Ziassan. Thanks for sharing!
Coming Out On Top - An Adult Gay Dating Sim
website

TrickWithAKnife
Eileen-Class Veteran
Posts: 1261
Joined: Fri Mar 16, 2012 11:38 am
Projects: Rika
Organization: Solo (for now)
IRC Nick: Trick
Location: Tokyo, Japan
Contact:

Re: Declare automatically images/sprites from your files nam

#4 Post by TrickWithAKnife »

Wow, I can't tell you how useful this will be. And easy to use too!
"We must teach them through the tools with which they are comfortable."
The #renpy IRC channel is a great place to chat with other devs. Due to the nature of IRC and timezone differences, people probably won't reply right away.

If you'd like to view or use any code from my VN PM me. All code is freely available without restriction, but also without warranty or (much) support.

User avatar
Sapphi
Eileen-Class Veteran
Posts: 1685
Joined: Fri Jun 05, 2009 3:31 am
Completed: Boku no Taisetsu na Yumeko
Projects: Twelve, PAW ★ PRINTS
Organization: Kitsch-soft
Location: Illinois, USA
Contact:

Re: Declare automatically images/sprites from your files nam

#5 Post by Sapphi »

Correct me if I'm wrong, but isn't Ren'Py set up this way so that it can load all the images at the start of the game so that the game works faster in the end? I thought I read that somewhere...
"It is [the writer's] privilege to help man endure by lifting his heart,
by reminding him of the courage and honor and hope and pride
and compassion and pity and sacrifice which have been the glory of his past."
— William Faulkner
▬▬▬▬▬▬▬▬▬▬..+X+..▬▬▬▬▬▬▬▬▬▬
Image

User avatar
Ziassan
Veteran
Posts: 231
Joined: Thu Jun 09, 2011 8:14 pm

Re: Declare automatically images/sprites from your files nam

#6 Post by Ziassan »

Hmm does that make a difference ? I mean, the code I wrote makes pretty much the same thing than declaring the images normally.
This line "renpy.image(name, Image(file, yanchor=400))" is the equivalent of the "image" thing. The difference is that here, I'm building the images declarations from the names of the data directly.

User avatar
netravelr
Miko-Class Veteran
Posts: 504
Joined: Thu Jan 28, 2010 2:31 am
Completed: Culina: Hands in the Kitchen, Culina: The Spirit of Cooking, Saving Zoey
Projects: Love at the Laundromat
Organization: Lakeview Interactive
Deviantart: netravelr
Location: USA
Contact:

Re: Declare automatically images/sprites from your files nam

#7 Post by netravelr »

Just learned about this about a week ago. Python is so cool with having access to doing stuff like this. Great find!
Image
Technical Designer/Programmer
Game Design Portfolio - Project updates on my Twitter
Experienced in: C/C++/C#, Python, Unreal, Unity, and Flash
_________________
"Space can be very lonely. The greatest adventure is having someone share it with you."

User avatar
Sapphi
Eileen-Class Veteran
Posts: 1685
Joined: Fri Jun 05, 2009 3:31 am
Completed: Boku no Taisetsu na Yumeko
Projects: Twelve, PAW ★ PRINTS
Organization: Kitsch-soft
Location: Illinois, USA
Contact:

Re: Declare automatically images/sprites from your files nam

#8 Post by Sapphi »

Ziassan wrote:Hmm does that make a difference ? I mean, the code I wrote makes pretty much the same thing than declaring the images normally.
This line "renpy.image(name, Image(file, yanchor=400))" is the equivalent of the "image" thing. The difference is that here, I'm building the images declarations from the names of the data directly.
Oh, I'm sorry. That does make sense. (I feel so dumb...)
Now that I know this, I will probably use your code so thanks in advance :)
"It is [the writer's] privilege to help man endure by lifting his heart,
by reminding him of the courage and honor and hope and pride
and compassion and pity and sacrifice which have been the glory of his past."
— William Faulkner
▬▬▬▬▬▬▬▬▬▬..+X+..▬▬▬▬▬▬▬▬▬▬
Image

User avatar
Reizeron
Newbie
Posts: 3
Joined: Mon Aug 13, 2012 10:54 am
Projects: Fate/Protocol VI
Contact:

Re: Declare automatically images/sprites from your files nam

#9 Post by Reizeron »

Not working if file or folder names contains numbers.
Nevermind, just forgot that a number should not be first character.

User avatar
Shiz
Newbie
Posts: 18
Joined: Tue Jun 05, 2012 8:35 pm
Projects: Twofold
Organization: Salty Salty Studios
Contact:

Re: Declare automatically images/sprites from your files nam

#10 Post by Shiz »

This has been in Ren'Py itself since version 6.10.0 already: http://renpy.org/wiki/renpy/doc/referen ... tic_images

User avatar
Ziassan
Veteran
Posts: 231
Joined: Thu Jun 09, 2011 8:14 pm

Re: Declare automatically images/sprites from your files nam

#11 Post by Ziassan »

Oh, indeed, I wonder how many people knew about it.

Well I'll still leave my code as it allows more flexibility and controle if someone wants to do something specific about that. But thanks, I'll edit the first post about that.

User avatar
PyTom
Ren'Py Creator
Posts: 16093
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: Declare automatically images/sprites from your files nam

#12 Post by PyTom »

Ziassan's method is probably better than the built in one, as it gives far more control. The built-in method predates me making renpy.list_files() public - if the archive format had been opened up earlier, there never would have been a config.automatic_images.
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
leon
Miko-Class Veteran
Posts: 554
Joined: Sun Oct 09, 2011 11:15 pm
Completed: Visual Novel Tycoon, Night at the Hospital, Time Labyrinth, The Buried Moon, Left of Center, Super Otome Quest
Projects: Lemon Project, Porcelain Heart, Dream's Dénouement
Organization: Team ANARKY
Contact:

Re: Declare automatically images/sprites from your files nam

#13 Post by leon »

If anyone needs flipped sprites (facing the other way) as well:

Code: Select all

init python hide:
    for file in renpy.list_files():
        if (file.startswith('sprites/')) and (file.endswith('.png')):
            name = file.replace('sprites/','').replace('/', ' ').replace('.png','')
            name_flipped = name + ' flip'
            renpy.image(name, Image(file, yalign=1.0))
            renpy.image(name_flipped, im.Flip(Image(file), horizontal=True, yalign=1.0))

inarizushi
Newbie
Posts: 3
Joined: Thu Jan 02, 2014 8:41 pm
Contact:

Re: Declare automatically images/sprites from your files nam

#14 Post by inarizushi »

I'm using your method to load images but ran into a strange problem with side images. From the documentation I read, it looks like the "side" tag needs to be added to an image the same way "happy" would. So if I have the image "img/side/sue/happy.png", when it loops through the directories it should create an image like so:

renpy.image("side sue happy", Image("img/side/sue/happy.png"))

which is the same, hopefully, as declaring this:

image side sue happy = "img/side/sue/happy.png"

Except I can only get the second way to work. I know the image is being loaded in the first example because "show side sue happy" works.

Any ideas?

Post Reply

Who is online

Users browsing this forum: No registered users