File: color_tool.py

package info (click to toggle)
openstructure 2.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 206,240 kB
  • sloc: cpp: 188,571; python: 36,686; ansic: 34,298; fortran: 3,275; sh: 312; xml: 146; makefile: 29
file content (59 lines) | stat: -rw-r--r-- 1,805 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
This script illustrates how to implement a custom tool for interacting with the
3D scene.
"""
from ost import gui


class ColorTool(gui.Tool):
  def __init__(self):
    gui.Tool.__init__(self, "Color Tool")   
    self.tool_options.AddOption(gui.ToolOptionFloat("r", "Red", 0.5, 0.0, 1.0))
    self.tool_options.AddOption(gui.ToolOptionFloat("g", "Green", 0.5, 0.0, 1.0))
    self.tool_options.AddOption(gui.ToolOptionFloat("b", "Blue", 0.5, 0.0, 1.0))
    apply_to=gui.ToolOptionEnum("apply_to", "Apply To")
    apply_to.Add("Atom", 0)
    apply_to.Add("Residue", 1)
    apply_to.Add("Chain", 2)
    apply_to.SetIndex(0)
    self.tool_options.AddOption(apply_to)

  def GetColor(self):
    return gfx.Color(self.tool_options.GetOption('r').value,
                     self.tool_options.GetOption('g').value,
                     self.tool_options.GetOption('b').value)
                     
  def CanOperateOn(self, obj):
    return True
  def GetIconPath(self):
    return ''
  def GetAtomsToColor(self, atom):
    apply_to=self.tool_options.GetOption('apply_to').value
    if apply_to==0:
      return [atom]
    if apply_to==1:
      return atom.residue.atoms
    if apply_to==2:
      l=[]
      for r in atom.residue.chain.residues:
        for a in r.atoms:
          l.append(a)
      return l

  def Click(self, event):
    obj, atom=gfx.PickAtom(scene, event.pos.x(), event.pos.y())
    if atom.IsValid():
      atoms=self.GetAtomsToColor(atom)
      color=self.GetColor()
      for atom in atoms:
        obj.SetColorForAtom(color, atom.handle)

color_tool=ColorTool()

gui.ToolManager.Instance().AddTool(color_tool)

e=io.LoadEntity(len(sys.argv)>1 and sys.argv[1] or '../entity/fragment.pdb')
g=gfx.Entity('e', e)
g.SetRenderMode(gfx.RenderMode.CUSTOM)
scene.Add(g)
scene.center=g.center