<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Reading depth from maya viewport in Maya Programming Forum</title>
    <link>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7011216#M12014</link>
    <description>&lt;P&gt;Hmm, sounds like one of Maya devkit samples:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://help.autodesk.com/view/MAYAUL/2017/ENU/?guid=__cpp_ref_point_manip_2point_manip_8cpp_example_html" target="_self"&gt;pointManip&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please check it out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yours,&lt;/P&gt;
&lt;P&gt;Li&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Apr 2017 07:17:38 GMT</pubDate>
    <dc:creator>cheng_xi_li</dc:creator>
    <dc:date>2017-04-12T07:17:38Z</dc:date>
    <item>
      <title>Reading depth from maya viewport</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7007114#M12009</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I'm trying to read the depth from a maya viewport without much luck &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt;&lt;BR /&gt;Here's my simple code - which is giving me nothing but 0's&lt;/P&gt;&lt;P&gt;&lt;A href="https://pastebin.com/sck9njWP" target="_blank" rel="nofollow noopener"&gt;https://pastebin.com/sck9njWP&lt;SPAN&gt;2&lt;/SPAN&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Has anybody managed to do this?&lt;BR /&gt;Thanks so much in advance!&lt;/P&gt;&lt;P&gt;Seb&lt;/P&gt;</description>
      <pubDate>Mon, 10 Apr 2017 16:22:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7007114#M12009</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-04-10T16:22:31Z</dc:date>
    </item>
    <item>
      <title>Re: Reading depth from maya viewport</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7008248#M12010</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you using it with VP2? M3dView::readDepthMap is deprecated In VP2, you'll have to use &lt;A href="http://help.autodesk.com/view/MAYAUL/2016/ENU/?guid=__cpp_ref_class_m_h_w_render_1_1_m_render_target_manager_html" target="_self"&gt;acquireRenderTarget/&lt;SPAN&gt;acquireRenderTargetFromScreen&lt;/SPAN&gt;&amp;nbsp;&lt;/A&gt;to get the depth map.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yours,&lt;/P&gt;
&lt;P&gt;Li&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 05:25:29 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7008248#M12010</guid>
      <dc:creator>cheng_xi_li</dc:creator>
      <dc:date>2017-04-11T05:25:29Z</dc:date>
    </item>
    <item>
      <title>Re: Reading depth from maya viewport</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7008585#M12011</link>
      <description>&lt;P&gt;Hi Li,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks a lot for getting back to me. That is sort of good to know ..&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I switch to the old renderer it still won't work though - is that expected?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using VP2 and rendertargets &amp;nbsp;seems very complicated, I was really hoping there would be a simpler way to get to the depth.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm fine with loading a rendered image (i.e. from arnold) but also that I can't get to work:&lt;/P&gt;&lt;P&gt;In the following example the image is either a 8 or 32 bit per channel 512x512 tiff&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import maya.OpenMaya as om&lt;BR /&gt;import maya.OpenMayaUI as omUI&lt;/P&gt;&lt;P&gt;image = om.MImage()&lt;BR /&gt;image.readFromFile("D:/GoogleDrive/Work/ProjectTool/z.tif") &amp;nbsp;# works fine&lt;BR /&gt;d = om.MFloatArray(512 *512)&lt;BR /&gt;image.setDepthMap(d, 512, 512)&lt;BR /&gt;print image.haveDepth() # True&lt;BR /&gt;image.readDepthMap("D:/GoogleDrive/Work/ProjectTool/z.tif") # unexpected internal failure with 8 bit&amp;nbsp;&lt;BR /&gt;#&amp;nbsp;# Error: Image conversion (to IFF) failed # &amp;nbsp;(with 32 bit)&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;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.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;If you have some more tips to get me my depth please, please tell me &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;seb&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 08:46:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7008585#M12011</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-04-11T08:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: Reading depth from maya viewport</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7008693#M12012</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've tried your code with Maya 2017. It looks working fine when I changed the unpack from big-endian to little-endian(&amp;lt;f). &amp;nbsp;I guess it is still working just not recommended.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;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('&amp;lt;f', b)

print readDepthMap(1,1)
[0, 255, 127, 59]
(0.0039061903953552246,)&lt;/PRE&gt;
&lt;P&gt;About MImage:readDepthMap, I think it should only work with .iff file type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yours,&lt;/P&gt;
&lt;P&gt;Li&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 09:32:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7008693#M12012</guid>
      <dc:creator>cheng_xi_li</dc:creator>
      <dc:date>2017-04-11T09:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: Reading depth from maya viewport</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7009570#M12013</link>
      <description>&lt;P&gt;Thanks a lot Li! Now I'm actually getting some sort of data :)))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm really trying to do is this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Project a world space point through the camera onto the scene geometry.&lt;/P&gt;&lt;P&gt;I get the point into screen space, look up the depth and get it back into world space.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my function for it:&lt;/P&gt;&lt;P&gt;&lt;A href="https://pastebin.com/J8iMT4xm" target="_blank"&gt;https://pastebin.com/J8iMT4xm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but the crux is :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;depth = readDepthMap(x,y)&lt;BR /&gt;ray = om.MVector()&lt;BR /&gt;wp = om.MPoint()&lt;BR /&gt;&lt;BR /&gt;view.viewToWorld(x,y,wp, ray)&lt;/P&gt;&lt;P&gt;pProjected = wp + ray * depth&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which sadly doesn't work as expected. Do you have insight to why not?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again..&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;seb&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 15:27:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7009570#M12013</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-04-11T15:27:01Z</dc:date>
    </item>
    <item>
      <title>Re: Reading depth from maya viewport</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7011216#M12014</link>
      <description>&lt;P&gt;Hmm, sounds like one of Maya devkit samples:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://help.autodesk.com/view/MAYAUL/2017/ENU/?guid=__cpp_ref_point_manip_2point_manip_8cpp_example_html" target="_self"&gt;pointManip&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please check it out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yours,&lt;/P&gt;
&lt;P&gt;Li&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Apr 2017 07:17:38 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7011216#M12014</guid>
      <dc:creator>cheng_xi_li</dc:creator>
      <dc:date>2017-04-12T07:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: Reading depth from maya viewport</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7014186#M12015</link>
      <description>&lt;P&gt;Oh brilliant, that seems exactly what I need!!&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 07:48:42 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7014186#M12015</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-04-13T07:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: Reading depth from maya viewport</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7015073#M12016</link>
      <description>&lt;P&gt;Hello Li,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so it's still not working for me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;When I switch to the old viewport renderer it crashes maya.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Oh well, I might just create a maya command to give me what I want.. still the crash there is a bit worrying.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your extended help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;seb&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Apr 2017 14:42:46 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/reading-depth-from-maya-viewport/m-p/7015073#M12016</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-04-13T14:42:46Z</dc:date>
    </item>
  </channel>
</rss>

