Hi Christian,

the pymol.session variable is not really part of the API -- means, not intended 
to be modified or accessed by the user. However, it's all Python and obviously 
not hidden, so you can mess around with it, if you do it right. Short answer 
is: Always access it as "pymol.session", don't do "from pymol import session". 
This works:

--- script.py ---
import pymol
from pymol import cmd

@cmd.extend
def print_session_id():
    print id(pymol.session)

@cmd.extend
def set_foo(value):
    pymol.session.foo = value

@cmd.extend
def print_foo():
    print pymol.session.foo
--- end script.py ---

Cheers,
  Thomas

On 29 Jan 2015, at 20:20, Christian Becke <christian.be...@fu-berlin.de> wrote:

> Dear pymol experts,
> 
> I found some odd behaviour when trying to use pymol.session in my 
> script. I'm using open source pymol 1.7.4.0 (built from svn) on linux. 
> Here comes a minimal example:
> 
> 1) Create a directory, containing the following 2 files:
> 
> --- script.py ---
> from pymol import cmd, session
> 
> def print_session_id():
>     print id(session)
> cmd.extend("print_session_id", print_session_id)
> 
> def set_foo(value):
>     session.foo = value
> cmd.extend("set_foo", set_foo)
> 
> def print_foo():
>     print session.foo
> cmd.extend("print_foo", print_foo)
> --- end script.py ---
> 
> --- .pymolrc.py ---
> import script
> --- end .pymolrc.py ---
> 
> 2) Change to this directory and start pymol.
> 
> 3) In the pymol CLI, do:
> 
> script_set_foo bar
> script_print_foo
> script_session_id
> python
> print id(session)
> print session.foo
> session.foo = "foo"
> python end
> script_print_foo
> save test.pse
> 
> This produces the output given below. Note that the id of the session 
> object is the same when printed from the script and from within the 
> pymol session, and how the script reflects changes made to the 
> session.foo variable from the pymol CLI and vice versa.
> 
> --- pymol log ---
> PyMOL>script_set_foo bar
> PyMOL>script_print_foo
> bar
> PyMOL>script_session_id
> 139958033268464
> PyMOL>python
> PyMOL>print id(session)
>     1:print id(session)
> PyMOL>print session.foo
>     2:print session.foo
> PyMOL>session.foo = "foo"
>     3:session.foo = "foo"
> PyMOL>python end
> PyMOL>python end
> 139958033268464
> bar
> PyMOL>script_print_foo
> foo
> PyMOL>save test.pse
>  Save: Please wait -- writing session file...
>  Save: wrote "test.pse".
> PyMOL>quit
>  PyMOL: normal program termination.
> --- end pymol log ---
> 
> 4) If I now start pymol, load test.pse and run the following commands in 
> the pymol CLI:
> 
> script_print_foo
> script_session_id
> script_set_foo bar
> python
> print id(session)
> print session.foo
> python end
> 
> I get the following results:
> 
> --- pymol log ---
>  Executive: Loading version 1.740 session...
> PyMOL>script_print_foo
> Traceback (most recent call last):
>   File "/usr/lib64/python2.7/site-packages/pymol/parser.py", line 256, 
> in parse
>     self.result=apply(layer.kw[0],layer.args,layer.kw_args)
>   File "script.py", line 12, in script_print_foo
>     print session.foo
> AttributeError: Session_Storage instance has no attribute 'foo'
> PyMOL>script_session_id
> 139950852452080
> PyMOL>script_set_foo bar
> PyMOL>python
> PyMOL>print id(session)
>     1:print id(session)
> PyMOL>print session.foo
>     2:print session.foo
> PyMOL>python end
> PyMOL>python end
> 139950603504528
> foo
> PyMOL>script_print_foo
> bar
> --- end pymol log ---
> 
> Note that the ids of the session objects differ when printed from the 
> script or from the pymol CLI, and the contents of the variable 
> session.foo differs between pymol CLI and script.
> 
> If I do not import script.py from .pymolrc.py, everything works as expected.
> 
> Apparently, loading of a new session file creates a new instance of the 
> pymol.session object. Shouldn't the pymol.session instance be created 
> once upon startup, so it can be shared between all scripts? And only 
> values assigned to this instance upon loading a session from file?
> 
> Is there a way to import scripts from .pymolrc.py, load a session file 
> *and* get the correct pymol.session object within the script?
> Or is there an alternative way to get custom data saved in session files 
> that can be used in scripts?
> 
> 
> Best,
> 
> Christian

-- 
Thomas Holder
PyMOL Principal Developer
Schrödinger, Inc.


------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
PyMOL-users mailing list (PyMOL-users@lists.sourceforge.net)
Info Page: https://lists.sourceforge.net/lists/listinfo/pymol-users
Archives: http://www.mail-archive.com/pymol-users@lists.sourceforge.net

Reply via email to