This is a read-only mirror of pymolwiki.org

Difference between revisions of "Rendering plugin"

From PyMOL Wiki
Jump to navigation Jump to search
m (12 revisions)
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{Infobox script-repo
 +
|type      = plugin
 +
|filename  = plugins/rendering_plugin.py
 +
|author    = [[User:Mglerner|Michael G. Lerner]]
 +
|license  = BSD
 +
}}
 +
 
==Description==
 
==Description==
 
Here is a small plugin to render images with a given DPI.
 
Here is a small plugin to render images with a given DPI.
  
The "Ray" button raytraces, and the "Draw" button just draws the image without raytracing (a fast way to see that the height/width look good).
+
The "Draw" button just draws the image without raytracing (a fast way to see that the height/width look good).<br>
 
+
The "Ray" button raytraces first, before saving.
It warns you not to have the plugin window on top of your main PyMOL window when you click "Ray". That doesn't seem to be necessary anymore, at least on my MacBook Pro.
 
  
 
The functionality is also available as a script (see my .pymolrc [[User:Mglerner|here]]).
 
The functionality is also available as a script (see my .pymolrc [[User:Mglerner|here]]).
  
Here's the imperial version. The metric version follows.
+
First the imperial version. The metric version follows.
<source lang="python">
 
#!/usr/bin/env python
 
# Plugin contributed by Michael Lerner (mglerner@gmail.com)
 
 
 
import Tkinter
 
import Pmw
 
 
 
try:
 
    import pymol
 
    REAL_PYMOL = True
 
except ImportError:
 
    REAL_PYMOL = False
 
    class pymol:
 
        class cmd:
 
            def ray(*args):
 
                print "ray",args
 
            def png(*args,**kwargs):
 
                print "png",args,kwargs
 
            def draw(*args,**kwargs):
 
                print "draw",args,kwargs
 
            ray = staticmethod(ray)
 
            png = staticmethod(png)
 
            draw = staticmethod(draw)
 
 
 
def __init__(self):
 
    self.menuBar.addmenuitem('Plugin', 'command',
 
                            'Launch Rendering Plugin',
 
                            label='Rendering...',
 
                            command = lambda s=self: RenderPlugin(s))
 
class RenderPlugin:
 
 
 
    def __init__(self,app):
 
        self.parent = app.root
 
        self.dialog = Pmw.Dialog(self.parent,
 
                                buttons = ('Ray','Draw','Exit'),
 
                                title = 'PyMOL Rendering Plugin',
 
                                command = self.execute)
 
        self.dialog.withdraw()
 
        Pmw.setbusycursorattributes(self.dialog.component('hull'))
 
 
 
        w = Tkinter.Label(self.dialog.interior(),
 
                                text = 'PyMOL Rendering Plugin\nMichael Lerner, 2006 - www.umich.edu/~mlerner/PyMOL\nNOTE: make sure this window is not on top of the PyMOL window.',
 
                                background = 'black',
 
                                foreground = 'white',
 
                                )
 
        w.pack(expand = 1, fill = 'both', padx = 4, pady = 4)
 
 
 
        self.notebook = Pmw.NoteBook(self.dialog.interior())
 
        self.notebook.pack(fill='both',expand=1,padx=10,pady=10)
 
 
 
        # Set up the Main page
 
        page = self.notebook.add('Main')
 
        group = Pmw.Group(page,tag_text='Main options')
 
        group.pack(fill = 'both', expand = 1, padx = 10, pady = 5)
 
        self.filename = Pmw.EntryField(group.interior(),
 
                                        labelpos='w',
 
                                        label_text='Filename',
 
                                        value='picture.png',
 
                                        )
 
        self.height = Pmw.EntryField(group.interior(),labelpos='w',
 
                                  label_text = 'Height (inches):',
 
                                  value = str(4.0),
 
                                  validate = {'validator' : 'real',
 
                                              'min':0,}
 
                                  )
 
       
 
       
 
        self.width = Pmw.EntryField(group.interior(),labelpos='w',
 
                                  label_text = 'Width (inches):',
 
                                  value = str(4.0),
 
                                  validate = {'validator' : 'real',
 
                                              'min':0,}
 
                                  )
 
        self.dpi = Pmw.EntryField(group.interior(),labelpos='w',
 
                                  label_text = 'DPI:',
 
                                  value = str(300),
 
                                  validate = {'validator' : 'integer',
 
                                              'min':0,}
 
                                  )
 
        entries = (self.height,self.width,self.filename,self.dpi)
 
        for entry in entries:
 
            #entry.pack(side='left',fill='both',expand=1,padx=4) # side-by-side
 
            entry.pack(fill='x',expand=1,padx=4,pady=1) # vertical
 
        self.notebook.setnaturalsize()
 
        self.showAppModal()
 
       
 
    def showAppModal(self):
 
        #self.dialog.activate(geometry = 'centerscreenalways', globalMode = 'nograb')
 
        self.dialog.show()
 
        #self.dialog.activate(geometry = 'centerscreenalways')
 
       
 
    def execute(self, result):
 
        if result == 'Ray':
 
            h = int(float(self.height.getvalue()) * int(self.dpi.getvalue()))
 
            w = int(float(self.width.getvalue()) * int(self.dpi.getvalue()))
 
            pymol.cmd.ray(w,h)
 
            pymol.cmd.png(self.filename.getvalue(), dpi=int(self.dpi.getvalue()))
 
        elif result == 'Draw':
 
            h = int(float(self.height.getvalue()) * int(self.dpi.getvalue()))
 
            w = int(float(self.width.getvalue()) * int(self.dpi.getvalue()))
 
            pymol.cmd.draw(w,h)
 
            pymol.cmd.png(self.filename.getvalue(), dpi=int(self.dpi.getvalue()))
 
        else:
 
            #
 
            # Doing it this way takes care of clicking on the x in the top of the
 
            # window, which as result set to None.
 
            #
 
            if __name__ == '__main__':
 
                #
 
                # dies with traceback, but who cares
 
                #
 
                self.parent.destroy()
 
            else:
 
                self.dialog.withdraw()
 
           
 
                     
 
