Answers.Body: <p>I finally found how to do this on the internet. Just call this procedure with a camera name and the worldspace coordinate of a point as attributes.</p>
  
  <pre><code>def worldSpaceToScreenSpace(camera, worldPoint):
  
      # get current resolution
      resWidth = mc.getAttr('defaultResolution.width')
      resHeight = mc.getAttr('defaultResolution.height')
  
      # get the dagPath to the camera shape node to get the world inverse matrix
      selList = om.MSelectionList()
      selList.add(camera)
      dagPath = om.MDagPath()
      selList.getDagPath(0,dagPath)
      dagPath.extendToShape()
      camInvMtx = dagPath.inclusiveMatrix().inverse()
  
      # use a camera function set to get projection matrix, convert the MFloatMatrix 
      # into a MMatrix for multiplication compatibility
      fnCam = om.MFnCamera(dagPath)
      mFloatMtx = fnCam.projectionMatrix()
      projMtx = om.MMatrix(mFloatMtx.matrix)
  
      # multiply all together and do the normalisation
      mPoint = om.MPoint(worldPoint[0],worldPoint[1],worldPoint[2]) * camInvMtx * projMtx;
      x = (mPoint[0] / mPoint[3] / 2 + .5) * resWidth
      y = (mPoint[1] / mPoint[3] / 2 + .5) * resHeight
  
      return [x,y]
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-21T11:09:22.793Z
Answers.LastActivityDate: 2018-02-21T11:09:22.793Z
Answers.UserDisplayName: Fabrice Laroche
Body: <p>I'd like to convert a worldspace position (x,y,z) to screenspace coordinates (x,y) from the selected camera point of view. I'm a bit limited by the fact I can't load extra modules (e.g. numpy). Is there an affordable way to do this?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-04-22T16:58:28.827Z
Comments.Text: Why can't you load other modules?
Comments.UserDisplayName: gucciferXCIV
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-05-12T09:00:48.110Z
Comments.Text: Not allowed to manage my computer and add extra modules...
Comments.UserDisplayName: Fabrice Laroche
Tags: <maya>
Title: Maya Python: worldspace to screenspace coordinates
UserDisplayName: Fabrice Laroche
ThreadText: # Maya Python: worldspace to screenspace coordinates
  
  (Asked by: username_0 on 2018-02-09)
  
  I'd like to convert a worldspace position (x,y,z) to screenspace coordinates (x,y) from the selected camera point of view. I'm a bit limited by the fact I can't load extra modules (e.g. numpy). Is there an affordable way to do this?
  
  **Tags:** maya
  
  > **Comments:**
  > **username_1**: Why can't you load other modules?  
  > **username_0**: Not allowed to manage my computer and add extra modules...  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2018-02-21. Score: 2
  
  I finally found how to do this on the internet. Just call this procedure with a camera name and the worldspace coordinate of a point as attributes.
  
  ```
  def worldSpaceToScreenSpace(camera, worldPoint):
  
      # get current resolution
      resWidth = mc.getAttr('defaultResolution.width')
      resHeight = mc.getAttr('defaultResolution.height')
  
      # get the dagPath to the camera shape node to get the world inverse matrix
      selList = om.MSelectionList()
      selList.add(camera)
      dagPath = om.MDagPath()
      selList.getDagPath(0,dagPath)
      dagPath.extendToShape()
      camInvMtx = dagPath.inclusiveMatrix().inverse()
  
      # use a camera function set to get projection matrix, convert the MFloatMatrix 
      # into a MMatrix for multiplication compatibility
      fnCam = om.MFnCamera(dagPath)
      mFloatMtx = fnCam.projectionMatrix()
      projMtx = om.MMatrix(mFloatMtx.matrix)
  
      # multiply all together and do the normalisation
      mPoint = om.MPoint(worldPoint[0],worldPoint[1],worldPoint[2]) * camInvMtx * projMtx;
      x = (mPoint[0] / mPoint[3] / 2 + .5) * resWidth
      y = (mPoint[1] / mPoint[3] / 2 + .5) * resHeight
  
      return [x,y]
  
  ```
  
  
  ---
  
  