Center Axes / Object Hierarchy



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 26/11/2006 at 04:58, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:    
    Platform:    Mac  ;  
    Language(s) :   C.O.F.F.E.E  ;

    ---------
    subject : center axes (of all polygon objects in document)

    i have to do this often because i do import a lot of files
    and sometimes all axis centers are at (0,0,0) instead of
    being in the centers of the objects.

    i got the base script somewhere else, and added the code
    to process the full hierarchy of the document to find all
    polygon objects.

    help needed : this script has a problem with hierarchies!

    • generate 3 cubes, convert them to polygon objects and
      drop them on each other so that the second is the firsts
      child and the third is the seconds child.
      -> use the object axis tool to move the first cubes axis
      a little bit off center, then start the script.

    you'll see the misbehaviour, by reseting the axis center
    of the first object the childs move!

    i could not find the way to correct this, and would be
    very happy about a little help ! :)

    thanks for any reply!
    zeo

    ps. see console output to have some debug info :)

      
    center_axes(obj);  
      
    main(doc,op)  
    {  
         println (" ");  
         println ("CenterAllAxes 0.6");  
      
         var doc=GetActiveDocument();  
         var obj=doc->GetFirstObject();  
         var count_all=0, count_done=0, level=0;  
      
         while(obj)  
         {       
              print ("- "); var j; for(j=0;j<level;j++) { print(" "); }  
              println (obj->GetName(), " (", obj->GetType(), ")");  
      
              count_all++;  
              if(obj->GetType()==5100) { center_axes(obj); count_done++; }  
      
              if(obj->GetDown()) { obj=obj->GetDown(); level++; continue; }  
              while(! obj->GetNext() && level>0) { obj=obj->GetUp(); level--; }  
              obj=obj->GetNext();  
         }  
         println (count_all," objects processed, ", count_done, " axes centered.");  
    }  
      
    center_axes(obj)  
    {  
         var i, sumV=vector(0);  
         var matG=obj->GetMg(), matG_neu=new(Matrix);  
         var punkte=obj->GetPoints();  
         var anzahl=obj->GetPointCount();  
         var y_min = punkte[0].y, y_max = punkte[0].y;  
      
         if(anzahl==0) return false;  
      
         for(i=0; i<anzahl; i++) { sumV+=matG->GetMulP(punkte _); }  
         sumV/=anzahl;  
      
         /*  
         // optional code to set the center to the bottom of the object  
         for(i=0; i<anzahl; i++) {  
              if(y_min > punkte _.y) { y_min = punkte _.y; }  
              if(y_max < punkte _.y) { y_max = punkte _.y; }  
         }  
         sumV.y = y_min;  
         */  
      
         matG_neu->SetV1(matG->GetV1());  
         matG_neu->SetV2(matG->GetV2());  
         matG_neu->SetV3(matG->GetV3());  
         matG_neu->SetV0(sumV);  
         obj->SetMg(matG_neu);  
      
         matG_neu->Invert();  
      
         for(i=0; i<anzahl; i++) { punkte _=matG_neu- >GetMulP(matG->GetMulP(punkte _)); }  
         obj->SetPoints(punkte);  
      
         obj->Message(MSG_UPDATE);  
    }  
    


  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 27/11/2006 at 02:26, xxxxxxxx wrote:

    You have to pass an offset position to your childs. Here a working script:

      
    centeraxis(op,off)  
    {  
         while(op)  
         {  
              var posdelta = vector(0);  
              if (getclass(op)==PolygonObject)  
              {  
                   var i;  
                   var center = vector(0);  
                   var pcnt = op->GetPointCount();  
                   for(i=0; i<pcnt; i++)  
                   {  
                        center = center + op->GetPoint(i);   
                   }  
                   center /= pcnt;  
                   for(i=0; i<pcnt; i++)  
                   {  
                        op->SetPoint(i, op->GetPoint(i) - center);  
                   }  
                   center = op->GetMg()->GetMulP(center);   
                   posdelta = op->GetPosition() - center;  
                   println(tostring(center));  
                   op->SetPosition(center + off);  
                   op->Message(MSG_UPDATE);  
              }  
              centeraxis(op->GetDown(), posdelta);  
              op = op->GetNext();  
         }  
    }  
      
    main(doc,op)  
    {  
         centeraxis(doc->GetFirstObject(),vector(0));  
    }  
    

    cheers,
    Matthias



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 30/11/2006 at 03:09, xxxxxxxx wrote:

    sorry to say, but your script doesnt work, too.
    try if the objects are located off center :)
    but it gave me some inputs to finish my version
    ->
    this is now an hierarchy proof center axes which
    centers the axes for all poly objects in the document.

    ...

    center_axes(obj, offset);

    main(doc,op)
    {
         println (" ");
         println ("CenterAllAxes 2.2");

    var count_all=0, count_done=0, level=0;
         var obj, doc=GetActiveDocument();
         if(op) { obj=doc->GetActiveObject(); } else { obj=doc->GetFirstObject(); }

    // AXIS CENTER OFFSET (x,y,z)
         // use (0,0,0) for center of object
         // use (0,-1,0) for bottom center of object
         // use (1,1,-1) for a corner point of the object
         var offset=vector(0,0,0);

    while(obj)
         {     
              count_all++;
              if(center_axes(obj, offset)) { count_done++; }

    if(obj->GetDown()) { obj=obj->GetDown(); level++; continue; }
              while(! obj->GetNext() && level>0) { obj=obj->GetUp(); level--; if(op) { break; } }
              obj=obj->GetNext();
         }
         println (count_all," objects processed, ", count_done, " axes centered.");
    }

    center_axes(obj, offset)
    {
         if (getclass(obj) != PolygonObject) return false;
         
         var i, gCenter=vector(0), lCenter=vector(0);
         var matG=obj->GetMg(), matG_neu=new(Matrix);
         var punkte=obj->GetPoints();
         var anzahl=obj->GetPointCount();
         var y_min = punkte[0].y, y_max = punkte[0].y;

    if(anzahl==0) return false;

    for(i=0; i<anzahl; i++) {
              gCenter+=matG->GetMulP(punkte _);
              lCenter += obj->GetPoint(i);
         }
         gCenter/=anzahl;
         lCenter/=anzahl;

    var center_off=vector(0);
         var x_min = lCenter.x, x_max = lCenter.x;
         var y_min = lCenter.y, y_max = lCenter.y;
         var z_min = lCenter.z, z_max = lCenter.z;
         for(i=0; i<anzahl; i++) {
              if(x_max < punkte _.x) { x_max = punkte _.x; } if(x_min > punkte _.x) { x_min = punkte _.x; }
              if(y_max < punkte _.y) { y_max = punkte _.y; } if(y_min > punkte _.y) { y_min = punkte _.y; }
              if(z_max < punkte _.z) { z_max = punkte _.z; } if(z_min > punkte _.z) { z_min = punkte _.z; }
         }
         var size = vector(0);
         size.x = x_max-x_min, size.y = y_max-y_min, size.z = z_max-z_min;
         center_off.x += size.x / 2 * offset.x;
         center_off.y += size.y / 2 * offset.y;
         center_off.z += size.z / 2 * offset.z;
        gCenter += center_off;
        lCenter += center_off;

    matG_neu->SetV1(matG->GetV1());
         matG_neu->SetV2(matG->GetV2());
         matG_neu->SetV3(matG->GetV3());
         matG_neu->SetV0(gCenter);
         obj->SetMg(matG_neu);

    matG_neu->Invert();
         for(i=0; i<anzahl; i++) { punkte _=matG_neu- >GetMulP(matG->GetMulP(punkte _)); }
         obj->SetPoints(punkte);
         obj->Message(MSG_UPDATE);

    var child = obj->GetDown();
         while(child) {
              child->SetPosition(child->GetPosition() - lCenter);
              child->Message(MSG_UPDATE);
              child=child->GetNext();
         }
         return true;
    }



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 30/11/2006 at 04:57, xxxxxxxx wrote:

    I copied and pasted Mattias code into the script manager and it worked fine.
    Both in R9.6 and R10
    Cheers
    Lennart



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 30/11/2006 at 06:33, xxxxxxxx wrote:

    set up this scene :

    sphere (at 0,0,0)
    +cylinder (child of sphere) (at 0,0,300)
    ++cube (child of cylinder) (at 0,100,500)
    ++cube (child of cylinder) (at 0,-100,500)

    start mathias script.

    use the same scene and move sphere off center

    start mathias script.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 30/11/2006 at 14:38, xxxxxxxx wrote:

    Your right.
    See if you can get it going, it would be good:)
    In the mean time you can select all your objects and put them under a polygon object and run "Axis Center" (Included in R10 and a UB version is to be found at www.bonkers.de.
    Select the polygon parent and check "Include Children" in Center Axis.
    Then there is the "Delete Parent" command (at least in R10) to use.

    Cheers
    Lennart



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 01/12/2006 at 04:04, xxxxxxxx wrote:

    It looks as if I have to correct myself here regarding AxisCenter.
    It is included in R10, but not as a separate plugin as far as I can see..
    And it was the Tag ManagerUB I was thinking of at www.bonkers.de,
    not AxisCenter.

    I'm sorry for the confusion.

    Cheers
    Lennart


Log in to reply