import pymol from pymol import _cmd import threading import sys pymol2_lock = threading.RLock() ## ## FIXME: The PyMOL and SingletonPyMOL classes are partly redundant with the ## instance tracking of the "cmd" module (and the pymol2.cmd2.Cmd class), ## which also holds the _COb pointer. ## class SingletonPyMOL: ''' Start an exclusive PyMOL instance, only one instance allowed ''' def __init__(self, options=None): if options is None: self.options = pymol.invocation.options else: self.options = options def idle(self): return _cmd._idle(self._COb) def getRedisplay(self, reset=True): return _cmd._getRedisplay(self._COb, reset) def reshape(self, width, height, force=0): _cmd._reshape(self._COb, width, height, force) def draw(self): _cmd._draw(self._COb) def button(self, button, state, x, y, modifiers): _cmd._button(self._COb, button, state, x, y, modifiers) def drag(self, x, y, modifiers): _cmd._drag(self._COb, x, y, modifiers) def start(self): cmd = pymol.cmd if cmd._COb is not None: raise RuntimeError('can only start SingletonPyMOL once') with pymol2_lock: cmd._COb = _cmd._new(pymol, self.options, True) #! 此处对pymol-open-source源码进行修改,方便启动的时候修改参数,具体参考options _cmd._start(cmd._COb, cmd) self._COb = cmd._COb self.cmd = cmd def stop(self): with pymol2_lock: _cmd._stop(self._COb) pymol.cmd._COb = None def __enter__(self): self.start() return self def __exit__(self, exc_type, exc_value, tb): self.stop() @classmethod def start_with_options(cls, options): # new add instance = cls(options) instance.start() return instance ''' # 使用示例 if __name__ == '__main__': from pymol.invocation import options options.rpcServer = 1 # 使用类方法创建并启动实例 pymol_instance1 = SingletonPyMOL.start_with_options(options) # 使用上下文管理器 with SingletonPyMOL() as pymol_instance2: # pymol_instance2 使用默认的 options pass # 先创建实例,然后单独启动 pymol_instance3 = SingletonPyMOL() pymol_instance3.start() # 使用默认的 options # 启动pymol GUI,可以在一个新的进程中启动,通过rpcServer 默认端口是:9123 localhost # 或者在一个新的线程中启动界面,不堵塞主线程 from pmg_qt import pymol_qt_gui pymol_qt_gui.execapp() '''