Open File Through BGE

Hello all,

I’m putting together a portfolio of my hobbies, which ranges through a variety of mediums. I thought a good way to do this was showcase and link it together in an intractable 3D world, ie in a ‘game’.

While I can use the game engine to, for example, run movie files inside the BGE itself, it’s not the best way to showcase that type of file, or any (especially since sound has to be encoded into mono parts, etc.). Basically, what I want is a way of making a hyperlink through the running BGE.

In short, if I click on a screen button/a property is set to the file name and folder location, I want the BGE to execute a script that will open the file in it’s default application: If the movie file icon/object is clicked/activated inside the game engine, the script would open the file it is indicating and, say, quicktime would open with that movie clip (from a subfolder the game file is in)

Is this even remotely possible? I’ve never heard of a script being able to do this… (The player certainly couldn’t be fullscreen, otherwise one couldn’t easily switch between the ‘game’ and the files opened through it).

Edit: The idea of showcasing my hobbies would also include showcasing mini games I’ve made, which means it would have to be able open another game window: Just an example of how it really HAS to open files outside of the application it is running through, or it won’t work at all

I guess blender is not the right tool for that yet, try demicron wirefusion, they have a free version! the site is

I believe it can be done with python. If you’re planning for it to be used on a Windows system you can open files in whatever the user has set as the default program for that file type this way:

import os

os.startfile("path	o\your\file.extension")

For Macs and Unix systems you can use os.system (note, I don’t have a mac so can’t test this code) :

import os

# Checks if the user is running windows
if == "nt":

os.startfile("path	o\your\file.extension")

# If the user is running a Mac OS
elif == "mac":

os.system('open "path	o\your\file.extension"')

For Windows you can also use os.system(‘start “path o\your\file.extension”’), I can’t seem to get this way working - so the mac code might also not work properly too. I quick google suggests something about various quoting conventions used by different shells. The better method that avoids this problem and should work with most systems is this (the subprocess module is recommended over the os.system module):

import subprocess

File = "path	o\your\file.extension"
# You have to specify the program you want to run the file with, 
OpenWith = r"path	o\your\program.exe"

subprocess.Popen([OpenWith, File])

There are also ways to open files using which gets python to wait until the called file is completed. The subprocess module has many different ways to open files/programs and pass parameters to opened program, worth a look to find the right setup for you. Any issues let us know.

Hope this helps.

Awesome, and thanks for the quick reply. I have a mac and pc, so I can try to see if I get it working on both (haven’t had the chance to just yet)

If I export it as a runtime.exe, place this exe (and the dlls) and the folder with the files to which it would be linked, can I just use the path (“folder_same_as_exe/nested_folder_name/q/w/e/r/t/y”)
Or essentially, get it to look in the current folder and further in- otherwise sharing the presentation won’t work if the whole folder is moved around.

I try it out myself and see what happens :slight_smile:
I’ve never seen os script command, but makes perfect sense considering where python came from… Is there a way to search for a specific program, confirming it is there or just (hence if someone doesn’t have quicktime, the script will then search for VLC etc.)? At that point, I’d just have to hope others always to decide to place their programs under the normal program folder to search for them

Thanks again!
I can’t deny blender isn’t the right tool, but it’s something I already work with and I may as well benefit from the widest capabilities it has. But thanks for the reference

you can use the os.path.exists() to check if a file exists, so you could run through all the common types of video player and check their default install paths. Something like:

import os.path

vlc = r'path	o\program\install_directory\vlc.exe'
quicktime = r'path	o\program\install_directory\quicktime.exe'
divx = r'path	o\program\install_directory\divx.exe'

if os.path.exists(vlc):
OpenWith = vlc 
elif os.path.exists(quicktime):
OpenWith = quicktime 
elif os.path.exists(divx):
OpenWith = divx 

There’s many downfalls in this solution, but it’s the only one I can think of at the moment. Problem is that different OS’s handle paths differently and have different install directories, also you would need some code to find out what the default drive is. Not everyone has C:\ as their main drive for installing programs to. There is a few methods to scan the registry using python to check for installed programs, but I’ve only seen examples for getting windows programs and this requires extra modules not included as standard with python. If I get chance I’ll have a play and see if I can’t come up with a better way to check various video players.

This is really interesting for a potential project I had in mind. I’d ruled out using Blender for it because I didn’t think this was possible.

Would it be possible to load all files within a directory without specifying filenames? For a dynamic photo gallery for example?

Yes, the os module as a couple of ways for collecting files. You can use this if all the files are in one folder (no nested folders):

import os

Files = os.listdir('path	o\folder\with\files') # Returns a list of all files in the directory

for f in Files:
print f 

A comprehensive list, including subfolders and files in them, can be done with the os.walk() function:

import os
path = "Directory\you\with	o\walk	hrough"

for (path, dirs, files) in os.walk(path):
print path
print dirs
print files
print "----"

Hope this helps!

Wow this is fantastic. Thanks for the examples :smiley:

Wow, battery you’re amazing! I’m curious, do you go through training videos that cover things like this, or is it jsut you’re clearly wider knowledge of python programming that you come up with this?

Anyways, I indeed got it to work opening a file with a specific location (both exported runtime and in .blend file)…
…but as I speculated, I couldn’t come up with a way to make it look only in the folder the file is located in.

In my specific case, I was intending to eventually burn the whole presentation to a CD, which then it would have a non-changing drive and path location (or does the d drive or whichever it might be change per computer? If that’s the case, then that options to shot as well)…but beside that, how else could you get it to be movable between computers? Okay, you could do search methods for drivers, but there must be a way to access the “activated” folder, the one the .exe/.blend is in. I followed one tut some time ago that had the realtime video inside the game engine, connected to a video file, and the way that was set up somehow all you had to do was put in the path of the sub folders…

I’ll look that up again, see how that script was set-up, because at the time I probably didn’t understand what even import GameLogic meant.

No worries. I started out in python (I found Swaroop’s A Byte Of Python really helpful) and toying with pygame for a bit then moved to blender. My python’s a little rusty but I can still remember some of the useful modules in the python standard library that aren’t overly used in the BGE. You can more or less interact with the whole PC with python, not just the game engine. I imagine there aren’t many python modules you can’t use in the BGE knowing a few of these can sometimes add new functionality to games.

CD drive letters will also change from PC to PC (depending on the users set up). However there are ways to access the folder that the .blend/.exe is running from - the current working directory. Then you could access and folders or files in the current working directory without knowing the full path to the directory. I’ve never tried it running from a CD but it should work.

import os

# Blender's game logic module has a function that will do it
# Where // means the current working directory and / can be used as a directory separator 
PathToFiles = GameLogic.expandPath("//FolderWithFiles")

# get the file to run
ToRun = PathToFiles + "\FileName.extension"
# and run it
os.startfile("ToRun") # Or whatever method you used

# If you didn't know what files are there you can also easily create a list 
# of the files in the folder
Files = os.listdir(PathToFiles)

# Then you can loop through the list, or some bit of code to search for particular files
# and then run one
os.startfile(PathToFiles + "\\" + Files[x])

Alternatively, the os module can get the current working directory as well:

CurrentDirectory = os.getcwd()
ToRun = CurrentDirectory + "\FolderWithFiles\FileName.extension"

Note: for these scripts to work you have to access the .blend directly from the folder its saved in and not opening blender then hitting open as the current working directory is based on where the script is being run from.

Edit: You could use GameLogic.expandPath and os.listdir() to get any videos/pics placed in a subfolder then it’s just a bit more code to get a video texture to render them in the BGE and update dynamically.

Hope this Helps!

I was going to ask for what possible reason the script would work on one pc and not another, but then I realized I didn’t pack in the script, which might have been intelligent :slight_smile:

But that other pc I tried it on got me wondering…because it was dual monitor :slight_smile:
Since you can access a computer’s settings or processes through python, is there by any abstract sort of way you can check/access a secondary monitor?
Just to eliminate possible misinterpretation, I’m thinking of a multiplayer game that gives the option to split one screen for both players (who interact in a common scene) or assign one camera/player per monitor; setting camera #2 as the additional screen.

Just an interesting and potentially incredible possibility :slight_smile: any way this would be possible through python you know?

Hmm, an interesting question, and if pulled off could be quite an impressive effect. I don’t know of any games that make use of dual monitors. And this is made all the harder as I don’t have dual monitors so I’m not too up on how they work and I can’t test any ideas.

As far as I’m aware (correct me if I’m wrong) the two monitors are controlled by either the graphics card drivers or some 3rd party program. Getting one blender file to open across two monitors would be hard. Is it possible to push the screen resolution so it forces blender across both monitors?

Assuming that accessing the graphics card directly would be very difficult I can think of one rather cack-handed method. Basically the first blend opens in the first monitor and then with python either accessing command lines or with the subprocess module could possibly open up a second blend file sending a command to the program controlling the two monitors telling it to open the second blend in the second monitor. But then I don’t think you can control two active programs at once - ie, the two blends would be fighting for the keyboard and mouse. Also they would have to be kept in sync in someway.

Can’t find much on google, Gonna sleep on this one and try a few things! Anyone else got any ideas?

Well, I’ve been lookign through it a little, and it seems there is another thread that covers the range of possibilities and requirements for it… but it’s dependant more on the set-up and way the dual monitors is set-up (which to be honest, I don’t think anyone can be too surprised about)

Form that thread, I’ve gathered that you can ‘simply’ just extend the game window across the dual screens or set-up it up with a sort of added hardware that will make the computer consider both monitors as one (which would mean you could have fullscreen, where in the other you couldn’t)

Might be too much of a limitation in set-up, and it’s not like it would be applicable for many people.
I do have ‘access’ to a dual screen, but not at home and so my time to work and test it would be limited. Well, it was indeed a thought