Discussion:
[Maya-Python] HUD Distance from camera.
Macbeth R.
10 years ago
Permalink
Does somebody know how the Distance From Camera info in the Heads Up
Display is being query?

I canÂŽt find the script inside the maya folder, it is a pre script but is
not being show in iniHUDScripts.mel or iniHUD.mel is
named: HUDObjDetDistFromCam

I try different ways of getting the same value, xform, translate, center,
bounding box, none is giving me the same numbers as the HUD gives me,
besides, I had to use different method for different objects (alembics,
plugin caches like yeti fur, arnold standins) but the HUD seems to be
always right doesn't matter which kind of object you select, how is this
being achieved?

I know I can query the HUD result directly from there, but I had to select
every object before doing that, and turn on Object Details in HUD

Can I call the procedure HUD is using, directly from mel or python?

Some examples, :


import maya.cmds as cmds
import maya.OpenMaya as om
import maya.OpenMayaUI as mui

#-------------------------- HUD Distance from Camera (you need to turn on
Object Details)

print(cmds.headsUpDisplay("HUDObjDetDistFromCam", q=1, sr=1))

#------------------------- X FORM
P1 = cmds.xform("camara", q=1, ws=1, t=1)
P2= cmds.xform(cmds.ls(sl=1), q=1, ws=1, t=1)
v1, v2 = om.MVector(*P1), om.MVector(*P2)
distanceFromCamera=om.MVector(v2-v1).length()
print "Distancia", distanceFromCamera

#------------------------- TRANSLATE
P1 = cmds.getAttr("camara.translate")
P2= cmds.getAttr(cmds.ls(sl=1)[0]+".translate")
v1, v2 = om.MVector(*P1[0]), om.MVector(*P2[0])
distanceFromCamera=om.MVector(v2-v1).length()
print "Distancia", distanceFromCamera

#------------------------- CENTER
P1 = cmds.getAttr("camara.translate")
P2= cmds.getAttr(cmds.ls(sl=1)[0]+".center")
v1, v2 = om.MVector(*P1[0]), om.MVector(*P2[0])
distanceFromCamera=om.MVector(v2-v1).length()
print "Distancia", distanceFromCamera
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/7be9473d-745e-4c13-b892-69058db807f1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Anthony Tan
10 years ago
Permalink
Bit quiet at work today while I wait for a shot to bubble through, so
thought I'd do a quick writeup

*TL;DR - it's not geometric distance, it's the focal plane distance (as
in, the distance along the camera axis to the focal plane that contains
the point you're looking at) - *if anyone wants I can do up a sketch and
a decenter write up, just yell, this was me poking around in maya more
than the maths


*As a simple proof/demo:*
- create a camera, create a polycube.
- set the transform on the polycube to be -1 on Z. Distance will be 1.
- set the transform on the polycube to be -1 on Z, and 1 on X.
Distance is still 1, even though we have clearly changed the
geometric distance, it's still sliding on the focal plane 1 unit
away from the camera.
- move your camera wherever you want on the X/Y plane. Distance is still
1, because you're moving on the plane.
- Rotate your camera. Distance changes because by rotation, you're
shifting your focal plane around


*And the longwinded answer..* Under the hood in Maya, I suspect the
object's bounding box center is being used (I thought it was the
worldMatrix at first, but you can see this isn't the case trivially by
taking a cube, and moving all the verts. Distance will change, even
though the worldMatrix is static). Disclaimer - I've only done limited
testing so you might find a novel case where this doesn't hold, but the
problem is more for you to figure out what point you want to test.

I am pretty sure this is the case in the HUD however, because it makes
sense AND it explains the funny quirk that a camera is always apparently
0.057 units away from itself - the camera's bbox center happens to be
[0,0.210,-0.057] units in local space offset and since it's in local
space the only distance we care about is the traversal along the cam
normal which is -Z, and 0.057 units. Tada! :D


# here's some sample code to illustrate, more verbose than usual by way
# of documentation - we use PyMEL here at the office by the way, but
# that doesn't change the principles

import pymel.core as pm

# select camera, and apply the worldMatrix to the -Z basis vector to get
# the perturbed camera normal. this will accommodate scale/rotate/skew
# if you really really wanted to do that, and because we're using the WM
# it should manage all manner of grouping etc etc (camera points down
# -ve Z axis unless you've done something *incredibly* funky)

cam = pm.PyNode('camera1') # just selecting the camera I have here
cam_normal = cam.getAttr('worldMatrix').transpose() *
pm.dt.Vector([0,0,-1])
# Transpose to switch between row vs col major w/pymel kindly mangling
# the vector with an implicit zero element in position 4 I believe. You
# probably want to rewrite this for MVectors if you're happier with them

cam_normal.normalize() cam_position =
cam.getAttr('worldMatrix').translate
# do NOT use center, it will be wrong even if you account for the parent

# this is where you'd want to start to loop over your object list
distance_object = pm.PyNode('pCube2') object_vector =
distance_object.getAttr('parentMatrix').translate +
distance_object.getAttr('center') - cam_position

# GOTCHA: the .center attribute folds in the local object space
# transform, but won't give you parent level transforms so you need to
# do that explicitly if you use this approach style.

print object_vector.dot(cam_normal)
# project the object vector onto the camera normal (yay for dot
# products) to find out how may units along it we are. vector and normal
# point away, so +ve numbers are in front, and -ve are behind.


-Anthony
...
Links:

1. https://groups.google.com/d/msgid/python_inside_maya/7be9473d-745e-4c13-b892-69058db807f1%40googlegroups.com?utm_medium=email&utm_source=footer
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/1429804387.3081289.257639721.409B9198%40webmail.messagingengine.com.
For more options, visit https://groups.google.com/d/optout.
Macbeth R.
10 years ago
Permalink
Thanks a lot! Anthony, I will take a look and tell you the results after
weekend.

Greetings!
Thanks again.
...
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAB3%3DZYrqQitPOvH74jtU4qZes77EiuXDuU%3Dpz5vacwZ-rr-cnA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...