Feedback

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

Monday 18 February 2013

LScript - Generic_StackIt (v1.2)

LScript (Layout) to stack an object upon another while parenting them. Select item to stack and then item to stack upon.

Changes
  • Now stacks multiple objects (v1.2)
  • Fixed pivot offset (v1.1)
  • Fixed getting correct boundingbox extents from layers
  • Fixed issue requiring pivot to always be at objects lowest extent
Compatible with Newtek LightWave 9.6 and above.

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

/*  
    LScript Generic - StackIt

    Generic_StackIt.ls

*/

@version 2.2
@warnings
@script generic
@name *Stack It

    sTitle = "*Stack It";
    sVersion = "v1.2";    

generic
{

    // Parent in Place
    bParentInPlace = Scene().generalopts[3];
    if(bParentInPlace){ParentInPlace();}

    // Item Select
    Items = Scene().getSelect();  
    if(Items == nil || Items.size() < 2)
      {
      info("Select an object to stack, then select object to stack upon.");
      return;
      }

    for(i = 1; i <= (Items.size() - 1);++i)
      {
      if(Items[i].genus == MESH && !Items[i].null && Items[i + 1].genus == MESH && !Items[i + 1].null)
        {
        GoToFrame(0); // Go To Frame 0
        vPivot1 = Items[i].getPivot(0.0);
        vPivot2 = Items[i + 1].getPivot(0.0);
        aBoundingBox1 = Items[i].position(getlayernumber(Items[i])); // Bounding Box      
        aBoundingBox2 = Items[i + 1].position(getlayernumber(Items[i + 1])); // Bounding Box      
        aBoundingBox1[1] -= vPivot1; 
        aBoundingBox1[2] -= vPivot1; 
        aBoundingBox2[1] -= vPivot2; 
        aBoundingBox2[2] -= vPivot2; 
        Items[i + 1].select();
        Position(vPivot1.x,aBoundingBox1[2].y - aBoundingBox2[1].y + vPivot1.y,vPivot1.z);
        CreateKey(0);    
        ParentItem(Items[i].id); // Parent
        }
      else
        {
        error("Only objects can be stacked."); 
        }
      }

    // Parent in Place
    if(bParentInPlace){ParentInPlace();}

    info(sTitle + " " + sVersion + " - developer: Stephen Culley - http://www.stephenculley.co.uk");
}

getlayernumber: item
{
    if(item.totallayers == 1){return(1);}
    sTokens = parse(":",item.name);
    iLayer = 0;
    if (strleft(sTokens[2],5)=="Layer")
      {
      iLayer = integer(sTokens[2]);
      }
    else
      {
      iTotal = item.totallayers;
      for(i = 1; i <= iTotal; i++)
        {
        if(item.layerVisible(i) == 1)
          {
          if(item.layerName(i) == sTokens[2])
            {
            iLayer = i;
            break;
            }
          }
        else
          {
          iTotal++;
          }
        }
      }
  return iLayer;
}


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

7 comments:

  1. Thanks for a great script!
    Would it be possible to make the script allow the user to stack more than one at a time?

    Brilliant script anyway!

    ReplyDelete
  2. Hey Darren, in what way would you want it to allow stacking multiple objects? All selected objects stack on last chosen object similiar to how it operates at the moment or each object stacking on the last?

    I did have another variant of the StackIt script which also stacks objects but based on the world points rather than bounding box but I have yet to post it because for most needs its slow.

    Stephen

    ReplyDelete
  3. Hi Stephen, I was curious if it could be used to create towers of blocks in one go, for knocking down with dynamics etc.

    ReplyDelete
  4. It was never inteded to be used with dynamics, but I should think with a few changes and removing parenting from the script it might be possible. I would have thought it would be simpler to model the blocks stacked in Modeler and run dynamics on parts rather than construct individual blocks and stack them in Layout.

    ReplyDelete
  5. Oh well, it was just an idea. I can get around it as it is.

    Thanks for a good script!

    ReplyDelete
  6. Thanks for the new version, works brilliantly!

    ReplyDelete