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
Ladycardboard
Newbie
Posts: 22
Joined: Tue Jul 28, 2015 6:17 pm
Contact:

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

#31 Post by Ladycardboard » Sun Aug 02, 2015 3:18 pm

Getting an invalid syntax error

http://imgur.com/9Tuj2bY

User avatar
Zetsubou
Veteran
Posts: 475
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

#32 Post by Zetsubou » Sun Aug 02, 2015 3:26 pm

You can't do that with python methods. You can make arguments optional by giving them a default value, but you can't insert a value by itself. (ie. "def define_characters(characterImageFolder='sprites/')" would work, but "def define_characters('sprites/')" won't)

You should be calling the method with your own parameters, not editing the method directly. Only the character indexes method should be edited.
Put the define_characters method back to how it was, then call "define_characters('sprites/')" at the end of the script.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Circadian Loop, My Heart Grows Fonder + VN writing commissions

Image

Ladycardboard
Newbie
Posts: 22
Joined: Tue Jul 28, 2015 6:17 pm
Contact:

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

#33 Post by Ladycardboard » Tue Aug 04, 2015 4:02 pm

i put the code back in fresh after deleting the whole thing with my stupid earlier mistakes in it. Got another error after removing the '#' in front of the example you put at the bottom.

http://imgur.com/w4lCYEh

User avatar
Zetsubou
Veteran
Posts: 475
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

#34 Post by Zetsubou » Tue Aug 04, 2015 6:37 pm

It looks like your get_character_indexes method needs updating.
That error should mean the script found an "ears" directory in one of your characters, but it wasn't defined in the above method.

What's your get_character_indexes method currently look like? And what is the directory structure you're using?
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Circadian Loop, My Heart Grows Fonder + VN writing commissions

Image


Hseo
Regular
Posts: 27
Joined: Thu Sep 24, 2009 10:42 am
Completed: High School Life:HSL
Deviantart: greeeed
Contact:

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

#36 Post by Hseo » Fri Mar 17, 2017 11:50 am

Thanks.
It help to save a lot of my time.

And for people with error as this one
Ladycardboard wrote:Tried it and got this error:

Image
The answer is simple.
put define_characters("comm", 1) inside same the python code like this.

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))
                    
    define_characters("images", 1)
And not like this

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))
    
init:                
    image bg testing1 = "images/bg 0001.jpg"
    define_characters("images", 1, False)
Yes, I made the same error as him :mrgreen:

User avatar
ariasune
Newbie
Posts: 3
Joined: Sun Nov 19, 2017 6:00 am
Tumblr: ariasune
Deviantart: ariasune
Contact:

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

#37 Post by ariasune » Sun Nov 19, 2017 9:30 am

I'm a beginner, so I'm concerned I'm making a rookie mistake that can be spotted in a split second. That being said, I'm having a hard time working this script into even the very base script, with none of the images being defined.

Oddly enough it seemed to work for a bit, but then refused to work later, even after reverting everything to the state when it was working. I really feel I must be missing something, and I hate to bother people.

My file directory for sprites is as follows:

game/images/sprites/[character]/[emotion].png

My script file is as follows:

Code: Select all

init python:
    import os
    def define_characters(images, excludeFirstXFolders=0, flip=True):
        for path in renpy.list_files():
            if path.startswith(images + "/"):
                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))
                    
    define_characters("sprites", 1)
    
init:
    $ k = Character("Kaiba")

image base = "base.jpg"

label start:

    scene base
    show kaiba smile

    k "A puppy die everytime I smile."

return

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

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

#38 Post by Zetsubou » Sun Nov 19, 2017 3:14 pm

I really haven't touched this script or checked this thread in a long time, but at a glance, I think your problem is the

Code: Select all

define_characters("sprites", 1)
line.
Your sprites are in images/sprites, so your function should be

Code: Select all

define_characters("images/sprites", 1)
That being said, if you're using the images directory anyway, and you don't need image flipping or to perform any other functions on the images, you might as well get rid of this script and just use Renpy's automatic image defining mechanism.
eg. instead of: game/images/sprites/[character]/[emotion].png
have: game/images/sprites/[character]/[character] [emotion].png

