"""UE5 Python API collision/trace 探测脚本。 测试哪些碰撞查询 API 在 UE5.7 Python 中可用。 用法: exec(open(r"...\probe_collision_api.py").read()) """ import unreal print("=" * 60) print("UE5 Collision API Probe") print("=" * 60) # ---- 1. ETraceTypeQuery enum ---- print("\n--- ETraceTypeQuery ---") try: val = unreal.ETraceTypeQuery.TRACE_TYPE_QUERY1 print(f" ETraceTypeQuery.TRACE_TYPE_QUERY1 = {val}") except Exception as e: print(f" ETraceTypeQuery: FAIL - {e}") try: val = unreal.TraceTypeQuery.TRACE_TYPE_QUERY1 print(f" TraceTypeQuery.TRACE_TYPE_QUERY1 = {val}") except Exception as e: print(f" TraceTypeQuery: FAIL - {e}") # Try integer directly print("\n--- line_trace_single with integer channel ---") try: # Spawn a test cube mesh = unreal.load_asset("/Game/BlockingStarterPack/Meshes/Primitives/SM_Cube.SM_Cube") if mesh: subsys = unreal.get_editor_subsystem(unreal.EditorActorSubsystem) actor = subsys.spawn_actor_from_object(mesh, unreal.Vector(0, 0, 0)) if actor: bb = mesh.get_bounding_box() print(f" Cube bbox: min=({bb.min.x},{bb.min.y},{bb.min.z}) max=({bb.max.x},{bb.max.y},{bb.max.z})") # Try line_trace_single with int 0 try: hit = unreal.SystemLibrary.line_trace_single( actor, unreal.Vector(50, 50, 200), unreal.Vector(50, 50, -200), 0, # integer instead of enum False, [], None, ) if hit: print(f" line_trace_single(int=0): HIT at ({hit.impact_point.x},{hit.impact_point.y},{hit.impact_point.z})") else: print(f" line_trace_single(int=0): no hit") except Exception as e: print(f" line_trace_single(int=0): FAIL - {e}") # Try with None for draw_debug try: hit = unreal.SystemLibrary.line_trace_single( actor, unreal.Vector(50, 50, 200), unreal.Vector(50, 50, -200), 0, False, [], ) if hit: print(f" line_trace_single(6-arg): HIT at ({hit.impact_point.x},{hit.impact_point.y},{hit.impact_point.z})") else: print(f" line_trace_single(6-arg): no hit") except Exception as e: print(f" line_trace_single(6-arg): FAIL - {e}") # ---- 2. sphere_overlap_components ---- print("\n--- sphere_overlap_components ---") try: result = unreal.SystemLibrary.sphere_overlap_components( actor, unreal.Vector(50, 50, 50), 20.0, [], # object types unreal.StaticMeshComponent, ) print(f" sphere_overlap_components: found {len(result) if result else 0} components") except Exception as e: print(f" sphere_overlap_components: FAIL - {e}") # ---- 3. box_overlap_components ---- print("\n--- box_overlap_components ---") try: result = unreal.SystemLibrary.box_overlap_components( actor, unreal.Vector(50, 50, 50), unreal.Vector(10, 10, 10), [], unreal.StaticMeshComponent, ) print(f" box_overlap_components: found {len(result) if result else 0} components") except Exception as e: print(f" box_overlap_components: FAIL - {e}") # ---- 4. component-level queries ---- print("\n--- Component-level queries ---") try: smc = actor.get_component_by_class(unreal.StaticMeshComponent) if smc: print(f" SMC found: {smc.get_name()}") # get_closest_point_on_collision try: cp = smc.get_closest_point_on_collision(unreal.Vector(0, 0, 200)) if cp: print(f" get_closest_point_on_collision(0,0,200): ({cp.x},{cp.y},{cp.z})") else: print(f" get_closest_point_on_collision: returned None") except Exception as e: print(f" get_closest_point_on_collision: FAIL - {e}") # get_bounding_box on component try: cbb = smc.get_bounding_box() print(f" component.get_bounding_box: min=({cbb.min.x},{cbb.min.y},{cbb.min.z}) max=({cbb.max.x},{cbb.max.y},{cbb.max.z})") except Exception as e: print(f" component.get_bounding_box: FAIL - {e}") except Exception as e: print(f" Component queries: FAIL - {e}") # Cleanup unreal.EditorLevelLibrary.destroy_actor(actor) except Exception as e: print(f" Setup error: {e}") # ---- 5. Check what SystemLibrary has ---- print("\n--- SystemLibrary overlap/trace methods ---") methods = [m for m in dir(unreal.SystemLibrary) if 'overlap' in m.lower() or 'trace' in m.lower()] for m in methods: print(f" {m}") print("\n--- EditorLevelLibrary methods ---") methods = [m for m in dir(unreal.EditorLevelLibrary) if 'overlap' in m.lower() or 'trace' in m.lower()] for m in methods: print(f" {m}") print("\n" + "=" * 60) print("Probe complete.") # Write results to file try: from pathlib import Path out = Path(__file__).parent.parent.parent / "output" / "asset-catalog" / "probe_result.txt" out.parent.mkdir(parents=True, exist_ok=True) with open(out, "w", encoding="utf-8") as f: f.write("See UE5 Output Log for probe results.\n") print(f"Result marker written to {out}") except Exception: pass # Quit editor unreal.SystemLibrary.execute_console_command(None, "quit")