Crap, sorry for my pyC++ code, I snagged that out of apparently a half
baked plugin I wrote that I never fully converted to python. Here is
an example of an iterator that prints all the mesh shapes in your
scene:
import maya.OpenMaya as OpenMaya
# Create an itorator to iterate over the selection
# Use the MFn class to as a filter to filter geometry types
iter = OpenMaya.MItDag( OpenMaya.MItDag.kBreadthFirst,
OpenMaya.MFn.kGeometric )
# Iterate over selected geometry nodes
while not iter.isDone():
#Define local vars
selectMesh = OpenMaya.MFnMesh()
name = ""
dagpath = OpenMaya.MDagPath()
#Get the dag path of the geometry node
iter.getPath( dagpath )
#Get the mesh from iter
iterMesh = iter.currentItem()
print dagpath.fullPathName()
iter.next()
On Feb 12, 8:44 am, "[email protected]"
<[email protected]> wrote:
> Hey, i ve been writing a bullet collsion tool and have completed the
> first part of the technical part. basically the function is to fire a
> ray and detect the first intersection with a mesh. a big thanks to all
> the people who post here cuz without your help i would nt of been able
> to begin to understand what the hell any of this means! the code is
> good and with 2 locators as ray start and direction it detects
> intersection the name of the face intersected and the normals of the
> face.
>
> The next stage is to build in an itterator that will cycle through all
> the meshs in a scene? any suggestions ? at the moment it just looks
> for pcube1
>
> import maya.cmds as mc
> import maya.OpenMaya as om
>
> ## create 2 locators named start and finish to angle the ray points
>
> startWP = mc.xform('start',q=1,rp=1,ws=1)
> FinishWP = mc.xform('Finish',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]
> 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
--~--~---------~--~----~------------~-------~--~----~
Yours,
Maya-Python Club Team.
-~----------~----~----~----~------~----~------~--~---