"[character] [emotion].png", inside of the "images" directory, will automatically be defined by Renpy, without the need for any script like this.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Circadian Loop, My Heart Grows Fonder + VN writing commissions

Image

User avatar
ariasune
Newbie
Posts: 3
Joined: Sun Nov 19, 2017 6:00 am
Tumblr: ariasune
Deviantart: ariasune
Contact:

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

#39 Post by ariasune » Tue Nov 21, 2017 12:50 am

Thank you very much. I've opted to go with Renpy's automatic image defining mechanism, since I'm not making use of flips. I appreciate you taking the time to glance over the code and solve the issue, as well as offer an alternative.

Again, thank you very much!

ThrashNeon
Newbie
Posts: 12
Joined: Thu Dec 28, 2017 8:21 am
Contact:

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

#40 Post by ThrashNeon » Fri Jan 19, 2018 6:00 am

I know that this is an old thread (and old code), but it really looks like something I'd like to use/adapt for sprite handling.
I've downloaded all the Renpy Image Definer files from github, carefully read all the documentation and comments in the code (several times).

I simply can't get any of these scripts to work. :cry:

I'm sure I'm missing something extremely basic (I'm new to Ren'Py, but not to programming in general). :oops:

I've created a blank, new, clean project to test this... and tried starting with define-all-simple.rpy

For the directory setup, I'm using:

Code: Select all

[game]
	[images]
		[bg]
			washington.png
		[characters]
			[sara]
				happy.png
				[uniform]
					concerned.png
...so for my script.rpy, I've tried this:

Code: Select all

init python:

    def define_images(imageFolder, excludeFirstXFolders=0, flip=True):
            for path in renpy.list_files():
                if path.startswith(imageFolder + "/"):
                    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))

    define_images('images/bg/', 1, False) # set to 1 to allow "bg" in the scene declaration?
    define_images('images/characters/', 2) # set to 2 to remove images/characters/ from the show declaration?

    #Another example: let's say we wanted to define some backgrounds as well.
    #We have a folder game/bg/, and in it there's the image "school.png".
    #We could use the function:
    #define_images('bg/', 0, False)
    #after which we'd be able to call the image with
    #scene bg school

define s = Character("Sara")
# Game start
label start:
	scene bg washington
	show sara happy
	s "You've created a new Ren'Py game."
	show sara uniform concerned
Running this, I don't get any errors, but images are not displayed, and lint shows:

Code: Select all

game/script.rpy:32 The image named 'bg washington' was not declared.

game/script.rpy:38 The image named 'sara happy' was not declared.

game/script.rpy:44 The image named 'sara uniform concerned' was not declared.
I've tried changing the directory structure, changing the parameters, changing the definitions, etc. Nothing seems to work. :(

Zetsubou: Would it be possible to just get a zipped working example project of your scripts?
I'd really like to expand on this using the define-characters-livecomposite.rpy for my project - using poses, expressions, etc... but I'm dead in the water if I can't even get the basics to work.
As I said, I'm probly just missing something really simple.

I've searched the web and the cookbook for alternate ways of doing this, but haven't found anything that works (or that would be a better solution).
I'm also studying up on DynamicDisplayables - but I really don't want to have to reinvent the wheel if I can avoid it.

Does anyone else have any tips/samples (actual working code) of how they are efficiently handling layered sprites?

Thanks for any help.

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

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

#41 Post by Zetsubou » Fri Jan 19, 2018 8:01 pm

For your backgrounds (eg. "bg washington"), I'd recommend not using this script. Images in the "images" directory are automatically defined by Renpy itself nowadays anyway. So you'd be better off having something like "game/images/bg/bg washington.png". Renpy would see the file "bg washington.png" and define it, so you could call "scene bg washington". It's faster and easier than using a script like this.

For character sprites, you don't need this script if you're going to use the livecomposite one. Using both would just wind up leading to images being defined twice.
So it might be better to ditch this script completely. Try the livecomposite script for sprites, and use Renpy's own image defining for everything else.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Circadian Loop, My Heart Grows Fonder + VN writing commissions

Image

ThrashNeon
Newbie
Posts: 12
Joined: Thu Dec 28, 2017 8:21 am
Contact:

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

