Issues with Alpha Mask for a Character Creaation Script

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Jac_Skulls
Newbie
Posts: 6
Joined: Thu Sep 30, 2021 5:17 pm
Deviantart: JACSKULLS
Contact:

Issues with Alpha Mask for a Character Creaation Script

#1 Post by Jac_Skulls » Tue Oct 05, 2021 4:20 pm

Hello, all! First timer here on this website.

I am having issues trying to get this code to work for a Character Creation game. Meaning... Once I'm done with the Art Assets, I do plan to release it. But I want to release it with the codes to help other people for fun.

I went on Discord and asked questions and someone took the time to help me, however, it was short as we were both tired.

Code: Select all

    group ear:
        attribute ear default AlphaMask("Sprites/full/natural/Alpha_0_[pose]_[hair].png")
I tried reading up on the other websites to see what I did wrong but kept getting this error.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/Appearance.rpy", line 230, in script
    layeredimage mc1:
  File "renpy/common/00layeredimage.rpy", line 779, in execute_layeredimage
    rai.execute()
  File "renpy/common/00layeredimage.rpy", line 771, in execute
    l.extend(i.execute())
  File "renpy/common/00layeredimage.rpy", line 297, in execute
    rv.extend(i.execute(group=group, properties=properties))
  File "renpy/common/00layeredimage.rpy", line 263, in execute
    image = eval(self.image)
  File "game/Appearance.rpy", line 331, in <module>
    attribute ear default AlphaMask("Sprites/full/Alpha_0_[pose]_[hair].png")
TypeError: __init__() takes exactly 3 arguments (2 given)
And sometimes, it would show this.

group ear ->:

As for what I'm trying to do... I have this front hair that interferes with the Ears. So in order to solve this, I put the ears in front, hoping to save me many pictures just for making the ears fit with the hair. So I'm trying to use the Alpha Mask to help save me both data and time. Although, I'm not an impatience man. Lol.

I guess I need to figure out where to put it and figure out what I'm missing. Any help would be highly appreciated.

PS- if I'm not clear on certain things, feel free to ask me. I don't mind providing information.

Jac.

jeffster
Regular
Posts: 123
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Issues with Alpha Mask for a Character Creaation Script

#2 Post by jeffster » Wed Oct 06, 2021 4:14 am

Let's see the error message:

Code: Select all

  File "game/Appearance.rpy", line 331, in <module>
    attribute ear default AlphaMask("Sprites/full/Alpha_0_[pose]_[hair].png")
TypeError: __init__() takes exactly 3 arguments (2 given)
It says that function __init__ expects 3 parameters, but you gave her only 2, and she has no idea where to get the third one. :P

Let me explain.

When you write AlphaMask(something), you create an object of class AlphaMask.

It's easy to see such things because per usual agreement, classes' names start with capital letters. So anything like

Code: Select all

add CapitalizedName()

# or

a = CapitalizedName(something)
means that you create an object of that class (in my example, "CapitalizedName"), initializing that object with the parameters you put in parentheses.

When you initialize (create) an object of some class, it's done internally by running the function __init__ from that class.

In those class definitions, the first parameter of __init__ is usually "self", it doesn't count among the parameters you set.

So if __init__ of AlphaMask expects 3 parameters, there is "self", and two more parameters must be supplied by you.

You call it:

Code: Select all

AlphaMask("Sprites/full/Alpha_0_[pose]_[hair].png")
That's one parameter. Where is the second?

Let's see the docs:

https://www.renpy.org/doc/html/displaya ... #AlphaMask

AlphaMask(child, mask, **properties)

Perhaps you should make a second image, for alpha mask?

Or if you don't use alpha mask, instead of AlphaMask("Sprites/full/Alpha_0_[pose]_[hair].png") try just

Code: Select all

    attribute ear default "Sprites/full/Alpha_0_[pose]_[hair].png"
?

Jac_Skulls
Newbie
Posts: 6
Joined: Thu Sep 30, 2021 5:17 pm
Deviantart: JACSKULLS
Contact:

Re: Issues with Alpha Mask for a Character Creaation Script

#3 Post by Jac_Skulls » Wed Oct 06, 2021 5:41 am

Hello, there! Thank you for replying.

Thanks to your example and the website, I managed to finally get there. There was two flaws I had involving this line of codes... First being is that I didn't have the Child mixed with the Mask. Though, don't know much about the property part yet. It's as you pointed out. :D
And second... I didn't have it under layeredimage. Group is still something new I'm dealing with.


So that leaves only one new issues I managed to find.

Exception: DynamicImage u'Sprites/full/Alpha_0_[pose]_[hair].png': could not find image. (u'Sprites/full/Alpha_0_[pose]_[hair].png')

This occurs only when I try to use the variables instead of the actual name.

Main reason is because I'm using if and elif for the Variables. So when a pose change, that should too. However... That exception tells me that it's actually searching for [pose] and [hair] instead of the variable numbers. At least, that's what I think. I'm no professional, sadly. If what that exception tells me is actually what I think, I may have to do things a little more differently somehow.

This is what I came up with on that code.

Code: Select all

layeredimage mc1:

    group ear:
        attribute ear default AlphaMask("Sprites/full/Alpha_0_[pose]_[hair].png", "Sprites/full/ear_[ear]_000[skin].png")
        
        

And to clarify... I got it under the same layeredimage with the if and elif.

This is just part of the codes.

Code: Select all

layeredimage mc1:

    group ear:
        attribute ear default AlphaMask("Sprites/full/Alpha_0_[pose]_[hair].png", "Sprites/full/ear_[ear]_000[skin].png")

    #ear color
    if ear == 0:
        "Sprites/full/ear_[ear]_000[skin].png"
    elif ear == 1:
        "Sprites/full/ear_[ear]_000[skin].png"
    elif ear == 2:
        ""Sprites/full/ear_[ear]_000[skin].png"
    elif ear == 3:
        "Sprites/full/ear_[ear]_000[skin].png"
    elif ear == 4:
        "Sprites/full/ear_[ear]_000[skin].png"
Thank you for taking the time to give me that step forward. Your demonstration helped out a lot.

jeffster
Regular
Posts: 123
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Issues with Alpha Mask for a Character Creaation Script

#4 Post by jeffster » Wed Oct 06, 2021 7:00 am

I'm not sure what you are trying to achieve.

1. Why do you use ear as the alpha mask for hair?

If you just place the ear in front of hair, the hair would be visible through the transparent parts of the ear image. So it should be alright.

Or if you place the hair picture on top of the ear picture, the hair might have transparent parts through which some ear could be seen.

2. Why do you use the same expression under "if ear == "?

You don't need to examine different conditions to invoke the same resulting code.

3. I think that Ren'Py substitutions (variables in square brackets) work when you show some text, because Text() class performs those substitutions. But in usual strings I don't think substitutions happen. So in order to use variable values in strings, you use things like:

Code: Select all

"Sprites/full/Alpha_0_" + pose + "_" + hair + ".png"
And if "pose" and "hair" in this example are integers, then you need to convert them to strings (in order to join with other strings):

Code: Select all

"Sprites/full/Alpha_0_" + str(pose) + "_" + str(hair) + ".png"
Likewise:

Code: Select all

