You aren't using MScriptUtil properly.  You must be sure to keep MScriptUtil
objects around while you are using them, otherwise you're playing with
dangling pointers.  IE:

No!:
hitFacePtr = om.MScriptUtil().asIntPtr()

Ok:
hitFacePtr_u = om.MScriptUtil()
hitFacePtr = hitFacePtr_u.asIntPtr()   # hitFacePtr only safe to use while
hitFacePtr_u lives

There's a paragraph in the docs that explains it:
http://download.autodesk.com/us/maya/2011help/API/class_m_script_util.html

- Chris

On Mon, May 16, 2011 at 7:42 PM, Rik <[email protected]> wrote:

> Sorry should of said which maya I was working with, currently testing
> in maya2011
>
> So Ive tested this out in a few versions of maya, 2010 for example
> this code works fine
> Ive tracked it down to what seems to be the facePtr?
> facePtr = None, code runs no problems and gives correct results
>
> now..if I move
> hitFacePtr = om.MScriptUtil().asIntPtr()
>
> to be declared the line before the hit takes place it works no
> problem...
> again I have only seem to have found this in maya 2011 default or sp1
>
> Strange...
>
> it seems that this combination, declaring our om.MFloatPoint() after
> we try to create a pointer will trip out in maya2011?
> hitFacePtr = om.MScriptUtil().asIntPtr()
> hitPoint   = om.MFloatPoint()
>
>
> On May 13, 4:45 pm, Rik <[email protected]> wrote:
> > Hey guys,
> >
> > First of all a big thank you to all that have contributed to this
> > list, its helped me no end!
> > Its my first time here so apologies if this is a dumb question or Ive
> > completely over looked something??
> >
> > I'm a little mystified.... when I do a meshFn.closestIntersection() I
> > get hitPoint.x = 0?
> > It seems to find y and z fine?
> >
> > Just to show a simplified example the code below is a slightly adapted
> > version from::  [Maya-Python Club:1743] Re: Need an itterator HELP ! !
> >
> > import maya.cmds as mc
> > import maya.OpenMaya as om
> >
> > mc.file(new=True, force=True)
> > mc.polyCube()
> > mc.spaceLocator(name='originLOC')
> > mc.spaceLocator(name='finishLOC')
> > mc.spaceLocator(name='hitLOC')
> > mc.setAttr('originLOC.tx', 5)
> > mc.setAttr('finishLOC.tx', 3)
> > mc.setAttr('hitLOC.tz', 5)
> >
> > ## create 2 locators named start and finish to angle the ray points
> >
> > startWP = mc.xform('originLOC',q=1,rp=1,ws=1)
> > FinishWP = mc.xform('finishLOC',q=1,rp=1,ws=1)
> >
> > vectBtwPnts=  ((startWP  [0] -FinishWP  [0])*-1), ((startWP  [1] -
> > FinishWP  [1])*-1), ((startWP  [2] -FinishWP  [2])*-1)
> > vectorToFinish =
> > om.MFloatVector(vectBtwPnts[0],vectBtwPnts[1],vectBtwPnts[2])
> >
> > def nameToNode(name ):
> >         selectionList = om.MSelectionList()
> >         selectionList.add( name )
> >         node = om.MObject()
> >         selectionList.getDependNode( 0, node )
> >         return node
> >
> > #retireves the right DAG node for selected objects
> >
> > def nameToDag( name ):
> >         selectionList = om.MSelectionList()
> >         selectionList.add( name )
> >         node = om.MDagPath()
> >         selectionList.getDagPath( 0, node )
> >         return node
> >
> > ## this is where the itterator is needed ? ?
> >
> > dag = nameToDag(" pCube1 " )
> >
> > meshFn = om.MFnMesh()
> > meshFn.setObject( dag )
> >
> > raySource = om.MFloatPoint(startWP[0],startWP[1],startWP[2])
> > rayDirection = vectorToFinish
> > rayDirection = rayDirection.normal()
> >
> > hitFacePtr = om.MScriptUtil().asIntPtr()
> > hitPoint   = om.MFloatPoint()
> >
> > idsSorted          = False
> > testBothDirections = False
> > faceIds            = None
> > triIds             = None
> > accelParams        = None
> > hitRayParam        = None
> > hitTriangle        = None
> > hitBary1           = None
> > hitBary2           = None
> >
> > maxParamPtr                  = 99999999
> > worldSpace = om.MSpace.kWorld
> >
> > hit =  meshFn.closestIntersection(raySource,
> > rayDirection,
> > faceIds,
> > triIds,
> > idsSorted,
> > worldSpace,
> > maxParamPtr,
> > testBothDirections,
> > accelParams,
> > hitPoint,
> > hitRayParam,
> > hitFacePtr,
> > hitTriangle,
> > hitBary1,
> > hitBary2)
> >
> > if hit:
> >         hitFace = om.MScriptUtil ( hitFacePtr ).asInt()
> >
> >         faceNumber = hitFace
> >         vector = om.MVector()
> >         NormalFn = om.MFnMesh(dag)
> >         NormalFn.getPolygonNormal(faceNumber, vector, worldSpace)
> >         hitFace = om.MScriptUtil ( hitFacePtr ).asInt()
> >         print "The hit point in X is %f " %hitPoint[0]   # result 0?
> >         print "The hit point in Y is %f " %hitPoint[1]
> >         print "The hit point in Z is %f " %hitPoint[2]
> >         print "The number of the face hit is %d" %hitFace
> >
> >         print "The normal of the hit face is %f in x"   %vector.x
> >         print "The normal of the hit face is %f in y"   %vector.y
> >         print "The normal of the hit face is %f in z"   %vector.z
> >
> >         mc.setAttr("hitLOC.tx", hitPoint[0]) # result 0?
> >         mc.setAttr("hitLOC.ty", hitPoint[1])
> >         mc.setAttr("hitLOC.tz", hitPoint[2])
>
> --
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings:
> http://groups.google.com/group/python_inside_maya/subscribe
>

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to