[Maya] viewToWorld inverted Y axis Problem

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)

SOLVED - this code worked for me:

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.MouseButtonPress:
            if not self.shift_key_filter.shift_down:
                return False
            try:
                #get pixel position and 3d ray
                pos = event.pos()
                height = active_view.portHeight()
                x = pos.x()
                y = height-pos.y()#*-1
                ray_source = om.MPoint()
                ray_direction = om.MVector()
                active_view.viewToWorld(x,y, ray_source, ray_direction)
                
                #get mesh dag path
                sel = om.MSelectionList()
                sel.add(self.mesh)
                mesh_dag = om.MDagPath()
                sel.getDagPath(0,mesh_dag)
                
                #cast the ray at the mesh and find out if and where it hits
                hit_pnt, face_idx = closest_mesh_intersection(mesh_dag, ray_source, ray_direction)
                hit_pnt = om.MPoint(hit_pnt.x, hit_pnt.y, hit_pnt.z)
                
                if face_idx > -1:
                    #get normal
                    mitface = om.MItMeshPolygon(mesh_dag)
                    dummy_int_util = om.MScriptUtil()
                    dummy_int_ptr = dummy_int_util.asIntPtr()   
                    mitface.setIndex(face_idx, dummy_int_ptr)
                    normal = om.MVector()
                    mitface.getNormal(normal)
                    
                    create_block(self.mesh, hit_pnt, normal)        
                return False
            except:
                qt_maya_window.removeEventFilter(shift_key_filter)
                qt_active_view.removeEventFilter(minecraft_click_filter)
                raise
        return False

def closest_mesh_intersection(mesh_dag, ray_source, ray_direction):
    meshFn = om.MFnMesh(mesh_dag)
    hit_pnt = om.MFloatPoint()
    ray_source_float = om.MFloatPoint(ray_source.x, ray_source.y, ray_source.z)
    ray_direction_float = om.MFloatVector(ray_direction.x, ray_direction.y, ray_direction.z)
    
    #face idx pointer
    face_idx_util = om.MScriptUtil()
    face_idx_util.createFromInt(-1)
    face_int_ptr = face_idx_util.asIntPtr()
    
    meshFn.closestIntersection(
        ray_source_float,#const MFloatPoint &   raySource,
        ray_direction_float,#const MFloatVector &   rayDirection,
        None, #const MIntArray *    faceIds,
        None, #const MIntArray *    triIds,
        False, #bool    idsSorted,
        om.MSpace().kWorld, #MSpace::Space  space,
        9999, #float    maxParam,
        False, #bool    testBothDirections,
        None, #MMeshIsectAccelParams *  accelParams,
        hit_pnt, #MFloatPoint &     hitPoint,
        None, #float *  hitRayParam,
        face_int_ptr, #int *    hitFace,
        None, #int *    hitTriangle,
        None, #float *  hitBary1,
        None, #float *  hitBary2,
        .000001 #float  tolerance = 1e-6,
        )
        
    face_idx = face_idx_util.getInt(face_int_ptr)   
    return (hit_pnt, face_idx)