check for surface collisions along particular vector

28 views
Skip to first unread message

e955...@gmail.com

unread,
Oct 8, 2014, 9:17:47 AM10/8/14
to python_in...@googlegroups.com
Hi,

am struggling a little with MfnMesh stuff. I have the coords of a point in space and i want to be able to fire off a vector from this point with direction in negative x. and then i want the script to print the coords of any collision points with a mesh.

here is some code i found which comes very close to solving it. but im just not smart enough to adapt slightly.

create a polySphere and run this:
#########################################

import maya.cmds as cmds
import maya.OpenMaya as om

def returnCollisionPoints(point=(10.0,2.0,3.0), dir=(-1.0, 0.0, 0.0)):
sel = om.MSelectionList()
dag = om.MDagPath()

#replace torus with arbitrary shape name
sel.add("pSphereShape1")
sel.getDagPath(0,dag)

mesh = om.MFnMesh(dag)

point = om.MFloatPoint(*point)
dir = om.MFloatVector(*dir)
hitPoints = om.MFloatPointArray()

mesh.allIntersections(
point, dir,
None, None,
False, om.MSpace.kWorld,
10000, False,
None,
None,
hitPoints,
None, None,
None, None,
None
)
return hitPoints

print returnCollisionPoints()
################################################

so it to be able to print out hitPoints as coordinates i guess. Does anyone know how to do this?

thanks alot,
Sam

Jack Straw

unread,
Oct 8, 2014, 12:02:23 PM10/8/14
to python_in...@googlegroups.com, e955...@gmail.com
Heres a little bit of code to print out the corrdinates;

intersections = returnCollisionPoints()
for pnt_idx in range(intersections.length()):
    print intersections[pnt_idx][0], intersections[pnt_idx][1], intersections[pnt_idx][4]

Writing it using the Python 2.0 API

Jack Straw

unread,
Oct 8, 2014, 12:04:32 PM10/8/14
to python_in...@googlegroups.com, e955...@gmail.com
Writing it using the maya Python 2.0 API might make it simpler though as the array types can be iterated over much more easily.

e955...@gmail.com

unread,
Oct 8, 2014, 2:34:13 PM10/8/14
to python_in...@googlegroups.com, e955...@gmail.com
wow, brilliant, thanks so much Jack

sam;)

Reply all
Reply to author
Forward
0 new messages