Advertisement
nux95

cinema 4d userarea splinegui 01

Jun 26th, 2011
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. # # # # # # # # # # # # # # # # # # # # #
  2. # for cinema 4d py4d script manager.
  3. # displays a userarea with 2 rectangles
  4. # you can move around.
  5. # r12 only
  6. # # # # # # # # # # # # # # # # # # # # #
  7.  
  8. import  c4d
  9. from    c4d.gui         import GeUserArea, GeDialog
  10. from    time            import sleep
  11.  
  12.  
  13. class SplVector(object):
  14.     __slots__ = 'x', 'y'
  15.  
  16.     def __init__(self, x = 0, y = None):
  17.         if y is None: y = x
  18.         else: y = float(y)
  19.         self.x, self.y = x, y
  20.  
  21.     def __setattr__(self, n, v):
  22.         object.__setattr__(self, n, float(v))
  23.  
  24.     def __getattribute__(self, n):
  25.         try:
  26.             return int(object.__getattribute__(self, n))
  27.         except:
  28.             return object.__getattribute__(self, n)
  29.  
  30.     def __repr__(self):
  31.         return '<SplVector %s, %s>' % (
  32.             round(self.x,3),
  33.             round(self.y,3),
  34.         )
  35.  
  36.     def __sub__(v1, v2):
  37.         return SplVector(
  38.             v1.x - v2.x,
  39.             v1.y - v2.y,
  40.         )
  41.  
  42.     def __rsub__(v1, v2):
  43.         return SplVector(
  44.             v1.x - v2.x,
  45.             v1.y - v2.y,
  46.         )
  47.  
  48.     def copy(self):
  49.         return SplVector(self.x, self.y)
  50.  
  51.     def len(self):
  52.         return (self.x ** 2 + self.y ** 2) ** .5
  53.  
  54.  
  55. class SplineGUI(GeUserArea):
  56.     __slots__ = 'knots', 'activeKnot'
  57.  
  58.     settings    = {
  59.         'knot_rad':             3,
  60.     }
  61.     draw        = settings
  62.  
  63.     def __init__(self, knots = None):
  64.         if knots is None: knots = list()
  65.         else: knots = list(knots)
  66.  
  67.         self.knots = knots
  68.         self.activeKnot = None
  69.  
  70.     def DrawMsg(self, x1, y1, x2, y2, msg):
  71.         # fill background
  72.         self.DrawSetPen(c4d.Vector(.2))
  73.         self.DrawRectangle(x1, y1, x2, y2)
  74.  
  75.         self.DrawSetPen(c4d.Vector(.2, .7, .45))
  76.         _knotRad        = self.draw["knot_rad"]
  77.         for knot in self.knots:
  78.             self.DrawRectangle(knot.x - _knotRad, knot.y - _knotRad, knot.x + _knotRad, knot.y + _knotRad)
  79.  
  80.     def InputEvent(self, msg):
  81.         # drag event
  82.         x, y            = self.GetMouse(msg)
  83.         _activeKnot     = self.GetKnotNearMouse(x, y)
  84.         if _activeKnot:
  85.             while self.GetInputState(c4d.BFM_INPUT_MOUSE, c4d.BFM_INPUT_MOUSELEFT, msg) and msg.GetLong(c4d.BFM_INPUT_VALUE):
  86.                 x, y        = self.GetMouse(msg)
  87.                 _activeKnot.x, _activeKnot.y    = x, y
  88.                 self.Redraw()
  89.                 sleep(1./30)        # 30 fps
  90.  
  91.         return True
  92.  
  93.     def DrawCircle(self, pos, rad):
  94.         # delivers unsatisfactory results
  95.         def isincircle(x, y):
  96.             return True
  97.  
  98.         upperLeft       = SplVector(pos.x - rad, pos.y - rad)
  99.  
  100.         # iterate over the bounding box
  101.         for x in xrange(int(upperLeft.x), int(upperLeft.x + rad * 2), 1):
  102.             for y in xrange(int(upperLeft.y), int(upperLeft.y + rad * 2), 1):
  103.                 if (SplVector(x, y) - pos).len() <= rad:
  104.                     self.DrawRectangle(x, y, x, y)
  105.  
  106.     def GetMouse(self, msg):
  107.         g       = self.Global2Local()
  108.         return msg[c4d.BFM_INPUT_X] + g["x"], msg[c4d.BFM_INPUT_Y] + g["y"]
  109.  
  110.     def GetKnotNearMouse(self, x, y, radius = None, copy = False):
  111.         "Returns the knot, in which radius the mouse is in or None"
  112.         if radius is None:
  113.             radius = self.settings['knot_rad']
  114.  
  115.         msePos = SplVector(x, y)
  116.  
  117.         rKnot = None
  118.         for knot in self.knots:
  119.             distance = (msePos - knot).len()
  120.             if distance <= radius:
  121.                 if (rKnot
  122.                 and rKnot[0] > distance) \
  123.                 or rKnot is None:
  124.                     rKnot = (distance, knot)
  125.  
  126.         if rKnot:
  127.             if copy: return rKnot[1].copy()
  128.             else: return rKnot[1]
  129.  
  130.  
  131. class Dialog(GeDialog):
  132.     def CreateLayout(self):
  133.         self.ua = SplineGUI(
  134.             (
  135.                 SplVector(20,20),
  136.                 SplVector(40,30),
  137.             )
  138.         )
  139.  
  140.         self.AddUserArea(10000, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT, 200, 60)
  141.         self.AttachUserArea(self.ua, 10000)
  142.  
  143.         return True
  144.  
  145.  
  146. Dialog().Open(c4d.DLG_TYPE_MODAL_RESIZEABLE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement