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
cuttlefish
Regular
Posts: 130
Joined: Thu Jul 05, 2012 1:23 am
Contact:

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

#16 Post by cuttlefish » Sun Mar 08, 2015 11:32 pm

Ah! You’re right!

In the end, I ended up with this code and using show_side_image=ShowingSwitch() with the path_lists and paths the function collected.

Thanks for all of your help!

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

#17 Post by Zetsubou » Mon Mar 09, 2015 12:36 am

Glad you got it working. Looking at the number of different layers you're using, I can see why you wanted to use this script :lol:

ps.

Code: Select all

if side_image:
    sidedict[' '.join(shortname)] = LiveComposite(image_size, *layers)
Brevity ftw.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers

Working on:
Sable's Grimoire + several 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

#18 Post by Ladycardboard » Tue Jul 28, 2015 6:21 pm

I'm having trouble getting your script to work right. I copy and pasted the script into my renpy script but get these errors:

Code: Select all

File "game/script.rpy", line 27: expected statement.
    def define_image(size, side, path_tuple, argList):
                    ^

File "game/script.rpy", line 48: expected statement.
    def get_character_indexes():
                             ^

File "game/script.rpy", line 51: expected statement.
    def define_chars (characterImageFolder, size=(370, 512), flip=True, side=False):
                     ^

File "game/script.rpy", line 113: expected statement.
    def define_extra (size, side, character, pose, outfit, cKey, oKey, pKey):
                     ^
Can you help me out?

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

#19 Post by Zetsubou » Tue Jul 28, 2015 6:44 pm

I'd need to see your scripy.rpy file to be sure, but my guess is that you didn't put the code in an init python block.
ie. rather than

Code: Select all

def define_image(size, side, path_tuple, argList):
    #...
it should be

Code: Select all

init python:
    def define_image(size, side, path_tuple, argList):
        #...
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers

Working on:
Sable's Grimoire + several 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

#20 Post by Ladycardboard » Tue Jul 28, 2015 10:40 pm

That error is solved but now all I'm getting is a grey placeholder picture with the text written in the middle of it. Here's the script:

Code: Select all

[spoiler]init python:
    
  def define_image(size, side, path_tuple, argList):
      path_tuple = tuple(entry for entry in path_tuple if entry is not None)
      if side:
          path_tuple = ('side', ) + path_tuple
      args = []
      for i in xrange(1, len(argList), 2):
          if argList[i] is not None:
              args.extend([(0, 0), argList[i]])
      renpy.image(path_tuple, LiveComposite(size, *args))
      if 'valerie' in path_tuple:
          print path_tuple

      if not side:

          flipArr = args
          for i in xrange(1, len(args), 2):
              flipArr[i] = im.Flip(args[i], horizontal=True)
          plist = list(path_tuple)
          plist.insert(1, 'flip') #flip after char name. eg. sara flip arms_crossed etc.
          renpy.image(tuple(plist), LiveComposite(size, *flipArr))

  def get_character_indexes():
      return {'expressions': {}, 'poses': {}, 'ears': {}, 'freya': {}, 'freya2': {}, 'arms': {}}

  def define_chars(characterImageFolder, size=(370, 512), flip=True, side=False):
      chars = {}

      for path in renpy.list_files():
          if path.startswith(characterImageFolder):
            pList = path.split("/")

            if len(pList) >= 4 and path.startswith(characterImageFolder + pList[-4] + "/poses/"):
                  if pList[-4] not in chars:
                    chars[pList[-4]] = get_character_indexes() #New character
                  if pList[-2] not in chars[pList[-4]]['poses']:
                    chars[pList[-4]]['poses'][pList[-2]] = {} #New pose
                  splitVal = os.path.splitext(pList[-1])[0]
                  if splitVal not in chars[pList[-4]]['poses'][pList[-2]] and splitVal != 'pose':
                    chars[pList[-4]]['poses'][pList[-2]][splitVal] = path #New outfit
            elif len(pList) >= 4 and path.startswith(characterImageFolder + pList[-4] + "/outfits_extra/"):
                  if pList[-4] not in chars:
                    chars[pList[-4]] = get_character_indexes() #New character
                  if pList[-2] not in chars[pList[-4]]['outfits_extra']:
                    chars[pList[-4]]['outfits_extra'][pList[-2]] = {} #New outfit extra
                  splitVal = os.path.splitext(pList[-1])[0]
                  if splitVal not in chars[pList[-4]]['outfits_extra'][pList[-2]]:
                    chars[pList[-4]]['outfits_extra'][pList[-2]][splitVal] = path #New pose
            elif path == characterImageFolder + pList[-2] + '/base.png':
              if pList[-2] not in chars:
                chars[pList[-2]] = get_character_indexes() #New character
              chars[pList[-2]]['base'] = path
            elif len(pList) >= 3:
              for val in ['outfits', 'poses', 'expressions', 'outfits_extra']:
                if path.startswith(characterImageFolder + pList[-3] + "/" + val + "/") and pList[-2] == val:
                  if pList[-3] not in chars:
                    chars[pList[-3]] = get_character_indexes()
                  splitVal = os.path.splitext(pList[-1])[0]
                  if splitVal not in chars[pList[-3]][pList[-2]]:
                    chars[pList[-3]][pList[-2]][splitVal] = path
                  break

      for cKey in chars:
          character = chars[cKey]
          if len(character['outfits']) > 0: #Standard directory structure
            for oKey in character['outfits']:
              outfit = character['outfits'][oKey]
              if len(character['poses']) > 0:
                  for pKey in character['poses']:
                      pose = character['poses'][pKey]
                      define_extra(size, side, character, pose, outfit, cKey, oKey, pKey)
              else:
                  define_extra(size, side, character, None, outfit, cKey, oKey, None)
          if len(character['poses']) > 0: #Pose-specific outfits
            for pKey in character['poses']:
              pose = character['poses'][pKey]
              if isinstance(pose, dict):
                posePath = characterImageFolder + cKey + '/poses/' + pKey + '/' + 'pose.png'
                for oKey in pose:
                  outfit = pose[oKey]
                  define_extra(size, side, character, posePath, outfit, cKey, oKey, pKey)
              else:
                  define_extra(size, side, character, pose, None, cKey, None, pKey)
          elif len(character['outfits']) == 0 and len(character['expressions']) > 0:
              #Base sprite contains pose and outfit
              define_extra(size, side, character, None, None, cKey, None, None)

  def define_extra(size, side, character, pose, outfit, cKey, oKey, pKey):
      for eKey in character['expressions']:
          expression = character['expressions'][eKey]
          nameList = tuple([cKey, oKey, pKey, eKey])
          args = [(0, 0), pose, (0, 0), outfit, (0, 0), expression]

          if 'base' in character:
              args = [(0, 0), character['base']] + args
          define_image(size, side, nameList, args)
          for oeKey in character['outfits_extra']:
              outfit_extra = character['outfits_extra'][oeKey]
              if isinstance(outfit_extra, dict):
                if pKey in outfit_extra: #For each pose
                  nameList = tuple([cKey, oKey, oeKey, pKey, eKey])
                  define_image(size, side, nameList, args + [(0, 0), outfit_extra[pKey]])
              else:
                  nameList = tuple([cKey, oKey, oeKey, pKey, eKey])
                  define_image(size, side, nameList, args + [(0, 0), outfit_extra])
[/spoiler]

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

#21 Post by Zetsubou » Wed Jul 29, 2015 7:19 am

Ladycardboard wrote:That error is solved but now all I'm getting is a grey placeholder picture with the text written in the middle of it. Here's the script:
That grey placeholder with text means the image you tried to call hasn't been defined.
Given that you've added fields to the get_character_indexes function, I'm guessing the issue is that your folder/image structure doesn't match the rest of the script.
ie. The script as it is only looks for 'outfits', 'poses', 'expressions' and 'outfits_extra' directories, in addition to the base.png image.

So to add your extra fields (ears, freya, freya2, arms) you would need to adjust the define_chars and define_extra methods. The exact modifications will depend on the order you want the images to be stacked, and which, if any, should be optional.

I might have a go at making this script cleaner and more generic tomorrow. As it is, you certainly can add extra fields, but it isn't a very simple thing to do.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers

Working on:
Sable's Grimoire + several 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

#22 Post by Ladycardboard » Wed Jul 29, 2015 2:33 pm

that would be so helpful and nice! ;-; Thank you so much.

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

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

#23 Post by Ladycardboard » Thu Jul 30, 2015 5:27 pm

Okay, while waiting for you to update the script I tried renaming my folders and organizing them into the method used in the script but came up with an error when i tried to call it like in your read-me.

Code: Select all

File "game/script.rpy", line 110: invalid syntax
    define_characters("sprites/")
I tried adding "def" before the "define_characters" but that didn't fix the error. :(

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

#24 Post by Zetsubou » Thu Jul 30, 2015 6:13 pm

Still working on a more generic version of the script. Difficult stuff.

That particular error is because the documentation (wrongly) lists the method as "define_characters" while the function is actually called "define_chars". So change either the function name or the call so they both match.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers

Working on:
Sable's Grimoire + several 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

#25 Post by Ladycardboard » Thu Jul 30, 2015 6:43 pm

Alright. I'll give it a try. I'll keep my eye on the thread to see what you come up with on the updated version. You're a miracle worker. :>

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

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

#26 Post by Ladycardboard » Thu Jul 30, 2015 6:50 pm

Tried it and got this error:

Image

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

#27 Post by Zetsubou » Thu Jul 30, 2015 6:51 pm

Remove the "def".
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers

Working on:
Sable's Grimoire + several 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

#28 Post by Ladycardboard » Thu Jul 30, 2015 9:00 pm

i'm gonna stop playing around with the old script and wait for the new one.

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

#29 Post by Zetsubou » Thu Jul 30, 2015 9:07 pm

If you're only doing basic layers, it'll probably work as-is: http://pastebin.com/3q88xWUi
I haven't finished nested directories or tested optional layers yet.
I also haven't actually tested the script in a game yet, so it might not even work :lol:
I'll try to finish it off a bit later today and test it at home this evening.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers

Working on:
Sable's Grimoire + several VN writing commissions

Image

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

#30 Post by Zetsubou » Fri Jul 31, 2015 3:30 am

Okay, this should do it: https://github.com/koroshiya/renpy-imag ... posite.rpy
I haven't tried using it in an actual game though.
Documentation in the file. Let me know if I missed anything.
Basically you just edit the dictionary variable in the get_character_indexes function.
Finished games
-My games: Sickness, Wander No More, Max Massacre, Humanity Must Perish, Tomboys Need Love Too
-Commissions: No One But You, Written In The Sky, Diamond Rose, To Libertad, Catch Canvas, Love Ribbon, Happy Campers

Working on:
Sable's Grimoire + several VN writing commissions

Image

Post Reply

Who is online

Users browsing this forum: No registered users