On 05/01/2013 at 12:00, xxxxxxxx wrote:
Hi :D
i have used the free script code "SplineDraw" by Scott Ayers
http://sites.google.com/site/scottayersmedia/SplineDraw_Python.zip?attredirects=0
My code, move the active object on mouse position.
but the snap not work...
is there a way to snap the active object to another object?
Thanks :D
import c4d
import os
import time
from c4d import gui, plugins, bitmaps
PLUGIN_ID = 1028184
#for GeLoadString values must match with the header file
IDS_SPLINEDRAW = 50000
MY_COMBOBUTTON = 100012
MY_EDITNUMBER = 100013
def ArrangeJoints(doc,obj) : #This method will be used to arrange the drawn object in a parent->child tree later on
obj = doc.GetFirstObject()
child = obj.GetDown()
while child:
child.SetBit(c4d.BIT_ACTIVE)
m = child.GetMg()
if child.GetNext() is not None:
next = child.GetNext()
child.InsertUnder(next)
child.SetMg(m)
child = child.GetNext()
class SettingsDialog(gui.SubDialog) :
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
def __init__(self, arg) :
self.myDict = arg
def InitDialog(self) :
bc = c4d.plugins.GetToolData(c4d.documents.GetActiveDocument(),PLUGIN_ID) #Gets the tools container data if needed
if bc == None: return False
return True
def CreateLayout(self) :
self.GroupBegin(id=1000, flags=c4d.BFH_SCALEFIT, cols=2, rows=1)
self.GroupBorderSpace(10, 10, 10, 10)
self.element = self.AddStaticText(id=1001, flags=c4d.BFH_MASK, initw=120, name="Drawing Mode", borderstyle=c4d.BORDER_NONE)
self.AddComboBox(MY_COMBOBUTTON, c4d.BFH_CENTER, 150, 15, specialalign=False) #Adds combobox button gizmo to the GUI
self.AddChild(MY_COMBOBUTTON, 0, "FaceScreen&i" + str(c4d.RESOURCEIMAGE_MOVE) + "&") #Adds the move icon to the first button option
self.AddChild(MY_COMBOBUTTON, 1, "Lock Along Z" )
self.AddChild(MY_COMBOBUTTON, 2, "Lock On Floor")
self.AddChild(MY_COMBOBUTTON, 3, "Lock Along X")
self.SetLong(MY_COMBOBUTTON, self.myDict['Axis_Snap']) #Sets the button to the value of the Axis_Snap variable
self.element = self.AddStaticText(id=1002, flags=c4d.BFH_MASK, initw=120, name="Spacing", borderstyle=c4d.BORDER_NONE)
self.AddEditNumberArrows(MY_EDITNUMBER, c4d.BFH_MASK, 20, 0) #width=20, height=0
self.SetReal(MY_EDITNUMBER, self.myDict['spacevalue'], 0.0, 100, 1) #min=0.0, max=0.25, step=.01
self.GroupEnd()
return True
def InitValues(self) :
self.SetLong(MY_COMBOBUTTON, 0) #Sets the combobox to the first option when plugin opens
return True
def Command(self, id, msg) :
if id==MY_COMBOBUTTON: self.myDict['Axis_Snap'] = self.GetLong(MY_COMBOBUTTON) #If the button is changed. Get the new value
if id==MY_EDITNUMBER: self.myDict['spacevalue'] = self.GetReal(MY_EDITNUMBER) #If the value is changed. Get the new value
return True
class SplineDraw(plugins.ToolData) :
"""Inherit from ToolData to create your own tool"""
def __init__(self) :
self.data = dict(Axis_Snap=0, spacevalue=0.0)
def GetState(self, doc) :
if doc.GetMode()==c4d.Mpaint: return 0
return c4d.CMD_ENABLED
def KeyboardInput(self, doc, data, bd, win, msg) :
key = msg.GetLong(c4d.BFM_INPUT_CHANNEL)
cstr = msg.GetString(c4d.BFM_INPUT_ASC)
if key==c4d.KEY_ESC:
#do what you want
#return True to signal that the key is processed
return True
return False
def MouseInput(self, doc, data, bd, win, msg) :
mx = msg[c4d.BFM_INPUT_X]
my = msg[c4d.BFM_INPUT_Y]
device = 0
if msg[c4d.BFM_INPUT_CHANNEL]==c4d.BFM_INPUT_MOUSELEFT:
device = c4d.KEY_MLEFT
elif msg[c4d.BFM_INPUT_CHANNEL]==c4d.BFM_INPUT_MOUSERIGHT:
device = c4d.KEY_MRIGHT
else:
return True
axis = self.data['Axis_Snap'] #Get the Axis_Snap key name from myDict in the subdialog and assign it to a variable
spacing = self.data['spacevalue'] #Get the spacevalue key name from myDict in the subdialog and assign it to a variable
#Spline stuff here
doc.StartUndo()
dx = 0.0
dy = 0.0
win.MouseDragStart(button=device, mx=int(mx), my=int(my), flags=c4d.MOUSEDRAGFLAGS_DONTHIDEMOUSE)
result, dx, dy, channel = win.MouseDrag()
while result==c4d.MOUSEDRAGRESULT_CONTINUE:
mx += dx
my += dy
cursorpos = bd.SW(c4d.Vector(mx,my,400)) #screen to world conversion
if(axis == 1) :
cursorpos.x = 0
elif(axis == 2) :
cursorpos.y = 0 #Constrain drawing along an axis based on the comboButton's value
elif(axis == 3) :
cursorpos.z = 0
joint = doc.GetActiveObject() #Add joints which we will later convert to a spline
doc.AddUndo(c4d.UNDOTYPE_NEW, joint)
bc = joint.GetData()
joint.SetAbsPos(cursorpos)
joint.SetData(bc)
joint.Message(c4d.MSG_UPDATE)
c4d.DrawViews(c4d.DA_ONLY_ACTIVE_VIEW|c4d.DA_NO_THREAD|c4d.DA_NO_ANIMATION)
result, dx, dy, channel = win.MouseDrag()
if win.MouseDragEnd()==c4d.MOUSEDRAGRESULT_ESCAPE:
print "Hello World"
c4d.EventAdd()
return True
def Draw(self, doc, data, bd, bh, bt, flags) :
return c4d.TOOLDRAW_HANDLES|c4d.TOOLDRAW_AXIS
def GetCursorInfo(self, doc, data, bd, x, y, bc) :
if bc.GetId()==c4d.BFM_CURSORINFO_REMOVE:
return True
bc.SetString(c4d.RESULT_BUBBLEHELP, plugins.GeLoadString(IDS_SPLINEDRAW))
bc.SetLong(c4d.RESULT_CURSOR, c4d.MOUSE_SPLINETOOLS)
return True
def AllocSubDialog(self, bc) :
return SettingsDialog(self.data) #always return new instance(self.data)
if __name__ == "__main__":
bmp = bitmaps.BaseBitmap()
dir, file = os.path.split(__file__)
fn = os.path.join(dir, "res", "Icon.tif")
bmp.InitWith(fn)
plugins.RegisterToolPlugin(id=PLUGIN_ID, str="SplineDraw",info=0, icon=bmp, help="Statusbar Text",dat=SplineDraw())