Script to render and export text to an image with alpha in the game engine - is this possible?

Not entirely sure if the BGE is capable of this, or if it is possible to write something in python to do it. I know there’s a “screenshot” actuator/feature and I wanted to see if I could get access to that, and for the screenshot being saved to replace the “sky” areas of the image with just alpha so that it only renders the objects in the scene as a png. And save that temporarily. As an image to a directory while the game is running. Can this be done?

| PYTHON | function for taking a screen-shot is -

 render.makeScreenshot()

It’s literally the same function for the actuator, so nothing special there.

For blocking out just the sky areas, you could make the object invisible when you activate the screen-shot function.

Here is this script :arrow_down: we state if FUNCTION (which is the control key activating the function) if clicked -> makes any objects in the scene with the word SKY invisible (you could modify the script to search for a certain property instead of a object name)

| FULL SCRIPT |

from bge import render

def main(cont):
    
    if FUNCTION:
        render.makeScreenshot()
        for obj in cont.owner.scene.objects:
            if "SKY" in obj.name:
                obj.visible = False
    else:
        for obj in cont.owner.scene.objects:
            if "SKY" in obj.name:
                obj.visible = False
1 Like

Thank you, but this only answers half of my question. I worded the OP badly, I mean I want to render everything BUT the sky and have the sky = alpha so the image output is just a transparent PNG of my foreground objects. Is there a way to do that?

| PYTHON | should be able to do that - I’m just not smart enough to know the answer tho :smile:

If real-time game-play in the Game Engine isn’t mandatory - You could just open up a in-game taken image in almost any 2D Image Editor like GIMP & manually select the solid sky-color & set it to the desired alpha amount. Or you could composite the taken image in Blender it-self - but that’s ways more work :slight_smile:

As @RPaladin suggested making the sky object invisible does the trick. As far as i know BGE will render an alpha background if you use the .png extension. But you need to use 2 functions in combination with an property, else you can’t hide the sky.

So here is a blend that hides the sky, take a screenshot, and restores the sky. Downside is you got no sky for 1 frame (sky will flicker while taken a screenshot( you can’t simply hide/unhide objects in one frame)).

the picture made:

the .blend:
alpha screenshot.blend (508.0 KB)

1 Like

Excuse me for cutting-in, but I think this function might help with that problem -

logic.NextFrame()

It forces Blender to continue to the next Frame-Time whether it’s complete or not.

That could work, but the problem is… Can you skipp a frame while ingame, it can mess up other scripts or things that are executed.

if you just use the scene to make a screenshot then yes that could be used else i suggest to use a property and let the game engine go to the next frame on it’s own

#edit

also you would still have the issue of a flickering background, only thing is you command it to go to the next frame instantly, still it’s not possible to hide and unhide something in 1 frame, so for the flicker you speed it up a tiny bit but the problem or rather flicker of the sky still exists.

1 Like

Thank you guys so much! Going to implement these right away in my project today :smile:

1 Like

This is so weird! o_O
It was bugging me to much that there wasn’t a valid answer for what you where asking for (Transparent Background in taking image with Screen-Shot function)

& do believe I finally found the solution.
1st, this function only seems to work when the screen-shot is taken with the Python function :point_down: & not the screen-shot (Game) actuator (Dev problem I guess)

render.makeScreenshot()

Now this is the SUPER cool thing. You can choose whether you want the world background RGB color showing (or) fully transparent.
How do you do this?

  1. Take a screen-shot with the Python function explained above.
  2. On the object (Like a simple plane) that you want the screen-shot image to be displayed on :point_down:
    [1] For Sky Color Included In Image: Enable texture Alpha for the Material Texture.
    [2] For Transparent Background: Enable Material Transparency + Enable texture Alpha for the Material Texture.

Downloaded the blend file and while it does take a png screenshot, it still doesn’t have the world as alpha. But yours has alpha though…ahh this is weird. I’m not sure what I’m doing wrong
screenshot
This was mine

Oh right, Sorry i made it in blender, didn’t even look at the topic tags my bad.

Upbge indeed does not render alpha background, i believe that this is an old bug that was present 3 years ago as well, they fixed it then. I guess the bug has returned for upbge.

Nothing we can do about it, at this point they are not working on upbge lower then 3.0 anymore, maybe @aWeirdOwl can confirm this.

ok. I should have mentioned, I’m using the UPBGE 0.2.4b Windows 64, for blender 2.79b. I downloaded this assuming it was the latest stable release for UPBGE. is there a more up-to-date version that I can download?

Ok, I still see that you don’t get what I’m saying so I’ll post a blend file & screen-shot so you can get a better idea of what I’m talking about :slight_smile: (Sorry if I was unclear)

Left 3D View-Port shows 3 objects with 3 different materials & material-textures but with the same image-texture.
1st object has nothing to it except a UV to make the image visible.
2nd object has a UV + Alpha enabled in the texture-block in the material panel.
3rd object has UV + Alpha enabled in the texture-block in the material panel + transparency enabled in the material panel.

My blend file so you can try it out your-self.
I also don’t now if UPBGE has a broken screen-shot Python function but I guess we’ll have to see :slight_smile:
Alpha In Screen-Shot.zip (138.2 KB)

image
Yep, I think it’s a bug in the version of blender I’m using. As soon as I started the player, the screenshot changed right back to not having alpha.
Anyways, thank you guys for your help so far…I’m hoping they will fix this in future upbge versions

1 Like

No, well upbge 3.0 is out and is based on blender 2.8 (evee), but this has a lot of limitation (lots of things you can’t do at the moment), they think it will atleast take a year or 2 until it really can be called stable engine. There are only 2 people working to build upbge 3.0 and even they don’t work that much on it, so it’s a slow process.

But depending on what you really need from upbge you can simply go back to bge (where it works)

Actually I think it’s 3 people -

  1. youle
  2. ExxiL
  3. BluePrintRandom (Occasionally)
1 Like

I test / report bugs / try to work on it / am not super great at it.

UPBGE 0.3.0 is mostly the work of youle and then loki joined on,

they recently made it so you can use all the render engines - not just eevee - even in standalone I think.

1 Like