Character Generator: Merge .png files into one and use for Face and Walk of MC

Please help. :LZSteary: I've tried to look this up online, but to limited avail because I don't know JavaScript deeply enough. I know a lot about math, a good deal about being careful with logic, and just enough coding to sound like I know what I'm talking about, but otherwise I'm probably more a danger to myself than anything (just as an FYI). I'm pretty sure in RMMV I need a script that uses nodejs to function. I have a structure set up currently where the player clicks on an image from a menu to select their base character template, then they click an image for their costume, they click an image for their hairstyle, and then get one last redirect to select an image of said hairstyle in the color they desire. Ultimately there are three .png file format layers to combine into the final Face image and Walk sprite sheet for the MC. I'm not doing side-view battlers. I guess what I would like to do to start off is just find out, if possible, how to use scripting to set up the identities of image 1 face and image 1 walk for use in the merges as soon as the first selection is made, then do the same for the image 2's. then do the same for the image 3's at the end of the selection process so that one script can fire up node-canvas and merge each identified set of three images in the proper order: base, then outfit, then hair into .png files with the MC name as the filename and store the face one in img/faces and the walk one in img/characters so the game will reference the player created character's images for the rest of the game.

This site depicts a large part of what I'm trying to do over-simplistically: https://npm.runkit.com/merge-images

