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_mobject(node_name):
- """
- Get the MObject for a given node name.
- """
- selection_list = om2.MGlobal.getSelectionListByName(node_name)
- if selection_list.length():
- return selection_list.getDependNode(0)
- return None
- 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 = []
- mesh1_fn = om2.MFnMesh(get_mobject(mesh1))
- mesh2_fn = om2.MFnMesh(get_mobject(mesh2))
- for i in range(mesh1_fn.numVertices):
- point = mesh1_fn.getPoint(i, om2.MSpace.kWorld)
- # Create a ray starting from the vertex position in the direction of the normal
- normal = mesh1_fn.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)
- # Check if the ray intersects with mesh2
- hit_point, hit_face = mesh2_fn.closestIntersection(
- om2.MFloatPoint(ray_source),
- om2.MFloatVector(ray_direction),
- om2.MSpace.kWorld,
- 10000,
- False)
- if hit_face != -1: # If hit_face is -1, there was no intersection
- 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
- 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