#!/usr/bin/env python from pymol import cmd from time import sleep import os,thread,threading VIEW_FILENAME = 'currentpymolview.txt' def gv(): '''write current view to a file (default currentpymolview.txt)''' # '...%s...'%VIEW_FILENAME doesn't end up as gv.__doc__ for some reason f = file(VIEW_FILENAME,'w') f.write(str(cmd.get_view())) f.close() def sv(): '''read current view from a file (default currentpymolview.txt)''' # '...%s...'%VIEW_FILENAME doesn't end up as sv.__doc__ for some reason f = file(VIEW_FILENAME) cmd.set_view(f.read()) f.close() cmd.extend('gv',gv) cmd.extend('sv',sv) SENDING_OR_RECEIVING = None SENDER_OR_RECEIVER_RUNNING = False def _send_or_receive_views(): """Helper function for sending and receiving views. There's no way to stop this thread .. if we wanted to, we could probably set SENDING_OR_RECEIVING to 'kill' and raise some exception (SystemExit?) """ global SENDING_OR_RECEIVING last_view = cmd.get_view() while not sleep(0.2): if SENDING_OR_RECEIVING == 'sending': #print "sending" cur_view = cmd.get_view() if last_view != cur_view: if not os.path.exists(VIEW_FILENAME): open(VIEW_FILENAME,'w').write(str(cur_view)) last_view = cur_view elif SENDING_OR_RECEIVING == 'receiving': #print "receiving" if os.path.exists(VIEW_FILENAME): try: cmd.set_view(eval(open(VIEW_FILENAME,'r').read())) os.unlink(VIEW_FILENAME) except: # if the file isn't yet complete pass else: #print "wtf?" pass def send_view(): """Set ourselves up as the controlling window for multiple-window sessions Often, I'll find myself looking at the same molecule under different conditions. It's really useful to be able to open up a two (or more) windows and control the scene in all of them via the mouse in one of them. send_view sets us up as the controlling window and receive_view sets us up to be controlled by that window. You can switch between sending and receiving on the fly. NOTE: these all dump the current view to currentpymolview.txt, so you can only have one group running on a machine at one time. That shouldn't really be a problem under my normal usage conditions, though. """ global SENDING_OR_RECEIVING SENDING_OR_RECEIVING = 'sending' if not SENDER_OR_RECEIVER_RUNNING: thread.start_new_thread(_send_or_receive_views,()) def receive_view(): """Set ourselves up as a receiving window for multiple-window sessions Often, I'll find myself looking at the same molecule under different conditions. It's really useful to be able to open up a two (or more) windows and control the scene in all of them via the mouse in one of them. send_view sets us up as the controlling window and receive_view sets us up to be controlled by that window. You can switch between sending and receiving on the fly. NOTE: these all dump the current view to currentpymolview.txt, so you can only have one group running on a machine at one time. That shouldn't really be a problem under my normal usage conditions, though. """ global SENDING_OR_RECEIVING SENDING_OR_RECEIVING = 'receiving' if not SENDER_OR_RECEIVER_RUNNING: thread.start_new_thread(_send_or_receive_views,()) cmd.extend('send_view',send_view) cmd.extend('receive_view',receive_view)