PDA

View Full Version : cPickle Support



Steve Baer
04-07-2010, 10:39 PM
From Henry Segerman on our Ning site-
I was working on a project involving a mathematical object called the Hopf fibration, which lives in 4D, but you can look at projections of it into 3D. So I found a library to do the things I needed to do to calculate in 4D, and tried to import it into my script to run in Rhino, but it didn't like it. So I tried using the pickle library to precalculate my data in another script which can use the library, then unpickle in rhino python... but there's no support for pickle either!

So I ended up copying and pasting lists of numbers into the rhino python script, and that worked. I guess support for general libraries is coming? Or did I need to do something to tell it the paths to follow to find the libraries?

Hi Henry,
I realize that I "should" know a lot more about the pickle module, but it is just one of those modules I've never spent any time using. It appears that the python engine in Rhino ships with cPickle built-in which is an optimized version of pickle. This works for me on the Mac


import cPickle

def pickletest():
# save a list to a textfile using cPickle
items = ["one", "two", 3, "abc", 3.14]
filename = "/Users/Steve/pickletest.txt"
file = open( filename, 'w' )
cPickle.dump( items, file )
file.close()

# read the list out of the file
file = open( filename, 'r' )
readitems = cPickle.load(file)
file.close()

print "original = ", items
print "read = ", readitems


if( __name__ == '__main__' ):
pickletest()

HenryS
04-08-2010, 03:37 PM
Thanks Steve, that seems to work for me as well.

Using pickle is a workaround though (but certainly better than pasting in lists of numbers).

Really I'd want to be able to import whatever libraries I want to use in the script Rhino runs...

Cheers,

Henry

Steve Baer
04-08-2010, 03:50 PM
What was the original library that you were trying to import?

HenryS
04-09-2010, 10:36 PM
Original library was http://cgkit.sourceforge.net/

alecperkins
04-11-2010, 07:59 AM
I've had some luck importing standard Python modules, and installed modules, from my system's Python (Python 2.6 on OS X) by appending my main Python path, and Python site-packages directory, to the Rhino Python path.

After doing this:

import sys
sys.path.append('/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6')
sys.path.append('/Library/Python/2.6/site-packages/')

I then import the modules I need:

import pickle, urllib2, json #, etc
import cgkit #only tried light version, seemed to import properly

I haven't run into any modules where this doesn't work, though I'd be surprised if there weren't still some problem ones. I found this trick here (http://www.johndcook.com/blog/2009/02/19/install-ironpython/), which uses this approach for Windows.