Hi all,
i got the following code to catch mouse position over viewport -> ray gets shoot from this position onto an object. If the ray hits the object the 3D coordinates of hit point is calculated.
Then Locator is moved to this coordinate…
It’s working so far BUT the y axis seems to be inverted. When moving the cursor up the Locator moves down. Right and Left are working as expected.
What am i donig wrong?
PS: The code is installed as event filter on a custom window covering the 3d view. The basic idea behind it is to move an object in the viewport even when another window is obscuring the 3d view…
Thanks in advance
if event.type() == QtCore.QEvent.MouseMove:
initPos = [event.globalX(), event.globalY()]
print 'x: ' + str(event.globalX()) + ' y: ' + str(event.globalY())
currentView = omui.M3dView().active3dView()
camDP = om.MDagPath()
currentView.getCamera(camDP)
camFn = om.MFnCamera(camDP)
farclip = camFn.farClippingPlane()
worldPos, worldDir = viewToWorld(event.globalX(), event.globalY())
closestHit = None
closestObj = None
state, hit, fnMesh, facePtr, triPtr = intersect(targetObj, worldPos, worldDir, farclip)
if state is True:
dif = [hit.x - worldPos[0],hit.y - worldPos[1],hit.z - worldPos[2]]
distToCam = math.sqrt(math.pow(float(dif[0]),2) + math.pow(float(dif[1]),2) + math.pow(float(dif[2]),2))
if closestHit == None:
closestHit = distToCam
closestObj = [state, hit, fnMesh, facePtr, triPtr]
elif distToCam < closestHit:
closestHit = distToCam
closestObj = [state, hit, fnMesh, facePtr, triPtr]
if closestObj is not None:
state, hit, fnMesh, facePtr, triPtr = closestObj
mHit = om.MPoint(hit)
normal = om.MVector()
fnMesh.getClosestNormal(mHit, normal, om.MSpace.kWorld, None)
tangent = crossProduct(normal,worldUp)
upAxis = crossProduct(normal,tangent)
matrix = [tangent.x, tangent.y, tangent.z, 0, upAxis.x, upAxis.y, upAxis.z, 0, normal.x, normal.y, normal.z, 0, hit.x, hit.y, hit.z, 0]
print 'hit! ' + ' x: ' + str(hit.x) + ' y: ' + str(hit.y) + ' z: ' + str(hit.z)