"Sprites/full/ear_" + str(ear) + "_000" + str(skin) + ".png"
But in Python direct addition of strings like that is not recommended (I'll omit the explanation here), so we better use formatting:

https://docs.python.org/2/tutorial/inputoutput.html

Like this:

Code: Select all

"Sprites/full/Alpha_0_{}_{}.png".format(pose, hair)
Function format() applies to a string and replaces curly brackets with the values we give it. Likewise:

Code: Select all

"Sprites/full/ear_{}_000{}.png".format(ear, skin)

Jac_Skulls
Newbie
Posts: 6
Joined: Thu Sep 30, 2021 5:17 pm
Deviantart: JACSKULLS
Contact:

Re: Issues with Alpha Mask for a Character Creaation Script

#5 Post by Jac_Skulls » Wed Oct 06, 2021 8:13 am

For what I'm trying to achieve...

(would show pictures but not sure how to do that with this site but instead, will try to make a video and share a link via Youtube)
I'm actually trying to take out a piece of the ear so that the hair would blend in with the ear. Main reason is because I had to draw over the base's head. And you are right on the mark with placing the ear in front of the hair. Lol. That's what I did. I mean, when you see Characters in art, you often see the hairs overlap the ears. And since this is Character creation... Meaning... one can select type of nose, type of eyes, type of hairs, type of clothes etc... even change colors for all but the nose. Though, going for a more Anime style this time using Blender 2.8. All 2D.

Problem is, if I were to do things with art only... it would take up a lot of spaces just to align each and every items I come across. Plus, would take a lot more time doing so for each poses.

As for why I used the same expression for each "if ear ==?", to be honest, I'm still new. May have started about 5 or 6 years ago, but I wasn't always on Renpy. Plus, I pretty much had the impression that Renpy Websites was the only way to advance with Renpy codes. And when I found out that there was actually other ways to do them (more advance ways), was when I started this small project. If there is a better way to do this, I would take that route. This is mainly due to the fact of the selection Screen where players could choose their own style of how they want their characters to be. Human, Elves, or whatever creatures with large ears that may or may not be human.


Warning. Long codes. Kind of.

Code: Select all

label start:
default backh = 1
default skin = 0
default clothes = 1
default ear = 0
default face = 0
default nose = 1
default fronth = 0
default eyeb = 0
default eyes = 0
default eyecol = 4
default col = 5
default hood = 1
default iris = 0
default pose = 0
screen maker():
    vbox:
        yalign 0.0
        xalign 0.0
        spacing 25

        vbox:
            text "face"

        hbox:
            textbutton "Prev" action SetVariable('face', (face - 1) % 4) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('face', (face + 1) % 4) # allows 0,1,2,3 then cycles

        text "ears"

        hbox:
            textbutton "Prev" action SetVariable('ear', (ear - 1) % 6) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('ear', (ear + 1) % 6) # allows 0,1,2,3 then cycles

        text "eyes"

        hbox:
            textbutton "Prev" action SetVariable('eyes', (eyes - 1) % 6) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('eyes', (eyes + 1) % 6) # allows 0,1,2,3 then cycles

        text "back-hair"

        hbox:
            textbutton "Prev" action SetVariable('backh', (backh - 1) % 5) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('backh', (backh + 1) % 5) # allows 0,1,2,3 then cycles

        text "front-hair"

        hbox:
            textbutton "Prev" action SetVariable('fronth', (fronth - 1) % 5) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('fronth', (fronth + 1) % 5) # allows 0,1,2,3 then cycles

        text "nose"

        hbox:
            textbutton "Prev" action SetVariable('nose', (nose - 1) % 5) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('nose', (nose + 1) % 5) # allows 0,1,2,3 then cycles

        text "Clothes"

        hbox:
            textbutton "Prev" action SetVariable('clothes', (clothes - 1) % 4) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('clothes', (clothes + 1) % 4) # allows 0,1,2,3 then cycles
    vbox:
        yalign 0.0
        xalign 0.15
        spacing 25

        vbox:
            text "skin"

        hbox:
            textbutton "Prev" action SetVariable('skin', (skin - 1) % 4) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('skin', (skin + 1) % 4) # allows 0,1,2,3 then cycles

        text "eye color"

        hbox:
            textbutton "Prev" action SetVariable('eyecol', (eyecol - 1) % 16) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('eyecol', (eyecol + 1) % 16) # allows 0,1,2,3 then cycles

        text "Iris"

        hbox:
            textbutton "Prev" action SetVariable('iris', (iris - 1) % 2) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('iris', (iris + 1) % 2) # allows 0,1,2,3 then cycles

        text "hair color"

        hbox:
            textbutton "Prev" action SetVariable('col', (col - 1) % 19) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('col', (col + 1) % 19) # allows 0,1,2,3 then cycles

        text "Eyebrows"

        hbox:
            textbutton "Prev" action SetVariable('eyeb', (eyeb - 1) % 8) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('eyeb', (eyeb + 1) % 8) # allows 0,1,2,3 then cycles

        text "Pose"

        hbox:
            textbutton "Prev" action SetVariable('pose', (pose - 1) % 7) # allows 0,1,2,3 then cycles
            textbutton "Next" action SetVariable('pose', (pose + 1) % 7) # allows 0,1,2,3 then cycles

    add "mcpose" zoom 2
    add "mc[pose]" xpos 1300 ypos 0
    textbutton "Done" xpos 1700 ypos 10 action Return()

As you can see above, this is what I'm using for selection. Nothing fancy. Blank background which turned out to be black so it worked perfectly in my favor there.

As for what you demonstrated... still learning it. Hoping to figure that out and how to fix it up. Thanks again for showing me that link.

Jac_Skulls
Newbie
Posts: 6
Joined: Thu Sep 30, 2021 5:17 pm
Deviantart: JACSKULLS
Contact:

Re: Issues with Alpha Mask for a Character Creaation Script

#6 Post by Jac_Skulls » Wed Oct 06, 2021 8:41 am

Here's the video of what I'm trying to do.

https://youtu.be/K5FYqOEcLMs

jeffster
Regular
Posts: 123
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Issues with Alpha Mask for a Character Creaation Script

#7 Post by jeffster » Wed Oct 06, 2021 9:01 am

How I understand "alpha mask" technique:

I make a head and ears.
head.png
head.png (1.58 KiB) Viewed 359 times
ears.png
ears.png (1.82 KiB) Viewed 359 times
Putting them together just like that isn't the result I want:
overlay.png
overlay.png (2.6 KiB) Viewed 359 times
So I put some foreground part on top of that:
foreground.png
foreground.png (1.71 KiB) Viewed 359 times
And it gives the proper result:
result.png
result.png (3.02 KiB) Viewed 359 times
Alpha mask is the same as placing some "subtraction" part over a layer. (To be precise, multiplication, actually.)

So instead of placing the foreground as additional layer, I use the foreground picture to make this mask:
alpha_mask1.png
alpha_mask1.png (4.02 KiB) Viewed 359 times
and use it as alpha mask for ears.

So you do that there: use hair of whatever shape you need to make alpha mask for ears. You might need more than one alpha mask with different hair shapes.

jeffster
Regular
Posts: 123
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Issues with Alpha Mask for a Character Creaation Script

#8 Post by jeffster » Wed Oct 06, 2021 9:11 am

PS. As an alternative to alpha masking, you can make 2 images for every hair type: one background, behind the head and the ears, and one foreground. (Just like I did in the beginning of the explanation in my previous post.)

Jac_Skulls
Newbie
Posts: 6
Joined: Thu Sep 30, 2021 5:17 pm
Deviantart: JACSKULLS
Contact:

Re: Issues with Alpha Mask for a Character Creaation Script

#9 Post by Jac_Skulls » Wed Oct 06, 2021 10:21 am

I really got to learn to add pictures on here. Lol. At least I'd be able to use Screen shots and all. Also, not bad the picture demonstration.

I actually already have a mask for it, similar to what you showed me. Only, I did it for the ears. I kept the part I wanted transparent while using the white background and aligned it with the front hair lines.

Actually, I would just edit the ears and do things like that but if I did that... I mean, the file it has is already at 750 mb. I scaled them all down so I could, too. But the biggest issue is the fact that I did not use the Alpha Masking and I edited each and every photos to fit each others. So in order to prevent too big of a file, I feel as though I'd have to use the Alpha Mask.

Plus, everything has numbers at the end of each images.

bh_0_000
clothes_0000

etc...

If it's not too much trouble, where and how do I put the codes inside the script/appearance file? (Appearance file I created to help me find them faster in case there's an issue, like this one Lol)

I keep trying to follow the guidance but I keep getting errors all around. So I deleted the the codes and decided to ask you if, and only if you want, if you could show me how you'd do the codes with the way I have it set up? Do I keep them away from Layered Image? Sorry if the question sounds stupid but I've been surprised before. Lol. :D

jeffster
Regular
Posts: 123
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Issues with Alpha Mask for a Character Creaation Script

#10 Post by jeffster » Wed Oct 06, 2021 10:38 am

To add pictures, you:

1. Check if you are writing in "Quick Reply" form.

In that case click the button below that says "Full Editor & Preview".

2. In Full Editor Reply form look below the text area and find the button that says "Attachments".

Click "Attachments" and upload the files. Make sure they aren't too large (100 kb or a bit more should be enough for a usual jpg of decent quality).

When you loaded files you can see there buttons "Place inline". So you can put them in your text.

3. Or you can use picture button above the text area, and link pictures from other sites I think.

About your request for some code, I think you are doing alright, keep improving your own code.

For now I see just one kinda error: place

Code: Select all

label start:
after the screen and the block of defaults:

Code: Select all

default pose = 0
#...
screen maker():
    vbox:
#...
# And after those initializations you actually use that screen and variables:
label start:
    show screen maker
because they are initializations, and label start marks what you actually start to show on the monitor, "the game script".

Also, if you need help with error messages, as the forum hints say, print the error message here.

Usually the most important (and often sufficient) is a part like this:

Code: Select all

  File "game/Appearance.rpy", line 331, in <module>
    attribute ear default AlphaMask("Sprites/full/Alpha_0_[pose]_[hair].png")
TypeError: __init__() takes exactly 3 arguments (2 given)

Jac_Skulls
Newbie
Posts: 6
Joined: Thu Sep 30, 2021 5:17 pm
Deviantart: JACSKULLS
Contact:

Re: Issues with Alpha Mask for a Character Creaation Script

#11 Post by Jac_Skulls » Thu Oct 07, 2021 7:48 am

Alright. Thanks.

Post Reply

Who is online

Users browsing this forum: GetOutOfMyLab, Google [Bot], Ocelot