viewToWorld

311 views
Skip to first unread message

vux

unread,
Aug 27, 2012, 9:59:15 AM8/27/12
to python_in...@googlegroups.com
I have:
1. object in camera view.
2. cameraRight, cameraUp, cameraDirection - (MFnCamera)
3. position under mouse (x,y) in world space - (M3dView.viewToWorld)
4. vectror of pos.3

What i need to substract with 3. to move object in camera space (2d) with correct(start) depth of object to camera

vux

unread,
Aug 27, 2012, 10:30:48 AM8/27/12
to python_in...@googlegroups.com
In other worlds:

I need intersection of [point under mouse\direction] with currentCamera plane through center of object.

Justin Israel

unread,
Aug 27, 2012, 11:20:12 AM8/27/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
Are you asking about being able to move the object based on the users mouse position? But since you say you only have the (x,y), that you want to also maintain the correct original z value? I am not sure my guess is even right. 
You want to move it in z based on what?


Justin Israel

unread,
Aug 27, 2012, 11:21:26 AM8/27/12
to python_in...@googlegroups.com, python_in...@googlegroups.com
Oh sorry. Didn't see your clarification. You want to find the distance of the object from the camera. 



On Aug 27, 2012, at 7:30 AM, vux <vux...@gmail.com> wrote:

In other worlds:

I need intersection of [point under mouse\direction] with currentCamera plane through center of object.

vux

unread,
Aug 27, 2012, 11:59:35 AM8/27/12
to python_in...@googlegroups.com
How to know depth (not distance) from camera to any point in world space?
I had view direction of camera. How to project point to cameraPlane by this vector?

vux

unread,
Aug 27, 2012, 12:01:56 PM8/27/12
to python_in...@googlegroups.com
distance --> is (cameraCenter-objectCenter).distance()
but i need length of perpendicular-projected point to cameraPlane

Justin Israel

unread,
Aug 27, 2012, 1:03:17 PM8/27/12
to python_in...@googlegroups.com
Let me know if I am completely going the wrong way on this...

import maya.OpenMaya as om
import maya.cmds as cmds

cam = cmds.camera(p=(-10,10,5))[0]
loc = cmds.spaceLocator()[0]
cmds.move(6, 2, -8, loc)
cmds.select(cam, loc, r=True)

dag = om.MDagPath()
sel = om.MSelectionList()
om.MGlobal.getActiveSelectionList(sel)
sel.getDagPath(0, dag)
camFn = om.MFnCamera(dag)
sel.getDagPath(1, dag)
tranFn = om.MFnTransform(dag)

p = camFn.eyePoint(om.MSpace.kWorld)
print p.x, p.y, p.z

p2 = tranFn.getTranslation(om.MSpace.kWorld)
print p2.x, p2.y, p2.z

print "Distance:", p.distanceTo(om.MPoint(p2))
print "Depth:", abs(p2.z - p.z)
# Distance: 22.1133443875
# Depth: 13.0

Wouldn't the depth just be the difference in z values between your world points?


On Mon, Aug 27, 2012 at 9:01 AM, vux <vux...@gmail.com> wrote:
> distance --> is (cameraCenter-objectCenter).distance()
> but i need length of perpendicular-projected point to cameraPlane
>>>
>>>

vux

unread,
Aug 27, 2012, 2:10:57 PM8/27/12
to python_in...@googlegroups.com
I need vector from point in 3d to camera with perpendicular projection

vux

unread,
Aug 27, 2012, 2:18:42 PM8/27/12
to python_in...@googlegroups.com


vux

unread,
Aug 27, 2012, 4:51:20 PM8/27/12
to python_in...@googlegroups.com
I think that image at top not good:



vux

unread,
Aug 28, 2012, 6:12:31 AM8/28/12
to python_in...@googlegroups.com
Ok, look at this scene and move cube. I need intersection of ray with camera plane. The ray is pushed from center of Cube with cameraDirection.
With resulting point i will compute depth with pythagora's theorem

vux

unread,
Aug 28, 2012, 9:23:55 AM8/28/12
to python_in...@googlegroups.com
Thanx for all! I found simple solution with trigonometric funcs:

itemsPos = mc.xform(items, q=1, ws=1, t=1)
camPos = mc.xform(currentCamera(), q=1, ws=1, t=1 )
camDir = self.mfnCamera.viewDirection( api.MSpace.kWorld ).normal()
camToObjVector = api.MVector( camPos[0]-itemsPos[0], camPos[1]-itemsPos[1], camPos[2]-itemsPos[2] )

angle = camToObjVector.angle( camDir )
depth = abs( camToObjVector.length() * math.cos(angle) )


vux

unread,
Aug 29, 2012, 6:19:16 AM8/29/12
to python_in...@googlegroups.com
items = mc.ls(sl=1)
viewport = ui.qGetViewport()
dp = api.MDagPath()
view = apiui.M3dView.active3dView()
view.getCamera(dp)
camFn = api.MFnCamera( dp )

itemsPos = mc.xform( items, q=1, ws=1, t=1 )
camPos = mc.xform( obj.currentCamera(), q=1, ws=1, t=1 )
camDir = camFn.viewDirection( api.MSpace.kWorld )
camUp = camFn.upDirection( api.MSpace.kWorld )
camRight = camFn.upDirection( api.MSpace.kWorld )
camObjVector=api.MVector(camPos[0]-itemsPos[0], camPos[1]-itemsPos[1], camPos[2]-itemsPos[2])
angle = camObjVector.angle(camDir)
depth = abs( camObjVector.length() * math.cos(angle) )
depth -= camFn.nearClippingPlane()

wp = api.MPoint()
wv = api.MVector()
view.viewToWorld( 0, 0, wp, wv )

v = camDir * depth

x = wp[0] + v[0]
y = wp[1] + v[1]
z = wp[2] + v[2]

mc.xform( items, t=(x,y,z), ws=True




I have this code to move object under mouse.
autoPlace(um=True) is good but i need more than viewport edges.
So i want manualy move object in camera projection without influences.
Code work well for orthographic views. But not for perspective. I need rotate camDir about somуthing according to camera angle of view. 
Reply all
Reply to author
Forward
0 new messages