#42 Post by ThrashNeon » Sun Jan 21, 2018 5:03 am

Thanks for the reply.. I really appreciate it.
Zetsubou wrote:
Fri Jan 19, 2018 8:01 pm
For your backgrounds (eg. "bg washington"), I'd recommend not using this script. Images in the "images" directory are automatically defined by Renpy itself nowadays anyway. So you'd be better off having something like "game/images/bg/bg washington.png". Renpy would see the file "bg washington.png" and define it, so you could call "scene bg washington". It's faster and easier than using a script like this.
Yeah... I was leaning that way for the backgrounds, that should be no problem.
Zetsubou wrote:
Fri Jan 19, 2018 8:01 pm
For character sprites, you don't need this script if you're going to use the livecomposite one. Using both would just wind up leading to images being defined twice.
So it might be better to ditch this script completely. Try the livecomposite script for sprites, and use Renpy's own image defining for everything else.
Unfortunately, it looks like some of the built in Ren'Py stuff is conflicting with using define-characters-livecomposite.rpy with regard to the character/image definitions:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/define-characters-livecomposite.rpy", line 145, in <module>
    define_chars("images/characters/")
  File "game/define-characters-livecomposite.rpy", line 97, in define_chars
    define_extra(size, side, character, None, outfit, cKey, oKey, None)
  File "game/define-characters-livecomposite.rpy", line 120, in define_extra
    define_image(size, side, nameList, args)
  File "game/define-characters-livecomposite.rpy", line 36, in define_image
    renpy.image(path_tuple, LiveComposite(size, *args))
Exception: Style 'image_placement' does not exist.

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/bootstrap.py", line 306, in bootstrap
    renpy.main.main()
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/main.py", line 367, in main
    renpy.game.script.load_script()  # sets renpy.game.script.
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/script.py", line 266, in load_script
    self.load_appropriate_file(".rpyc", ".rpy", dir, fn, initcode)
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/script.py", line 757, in load_appropriate_file
    self.finish_load(stmts, initcode, filename=lastfn)
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/script.py", line 449, in finish_load
    node.early_execute()
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/ast.py", line 900, in early_execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/python.py", line 1804, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/define-characters-livecomposite.rpy", line 145, in <module>
    define_chars("images/characters/")
  File "game/define-characters-livecomposite.rpy", line 97, in define_chars
    define_extra(size, side, character, None, outfit, cKey, oKey, None)
  File "game/define-characters-livecomposite.rpy", line 120, in define_extra
    define_image(size, side, nameList, args)
  File "game/define-characters-livecomposite.rpy", line 36, in define_image
    renpy.image(path_tuple, LiveComposite(size, *args))
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/display/layout.py", line 290, in LiveComposite
    rv = Fixed(xmaximum=width, ymaximum=height, xminimum=width, yminimum=height, **properties)
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/display/layout.py", line 1000, in Fixed
    return MultiBox(layout='fixed', **properties)
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/display/layout.py", line 513, in __init__
    self._clipping = self.style.clipping
  File "gen/styleclass.pxi", line 181, in renpy.styledata.styleclass.Style.clipping.__get__
  File "style.pyx", line 486, in renpy.style.StyleCore._get
  File "style.pyx", line 672, in renpy.style.build_style
  File "style.pyx", line 140, in renpy.style.get_full_style
  File "style.pyx", line 96, in renpy.style.get_style
Exception: Style 'image_placement' does not exist.

Darwin-15.6.0-x86_64-i386-64bit
Ren'Py 6.99.14.3135

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

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

#43 Post by Zetsubou » Sun Jan 21, 2018 4:00 pm

Maybe try changing the priority of the init python block? eg. "init +1 python:"
I'm guessing the script is running before some other part of Renpy is initialized.

From the stacktrace

Code: Select all

  File "game/define-characters-livecomposite.rpy", line 36, in define_image
    renpy.image(path_tuple, LiveComposite(size, *args))
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/display/layout.py", line 290, in LiveComposite
    rv = Fixed(xmaximum=width, ymaximum=height, xminimum=width, yminimum=height, **properties)
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/display/layout.py", line 1000, in Fixed
    return MultiBox(layout='fixed', **properties)
