Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # # # # # # # # # # # # # # # # # # # # #
- # for cinema 4d py4d script manager.
- # displays a userarea with 2 rectangles
- # you can move around.
- # r12 only
- # # # # # # # # # # # # # # # # # # # # #
- import c4d
- from c4d.gui import GeUserArea, GeDialog
- from time import sleep
- class SplVector(object):
- __slots__ = 'x', 'y'
- def __init__(self, x = 0, y = None):
- if y is None: y = x
- else: y = float(y)
- self.x, self.y = x, y
- def __setattr__(self, n, v):
- object.__setattr__(self, n, float(v))
- def __getattribute__(self, n):
- try:
- return int(object.__getattribute__(self, n))
- except:
- return object.__getattribute__(self, n)
- def __repr__(self):
- return '<SplVector %s, %s>' % (
- round(self.x,3),
- round(self.y,3),
- )
- def __sub__(v1, v2):
- return SplVector(
- v1.x - v2.x,
- v1.y - v2.y,
- )
- def __rsub__(v1, v2):
- return SplVector(
- v1.x - v2.x,
- v1.y - v2.y,
- )
- def copy(self):
- return SplVector(self.x, self.y)
- def len(self):
- return (self.x ** 2 + self.y ** 2) ** .5
- class SplineGUI(GeUserArea):
- __slots__ = 'knots', 'activeKnot'
- settings = {
- 'knot_rad': 3,
- }
- draw = settings
- def __init__(self, knots = None):
- if knots is None: knots = list()
- else: knots = list(knots)
- self.knots = knots
- self.activeKnot = None
- def DrawMsg(self, x1, y1, x2, y2, msg):
- # fill background
- self.DrawSetPen(c4d.Vector(.2))
- self.DrawRectangle(x1, y1, x2, y2)
- self.DrawSetPen(c4d.Vector(.2, .7, .45))
- _knotRad = self.draw["knot_rad"]
- for knot in self.knots:
- self.DrawRectangle(knot.x - _knotRad, knot.y - _knotRad, knot.x + _knotRad, knot.y + _knotRad)
- def InputEvent(self, msg):
- # drag event
- x, y = self.GetMouse(msg)
- _activeKnot = self.GetKnotNearMouse(x, y)
- if _activeKnot:
- while self.GetInputState(c4d.BFM_INPUT_MOUSE, c4d.BFM_INPUT_MOUSELEFT, msg) and msg.GetLong(c4d.BFM_INPUT_VALUE):
- x, y = self.GetMouse(msg)
- _activeKnot.x, _activeKnot.y = x, y
- self.Redraw()
- sleep(1./30) # 30 fps
- return True
- def DrawCircle(self, pos, rad):
- # delivers unsatisfactory results
- def isincircle(x, y):
- return True
- upperLeft = SplVector(pos.x - rad, pos.y - rad)
- # iterate over the bounding box
- for x in xrange(int(upperLeft.x), int(upperLeft.x + rad * 2), 1):
- for y in xrange(int(upperLeft.y), int(upperLeft.y + rad * 2), 1):
- if (SplVector(x, y) - pos).len() <= rad:
- self.DrawRectangle(x, y, x, y)
- def GetMouse(self, msg):
- g = self.Global2Local()
- return msg[c4d.BFM_INPUT_X] + g["x"], msg[c4d.BFM_INPUT_Y] + g["y"]
- def GetKnotNearMouse(self, x, y, radius = None, copy = False):
- "Returns the knot, in which radius the mouse is in or None"
- if radius is None:
- radius = self.settings['knot_rad']
- msePos = SplVector(x, y)
- rKnot = None
- for knot in self.knots:
- distance = (msePos - knot).len()
- if distance <= radius:
- if (rKnot
- and rKnot[0] > distance) \
- or rKnot is None:
- rKnot = (distance, knot)
- if rKnot:
- if copy: return rKnot[1].copy()
- else: return rKnot[1]
- class Dialog(GeDialog):
- def CreateLayout(self):
- self.ua = SplineGUI(
- (
- SplVector(20,20),
- SplVector(40,30),
- )
- )
- self.AddUserArea(10000, c4d.BFH_SCALEFIT | c4d.BFV_SCALEFIT, 200, 60)
- self.AttachUserArea(self.ua, 10000)
- return True
- Dialog().Open(c4d.DLG_TYPE_MODAL_RESIZEABLE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement