Trouble with my first coffee script



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

    On 19/03/2008 at 21:14, xxxxxxxx wrote:

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

    ---------
    Hi! Today I wrote my first coffee script, a simple script to generate sphere objects at the selected points of a polygon object.

    The script works well if I only select one point of a polygon object. But if I select more than one point, Cinema4D will not be responding, and I have to force quit the program.

    I was struggling to find out the problem, but have no results. Please have a look at the following code. Many thanks!

    > \> if(!instanceof(op, PolygonObject)) return; \> \> var bsOp = op->GetPointSelection(); \> if (bsOp->GetCount() == 0) return; \> \> var pt_list = new(array,100); //new array to store the selected point positions. \> var i, n = 0; \> \> for(i = 0; i < op->GetPointCount(); i++) \> { \> if(bsOp->IsSelected(i)) { \>           pt_list[n] = op->GetPoint(i); \>           n++; \>      } \> } \> \> var mOp = op->GetMg(); \> var j = 0; \> var pt; \> var new_sphere = AllocObject(Osphere); \> \> while (pt_list[j]) \> { \>      pt = mOp->GetMulP(pt_list[j]); \>      println(pt); \>       \>      doc->InsertObject(new_sphere, NULL, NULL); \>      new_sphere->SetPosition(pt); \> \>      j++; \> } \>



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

    On 20/03/2008 at 02:29, xxxxxxxx wrote:

    You have to do the allocation of the sphere object within the while loop otherwise you insert the same object several times into the documanet and that can not work.

    cheers,
    Matthias



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

    On 20/03/2008 at 07:49, xxxxxxxx wrote:

    Thank you! The script works fine now. I still have two questions :)

    (1) Are these two commands the same when create new objects?

    > \> new_sphere = new(SphereObject); \> new_sphere = AllocObject(Osphere); \>

    (2) When you create an array, must the array contain at least one nil element at the end? I ask this question because when I create an array with the same number of elements as the selected points, I get an error message "Subscript out of range". So I increase the array element number by one, and the script works.

    Here is the revised script:

    > \> if (!instanceof(op, PolygonObject)) return; \> \> var bsOp = op->GetPointSelection(); \> var npt_sel = bsOp->GetCount(); \> if (npt_sel == 0) return; \> \> var pt_list = new(array, npt_sel + 1); \> var i, n = 0; \> \> for (i = 0; i < op->GetPointCount(); i++) \> { \> if (bsOp->IsSelected(i)) \>      { \>           pt_list[n] = op->GetPoint(i); \>           n++; \>      } \> } \> \> var mOp = op->GetMg(); \> var j = 0; \> var pt; \> var new_sphere; \> \> while (pt_list[j]) \> { \>      pt = mOp->GetMulP(pt_list[j]); \>      new_sphere = AllocObject(Osphere); \>      doc->InsertObject(new_sphere, NULL, NULL); \>      new_sphere->SetPosition(pt); \>      j++; \> } \>



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

    On 20/03/2008 at 08:41, xxxxxxxx wrote:

    1)  These are different things.  For what you are doing you want to stick with AllocObject.  The new function is more generic just for creating objects of your classes or array instances.  If you try and create a sphere or something with new it won't work.  AllocObject is a C4d specific function for making C4d objects.
    2)  I did a test to see about your array question.

        
        
         var test=new(array,3);  
         var i;  
         for(i=0;i<3;i++)  
         {  
           test[i]=i;  
         }  
         for(i=0;i<3;i++)  
         {  
           println(test[i]);  
         }
    

    This works so I don't see why your code needs the extra array spot.  Try println on the on the number of point values you are retrieving.  Sometimes it gets confusing going back and forth between arrays and counters because of the arrays starting at 0.
    I could be wrong as I am beginning like you.



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

    On 20/03/2008 at 09:01, xxxxxxxx wrote:

    Thanks a lot superape! I think there might be something wrong in the part below. I modified David Wickenden's Points to Linear Spline to get it. Still not fully understand it though.

    > \> for (i = 0; i < op->GetPointCount(); i++) \> { \> if (bsOp->IsSelected(i)) \>      { \>           pt_list[n] = op->GetPoint(i); \>           n++; \>      } \> } \>


Log in to reply