Advertisement
nux95

Untitled

Jun 3rd, 2011
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.19 KB | None | 0 0
  1. """
  2. Memory Viewer
  3. Copyright: MAXON Computer GmbH
  4. Written for CINEMA 4D R12.016
  5.  
  6. Modified Date: 08/30/2010
  7. """
  8.  
  9. import c4d
  10. from c4d import gui, plugins, utils, bitmaps, storage
  11.  
  12. import collections, os
  13.  
  14. #be sure to use a unique ID obtained from www.plugincafe.com
  15. PLUGIN_ID = 1025249
  16.  
  17. def CalcValueToMB(value):
  18.     """Returns MB"""
  19.     return value/1024.0/1024.0
  20.  
  21. class MemoryInfo(gui.GeUserArea):
  22.    
  23.     values = None
  24.     division = 40
  25.    
  26.     value_max = 0
  27.     value_min = 0
  28.    
  29.     highlight_line = c4d.Vector(0, 0.6, 0)
  30.     black = c4d.Vector(0)
  31.     shadow_line = c4d.Vector(0.15)
  32.    
  33.     def Init(self):
  34.         self.values = collections.deque([0,]*self.division)
  35.         self.Update()
  36.         return True
  37.    
  38.     def DrawMsg(self, x1, y1, x2, y2, msg_ref):
  39.         #init draw region
  40.         self.OffScreenOn()
  41.         self.SetClippingRegion(x1, y1, x2, y2)
  42.         self.DrawRectangle(x1, y1, x2, y2)
  43.        
  44.        
  45.         self.DrawSetPen(self.black)
  46.         self.DrawRectangle(x1, y1, x2, y2)
  47.        
  48.         x_step = int((x2-x1)/self.division+1)
  49.         y_step = int((y2-y1)/self.division+1)
  50.        
  51.         self.DrawSetPen(self.shadow_line)
  52.         #draw guides
  53.         for i in xrange(int(self.division)):
  54.             self.DrawLine(x_step*i, y2-y1, x_step*i, y2-y2)
  55.             self.DrawLine(x1, y2-(y_step*i), x2, y2-(y_step*i))
  56.        
  57.         offset = 10
  58.         self.DrawSetPen(self.highlight_line)
  59.         for i, v in enumerate(self.values):
  60.             if i==len(self.values)-1:
  61.                 continue
  62.            
  63.             l_x1 = int(i*x_step)
  64.             l_y1 = int(utils.RangeMap(v, self.value_min, self.value_max, y1+offset, y2-offset, False))
  65.             l_x2 = int((i+1)*x_step)
  66.            
  67.             self.value_min+10, self.value_max, y1+offset, y2-offset, False
  68.             l_y2 = int(utils.RangeMap(self.values[i+1], self.value_min+10, self.value_max, y1+offset, y2-offset, False))
  69.             #2 and 3 too much
  70.             self.DrawLine(l_x1, y2-l_y1, l_x2, y2-l_y2)
  71.            
  72.         #draw legend
  73.         self.DrawSetTextCol(self.highlight_line, self.black)
  74.         vmax = ("%.3f MB" % (CalcValueToMB(self.value_max)))
  75.         vmin = ("%.3f MB" % (CalcValueToMB(self.value_min)))
  76.         self.DrawText(vmax, 0, 0)
  77.         self.DrawText(vmin, 0, y2-self.DrawGetFontHeight())
  78.        
  79.         return
  80.    
  81.     def Update(self):
  82.         bc = storage.GeGetMemoryStat()
  83.         self.values.rotate(-1)
  84.         v = bc[c4d.C4D_MEMORY_STAT_MEMORY_INUSE]
  85.         if v>self.value_max:
  86.             self.value_max = v
  87.         elif v<self.value_min:
  88.             self.value_min = v
  89.        
  90.         self.values[self.division-1] = v
  91.         self.Redraw()
  92.         return bc #return to make available
  93.  
  94. class Test(gui.GeDialog):
  95.    
  96.     mem_info = MemoryInfo()
  97.     cur_mem_info = None
  98.    
  99.     def __init__(self):
  100.         self.AddGadget(c4d.DIALOG_NOMENUBAR, 0)#disable menubar
  101.    
  102.     def CreateLayout(self):
  103.        
  104.         bc = c4d.GetMachineFeatures()
  105.         self.SetTitle(bc[c4d.MACHINEINFO_COMPUTERNAME])
  106.         self.GroupBegin(id=0, flags=c4d.BFH_SCALEFIT, rows=1, title="", cols=2, groupflags=c4d.BORDER_GROUP_IN)
  107.         self.AddGadget(c4d.DIALOG_PIN, 0)#enable WindowPin
  108.         self.cur_mem_info = self.AddStaticText(id=0, initw=0, inith=0, name="", borderstyle=0, flags=c4d.BFH_SCALEFIT)
  109.         self.GroupEnd()
  110.        
  111.         self.AddSeparatorH(inith=0)
  112.        
  113.         self.GroupBegin(id=0, flags=c4d.BFH_SCALEFIT|c4d.BFV_SCALEFIT, title="", rows=1, cols=1, groupflags=c4d.BORDER_GROUP_IN)
  114.         self.GroupBorderSpace(5, 5, 5, 5)
  115.        
  116.         #give really unique ID to userarea, otherwise the update process will fail!
  117.         area = self.AddUserArea(id=1001, flags=c4d.BFH_SCALEFIT|c4d.BFV_SCALEFIT)
  118.         self.AttachUserArea(self.mem_info, area)
  119.         self.GroupEnd()
  120.         return True
  121.    
  122.     def InitValues(self):
  123.         self.SetTimer(500)
  124.         return True
  125.    
  126.     def Timer(self, msg):
  127.         bc = self.mem_info.Update()
  128.         self.SetString(self.cur_mem_info, ("Current: %.3f MB" % (CalcValueToMB(bc[c4d.C4D_MEMORY_STAT_MEMORY_INUSE]))))
  129.  
  130. dlg = Test()
  131. dlg.Open(c4d.DLG_TYPE_ASYNC)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement