IMP  2.3.0
The Integrative Modeling Platform
_pivy.py
1 import sys
2 
3 
4 def not_exit():
5  raise RuntimeError("runtime error in PiVy")
6 
7 
8 def get_writer(parent):
9  class PivyWriter(parent):
10 
11  def __init__(self):
12  parent.__init__(self, "pivy")
13  self._setup_pivy()
14 
15  def _setup_pivy(self):
16  import pivy.sogui
17  import pivy.coin
18  import os
19  self.pivy = pivy
20  # actually, pivy seems to call exit on failure. Bah.
21  if "DISPLAY" not in os.environ.keys():
22  raise RuntimeError("No display variable found")
23  myWindow = pivy.sogui.SoGui.init(sys.argv[0])
24  if myWindow is None:
25  raise RuntimeError("Can't open PiVy window.")
26  self.window = myWindow
27  print "scene"
28  scene = pivy.coin.SoSeparator()
29 
30  # Create a viewer in which to see our scene graph.
31  viewer = pivy.sogui.SoGuiExaminerViewer(myWindow)
32 
33  # Put our scene into viewer, change the title
34  viewer.setSceneGraph(scene)
35  print "title"
36  viewer.setTitle("IMP")
37  print "show"
38  viewer.show()
39  self.root = scene
40  self.viewer = viewer
41 
42  def handle_color(self, parent, c):
43  color = self.pivy.coin.SoMFColor()
44  color.setValue(c.get_red(), c.get_green(), c.get_blue())
45  myMaterial = self.pivy.coin.SoMaterial()
46  myMaterial.diffuseColor.setValue(color)
47  parent.addChild(myMaterial)
48 
49  def handle_sphere(self, s, c, n):
50  sep = self.pivy.coin.SoSeparator()
51  self.root.addChild(sep)
52  s.set_was_used(True)
53  self.handle_color(sep, c)
54  tr = self.pivy.coin.SoTransform()
55  tr.translation.setValue(s.get_geometry().get_center()[0],
56  s.get_geometry().get_center()[1],
57  s.get_geometry().get_center()[2])
58  tr.scaleFactor.setValue(s.get_geometry().get_radius(),
59  s.get_geometry().get_radius(),
60  s.get_geometry().get_radius())
61  sep.addChild(tr)
62  sphere = self.pivy.coin.SoSphere()
63  sep.addChild(sphere)
64  return True
65 
66  def handle_label(self, s, c, n):
67  txt = s.get_text()
68  loc = s.get_location()
69  s.set_was_used(True)
70  sep = self.pivy.coin.SoSeparator()
71  self.handle_color(sep, c)
72  tr = self.pivy.coin.SoTransform()
73  tr.translation.setValue(
74  loc.get_center()[0],
75  loc.get_center()[1],
76  loc.get_center()[2])
77  sep.addChild(tr)
78  to = self.pivy.coin.SoText2()
79  to.string.setValue(txt)
80  sep.addChild(to)
81  return True
82 
83  def handle_cylinder(self, s, c, n):
84  s.set_was_used(True)
85  sep = self.pivy.coin.SoSeparator()
86  self.root.addChild(sep)
87  self.handle_color(sep, c)
88  tr = self.pivy.coin.SoTransform()
89  cyl = s.get_geometry()
90  seg = cyl.get_segment()
91  center = seg.get_middle_point()
92  #tr.translation.setValue(self.pivy.coin.SbVec3f(0, seg.get_length()/2, 0))
93  tr.translation.setValue(
94  self.pivy.coin.SbVec3f(center[0], center[1],
95  center[2]))
96  #tr.center.setValue(self.pivy.coin.SbVec3f(center[0], center[1], center[2]))
97  uv = seg.get_direction().get_unit_vector()
98  tr.rotation.setValue(
99  self.pivy.coin.SbRotation(self.pivy.coin.SbVec3f(0, 1, 0),
100  self.pivy.coin.SbVec3f(uv[0], uv[1], uv[2])))
101  # print "tr", tr.translation.getValue()[0], tr.translation.getValue()[1], tr.translation.getValue()[2]
102  # print "scale", tr.scaleFactor.getValue()[0], tr.scaleFactor.getValue()[1], tr.scaleFactor.getValue()[2]
103  # tr.rotation.getValue()
104  sep.addChild(tr)
105  sphere = self.pivy.coin.SoCylinder()
106  sphere.radius.setValue(cyl.get_radius())
107  sphere.height.setValue(cyl.get_segment().get_length())
108  # sphere.addPart(self.pivy.coin.SoCylinder.ALL)
109  sep.addChild(sphere)
110  return True
111 
112  def show(self):
113  self.set_was_used(True)
114  self.pivy.sogui.SoGui.show(self.window) # Display main window
115  self.pivy.sogui.SoGui.mainLoop()
116  try:
117  import pivy.sogui
118  except:
119  return None
120  return PivyWriter
VT get_unit_vector(VT vt)
Returns a unit vector pointing at the same direction as this vector.
Definition: VectorBaseD.h:229
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.