It says, "And that would update the img element to show this image:" at one point on that site, but I don't know how that translates to something usable in MV. I have the following script to merge the first two face images set up after the user makes the first two selections based on my understanding of what the site said, but don't know how to test it/see/use the result in MV (if I'm even up the right tree):

Spoiler: Code
const mergeImages = require('merge-images');

const Canvas = require('canvas');

mergeImages(['C:\Users\John\OneDrive\HDILTPV4\img\faces\MFFace1PV0.png',
'C:\Users\John\OneDrive\HDILTPV4\img\faces\Acrobat_Face.png'], {

Canvas: Canvas

})

.then(b64 => document.querySelector('img').src = b64);

// data:image/png;base64,iVBORw0KGgoAA...

If you can help me pick up the pieces I'm missing to learn this bit of JavaScript functionality, I'd greatly appreciate it. Thank you for your time, attention, consideration, and any and all assistance.

Sincerely,
why don't you just create a new bitmap and copy the layers onto it, without clearing the existing contents? No node.js required.
@Shaz I love it when something sounds super easy. ☺️ How do I get RMMV to create the bitmap and copy the layers as prescribed?

@Shaz EDIT: So that you know I don't just expect handholding and am working your suggestion on my end, I looked into "creating a bitmap" on this forum. Here's what I found:

Spoiler: Code
Bitmap.prototype._createCanvas = function(width, height){
this.__canvas = this.__canvas || document.createElement('canvas');
this.__context = this.__canvas.getContext('2d');

this.__canvas.width = Math.max(width || 0, 1);
this.__canvas.height = Math.max(height || 0, 1);

if(this._image){
var w = Math.max(this._image.width || 0, 1);
var h = Math.max(this._image.height || 0, 1);
this.__canvas.width = w;
this.__canvas.height = h;
this._createBaseTexture(this._canvas);

this.__context.drawImage(this._image, 0, 0);
}

this._setDirty();
};

So...I'm guessing that's the empty bitmap? Then I draw the desired images to the canvas one at a time. I think I can figure that out, too, but...when it's done, how do I reference it to make it the MC Face sheet/walk sprite sheet? That's probably the part I'm lost on the most. Am I getting warmer?

EDIT 2: Okay. I see in the code here the drawImage line. Is this.image the image selected for the current event? If so, I've got the draw part down. What's the setDirty? Is that the reference I need? Will keep plugging. 🙂
You are going too deep. That is the bitmap class. You don't need to create or change the class - you just need to use the one that's already there.

Look at the rpg_sprites.js file and see what's in there. Do a search for Bitmap.new and see what that comes up with. Stay away from rpg_core.js and any Pixi files. That's too low level for what you're after.


Edit ... have you actually looked to see if something like this already exists? I have a feeling there may already be something similar.
@Shaz Thanks for the pointers. Will do! 😉

EDIT: Okay. I found this:

Sprite_Character.prototype.setCharacterBitmap = function() {
this.bitmap = ImageManager.loadCharacter(this._characterName);
this._isBigCharacter = ImageManager.isBigCharacter(this._characterName);
};

Ignoring the BigCharacter, I gather this says the bitmap being set to be referenced for the Character Sprite is pulled from this._characterName and loaded for the character's visual representation by the Image Manager. Other lines not copied here referencing updates to frames as said sprite animates or changes state. So if the MC is a blank slate at the start of the game, I can do this at each selection to build the MC look for the walk sprite?

Sprite_Character.prototype.setCharacterBitmap = function() {
this.bitmap = this.__context.drawImage(this._image, 0, 0);
}

EDIT 2: Searching new Bitmap as suggested led me to this:

var bitmap = new Bitmap(20, 20);

So maybe the first selection would have the script:

Sprite_Character.prototype.setCharacterBitmap = function() {
var bitmap = new Bitmap(128, 128); //I'm using PV Games sprites. They're tall.
this.bitmap = this.__context.drawImage(this._image, 0, 0);
}

and the next two selections would update that without the new Bitmap line, where the third and final selection would wrap up with:

var sprite = new Sprite(bitmap);
this._Princess = sprite;
SceneManager._scene.addChild(sprite);

Princess is the filename my project seeks for the MC.

EDIT 3: @Shaz Before I say anything else, I want you to know I am truly appreciative for your help and quick responses. 👍👍
Edit ... have you actually looked to see if something like this already exists? I have a feeling there may already be something similar.

Spoiler: Response/Elaboration
That was how I started. My OP had the code I had found and was asking about. I think I have a better understanding to do a better job finding something, but as you can probably see, I still need some help understanding/learning JS. Hence, the post in this forum. 🤣 Concerning something existing that comes close, I was using SRDude's Character Creator EX plugin to give the MC up to 477,576 unique looks, but wanted the game to know things like eye color, hair color, skin color, age, and type of outfit selected so that NPCs could respond to player choices for flavor in game, and I had no way of knowing what the player picked while in the SRDude character creator or how to access the end result face/walk images it produces to read that information and get that interest for the player. I contacted SRDude on it. No reply. I had an inspiration to make my own seriously dumbed down version of a character creator that would efficiently let the player still create 71,040 of those looks and let the game still know the key features chosen by the player. I set up the menus and can select the choices. I just need to be able to layer corresponding images onto clear bitmaps as you suggested, then make said bitmaps the targets pointed to by RMMV for the MC face and walk (if not already working in the bitmaps RMMV targets for this). I tried to Frankenstein the code snippets I found into something workable and put the scripts in the project to see if I can get it to work just to layer the first 2 selections and I'm getting a "cannot read property of drawImage undefined" error. I figured maybe the this._image needed to be defined, so I tried setting

this._image = 'C:\Users\John\OneDrive\HDILTPV4\img\faces\MFFace1PV0.png'

No luck. I tried removing the ' ' and that gives an "unexpected :" error. There are more trees to shake, and I definitely will.

Thanks for all the help and I hope to update you again soon with good news of progress on any/all of this. ☺️ In the meantime, any tidbit about learning more JS is appreciated.

EDIT 4: I contacted Backlash404 because they made a character creator for PV Games that uses the exact same layer images I'm using to do the exact same production of composite images I'm trying to do that then get stored as .png files to a target directory using a standard naming convention (which, again, is just what I'm attempting to do...save to the project img/faces and img/characters files named Princess.png). The thing is, 404's character creator works using JS completely outside of RMMV, so they were not able to help me other than to point me to this: https://stackoverflow.com/questions/6765370/merge-image-using-javascript

It seemed like a step backward, but I tried to put the code into my program anyway. I get a "Cannot read property 'getContext' of null" error. IDK. Maybe Jesse - PVGames can help because they have announced a new character generator launching soon, but that's a stand-alone program, too, as I understand it, so... I'm still going at it one way or another. I know it can't be that hard, and when I figure it out it's going to be such a relief.
@mathmaster74 actually I don't really have clear ideas about what you need. But in case you didn't know, you could attach sprite as a children to other sprite. I create bust picture sprite named 'base body' so I could display it during message and on main menu and I add clothes sprite and face emotion sprite as children to 'base body'. This way, when I moved my 'base body' sprite, the cloth and face will automatically follow the base body position. I think you could do the same with your body sprite parts. My rough guess is that you'll need to inherit the way RM draw chara animation (e.g. using repeating horizontal three tiles) to your children parts so when you're updating your base sprite, you just need to call update to your children sprites.

And the method addChildrenAt() is handy to use, this way you can decide which layer is lower and which one is higher. Pretty handy, for example to draw back hair behind the helmet and then front hair on top of the two.

I hope this helps somehow
@Shaz First of all, you are a godsend. I would still be nowhere on this without your help, so know that I am in your debt. Newest update: Pounding the pavement has led me here: using RMMV's ImageManager class, I use the Body selection of the player to create two bitmaps which, for clarity, I'll call bitmapBodyFace and bitmapBodyWalk. Repeat on the Outfit selection to create those bitmaps. Repeat on the Hair selection to create those bitmaps and then:

Spoiler: Code
var bitmapFace = new Bitmap(576, 288); //Going to use the whole 8 face layout for expressions.
bitmap.blt(bitmapBodyFace, 1, 1, 576, 288, 0, 0);
bitmap.blt(bitmapOutfitFace, 1, 1, 576, 288, 0, 0);
bitmap.blt(bitmapHairFace, 1, 1, 576, 288, 0, 0);
$this_class.getFilename = function() {
return 'PrincessFace.png';
};
var bitmapWalk = new Bitmap(1536, 1024); //Going to use the standard walk sprite sheet size so RMMV reads properly.
bitmap.blt(bitmapBodyWalk, 1, 1, 1536, 1024, 0, 0);
bitmap.blt(bitmapOutfitWalk, 1, 1, 1536, 1024, 0, 0);
bitmap.blt(bitmapFaceWalk, 1, 1, 1536, 1024, 0, 0);
$this_class.getFilename = function() {
return 'PrincessWalk.png';
};

From what I've gathered, this will do what you said about creating empty bitmaps and layer in the body, then face, then hair and even save out the files with the proper naming convention and file type extension. Please confirm if this is correct, or still misguided thinking on my part. If correct, I still need to work out how ImageManager makes the selected image bitmaps to layer into the buildups at the beginning stages, and how to direct the completed files to their target folders. I suspect the former is relatively easy. I hope the latter is not too difficult either. Still hard at work.

@Heartbreak61 Thank you for that. I'm really finding out just how much of all this I don't know. For now, I'm going to try to stay true to the path I'm on. I think I'm close. I will keep your suggestion in mind as an alternative path should this one pan out empty for me.
Why do you want to save out the files? What if two people play your game and set the characters up differently? Will you be overwriting the first player's character with the second player's character?

Are bitmapFace and bitmapWalk new functions that you have created, or existing ones you're changing? If they're existing functions, do you want that logic to run for ALL sprites?

I think you would be better of saving the settings against $gameActor or even $gameSystem if it's only a single character they're changing, then calling the functions to build the character each time the game is started or loaded from save, and just keeping the image in memory rather than saving it out.
@Shaz

Thank you for your inquiries. 🙂

Why do you want to save out the files?

Because all I know (and still fully understand) about setting the MC Face, Walk, and SV Battler Sprites (the last of which, I'm not using) comes from working in the RMMV editor's database Actors tab, which looks for each file very specifically by its one folder in the project hierarchy only. Otherwise, I've used the change actor images event command which works identically, the event image box (which works identically), and the Set Movement Route event command with the Change Image movement command (which...well...you get it). All of my successful RMMV image setting and replacing knowledge comes from working in the editor. All of it. I have set up my project to look for the main character's face image in every Show Text event where they talk as Princess(0). Every show text event looks for a properly formatted face file with the name Princess.png in the img/faces folder and uses the top left expression at present. I do know that you can do things like use scripting to set a variable to an actor's name in an event command such as:
◆Control Variables:#0178 = $gameActors.actor(1).name()
I don't know all of the ins and outs of what's available with $gameActor or $gameSystem for use in coding, nor proper syntax. I need to learn. I have picked up bits and pieces by watching Yanfly tips and tricks videos and such, and I know the power of scripts/original code/programming. What is the proper use of $gameActors.actor(1) for face and walk images? Is there an equivalent to the change actor images event command I could use to script the final bitmaps in place? That would be amazingly helpful.

What if two people play your game and set the characters up differently? Will you be overwriting the first player's character with the second player's character?

I assumed save files took care of keeping track of the images used for the MC in said save. It could very well be a false assumption. I don't know what happens if one person saves and another person saves a different file and then the first person comes back to play the original save file. I guess if the game truly always pointed to the folder system the way I've been thinking, you'd get undesired results. Hence, I guess I need to better understand how RMMV keeps different images for the same MC in different playthroughs stored, and how it references the proper image for the save file being loaded. I know it's possible to have multiple save files with different MC images, because I've had that in the past with other people's character creators, but I don't know how they accomplished that. 🙁

Are bitmapFace and bitmapWalk new functions that you have created, or existing ones you're changing?

They are variables used to store blank bitmaps. var bitmapFace = new Bitmap(576, 288); There is no preceding code showing the context under which these lines would fall because I don't know proper syntax to start the script or to use the ImageManager class to create the requisite image bitmaps that get layered into bitmapFace and bitmapWalk. That's why I just said in plain English "using RMMV's ImageManager class..." like it's the lead in to the actual code lines I tried to write. What I am hoping is that I've even written the lines of code I presented in the correct syntax so that those parts of the process function properly when the rest is written around them. I need to get some part of the process right first. I prefer to start at the beginning or work backward from the end, but in this case, it seems like what's been easiest to figure out, if I even have that right, is the code that goes in the middle. Do I even have that right? :blush:

If they're existing functions, do you want that logic to run for ALL sprites?

Answered at this point, but...to be clear: All I want is all I ever wanted. One custom composite walk image and one custom composite face image built by the player and chosen in three steps so that the partial images layer together first body, then outfit, then hair to produce the MC look for the rest of the game. No other sprites. This will allow the player to enjoy "playing dress up" and "choosing a disguise" for their princess character in my medieval otome dating sim RPG. By controlling the selection process with a character creator I helped build, I am able to know what images they select so that the suitors can react to their appearance accordingly for flavor. This isn't a new project. It's been three years in the making, and I've had to restart it from scratch twice. It's been a true labor of love...labor at this point becoming the operative word. I may be polishing it early, but things of late have had it really coming together and shaping up and I'm getting excited for it to be done. I cannot apologize enough if my zeal comes off reading the wrong way.

I think you would be better of saving the settings against $gameActor or even $gameSystem if it's only a single character they're changing, then calling the functions to build the character each time the game is started or loaded from save, and just keeping the image in memory rather than saving it out.

Again, that sounds awesome. :LZSexcite: It is only the MC, and yes, my desire is for MC images of prior saves to return properly, not be overwritten.

Thank you, Shaz. With a depth of gratitude from the core of my heart...for your guidance in this labyrinth.

Sincerely,
Can anyone help me do as Shaz recommends?

I think you would be better of saving the settings against $gameActor or even $gameSystem if it's only a single character they're changing, then calling the functions to build the character each time the game is started or loaded from save, and just keeping the image in memory rather than saving it out.

My best guess about the "buildup steps" of the bitmap images hasn't changed since I formulated this:

Spoiler: Code?
var bitmapFace = new Bitmap(576, 288); //Going to use the whole 8 face layout for expressions.
bitmap.blt(bitmapBodyFace, 1, 1, 576, 288, 0, 0);
bitmap.blt(bitmapOutfitFace, 1, 1, 576, 288, 0, 0);
bitmap.blt(bitmapHairFace, 1, 1, 576, 288, 0, 0);
$this_class.getFilename = function() {
return 'PrincessFace.png';
};
var bitmapWalk = new Bitmap(1536, 1024); //Going to use the standard walk sprite sheet size so RMMV reads properly.
bitmap.blt(bitmapBodyWalk, 1, 1, 1536, 1024, 0, 0);
bitmap.blt(bitmapOutfitWalk, 1, 1, 1536, 1024, 0, 0);
bitmap.blt(bitmapFaceWalk, 1, 1, 1536, 1024, 0, 0);
$this_class.getFilename = function() {
return 'PrincessWalk.png';
};

from several pointers I received that led me to this point (except that I would likely remove the getFilename function lines since the save out doesn't need to be a "hard" save). Without said pointers, by myself I had a menu framework of clickable images that form a character creation system that...has no way to save the result...even simply to active memory. Thank you for any and all assistance.

Sincerely,

Original thread: https://forums.rpgmakerweb.com/threads/character-generator-merge-png-files-into-one-and-use-for-face-and-walk-of-mc.109265/