Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Intercepting Jmol console commands
#-*- coding:utf-8 -*-
# Copyright 2012 Adrià Cereto Massagué <>
# This is the jython version of Jmol's
# It is under public domain
An example of integrating the Jmol viewer into a jython application, with optional console.
from java.lang.System import exit
from java.awt import BorderLayout , Container, Dimension, Graphics
from java.awt.event import WindowAdapter
from javax.swing import JFrame, JPanel
from org.jmol.adapter.smarter import SmarterJmolAdapter
from org.jmol.api import JmolViewer
from org.jmol.util import Logger
from import AppConsole
strScript = "delay; move 360 0 0 0 0 0 0 0 4;"
def main():
Demonstrates a simple way to include an optional console along with the applet.
frame = JFrame("Hello", size = (410, 700), defaultCloseOperation = JFrame.EXIT_ON_CLOSE)
contentPane = frame.getContentPane()
jmolPanel = JmolPanel(preferredSize = Dimension(400, 400))
# main panel -- Jmol panel on top
panel = JPanel(layout = BorderLayout())
# main panel -- console panel on bottom
panel2 = JPanel(layout = BorderLayout(), preferredSize = Dimension(400, 300))
console = Console(jmolPanel.viewer, panel2,"History State Clear")
# You can use a different JmolStatusListener or JmolCallbackListener interface
# if you want to, but AppConsole itself should take care of any console-related callbacks
jmolPanel.viewer.jmolCallbackListener = console
panel.add("South", panel2)
jmolPanel.viewer.evalString("Function myCommand () {}")
jmolPanel.viewer.evalString("set syncscript on")
frame.visible = True
# sample start-up script
strError = jmolPanel.viewer.openFile("")
if strError == None:
class Console(AppConsole):
def notifyEnabled(self, callbacktype):
print callbacktype
if str(callbacktype) == 'SYNC':
return True
return AppConsole.notifyEnabled(self, callbacktype)
def notifyCallback(self, callbacktype, data):
print 'Callback is ' + str(callbacktype)
print 'data is ' + str(data)
if str(callbacktype) == 'SYNC':
print 'Script is:'
print data[1]
if len(data[1].split('##')) >=3:
command = data[1].split('##')[0]
if 'mycommand' in command.lower():
self.sendConsoleEcho("mycommand has been executed!")
AppConsole.notifyCallback(self, callbacktype, data)
class JmolPanel(JPanel):
def __init__(self, preferredSize = None):
self.preferredSize = preferredSize
self.currentSize = Dimension()
self.viewer = JmolViewer.allocateViewer(self, SmarterJmolAdapter(), None, None, None, None, None)
def paint(self, g):
self.viewer.renderScreenImage(g, self.currentSize.width, self.currentSize.height)
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment