Advertisement
Guest User

Check 2 mesh intersection

a guest
Apr 2nd, 2024
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. import maya.cmds as cmds
  2. import maya.api.OpenMaya as om2
  3. def get_mobject(node_name):
  4.     """
  5.    Get the MObject for a given node name.
  6.    """
  7.     selection_list = om2.MGlobal.getSelectionListByName(node_name)
  8.     if selection_list.length():
  9.         return selection_list.getDependNode(0)
  10.     return None
  11. def check_bounding_box_intersection(mesh1, mesh2):
  12.     """
  13.    Checks if the bounding boxes of two meshes intersect.
  14.    """
  15.     bbox1 = cmds.exactWorldBoundingBox(mesh1)
  16.     bbox2 = cmds.exactWorldBoundingBox(mesh2)
  17.    
  18.     return not (bbox1[3] < bbox2[0] or bbox1[0] > bbox2[3] or
  19.                 bbox1[4] < bbox2[1] or bbox1[1] > bbox2[4] or
  20.                 bbox1[5] < bbox2[2] or bbox1[2] > bbox2[5])
  21. def highlight_vertices(vertices):
  22.     """
  23.    Highlight the specified vertices for visual inspection.
  24.    """
  25.     cmds.select(vertices, replace=True)
  26. def ray_cast_check(mesh1, mesh2):
  27.     """
  28.    Perform a ray cast from each vertex of mesh1 to check if it intersects mesh2.
  29.    Highlight any vertices that do intersect.
  30.    """
  31.     intersecting_vertices = []
  32.     mesh1_fn = om2.MFnMesh(get_mobject(mesh1))
  33.     mesh2_fn = om2.MFnMesh(get_mobject(mesh2))
  34.    
  35.     for i in range(mesh1_fn.numVertices):
  36.         point = mesh1_fn.getPoint(i, om2.MSpace.kWorld)
  37.         # Create a ray starting from the vertex position in the direction of the normal
  38.         normal = mesh1_fn.getVertexNormal(i, True, om2.MSpace.kWorld)
  39.         ray_source = om2.MFloatPoint(point.x, point.y, point.z)
  40.         ray_direction = om2.MFloatVector(normal.x, normal.y, normal.z)
  41.        
  42.         # Check if the ray intersects with mesh2
  43.         hit_point, hit_face = mesh2_fn.closestIntersection(
  44.             om2.MFloatPoint(ray_source),
  45.             om2.MFloatVector(ray_direction),
  46.             om2.MSpace.kWorld,
  47.             10000,
  48.             False)
  49.        
  50.         if hit_face != -1:  # If hit_face is -1, there was no intersection
  51.             vertex_name = f"{mesh1}.vtx[{i}]"
  52.             intersecting_vertices.append(vertex_name)
  53.    
  54.     if intersecting_vertices:
  55.         highlight_vertices(intersecting_vertices)
  56.         cmds.warning(f"Intersection detected at vertices: {intersecting_vertices}")
  57.     else:
  58.         cmds.warning("No intersections detected.")
  59. def main():
  60.     selected_objects = cmds.ls(selection=True)
  61.    
  62.     if len(selected_objects) != 2:
  63.         cmds.warning("Please select exactly two meshes.")
  64.         return
  65.    
  66.     mesh1, mesh2 = selected_objects
  67.    
  68.     if not check_bounding_box_intersection(mesh1, mesh2):
  69.         cmds.warning("Bounding boxes do not intersect. No further checks performed.")
  70.         return
  71.    
  72.     ray_cast_check(mesh1, mesh2)
  73. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement