Get one objects position in another object's coordinate system?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
  11 November 2017
Get one objects position in another object's coordinate system?

Is there a way in mel or python in maya to get one object's position in the coordinate system of another object? I have a camera in a scene that may be rotated in any direction and am trying to measure the distance in its local Z axis to the vertexes of various objects in the scene. This obviously needs to be fast, since it will likely be run thousands of times across the scene.

In Maxscript the command would be something like " in coordsys $camera (" , but I have yet to find something like this in Maya. If there's no direct command to do this, does anyone have a way to calculate it using matrix math?
  11 November 2017
I'm not aware of anything as succint as your maxScript example, but something like this might do the trick!

import maya.OpenMaya as om

mSel = om.MSelectionList()
dagPath = om.MDagPath()
mSel.getDagPath(0, dagPath)

invMatrix = dagPath.inclusiveMatrixInverse()

mSel.getDagPath(1, dagPath)
iter = om.MItMeshVertex(dagPath)

# Storage for positions
points = om.MPointArray()

while not iter.isDone():
   # Multiply each vertex world position by cam inverse matrix
   points.set( iter.position(om.MSpace.kWorld) * invMatrix, iter.index())

# Check points
for i in range(points.length()):
   p = points[i]
   print p.x, p.y, p.z
reply share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 02:51 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.