Advertisement
Guest User

Untitled

a guest
Apr 4th, 2024
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. import maya.cmds as cmds
  2. import maya.api.OpenMaya as om2
  3.  
  4. def get_dag_path(node_name):
  5.     """
  6.    Get the MDagPath for a given node name.
  7.    """
  8.     selection_list = om2.MSelectionList()
  9.     selection_list.add(node_name)
  10.     try:
  11.         dag_path = selection_list.getDagPath(0)
  12.         return dag_path
  13.     except:
  14.         raise ValueError(f"Could not get MDagPath for {node_name}. Ensure it is a mesh.")
  15.  
  16. def check_bounding_box_intersection(mesh1, mesh2):
  17.     """
  18.    Checks if the bounding boxes of two meshes intersect.
  19.    """
  20.     bbox1 = cmds.exactWorldBoundingBox(mesh1)
  21.     bbox2 = cmds.exactWorldBoundingBox(mesh2)
  22.    
  23.     return not (bbox1[3] < bbox2[0] or bbox1[0] > bbox2[3] or
  24.                 bbox1[4] < bbox2[1] or bbox1[1] > bbox2[4] or
  25.                 bbox1[5] < bbox2[2] or bbox1[2] > bbox2[5])
  26.  
  27. def highlight_vertices(vertices):
  28.     """
  29.    Highlight the specified vertices for visual inspection.
  30.    """
  31.     cmds.select(vertices, replace=True)
  32.  
  33. def ray_cast_check(mesh1, mesh2):
  34.     """
  35.    Perform a ray cast from each vertex of mesh1 to check if it intersects mesh2.
  36.    Highlight any vertices that do intersect.
  37.    """
  38.     intersecting_vertices = []
  39.  
  40.     try:
  41.         dag_path1 = get_dag_path(mesh1)
  42.         dag_path2 = get_dag_path(mesh2)
  43.         mesh_fn1 = om2.MFnMesh(dag_path1)
  44.         mesh_fn2 = om2.MFnMesh(dag_path2)
  45.     except ValueError as e:
  46.         cmds.warning(str(e))
  47.         return
  48.  
  49.     for i in range(mesh_fn1.numVertices):
  50.         point = mesh_fn1.getPoint(i, om2.MSpace.kWorld)
  51.         normal = mesh_fn1.getVertexNormal(i, True, om2.MSpace.kWorld)
  52.        
  53.         ray_source = om2.MFloatPoint(point.x, point.y, point.z)
  54.         ray_direction = om2.MFloatVector(normal.x, normal.y, normal.z)
  55.        
  56.         hit_point, hit_face = mesh_fn2.closestIntersection(
  57.             om2.MFloatPoint(ray_source),
  58.             om2.MFloatVector(ray_direction),
  59.             om2.MSpace.kWorld,
  60.             10000,
  61.             False)
  62.        
  63.         if hit_face != -1:
  64.             vertex_name = f"{mesh1}.vtx[{i}]"
  65.             intersecting_vertices.append(vertex_name)
  66.    
  67.     if intersecting_vertices:
  68.         highlight_vertices(intersecting_vertices)
  69.         cmds.warning(f"Intersection detected at vertices: {intersecting_vertices}")
  70.     else:
  71.         cmds.warning("No intersections detected.")
  72.  
  73. def main():
  74.     selected_objects = cmds.ls(selection=True)
  75.    
  76.     if len(selected_objects) != 2:
  77.         cmds.warning("Please select exactly two meshes.")
  78.         return
  79.    
  80.     # Validate selected objects are meshes
  81.     for s in selected_objects:
  82.         relatives = cmds.listRelatives(s, shapes=True) or []
  83.         if not relatives:
  84.             cmds.warning(f"No shapes found in {s}. Ensure both selected objects are meshes.")
  85.             return
  86.         if not all(cmds.objectType(rel, isType='mesh') for rel in relatives):
  87.             cmds.warning(f"{s} is not a mesh.")
  88.             return
  89.    
  90.     mesh1, mesh2 = selected_objects
  91.    
  92.     if not check_bounding_box_intersection(mesh1, mesh2):
  93.         cmds.warning("Bounding boxes do not intersect. No further checks performed.")
  94.         return
  95.    
  96.     ray_cast_check(mesh1, mesh2)
  97.  
  98. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement