On 27/02/2015 at 11:50, xxxxxxxx wrote:
@Sebastian - thanks for that! Is this a different behavior from the C++ SDK? It seems like it handles it automatically in the PickTool example file.
For anyone that's trying to get object highlighting to work in their ToolData plugin, just drop in these methods:
def MouseToView(self, mouse_x, mouse_y, width, height) :
"""Clips mouse_x & mouse_y to view dimensions
view_x, view_y = self.MouseToView(mouse_x, mouse_y, width, height)"""
if (mouse_x is None) or (mouse_y is None) :
return None, None
view_x = int(mouse_x)
if view_x > width:
view_x = width
view_y = int(mouse_y)
if view_y > height:
view_y = height
return view_x, view_y
def GetViewData(self, bd) :
"""Retrieve helpful view information.
frame, left, right, top, bottom, width, height = self.GetViewData(bd)
"""
if bd is None:
return None, None, None, None, None, None, None
#Get View Data
frame = bd.GetFrame()
left = frame["cl"]
right = frame["cr"]
top = frame["ct"]
bottom = frame["cb"]
width = right - left + 1
height = bottom - top + 1
return frame, left, right, top, bottom, width, height
def GetObjectUnderCursor(self, bd, doc, x, y, rad=2) :
"""Return the object under the cursor.
Limitation: Only works w/ OGL turned on"""
frame, left, right, top, bottom, width, height = self.GetViewData(bd)
view_x, view_y = self.MouseToView(x, y, width, height)
nearest_objects = c4d.utils.ViewportSelect().PickObject(bd, doc, view_x, view_y, rad=rad, flags=c4d.VIEWPORT_PICK_FLAGS_OGL_ONLY_TOPMOST_WITH_OBJ)
if nearest_objects:
return nearest_objects[0]
return None
def Draw(self, doc, data, bd, bh, bt, flags) :
"""Ensure the highlighting is active."""
return c4d.TOOLDRAW_HIGHLIGHTS
def GetCursorInfo(self, doc, data, bd, x, y, bc) :
"""Highlight the objects under the cursor."""
hover_obj = self.GetObjectUnderCursor(bd, doc, x, y)
#They're the same object or None, so no need to update highlighting
if self.last_hover == hover_obj:
return True
#Clear out the highlighting on the old objects
if self.last_hover is not None:
if self.last_hover.IsAlive() :
self.last_hover.DelBit(c4d.BIT_HIGHLIGHT)
self.last_hover = None
#Highlight the object under the cursor, and store it for later comparison
if (hover_obj is not None) and hover_obj.IsAlive() :
self.last_hover = hover_obj
hover_obj.SetBit(c4d.BIT_HIGHLIGHT)
#Update the views.
c4d.DrawViews(c4d.DA_ONLY_ACTIVE_VIEW|c4d.DA_NO_THREAD|c4d.DA_NO_ANIMATION)
return True