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