detecting how the application is started

hi,
i’m trying to detect how the application is run.

  • run from python
  • p3d run locally
  • p3d run from webhost
    because of a error i am experiencing when the application is run in the webhost (loading a non exisiting prc file seems to block it forever). also i’d need to get this information before directstart is called.
    is there a way to figure this out? i’ve already looked into the apprunnner code, but i didnt find a function that gives me this information.

thanks

i’ve just come up with a method, but i’m not sure how consistent it is:

RUNTYPE = None
if len(sys.argv) > 0:
  if os.path.splitext(sys.argv[0])[1] == '.py':
    RUNTYPE = 'python'
  if os.path.splitext(sys.argv[0])[1] == '.p3d':
    RUNTYPE = 'local'
else:
  RUNTYPE = 'website'

it assumes that when run from a website there are no values in argv, is this allways true?

also, is there a way to detect the url hosting the p3d file?

You can check if base.appRunner is None. If so, it’s ran directly, if not, it’s ran from a .p3d file.

I think you can check if base.appRunner.dom is None to see if it is ran from a browser or not.

thanks, that got me into the right direction, i can do it this way (because i need it before directstart):

from direct.showbase import AppRunnerGlobal
if AppRunnerGlobal.appRunner is None:
  RUNTYPE = 'python'
else:
  if AppRunnerGlobal.appRunner.dom:
    RUNTYPE = 'website'
  else:
    RUNTYPE = 'local'

And, for completeness, you can get the hosting URL from appRunner.dom.location.

Tell me more about the nonexistent prc file problem, though. It really just hangs? What function are you using to read it?

David

i am trying to load a prc file before directstart. this does not make sense for a p3d-web-application, but i want the same p3d file to be able to run directly, being configurable trough a config.prc in the same directory as the p3d file.

print "import"
from panda3d.core import *
print "load"
loadPrcFile('test.prc')
print "start"
from direct.directbase import DirectStart
print "done"

only prints to the log, when the test.prc does not exist:

import
load

(the javascript access also doesnt work well for me, as described in the other thread)

Hmm, this one I’m unable to duplicate. I get:

import
load
:util(error): Unable to open test.prc
start
DirectStart: Starting the game.

Maybe it’s a problem that’s been recently fixed? I am using my own build, not the official release, which lags behind a bit. I can try it again with the official release.

Though note that this won’t necessarily work anyway–the “current directory” is changed when running a p3d file to be the directory ~/Library/Caches/Panda3D/start, not the same directory that held the p3d file. This is intended to make the runtime environment more predictable. (You can add the -c keep_user_env=1 flag to keep the current directory the same as it was when the user launched the p3d file, which will probably be the user’s home directory. Or you can allow the user to specify the location of the prc file on the command line.)

David

i know that the working dir’s are changed. this is the reason i needed to figure out what way the p3d is called.

this is the current code i have to get the folder names.

import sys, os

from direct.showbase import AppRunnerGlobal

if AppRunnerGlobal.appRunner is None:
  RUNTYPE = 'python'
else:
  print "dom", AppRunnerGlobal.appRunner.dom
  if AppRunnerGlobal.appRunner.dom:
    RUNTYPE = 'website'
  else:
    RUNTYPE = 'local'

# location of the main.py
mainLocation = None
# location where the p3d resides if it is run from a p3d file (url or path)
p3dLocation = None
if RUNTYPE in ['python']:
  mainLocation = '%s/' % os.path.dirname(sys.argv[0])
if RUNTYPE in ['local']:
  mainLocation = '/mf/'
  p3dLocation = '%s/' % os.path.dirname(sys.argv[0])
  print "location", AppRunnerGlobal.appRunner.dom.location
if RUNTYPE in ['website']:
  mainLocation = '/mf/'
  p3dLocation = AppRunnerGlobal.appRunner.dom.location.href

Why don’t you use $MAIN_DIR for determining the main directory?