Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import maya.cmds as cmds
- import maya.api.OpenMaya as om2
- def get_dag_path(node_name):
- """
- Get the MDagPath for a given node name.
- """
- selection_list = om2.MSelectionList()
- selection_list.add(node_name)
- try:
- dag_path = selection_list.getDagPath(0)
- return dag_path
- except:
- raise ValueError(f"Could not get MDagPath for {node_name}. Ensure it is a mesh.")
- def check_bounding_box_intersection(mesh1, mesh2):
- """
- Checks if the bounding boxes of two meshes intersect.
- """
- bbox1 = cmds.exactWorldBoundingBox(mesh1)
- bbox2 = cmds.exactWorldBoundingBox(mesh2)
- return not (bbox1[3] < bbox2[0] or bbox1[0] > bbox2[3] or
- bbox1[4] < bbox2[1] or bbox1[1] > bbox2[4] or
- bbox1[5] < bbox2[2] or bbox1[2] > bbox2[5])
- def highlight_vertices(vertices):
- """
- Highlight the specified vertices for visual inspection.
- """
- cmds.select(vertices, replace=True)
- def ray_cast_check(mesh1, mesh2):
- """
- Perform a ray cast from each vertex of mesh1 to check if it intersects mesh2.
- Highlight any vertices that do intersect.
- """
- intersecting_vertices = []
- try:
- dag_path1 = get_dag_path(mesh1)
- dag_path2 = get_dag_path(mesh2)
- mesh_fn1 = om2.MFnMesh(dag_path1)
- mesh_fn2 = om2.MFnMesh(dag_path2)
- except ValueError as e:
- cmds.warning(str(e))
- return
- for i in range(mesh_fn1.numVertices):
- point = mesh_fn1.getPoint(i, om2.MSpace.kWorld)
- normal = mesh_fn1.getVertexNormal(i, True, om2.MSpace.kWorld)
- ray_source = om2.MFloatPoint(point.x, point.y, point.z)
- ray_direction = om2.MFloatVector(normal.x, normal.y, normal.z)
- hit_point, hit_face = mesh_fn2.closestIntersection(
- om2.MFloatPoint(ray_source),
- om2.MFloatVector(ray_direction),
- om2.MSpace.kWorld,
- 10000,
- False)
- if hit_face != -1:
- vertex_name = f"{mesh1}.vtx[{i}]"
- intersecting_vertices.append(vertex_name)
- if intersecting_vertices:
- highlight_vertices(intersecting_vertices)
- cmds.warning(f"Intersection detected at vertices: {intersecting_vertices}")
- else:
- cmds.warning("No intersections detected.")
- def main():
- selected_objects = cmds.ls(selection=True)
- if len(selected_objects) != 2:
- cmds.warning("Please select exactly two meshes.")
- return
- # Validate selected objects are meshes
- for s in selected_objects:
- relatives = cmds.listRelatives(s, shapes=True) or []
- if not relatives:
- cmds.warning(f"No shapes found in {s}. Ensure both selected objects are meshes.")
- return
- if not all(cmds.objectType(rel, isType='mesh') for rel in relatives):
- cmds.warning(f"{s} is not a mesh.")
- return
- mesh1, mesh2 = selected_objects
- if not check_bounding_box_intersection(mesh1, mesh2):
- cmds.warning("Bounding boxes do not intersect. No further checks performed.")
- return
- ray_cast_check(mesh1, mesh2)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement