Advertisement
nux95

Caleidos4D - Snap object in mouse input - with snap

Jan 8th, 2013
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. import c4d
  2. import os
  3. import time
  4. from c4d import gui, plugins, bitmaps
  5.  
  6. PLUGIN_ID = 1028184
  7.  
  8. #for GeLoadString values must match with the header file
  9. IDS_SPLINEDRAW = 50000
  10. MY_COMBOBUTTON = 100012
  11. MY_EDITNUMBER = 100013
  12.  
  13. def snap_value(value, step):
  14. rest = value % step
  15. on_grid = value - rest
  16.  
  17. if rest / float(step) >= 0.5:
  18. on_grid += step
  19.  
  20. return on_grid
  21.  
  22. def snap_grid(vector, gridsize):
  23. vector = c4d.Vector(vector)
  24. vector.x = snap_value(vector.x, gridsize)
  25. vector.y = snap_value(vector.y, gridsize)
  26. vector.z = snap_value(vector.z, gridsize)
  27. return vector
  28.  
  29. def ArrangeJoints(doc,obj): #This method will be used to arrange the drawn object in a parent->child tree later on
  30. obj = doc.GetFirstObject()
  31. child = obj.GetDown()
  32.  
  33. while child:
  34. child.SetBit(c4d.BIT_ACTIVE)
  35. m = child.GetMg()
  36. if child.GetNext() is not None:
  37. next = child.GetNext()
  38. child.InsertUnder(next)
  39. child.SetMg(m)
  40. child = child.GetNext()
  41.  
  42. class SettingsDialog(gui.SubDialog):
  43.  
  44. myDict = {'Axis_Snap':0, 'spacevalue':0} #Create a dictionary that will hold the names & values we'll use to plugin values into the gui items
  45.  
  46. def __init__(self, arg):
  47. self.myDict = arg
  48.  
  49. def InitDialog(self):
  50. bc = c4d.plugins.GetToolData(c4d.documents.GetActiveDocument(),PLUGIN_ID) #Gets the tools container data if needed
  51. if bc == None: return False
  52. return True
  53.  
  54. def CreateLayout(self):
  55. self.GroupBegin(id=1000, flags=c4d.BFH_SCALEFIT, cols=2, rows=1)
  56. self.GroupBorderSpace(10, 10, 10, 10)
  57. self.element = self.AddStaticText(id=1001, flags=c4d.BFH_MASK, initw=120, name="Drawing Mode", borderstyle=c4d.BORDER_NONE)
  58. self.AddComboBox(MY_COMBOBUTTON, c4d.BFH_CENTER, 150, 15, specialalign=False) #Adds combobox button gizmo to the GUI
  59. self.AddChild(MY_COMBOBUTTON, 0, "FaceScreen&i" + str(c4d.RESOURCEIMAGE_MOVE) + "&") #Adds the move icon to the first button option
  60. self.AddChild(MY_COMBOBUTTON, 1, "Lock Along Z" )
  61. self.AddChild(MY_COMBOBUTTON, 2, "Lock On Floor")
  62. self.AddChild(MY_COMBOBUTTON, 3, "Lock Along X")
  63. self.SetLong(MY_COMBOBUTTON, self.myDict['Axis_Snap']) #Sets the button to the value of the Axis_Snap variable
  64.  
  65. self.element = self.AddStaticText(id=1002, flags=c4d.BFH_MASK, initw=120, name="Spacing", borderstyle=c4d.BORDER_NONE)
  66. self.AddEditNumberArrows(MY_EDITNUMBER, c4d.BFH_MASK, 20, 0) #width=20, height=0
  67. self.SetReal(MY_EDITNUMBER, self.myDict['spacevalue'], 0.0, 100, 1) #min=0.0, max=0.25, step=.01
  68. self.GroupEnd()
  69. return True
  70. def InitValues(self):
  71. self.SetLong(MY_COMBOBUTTON, 0) #Sets the combobox to the first option when plugin opens
  72. return True
  73.  
  74. def Command(self, id, msg):
  75.  
  76. if id==MY_COMBOBUTTON: self.myDict['Axis_Snap'] = self.GetLong(MY_COMBOBUTTON) #If the button is changed. Get the new value
  77. if id==MY_EDITNUMBER: self.myDict['spacevalue'] = self.GetReal(MY_EDITNUMBER) #If the value is changed. Get the new value
  78.  
  79. return True
  80.  
  81.  
  82. class SplineDraw(plugins.ToolData):
  83. """Inherit from ToolData to create your own tool"""
  84.  
  85. def __init__(self):
  86. self.data = dict(Axis_Snap=0, spacevalue=0.0)
  87.  
  88. def GetState(self, doc):
  89. if doc.GetMode()==c4d.Mpaint: return 0
  90. return c4d.CMD_ENABLED
  91.  
  92.  
  93. def KeyboardInput(self, doc, data, bd, win, msg):
  94. key = msg.GetLong(c4d.BFM_INPUT_CHANNEL)
  95. cstr = msg.GetString(c4d.BFM_INPUT_ASC)
  96. if key==c4d.KEY_ESC:
  97. #do what you want
  98. #return True to signal that the key is processed
  99. return True
  100. return False
  101.  
  102.  
  103. def MouseInput(self, doc, data, bd, win, msg):
  104. mx = msg[c4d.BFM_INPUT_X]
  105. my = msg[c4d.BFM_INPUT_Y]
  106.  
  107. device = 0
  108. if msg[c4d.BFM_INPUT_CHANNEL]==c4d.BFM_INPUT_MOUSELEFT:
  109. device = c4d.KEY_MLEFT
  110. elif msg[c4d.BFM_INPUT_CHANNEL]==c4d.BFM_INPUT_MOUSERIGHT:
  111. device = c4d.KEY_MRIGHT
  112. else:
  113. return True
  114. axis = self.data['Axis_Snap'] #Get the Axis_Snap key name from myDict in the subdialog and assign it to a variable
  115. spacing = self.data['spacevalue'] #Get the spacevalue key name from myDict in the subdialog and assign it to a variable
  116. #Spline stuff here
  117. doc.StartUndo()
  118.  
  119. dx = 0.0
  120. dy = 0.0
  121.  
  122. win.MouseDragStart(button=device, mx=int(mx), my=int(my), flags=c4d.MOUSEDRAGFLAGS_DONTHIDEMOUSE)
  123. result, dx, dy, channel = win.MouseDrag()
  124. while result==c4d.MOUSEDRAGRESULT_CONTINUE:
  125. mx += dx
  126. my += dy
  127.  
  128. cursorpos = bd.SW(c4d.Vector(mx,my,400)) #screen to world conversion
  129. if(axis == 1):
  130. cursorpos.x = 0
  131. elif(axis == 2):
  132. cursorpos.y = 0 #Constrain drawing along an axis based on the comboButton's value
  133. elif(axis == 3):
  134. cursorpos.z = 0
  135.  
  136. joint = doc.GetActiveObject() #Add joints which we will later convert to a spline
  137. doc.AddUndo(c4d.UNDOTYPE_NEW, joint)
  138. bc = joint.GetData()
  139. joint.SetAbsPos(snap_grid(cursorpos, 50))
  140. joint.SetData(bc)
  141. joint.Message(c4d.MSG_UPDATE)
  142.  
  143. c4d.DrawViews(c4d.DA_ONLY_ACTIVE_VIEW|c4d.DA_NO_THREAD|c4d.DA_NO_ANIMATION)
  144. result, dx, dy, channel = win.MouseDrag()
  145.  
  146. if win.MouseDragEnd()==c4d.MOUSEDRAGRESULT_ESCAPE:
  147. print "Hello World"
  148. c4d.EventAdd()
  149. return True
  150.  
  151.  
  152. def Draw(self, doc, data, bd, bh, bt, flags):
  153. return c4d.TOOLDRAW_HANDLES|c4d.TOOLDRAW_AXIS
  154.  
  155.  
  156. def GetCursorInfo(self, doc, data, bd, x, y, bc):
  157. if bc.GetId()==c4d.BFM_CURSORINFO_REMOVE:
  158. return True
  159.  
  160. bc.SetString(c4d.RESULT_BUBBLEHELP, plugins.GeLoadString(IDS_SPLINEDRAW))
  161. bc.SetLong(c4d.RESULT_CURSOR, c4d.MOUSE_SPLINETOOLS)
  162. return True
  163.  
  164.  
  165. def AllocSubDialog(self, bc):
  166. return SettingsDialog(self.data) #always return new instance(self.data)
  167.  
  168.  
  169. if __name__ == "__main__":
  170. bmp = bitmaps.BaseBitmap()
  171. dir, file = os.path.split(__file__)
  172. fn = os.path.join(dir, "res", "Icon.tif")
  173. bmp.InitWith(fn)
  174. plugins.RegisterToolPlugin(id=PLUGIN_ID, str="SplineDraw",info=0, icon=bmp, help="Statusbar Text",dat=SplineDraw())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement