Is an object facing the camera?

On 05/09/2017 at 15:08, xxxxxxxx wrote:

I'm trying to find out if an object is facing the camera. By facing, I mean that the Z axis of the object is roughly pointing to the direction of the camera.
I'm using this code:

  
def main() :   
    rbd = doc.GetRenderBaseDraw()   
    cam = rbd.GetSceneCamera(doc)   
    if cam is None: cam = rbd.GetEditorCamera()   
       
    obj=op.GetObject()   
       
    mt_cam=cam.GetMg()   
    mt_obj=obj.GetMg()   
       
    hpb_cam=c4d.utils.MatrixToHPB(mt_cam,c4d.ROTATIONORDER_DEFAULT)   
    hpb_obj=c4d.utils.MatrixToHPB(mt_obj,c4d.ROTATIONORDER_DEFAULT)   
       
    hpb_cam.Normalize()   
    hpb_obj.Normalize()   
  
    dot=hpb_cam.Dot(hpb_obj)   
    print dot   

But it is not working.
It should be simple. How can I make it work?

On 06/09/2017 at 03:28, xxxxxxxx wrote:

Hi Rui Batista, thanks for writing us.

With reference to your support request I think the script below should work as expected:

  
import c4d  
  
def main() :  
  rbd = doc.GetRenderBaseDraw()  
  if rbd is None:  
      return  
    
  cam = rbd.GetSceneCamera(doc)  
  if cam is None:  
      return   
    
  # define the evaluation tolerance  
  tol = 1e-2  
    
  # retrieve camera position and camera z-axis  
  camPos = cam.GetMg().off  
  camV3 = cam.GetMg().v3.GetNormalized()  
    
  # loop throught the objects in the scene  
  obj = doc.GetFirstObject()  
  while obj is not None:  
      # just skip camera objects  
      if obj.GetType() == c4d.Ocamera:  
          obj = obj.GetNext()  
          continue  
        
      #retrieve obj pos and obj z-axis  
      objPos = obj.GetMg().off  
      objV3 = obj.GetMg().v3.GetNormalized()  
    
      # compute the obj/cam direction  
      ocDir = c4d.Vector(objPos - camPos).GetNormalized()  
        
      # evaluate if obj/cam dir and obj z-axis are aligned  
      objZAxisAlignedWithObjCamDir = abs(objV3.Dot(ocDir) + 1 ) < tol  
      # evaluate if obj/cam dir and cam z-axis are aligned  
      camZAxisAlignedWithObjCamDir = abs(camV3.Dot(ocDir) - 1 ) < tol  
        
      # if both their z-axis are aligned they are looking each other  
      if objZAxisAlignedWithObjCamDir and camZAxisAlignedWithObjCamDir:  
          print  obj.GetName() + " is looking at "+ cam.GetName()  
        
      # go next/down  
      tempObj = obj  
      obj = obj.GetDown()  
      if obj is None:  
          obj = tempObj.GetNext()  
  
if __name__=='__main__':  
  main()  

On top of that i warmly recommend not to normalize a vector representing rotation angles.

Best, Riccardo

On 06/09/2017 at 05:06, xxxxxxxx wrote:

Thank you so much, Riccardo.
I adapted it to my needs and it worked great.