Which makes me think it's to do with the new multiple textboxes feature. (I haven't tested this code with Renpy 6.99.14)
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Circadian Loop, My Heart Grows Fonder + VN writing commissions

Image

ThrashNeon
Newbie
Posts: 12
Joined: Thu Dec 28, 2017 8:21 am
Contact:

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

#44 Post by ThrashNeon » Mon Jan 22, 2018 6:34 am

Zetsubou wrote:
Sun Jan 21, 2018 4:00 pm
Maybe try changing the priority of the init python block? eg. "init +1 python:"
I'm guessing the script is running before some other part of Renpy is initialized.
...
Which makes me think it's to do with the new multiple textboxes feature. (I haven't tested this code with Renpy 6.99.14)
Yep, I was running the script as you had written (it was in a "init python early" block).
Changing this to init python or init +1 python results in a different error.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/define-characters-livecomposite.rpy", line 1, in script
    init +1 python:
  File "game/define-characters-livecomposite.rpy", line 145, in <module>
    define_chars("images/characters/")
  File "game/define-characters-livecomposite.rpy", line 95, in define_chars
    define_extra(size, side, character, pose, outfit, cKey, oKey, pKey)
  File "game/define-characters-livecomposite.rpy", line 120, in define_extra
    define_image(size, side, nameList, args)
  File "game/define-characters-livecomposite.rpy", line 36, in define_image
    renpy.image(path_tuple, LiveComposite(size, *args))
Exception: Not a displayable: {}

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/define-characters-livecomposite.rpy", line 1, in script
    init +1 python:
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/ast.py", line 848, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/python.py", line 1804, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/define-characters-livecomposite.rpy", line 145, in <module>
    define_chars("images/characters/")
  File "game/define-characters-livecomposite.rpy", line 95, in define_chars
    define_extra(size, side, character, pose, outfit, cKey, oKey, pKey)
  File "game/define-characters-livecomposite.rpy", line 120, in define_extra
    define_image(size, side, nameList, args)
  File "game/define-characters-livecomposite.rpy", line 36, in define_image
    renpy.image(path_tuple, LiveComposite(size, *args))
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/display/layout.py", line 297, in LiveComposite
    rv.add(Position(widget, xpos=xpos, xanchor=0, ypos=ypos, yanchor=0))
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/display/layout.py", line 321, in __init__
    self.add(child)
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/display/layout.py", line 167, in add
    child = renpy.easy.displayable(d)
  File "/Users/thrashneon/Development/Ren'Py Dev/Ren'Py/renpy/easy.py", line 108, in displayable
    raise Exception("Not a displayable: %r" % (d,))
Exception: Not a displayable: {}

Darwin-15.6.0-x86_64-i386-64bit
Ren'Py 6.99.14.3135
BlankClean 1.0
So it looks like the new Ren'Py image handling still doesn't like your code.

I DO like the idea of automatic dynamic image/character declaration especially for lots of characters/poses/outfits/expressions and nested folders, and doing it in an elegant and efficient way...

... but at this point, it's really not worth taking up any more of your time on this.
I do appreciate your willingness to help... and looking at your nicely commented code has been really instructional for me - so thanks again for that.

I think I'm just going to dig a little deeper into current Ren'Py LiveComposite and DynamicDisplayables and see what I can come up with.

Thanks again for your time. :)

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

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

#45 Post by Zetsubou » Mon Jan 22, 2018 3:25 pm

Personally, I haven't used either of these scripts for a long time. I wrote them for 6.99.8 originally, IIRC, used them in one game, then changed the way I do things.
The problem is, it slows down the launch (if you leave it in as-is when shipping the final build, instead of just for testing) and makes it difficult for lint and other code parsers to be effective.
Something which writes out the image definition statements into a .rpy file (maybe even doing a developer mode check) would be more useful and future-proof. Though for the time being I'm focusing on making games instead of utilities like this.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too, Sable's Grimoire
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers, Wolf Tails

Working on:
Circadian Loop, My Heart Grows Fonder + VN writing commissions

Image

Post Reply

Who is online

Users browsing this forum: No registered users