# Create demo in root window for testing.
 
if __name__ == '__main__':
 
    class App:
 
        def my_show(self,*args,**kwargs):
 
            pass
 
    app = App()
 
    app.root = Tkinter.Tk()
 
    Pmw.initialise(app.root)
 
    app.root.title('Some Title')
 
   
 
    widget = RenderPlugin(app)
 
    exitButton = Tkinter.Button(app.root, text = 'Exit', command = app.root.destroy)
 
    exitButton.pack()
 
    app.root.mainloop()
 
</source>
 
 
 
 
 
And here's the metric version
 
 
 
<source lang="python">
 
#!/usr/bin/env python
 
# Plugin contributed by Michael Lerner (mglerner@gmail.com)
 
 
 
import Tkinter
 
import Pmw
 
 
 
try:
 
    import pymol
 
    REAL_PYMOL = True
 
except ImportError:
 
    REAL_PYMOL = False
 
    class pymol:
 
        class cmd:
 
            def ray(*args):
 
                print "ray",args
 
            def png(*args,**kwargs):
 
                print "png",args,kwargs
 
            def draw(*args,**kwargs):
 
                print "draw",args,kwargs
 
            ray = staticmethod(ray)
 
            png = staticmethod(png)
 
            draw = staticmethod(draw)
 
 
 
def __init__(self):
 
    self.menuBar.addmenuitem('Plugin', 'command',
 
                            'Launch Metric Rendering Plugin',
 
                            label='Metric Rendering...',
 
                            command = lambda s=self: RenderPlugin(s))
 
class RenderPlugin:
 
 
 
    def __init__(self,app):
 
        self.parent = app.root
 
        self.dialog = Pmw.Dialog(self.parent,
 
                                buttons = ('Ray','Draw','Exit'),
 
                                title = 'PyMOL Rendering Plugin',
 
                                command = self.execute)
 
        self.dialog.withdraw()
 
        Pmw.setbusycursorattributes(self.dialog.component('hull'))
 
 
 
        w = Tkinter.Label(self.dialog.interior(),
 
                                text = 'PyMOL Rendering Plugin\nMichael Lerner, 2006 - www.umich.edu/~mlerner/PyMOL\nNOTE: make sure this window is not on top of the PyMOL window.',
 
                                background = 'black',
 
                                foreground = 'white',
 
                                )
 
        w.pack(expand = 1, fill = 'both', padx = 4, pady = 4)
 
  
        self.notebook = Pmw.NoteBook(self.dialog.interior())
+
To install, save the script as e.g. rendering_plugin.py or rendering_plugin_metric.py and install via PyMOL's Plugin --> Manage Plugins --> Install menu.
        self.notebook.pack(fill='both',expand=1,padx=10,pady=10)
 
  
        # Set up the Main page
+
The plugins are available through the project, [http://www.pymolwiki.org/index.php/Git_intro Pymol-script-repo].
        page = self.notebook.add('Main')
 
        group = Pmw.Group(page,tag_text='Main options')
 
        group.pack(fill = 'both', expand = 1, padx = 10, pady = 5)
 
        self.filename = Pmw.EntryField(group.interior(),
 
                                        labelpos='w',
 
                                        label_text='Filename',
 
                                        value='picture.png',
 
                                        )
 
        self.height = Pmw.EntryField(group.interior(),labelpos='w',
 
                                  label_text = 'Height (cm):',
 
                                  value = str(4.0),
 
                                  validate = {'validator' : 'real',
 
                                              'min':0,}
 
                                  )
 
       
 
       
 
        self.width = Pmw.EntryField(group.interior(),labelpos='w',
 
                                  label_text = 'Width (cm):',
 
                                  value = str(4.0),
 
                                  validate = {'validator' : 'real',
 
                                              'min':0,}
 
                                  )
 
        self.dpi = Pmw.EntryField(group.interior(),labelpos='w',
 
                                  label_text = 'DPI:',
 
                                  value = str(300),
 
                                  validate = {'validator' : 'integer',
 
                                              'min':0,}
 
                                  )
 
        entries = (self.height,self.width,self.filename,self.dpi)
 
        for entry in entries:
 
            #entry.pack(side='left',fill='both',expand=1,padx=4) # side-by-side
 
            entry.pack(fill='x',expand=1,padx=4,pady=1) # vertical
 
        self.notebook.setnaturalsize()
 
        self.showAppModal()
 
       
 
    def showAppModal(self):
 
        #self.dialog.activate(geometry = 'centerscreenalways', globalMode = 'nograb')
 
        self.dialog.show()
 
        #self.dialog.activate(geometry = 'centerscreenalways')
 
       
 
    def execute(self, result):
 
        in_per_cm = 0.393700787
 
        if result == 'Ray':
 
            h = int(float(self.height.getvalue()) * int(self.dpi.getvalue()))
 
            w = int(float(self.width.getvalue()) * int(self.dpi.getvalue()))
 
            h,w = in_per_cm*h,in_per_cm*w
 
            pymol.cmd.ray(w,h)
 
            pymol.cmd.png(self.filename.getvalue(), dpi=int(self.dpi.getvalue()))
 
        elif result == 'Draw':
 
            h = int(float(self.height.getvalue()) * int(self.dpi.getvalue()))
 
            w = int(float(self.width.getvalue()) * int(self.dpi.getvalue()))
 
            h,w = in_per_cm*h,in_per_cm*w
 
            pymol.cmd.draw(w,h)
 
            pymol.cmd.png(self.filename.getvalue(), dpi=int(self.dpi.getvalue()))
 
        else:
 
            #
 
            # Doing it this way takes care of clicking on the x in the top of the
 
            # window, which as result set to None.
 
            #
 
            if __name__ == '__main__':
 
                #
 
                # dies with traceback, but who cares
 
                #
 
                self.parent.destroy()
 
            else:
 
                self.dialog.withdraw()
 
           
 
                     
 
# Create demo in root window for testing.
 
if __name__ == '__main__':
 
    class App:
 
        def my_show(self,*args,**kwargs):
 
            pass
 
    app = App()
 
    app.root = Tkinter.Tk()
 
    Pmw.initialise(app.root)
 
    app.root.title('Some Title')
 
   
 
    widget = RenderPlugin(app)
 
    exitButton = Tkinter.Button(app.root, text = 'Exit', command = app.root.destroy)
 
    exitButton.pack()
 
    app.root.mainloop()
 
</source>
 
  
[[Category:Plugins]] [[Category:Coloring]]
+
[[Category:Plugins]]
 +
[[Category:Coloring]]
 +
[[Category:Pymol-script-repo]]

Latest revision as of 03:49, 28 March 2014

Type PyMOL Plugin
Download plugins/rendering_plugin.py
Author(s) Michael G. Lerner
License BSD
This code has been put under version control in the project Pymol-script-repo

Description

Here is a small plugin to render images with a given DPI.

The "Draw" button just draws the image without raytracing (a fast way to see that the height/width look good).
The "Ray" button raytraces first, before saving.

The functionality is also available as a script (see my .pymolrc here).

First the imperial version. The metric version follows.

To install, save the script as e.g. rendering_plugin.py or rendering_plugin_metric.py and install via PyMOL's Plugin --> Manage Plugins --> Install menu.

The plugins are available through the project, Pymol-script-repo.