pickle and p3d

Hello forum,

Apologies, I’m fairly new to panda.
I’m having trouble importing a pickled file when packing to a .p3d file.

The following function runs ok from python, but fails when running from the executable after creating a .p3d file.

I’ve included the error messages after the code listing.

Any help would be appreciated.

PaulM

def unpickle_view(filename):
“”“unpickles settings exported from rhino”""
# Get the location of the ‘py’ file I’m running:
mydir = os.path.abspath(sys.path[0])
# Convert that to panda’s unix-style notation.
mydir = Filename.fromOsSpecific(mydir).getFullpath()
# Now load the pkl:
infile = open(mydir + “/” + filename + ‘_views.pkl’, ‘rb’)
data = pickle.load(infile)
#print out the data
#pprint.pprint(data)
infile.close()
#do some type conversion
data[“camera_location”] = Lst2Vec3(data[“camera_location”])
data[“camera_direction”] = Lst2Vec3(data[“camera_direction”])
data[“camera_target”] = Lst2Point3(data[“camera_target”])
data[“camera_up”] = Lst2Vec3(data[“camera_up”])
return data

error listing:

Traceback (most recent call last):
File “C:\panda3d-1.7.0\built_cmu\direct\showbase\Messenger.py”, line 352, in _
_taskChainDispatch
File “C:\panda3d-1.7.0\built_cmu\direct\showbase\Messenger.py”, line 410, in _
_dispatch
File “C:\panda3d-1.7.0\built_cmu\direct\p3d\AppRunner.py”, line 493, in __star
tIfReady
File “VFSImporter”, line 153, in load_module
File “C:\files\panda\files\scenes\costa\main.py”, line 278, in
app = MyApp()
File “C:\files\panda\files\scenes\costa\main.py”, line 88, in init
self.view = unpickle_view(“costa”)
File “C:\files\panda\files\scenes\costa\main.py”, line 48, in unpickle_view
infile = open(mydir + “/” + filename + ‘_views.pkl’, ‘rb’)
File “C:\panda3d-1.7.0\built_cmu\direct\stdpy\file.py”, line 132, in init
IOError
:task(error): Exception occurred in PythonTask Messenger-default
Traceback (most recent call last):
File “C:\panda3d-1.7.0\built_cmu\direct\p3d\AppRunner.py”, line 411, in run
File “C:\panda3d-1.7.0\built_cmu\direct\task\Task.py”, line 496, in run
File “C:\panda3d-1.7.0\built_cmu\direct\task\Task.py”, line 454, in step
File “C:\panda3d-1.7.0\built_cmu\direct\showbase\Messenger.py”, line 352, in _
_taskChainDispatch
File “C:\panda3d-1.7.0\built_cmu\direct\showbase\Messenger.py”, line 410, in _
_dispatch
File “C:\panda3d-1.7.0\built_cmu\direct\p3d\AppRunner.py”, line 493, in __star
tIfReady
File “VFSImporter”, line 153, in load_module
File “C:\files\panda\files\scenes\costa\main.py”, line 278, in
app = MyApp()
File “C:\files\panda\files\scenes\costa\main.py”, line 88, in init
self.view = unpickle_view(“costa”)
File “C:\files\panda\files\scenes\costa\main.py”, line 48, in unpickle_view
infile = open(mydir + “/” + filename + ‘_views.pkl’, ‘rb’)
File “C:\panda3d-1.7.0\built_cmu\direct\stdpy\file.py”, line 132, in init
IOError
:TaskManager: TaskManager.destroy()
:display: Closing wglGraphicsWindow
Failure on startup.

sys.path[0] isn’t reliable in a p3d file. Instead, use base.appRunner.multifileRoot.

David

Or base.mainDir (which is really ExecutionEnvironment.getEnvironmentVariable(“MAIN_DIR”)), which should work both inside and outside the plug-in environment.

thanks for the help, but having further problems.

I get the error below when using the base.appRunner.multifileRoot to find the path to the files in the p3d.

I’m not sure whether “LookupError: unknown encoding: string-escape” is the result of a pathing problem or is to do with reading my .pkl file.

I would appreciate the help.

PaulM

def unpickle_view(filename):
“”“unpickles settings exported from rhino”""
# Get the location of the ‘py’ file I’m running:
mydir = base.appRunner.multifileRoot
print “mydir is:”
print mydir
# Now load the pkl:
infile = open(mydir + ‘/’ + filename + ‘_views.pkl’, ‘rb’)
data = pickle.load(infile)
#print out the data
pprint.pprint(data)
infile.close()
#do some type conversion
data[“camera_location”] = Lst2Vec3(data[“camera_location”])
data[“camera_direction”] = Lst2Vec3(data[“camera_direction”])
data[“camera_target”] = Lst2Point3(data[“camera_target”])
data[“camera_up”] = Lst2Vec3(data[“camera_up”])
return data

error listing:

mydir is:
/mf
Traceback (most recent call last):
File “C:\panda3d-1.7.0\built_cmu\direct\showbase\Messenger.py”, line 352, in _
_taskChainDispatch
File “C:\panda3d-1.7.0\built_cmu\direct\showbase\Messenger.py”, line 410, in _
_dispatch
File “C:\panda3d-1.7.0\built_cmu\direct\p3d\AppRunner.py”, line 493, in __star
tIfReady
File “VFSImporter”, line 153, in load_module
File “C:\files\panda\files\scenes\costa\main.py”, line 286, in
app = MyApp()
File “C:\files\panda\files\scenes\costa\main.py”, line 96, in init
self.view = unpickle_view(“costa”)
File “C:\files\panda\files\scenes\costa\main.py”, line 57, in unpickle_view
data = pickle.load(infile)
File “pickle”, line 1370, in load
File “pickle”, line 858, in load
File “pickle”, line 971, in load_string
LookupError: unknown encoding: string-escape
:task(error): Exception occurred in PythonTask Messenger-default
Traceback (most recent call last):
File “C:\panda3d-1.7.0\built_cmu\direct\p3d\AppRunner.py”, line 411, in run
File “C:\panda3d-1.7.0\built_cmu\direct\task\Task.py”, line 496, in run
File “C:\panda3d-1.7.0\built_cmu\direct\task\Task.py”, line 454, in step
File “C:\panda3d-1.7.0\built_cmu\direct\showbase\Messenger.py”, line 352, in _
_taskChainDispatch
File “C:\panda3d-1.7.0\built_cmu\direct\showbase\Messenger.py”, line 410, in _
_dispatch
File “C:\panda3d-1.7.0\built_cmu\direct\p3d\AppRunner.py”, line 493, in __star
tIfReady
File “VFSImporter”, line 153, in load_module
File “C:\files\panda\files\scenes\costa\main.py”, line 286, in
app = MyApp()
File “C:\files\panda\files\scenes\costa\main.py”, line 96, in init
self.view = unpickle_view(“costa”)
File “C:\files\panda\files\scenes\costa\main.py”, line 57, in unpickle_view
data = pickle.load(infile)
File “pickle”, line 1370, in load
File “pickle”, line 858, in load
File “pickle”, line 971, in load_string
LookupError: unknown encoding: string-escape
:TaskManager: TaskManager.destroy()
:display: Closing wglGraphicsWindow
Failure on startup.

Try adding -r morepy to the packp3d command line. You’re lacking some of the codecs definitions.

David

I’ll be including the ‘string-escape’ codec (together with ‘utf-8’ and ‘ascii’) in the upcoming 1.7.1 release because they are such common encodings used by so many Python modules.

thanks for the timely help, -r morepy fixed it.