Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nobolu-ootsuka-unrealengine/b7977d2a790522ea6b153f3e1a2e30b9 to your computer and use it in GitHub Desktop.
Save nobolu-ootsuka-unrealengine/b7977d2a790522ea6b153f3e1a2e30b9 to your computer and use it in GitHub Desktop.
Mayaで指定ベクトルの衝突判定を行う
# -*- coding: utf-8 -*-
#===========================================
# カメラから直線方向にある選択メッシュに対してRayを飛ばす
# (視野角は考慮してないんでカメラは方向だけです。)
#===========================================
import maya.api.OpenMaya as om
# vector用カメラの諸々を取得
camera_selList = om.MGlobal.getSelectionListByName("camera1")
camera_dag = camera_selList.getDagPath(0)
camera_transform = camera_dag.transform()
camera_transform_MFn = om.MFnTransform(camera_transform)
matrix = camera_transform_MFn.transformation()
camera_position = om.MFloatPoint(matrix.translation(om.MSpace.kWorld))
camera_MFn = om.MFnCamera(camera_dag)
camera_vector = om.MFloatVector(camera_MFn.viewDirection(om.MSpace.kWorld))
# メッシュごとのレイを取得
search_distance = 10000;
mesh_selList = om.MGlobal.getActiveSelectionList()
for n in xrange(mesh_selList.length()):
current_mesh_dag = mesh_selList.getDagPath(n)
current_mesh_MFn = om.MFnMesh(current_mesh_dag)
hitPoints,hitRayParams,hitFaces,hitTriangles,hitBary1s,hitBary2s = current_mesh_MFn.allIntersections(camera_position, camera_vector, om.MSpace.kWorld, search_distance, True)
# ちなみにanyIntersection関数だと貫通は判定しない
# hitPoints,hitRayParams,hitFaces,hitTriangles,hitBary1s,hitBary2s = current_mesh_MFn.anyIntersection(camera_position, camera_vector, om.MSpace.kWorld, search_distance, True)
print "\n --- {} ---".format(current_mesh_MFn.name())
print u" ヒットした座標 : {}".format(hitPoints)
print u" ヒットまでの距離 : {}".format(hitRayParams)
print u" ヒットしたフェースのID : {}".format(hitFaces)
print u" トライアングルのID : {}".format(hitTriangles)
print u" ヒットしたポイントの最初の重心位置 : {}".format(hitBary1s)
print u" ヒットしたポイントの二つ目の重心位置: {}".format(hitBary2s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment