Feedback

Please leave feedback and comments. I am always interested to hear how people get on using these LScripts!

Saturday 14 September 2013

LScript - Modeler_PointNormalToSkelegon


LScript (Modeler) to create skelegons based on the normals at point locations.

Compatible with Newtek LightWave 9.6 and above.

// LScript Modeler - www.StephenCulley.co.uk
//
// web   address: http://www.stephenculley.co.uk
// email address: email@stephenculley.co.uk

/*  
    LScript Modeler - Point Normal To Skelegon

    Modeler_PointNormalToSkelegon.ls

*/

@version 2.2
@warnings
@script modeler
@name *Point Normal To Skelegon

    // Title
    sTitle = "*Point Normal To Skelegon";

    // Version
    sVersion = "v1.0";

    // Variable
    aSelectedPoints = nil;

    ctrl_c0;

main
{

    // Store
    nSize = recall("nSize",0.1); 

    reqbegin(sTitle + " " + sVersion);

    // Reset
    ctrl_res0 = ctlbutton("Reset",50,"button_reset"); // Button Reset
    ctlsep();

    // Control
    ctrl_c0 = ctldistance("Size",nSize);

    // Developer
    ctlsep();
    ctrl_dev0 = ctltext("","developer: Stephen Culley","http://www.stephenculley.co.uk");

    return if !reqpost();

    nSize = getvalue(ctrl_c0);

    // Store
    store("nSize",nSize);

    // Selection - Point (DIRECT)
    selmode(DIRECT);
    iPointCount = pointcount();
    if(iPointCount < 1) error("None or not enough points selected.");

    editbegin();

        aSelectedPoints = points; // Store points for later use

        foreach(iSelectedPoint,aSelectedPoints)
          {
          aPolygons = nil;
          aPolygons = iSelectedPoint.polygon();
          // Error
          if(aPolygons == nil) error("Points must be connected to polygons.");
          foreach(iPolygons,aPolygons)
            {
            aPoints = nil;
            aPoints = polyinfo(iPolygons);
            // Error
            if(sizeof(aPoints) < 4) error("Polygons need a minimum of 3 points.");
          }
        }

    editend();

    undogroupbegin(); // Undo

    moninit(iPointCount,"Processing...");  // Progress Monitor

        foreach(iSelectedPoint,aSelectedPoints)
          {

    // Selection - Point (GLOBAL)
    selmode(GLOBAL);

    editbegin();

          vNorm = pointnormal(iSelectedPoint);   
          i1 = addpoint(pointinfo(iSelectedPoint)); // Returns ID
          i2 = addpoint(vNorm * nSize + pointinfo(iSelectedPoint)); // Returns ID
          aNormalPolygon[1] = i1;
          aNormalPolygon[2] = i2;
          iPolygon = addpolygon(aNormalPolygon);

    editend(); 

          // Selection - Polygon (USER)
          selmode(USER);

          selpolygon(CLEAR); 
          selpolygon(SET,POLYID,iPolygon);
          cmdseq("MakeSkelegons");
          delete(); 

          monstep(); // Progress Monitor
          }

    undogroupend(); // Undo

    info(iPointCount," skelegon(s) added.");
}

button_reset
{
    setvalue(ctrl_c0,0.1); // Size
}

pointnormal: point // v
{
    v = <0.0,0.0,0.0>;
    aPolygons = point.polygon();
    iPolygonCount = size(aPolygons);
    for(p = 1; p <= iPolygonCount; p++)
      {
      v += polynormal(aPolygons[p]);          
      }
    v /= iPolygonCount;
    return(normalize3D(v));
}

// INTERPOLATION

linear1D: n1,n2,i // i = interpolation point (0-1)
{
    return(n1 * (1 - i) + n2 * i);     
}

// VECTOR 3D

crossproduct3D: v1,v2 // v
{
    // Vector
    return();
}

distance3D: v1, v2 // n
{
    // Vector
    return(sqrt(((v2.x - v1.x) * (v2.x - v1.x)) +
                ((v2.y - v1.y) * (v2.y - v1.y)) + 
                ((v2.z - v1.z) * (v2.z - v1.z))));
}

magnitude3D: v // n
{
    // Vector
    return(sqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z)));
}

normalize3D: v // v
{
    // Vector normalize to 0 - 1
    nMagnitude = magnitude3D(v);
    if(nMagnitude <> 0) nMagnitude = 1 / nMagnitude;
    return(v * nMagnitude);
}


All scripts available at my Google Drive at
https://drive.google.com/open?id=1cR_q2GVUAJHumic1-A3eXV16acQnVTWs

No comments:

Post a Comment