the result i want
How to use PythonScript to make this in maya2022
I tried to use raycast to get point under the cube usingOpenMaya.MFnMesh.closestIntersection()
in order to fall the cube at the position of point.
But editor just give a RuntimeError at this line of codehit, _, hit_point, _ = mesh_fn.closestIntersection(ray_source, ray_direction, OpenMaya.MSpace.kWorld, 100, False)
without any extra infos.
Here is my code in total
<code>import maya.cmds as cmds
import maya.api.OpenMaya as OpenMaya
def ray_intersects_object(ray_start, ray_direction, object_name):
selection_list = OpenMaya.MSelectionList()
selection_list.add(object_name)
dag_path = selection_list.getDagPath(0)
mesh_fn = OpenMaya.MFnMesh(dag_path)
# ray_source = OpenMaya.MFloatPoint(ray_start[0], ray_start[1], ray_start[2])
ray_source = OpenMaya.MFloatPoint(0., 0., 0.)
ray_direction = OpenMaya.MFloatVector(ray_direction[0], ray_direction[1], ray_direction[2])
v = OpenMaya.MFloatVector(0.,1.,0.)
hit_point = OpenMaya.MFloatPoint()
hit, _, hit_point, _ = mesh_fn.closestIntersection(ray_source, ray_direction, OpenMaya.MSpace.kWorld, 100, False)
if hit:
return True, [hit_point.x, hit_point.y, hit_point.z]
else:
return False, None
start_point = (0, 10, 0)
direction = (0, 1, 0)
object_to_check = 'ground' # Replace with the name of your object in Maya
ray_intersects_object(start_point, direction, object_to_check)
</code>
<code>import maya.cmds as cmds
import maya.api.OpenMaya as OpenMaya
def ray_intersects_object(ray_start, ray_direction, object_name):
selection_list = OpenMaya.MSelectionList()
selection_list.add(object_name)
dag_path = selection_list.getDagPath(0)
mesh_fn = OpenMaya.MFnMesh(dag_path)
# ray_source = OpenMaya.MFloatPoint(ray_start[0], ray_start[1], ray_start[2])
ray_source = OpenMaya.MFloatPoint(0., 0., 0.)
ray_direction = OpenMaya.MFloatVector(ray_direction[0], ray_direction[1], ray_direction[2])
v = OpenMaya.MFloatVector(0.,1.,0.)
hit_point = OpenMaya.MFloatPoint()
hit, _, hit_point, _ = mesh_fn.closestIntersection(ray_source, ray_direction, OpenMaya.MSpace.kWorld, 100, False)
if hit:
return True, [hit_point.x, hit_point.y, hit_point.z]
else:
return False, None
start_point = (0, 10, 0)
direction = (0, 1, 0)
object_to_check = 'ground' # Replace with the name of your object in Maya
ray_intersects_object(start_point, direction, object_to_check)
</code>
import maya.cmds as cmds
import maya.api.OpenMaya as OpenMaya
def ray_intersects_object(ray_start, ray_direction, object_name):
selection_list = OpenMaya.MSelectionList()
selection_list.add(object_name)
dag_path = selection_list.getDagPath(0)
mesh_fn = OpenMaya.MFnMesh(dag_path)
# ray_source = OpenMaya.MFloatPoint(ray_start[0], ray_start[1], ray_start[2])
ray_source = OpenMaya.MFloatPoint(0., 0., 0.)
ray_direction = OpenMaya.MFloatVector(ray_direction[0], ray_direction[1], ray_direction[2])
v = OpenMaya.MFloatVector(0.,1.,0.)
hit_point = OpenMaya.MFloatPoint()
hit, _, hit_point, _ = mesh_fn.closestIntersection(ray_source, ray_direction, OpenMaya.MSpace.kWorld, 100, False)
if hit:
return True, [hit_point.x, hit_point.y, hit_point.z]
else:
return False, None
start_point = (0, 10, 0)
direction = (0, 1, 0)
object_to_check = 'ground' # Replace with the name of your object in Maya
ray_intersects_object(start_point, direction, object_to_check)
1