Creating an object at View Center [SOLVED]



  • On 10/02/2015 at 04:16, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   16.038 
    Platform:   Windows  ; Mac  ;  
    Language(s) :     C++  ;

    ---------
    Hi,
    I wonder if there is any function that helps to handle the object creation option "Create New Object at View Center" (Preferences/Interface). I'm using InsertCreateObject() to insert new objects and it already takes the other option "Insert New Object At" into account, but objects are always placed at the origin. If there is no functionality that I can reuse, how is the position computed by Cinema?
    I made some quick tests and it seems that the object is placed ~900 in z direction from the editor camera center when in perspective view, but never exactly 900..
    Best,
    Burak



  • On 10/02/2015 at 12:08, xxxxxxxx wrote:

    Hey Burak,

    First you should study "Matrix Fundamentals" in the  Cinema 4D R15.037 Python SDK Documentation.
    Then study "Freeze Transformations" in the same docs.

    Here are some other functions you will need to understand:

    BaseDocument::InsertObject()

    BaseObject::GetMg()
    BaseObject::SetMg()

    BaseDraw::GetSceneCamera()
    BaseDraw::GetEditorCamera()

    If you can get some working code posted that shows you understand these principals and functions, I'm sure the experts in the forum would be glad to help.

    Also, if you are just starting out, you might want to work in Python for a while.  It's way easier to learn because you can start with scripts.  Scripting is built in to the gui so no setup is needed.  Scripts also give you immediate feedback so you can tweak and run, tweak and run and tweak and run making your learning time way more productive.

    Python can do most things C++ can and most of the methods and classes are named the same. Python is slower than C++ in some areas but in most other areas you won't notice a thing.  C++ is mostly for masochists who really, really need speed 😈😠🙂.

    Here is a good place to get started with Python:
    http://www.cineversity.com/wiki/Category:Scripting/

    Just open up Cinema then Script->Console, Script->Script Manager and hack away.  In a few days of study, you will be able to place things in the scene anywhere you can imagine.

    Joe Buck



  • On 10/02/2015 at 14:30, xxxxxxxx wrote:

    Hey Joe,

    thanks for your suggestions, I really appreciate it. Didn't know about the Scripting Intro at cinversity.
    Actually it's not the case that I don't know how to implement it, but I thought if there is any functionality that we can reuse (as it's already existing somewhere) it would be nice. It's quite annoying to implement all special cases for the non-perspective views..

    Greetings,
    Burak



  • On 10/02/2015 at 15:10, xxxxxxxx wrote:

    Hi Burak,

    Here is some code that I found in the R15 docs late one night.  This is copied directly from the docs and if I recall, it has errors.  The source of this copy and paste is located in the R15 documentation for BaseView::GetViewParameter():

    The following is the code used internally to project points:  
    `    
    #define CAMDIST 0.05  
      
    Vector WorldToCamera(const Vector &p)  
    {  
      return p*inverse_camera_matrix;  
    }  
      
    Vector CameraToWorld(const Vector &p)  
    {  
      return p*camera_matrix;  
    }  
      
    Vector CameraToScreen(const Vector &pp)  
    {  
      Vector p = pp;  
        
      if (projection==Pperspective)  
      {  
        Float nz = p.z<=0.0 ? 1.0/CAMDIST : 1.0/(p.z + CAMDIST);  
        p.x = p.x*scale.x*nz+off.x;  
        p.y = p.y*scale.y*nz+off.y;  
        return p;  
      }  
        
      p.x = (p.x*scale.x)+off.x;  
      p.y = (p.y*scale.y)+off.y;  
        
      switch (projection)  
      {  
        case Pmilitary:  case Pfrog: case Pbird: case Pgentleman:  
          p.x += p.z*scale.x*scalez.x;  
          p.y -= p.z*scale.y*scalez.y;  
          break;  
      }  
        
      return p;  
    }  
      
    Vector ScreenToCamera(const Vector &pp)  
    {  
      Vector p = pp;  
        
      switch (projection)  
      {  
        case Pmilitary:  case Pfrog:  case Pbird: case Pgentleman:  
          p.x -= p.z*scale.x*scalez.x;  
          p.y += p.z*scale.y*scalez.y;  
          break;  
      }  
        
      p.x = (p.x-off.x)/scale.x;  
      p.y = (p.y-off.y)/scale.y;  
        
      if (projection==Pperspective)  
      {  
        Float nz = p.z + CAMDIST;  
        p.x *= nz;  
        p.y *= nz;  
      }  
      return p;  
    }  
    `  
    For non-axometric projection here is the code how to calculate off/scale:  
    `    
    void InitView(BaseObject *camera, Float xres, Float yres, Float pix_x, Float pix_y)  
    {  
      // xres/yres are the resolution  
      // pix_x/pix_y are the pixel aspect ratio (normally 1:1)  
        
      Matrix opm = camera->GetMg();  
      Float ap;  
      BaseContainer data = camera->GetDataInstance();  
        
      projection=data.GetInt32(CAMERA_PROJECTION,Pperspective);  
      if (projection!=Pperspective && projection!=Pparallel)  
      {  
        opm.v1 = Vector(1.0,0.0,0.0);  
        opm.v2 = Vector(0.0,1.0,0.0);  
        opm.v3 = Vector(0.0,0.0,1.0);  
      }  
        
      off.x = xres*0.5;  
      off.y = yres*0.5;  
        
      if (b_ab==Pperspective)  
      {  
        ap=data.GetFloat(CAMERAOBJECT_APERTURE,36.0);  
        scale.x = data.GetFloat(CAMERA_FOCUS,36.0) / ap * xres;  
      }  
      else  
        scale.x = xres/1024.0*data.GetFloat(CAMERA_ZOOM,1.0);  
        
      scale.y = -scale.x*pix_x/pix_y;  
    }`
    

    Maybe it will help with your non-perspective views.

    Joe Buck



  • On 11/02/2015 at 20:59, xxxxxxxx wrote:

    Hi Burak,

    I believe joebuck beat me to it, and then some!  Let me know if you still need help, but unfortunately, we (SDK support team) don't know of any shortcut, you really do have to retrieve various coordinates and convert them, and we know the z coordinate requires a bit more effort than the x and y coordinate values.  Let me know if you want me to look up how it's calculated in C4D.

    Joey Gaspe
    SDK Support Engineer



  • On 13/02/2015 at 00:51, xxxxxxxx wrote:

    Thanks you both Joe and Joey :)! I think I can manage it now.
    Greetings,
    Burak



  • On 16/02/2015 at 13:18, xxxxxxxx wrote:

    Hi Burak,

    Glad to hear you're on the right track with this issue.  I'll close the topic as solved.

    Joey Gaspe
    SDK Support Engineer


Log in to reply