Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Reading depth from maya viewport

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
sschoellhammer
761 Views, 7 Replies

Reading depth from maya viewport

Hello!

I'm trying to read the depth from a maya viewport without much luck 😕
Here's my simple code - which is giving me nothing but 0's

https://pastebin.com/sck9njWP2

Has anybody managed to do this?
Thanks so much in advance!

Seb

7 REPLIES 7
Message 2 of 8

Hi,

 

Are you using it with VP2? M3dView::readDepthMap is deprecated In VP2, you'll have to use acquireRenderTarget/acquireRenderTargetFromScreen to get the depth map.

 

Yours,

Li

Message 3 of 8

Hi Li,

 

thanks a lot for getting back to me. That is sort of good to know .. 

If I switch to the old renderer it still won't work though - is that expected?

 

Using VP2 and rendertargets  seems very complicated, I was really hoping there would be a simpler way to get to the depth.

 

I'm fine with loading a rendered image (i.e. from arnold) but also that I can't get to work:

In the following example the image is either a 8 or 32 bit per channel 512x512 tiff 

 

import maya.OpenMaya as om
import maya.OpenMayaUI as omUI

image = om.MImage()
image.readFromFile("D:/GoogleDrive/Work/ProjectTool/z.tif")  # works fine
d = om.MFloatArray(512 *512)
image.setDepthMap(d, 512, 512)
print image.haveDepth() # True
image.readDepthMap("D:/GoogleDrive/Work/ProjectTool/z.tif") # unexpected internal failure with 8 bit 
# # Error: Image conversion (to IFF) failed #  (with 32 bit) 

I can't believe this is so hard. The last solution I can think of is using openImage to load a 32 bit image back but getting that to build for maya alone is hell. 

If you have some more tips to get me my depth please, please tell me 🙂

 

seb

 

 

Message 4 of 8

Hi,

 

I've tried your code with Maya 2017. It looks working fine when I changed the unpack from big-endian to little-endian(<f).  I guess it is still working just not recommended.

 

import struct 
import maya.OpenMaya as om
import maya.OpenMayaUI as omUI


def readDepthMap(x, y):
    view = omUI.M3dView.active3dView()
    width = view.portWidth()
    height = view.portHeight()

    width = 1
    height = 1
    
    numPixels = width*height*4 
    depth = [0.0]*numPixels
    util = om.MScriptUtil()
    util.createFromList(depth, numPixels)
    utilPtr = util.asUcharPtr()

    view.readDepthMap(x, y, width, height, utilPtr, omUI.M3dView.kDepth_Float)

    b1 = om.MScriptUtil.getUcharArrayItem(utilPtr, 0)
    b2 = om.MScriptUtil.getUcharArrayItem(utilPtr, 1)
    b3 = om.MScriptUtil.getUcharArrayItem(utilPtr, 2)
    b4 = om.MScriptUtil.getUcharArrayItem(utilPtr, 3)

    data = [b1,b2,b3,b4]
    print data # always 0's....
    b = struct.pack('4B', *data)
    return struct.unpack('<f', b)

print readDepthMap(1,1)
[0, 255, 127, 59]
(0.0039061903953552246,)

About MImage:readDepthMap, I think it should only work with .iff file type.

 

Yours,

Li

Message 5 of 8

Thanks a lot Li! Now I'm actually getting some sort of data :)))

 

What I'm really trying to do is this:

 

Project a world space point through the camera onto the scene geometry.

I get the point into screen space, look up the depth and get it back into world space. 

 

Here is my function for it:

https://pastebin.com/J8iMT4xm

 

 

but the crux is :

 

depth = readDepthMap(x,y)
ray = om.MVector()
wp = om.MPoint()

view.viewToWorld(x,y,wp, ray)

pProjected = wp + ray * depth

 

which sadly doesn't work as expected. Do you have insight to why not?

  

Thanks again..

 

seb

 

 

 

Message 6 of 8

Hmm, sounds like one of Maya devkit samples:

 

pointManip

 

Please check it out.

 

Yours,

Li

 

Message 7 of 8

Oh brilliant, that seems exactly what I need!!

Message 8 of 8

Hello Li,

 

so it's still not working for me.

 

I built the plugin and I realized that it's working, but only with VP2 with OpenGl, DirectX doesn't work and always gives 0 depth.

When I switch to the old viewport renderer it crashes maya.

 

Sadly I cannot get the same behaviour in my python script even though I replicated the code, here I just get 0 from the depth map. 

 

Oh well, I might just create a maya command to give me what I want.. still the crash there is a bit worrying.

 

Thanks for your extended help!

 

seb

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums