On 14/01/2015 at 17:51, xxxxxxxx wrote:
I've done a decent amount of research on this and only found partial solutions so far. I'm trying to create a tool that allows me to move the selected object along the surface of any object in the view. I'm essentially looking to get something close to the behavior of 3D Snapping in Polygon mode, with the added effect of rotating the object to match the surface normal underneath the cursor and ensuring that it's the base of the object that slides along the surface, not the axis.
I'm able to get a list of objects underneath my cursor using ViewportSelect.PickObject(), but when I'm trying to calculate the exact position/normal of the intersection I run into the problem of the objects not being polygon objects.
What is the best way to take my list of objects and convert them into polygon objects (or even a single polygon object)? GetCache() works for me some of the time, but not all objects have caches, and sometimes the caches are hierarchies that are many levels deep. What I want is a 1:1 conversion of the objects I have to poly objects.
Thanks in advance!
Donovan
The current state of my code is below:
"""Place On Surface"""
import c4d
import os
PLUGIN_ID = 1034435
def polygonize_objects(objects) :
if objects is None:
return
poly_doc = c4d.BaseDocument()
class PlaceTool(c4d.plugins.ToolData) :
def InitTool(self, doc, data, bt) :
"""Called each time tool is selected."""
print "InitTool()"
return True
def FreeTool(self, doc, data) :
"""Called each time the user chooses another tool."""
print "FreeTool()"
return
def MouseInput(self, doc, data, bd, win, msg) :
"""Called when the user clicks with the mouse in any of the editor views."""
print "MouseInput()"
#Get Mouse Coordinates
mouse_x = msg[c4d.BFM_INPUT_X]
mouse_y = msg[c4d.BFM_INPUT_Y]
print "Mouse Coords: (%s, %s)" % (mouse_x, mouse_y)
#Create a vp select helper
viewport_select = c4d.utils.ViewportSelect()
#Retrieve the picked objects
pick_objects = viewport_select.PickObject(bd, doc, mouse_x, mouse_y, rad=0, flags=c4d.VIEWPORT_PICK_FLAGS_0)
print pick_objects
return True
def KeyboardInput(self, doc, data, bd, win, msg) :
"""Called when the user types something in any of the editor views."""
print "KeyboardInput()"
return True
def GetState(self, doc) :
"""Called to check if the tool should be enabled, checked or not."""
return c4d.CMD_ENABLED
if __name__ == "__main__":
bmp = c4d.bitmaps.BaseBitmap()
dir, file = os.path.split(__file__)
fn = os.path.join(dir, "res", "liquid.tif")
bmp.InitWith(fn)
c4d.plugins.RegisterToolPlugin(id=PLUGIN_ID, str="CV-Place on Surface",
info=0, icon=bmp,
help="Instances the selected object and places it on the surface under the cursor.",
dat=PlaceTool())
Edit: Andreas reformatted code