Difference between revisions of "Linkset resizer"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
 
(2 intermediate revisions by 2 users not shown)
Line 4: Line 4:
<br>
<br>
This script uses the [[LlGetLinkPrimitiveParams|llGetLinkPrimitiveParams()]] and [[LlSetLinkPrimitiveParamsFast|llSetLinkPrimitiveParamsFast()]] functions (introduced in server 1.38) to rescale every prim in a linkset.  This script works for both nonphysical objects rezzed on a parcel and attached objects.
This script uses the [[LlGetLinkPrimitiveParams|llGetLinkPrimitiveParams()]] and [[LlSetLinkPrimitiveParamsFast|llSetLinkPrimitiveParamsFast()]] functions (introduced in server 1.38) to rescale every prim in a linkset.  This script works for both nonphysical objects rezzed on a parcel and attached objects.
<lsl>
<source lang="lsl2">
// Linkset Resizer, by Maestro Linden, 2010.03.12
// Linkset Resizer, by Maestro Linden, 2010.03.12
// This script rescales all prims in a linkset, using a scaling factor specified
// This script rescales all prims in a linkset, using a scaling factor specified
Line 11: Line 11:
// it does *not* check the prim linkability rules, which are described in:
// it does *not* check the prim linkability rules, which are described in:
// http://wiki.secondlife.com/wiki/Linkability_Rules  
// http://wiki.secondlife.com/wiki/Linkability_Rules  
 
integer LISTEN_CHANNEL=4; // channel to listen on
integer LISTEN_CHANNEL=4; // channel to listen on
float MIN_DIMENSION=0.01; // the minimum scale of a prim allowed, in any dimension
float MIN_DIMENSION=0.01; // the minimum scale of a prim allowed, in any dimension
float MAX_DIMENSION=10.0; // the maximum scale of a prim allowed, in any dimension
float MAX_DIMENSION=10.0; // the maximum scale of a prim allowed, in any dimension
 
list link_positions;
list link_positions;
list link_scales;
list link_scales;
Line 22: Line 22:
float min_rescale_factor;
float min_rescale_factor;
float max_rescale_factor;
float max_rescale_factor;
 
integer listener;
 
scanLinkset()
scanLinkset()
{
{
Line 31: Line 32:
   link_positions=[];
   link_positions=[];
   link_scales=[];
   link_scales=[];
 
   for(link=1; link<=total_links; link++)
   for(link=1; link<=total_links; link++)
   {
   {
       link_pos=llList2Vector(llGetLinkPrimitiveParams(link,[PRIM_POSITION]),0);
       link_pos=llList2Vector(llGetLinkPrimitiveParams(link,[PRIM_POSITION]),0);
       link_scale=llList2Vector(llGetLinkPrimitiveParams(link,[PRIM_SIZE]),0);
       link_scale=llList2Vector(llGetLinkPrimitiveParams(link,[PRIM_SIZE]),0);
     
       // determine the minimum and maximum prim scales in the linkset,  
       // determine the minimum and maximum prim scales in the linkset,  
       //  so that rescaling doesn't fail due to prim scale limitations
       //  so that rescaling doesn't fail due to prim scale limitations
Line 47: Line 48:
       if(link_scale.z<min_original_scale) min_original_scale=link_scale.z;
       if(link_scale.z<min_original_scale) min_original_scale=link_scale.z;
       else if(link_scale.z>max_original_scale) max_original_scale=link_scale.z;
       else if(link_scale.z>max_original_scale) max_original_scale=link_scale.z;
     
       link_scales+=[link_scale];
       link_scales+=[link_scale];
       link_positions+=[(link_pos-llGetRootPosition())/llGetRootRotation()];
       link_positions+=[(link_pos-llGetRootPosition())/llGetRootRotation()];
     
   }
   }
 
   max_rescale_factor=MAX_DIMENSION/max_original_scale;
   max_rescale_factor=MAX_DIMENSION/max_original_scale;
   min_rescale_factor=MIN_DIMENSION/min_original_scale;
   min_rescale_factor=MIN_DIMENSION/min_original_scale;
}
}
 
rescaleLinkset(float scale)
rescaleLinkset(float scale)
{
{
Line 63: Line 64:
   vector scale_to_set;
   vector scale_to_set;
   integer total_links=llGetListLength(link_positions);
   integer total_links=llGetListLength(link_positions);
 
   for(link=1; link<=total_links; link++)
   for(link=1; link<=total_links; link++)
   {
   {
       scale_to_set=llList2Vector(link_scales,link-1)*scale;
       scale_to_set=llList2Vector(link_scales,link-1)*scale;
       pos_to_set=llList2Vector(link_positions,link-1)*scale;
       pos_to_set=llList2Vector(link_positions,link-1)*scale;
     
       // don't move the root prim
       // don't move the root prim
       if(link==1) llSetLinkPrimitiveParamsFast(link,[PRIM_SIZE,scale_to_set]);
       if(link==1) llSetLinkPrimitiveParamsFast(link,[PRIM_SIZE,scale_to_set]);
Line 77: Line 78:
   }
   }
}
}
 
init()
init()
{
{
   llListen(LISTEN_CHANNEL,"",llGetOwner(),"");
   llListenRemove(listener);
  listener=llListen(LISTEN_CHANNEL,"",llGetOwner(),"");
   llOwnerSay("Say the scale that you want in channel "+(string)LISTEN_CHANNEL
   llOwnerSay("Say the scale that you want in channel "+(string)LISTEN_CHANNEL
     +" chat (e.g. \"/"+(string)LISTEN_CHANNEL+" 0.8\"), relative to the original scale.");
     +" chat (e.g. \"/"+(string)LISTEN_CHANNEL+" 0.8\"), relative to the original scale.");
Line 86: Line 88:
     +(string)min_rescale_factor+" - "+(string)max_rescale_factor);
     +(string)min_rescale_factor+" - "+(string)max_rescale_factor);
}
}
 
default
default
{
{
Line 94: Line 96:
         init();
         init();
   }
   }
 
   on_rez(integer reznum)
   on_rez(integer reznum)
   {
   {
       init();
       init();
   }
   }
 
 
   listen(integer channel, string name, key id, string msg)
   listen(integer channel, string name, key id, string msg)
   {
   {
       float scale=(float)msg;
       float scale=(float)msg;
     
       if(scale<min_rescale_factor*0.999)
       if(scale<min_rescale_factor*0.999)
       {
       {
           llSay(0,"ERROR: Cannot rescale to "+(string)scale
           llOwnerSay("ERROR: Cannot rescale to "+(string)scale
             +". The minimum allowed rescale factor for this object is "+(string)min_rescale_factor);
             +". The minimum allowed rescale factor for this object is "+(string)min_rescale_factor);
       }
       }
       else if(scale>max_rescale_factor/0.999)
       else if(scale>max_rescale_factor/0.999)
       {
       {
           llSay(0,"ERROR: Cannot rescale to "+(string)scale
           llOwnerSay("ERROR: Cannot rescale to "+(string)scale
             +". The maximum allowed rescale factor for this object is "+(string)max_rescale_factor);
             +". The maximum allowed rescale factor for this object is "+(string)max_rescale_factor);
       }
       }
Line 124: Line 126:
   }
   }
}
}
</lsl>
</source>
[[Category:LSL Examples]]
[[Category:LSL Examples]]

Latest revision as of 15:29, 24 January 2015

Rescale every prim in an object

by Maestro Linden 20:55, 12 March 2010 (UTC)
This script uses the llGetLinkPrimitiveParams() and llSetLinkPrimitiveParamsFast() functions (introduced in server 1.38) to rescale every prim in a linkset. This script works for both nonphysical objects rezzed on a parcel and attached objects.

// Linkset Resizer, by Maestro Linden, 2010.03.12
// This script rescales all prims in a linkset, using a scaling factor specified
// by the user over chat.  The script does some basic sanity checks (ensuring
// that each prim stays within the allowed PRIM_SIZE range of 0.01m to 10m, although
// it does *not* check the prim linkability rules, which are described in:
// http://wiki.secondlife.com/wiki/Linkability_Rules 
 
integer LISTEN_CHANNEL=4; // channel to listen on
float MIN_DIMENSION=0.01; // the minimum scale of a prim allowed, in any dimension
float MAX_DIMENSION=10.0; // the maximum scale of a prim allowed, in any dimension
 
list link_positions;
list link_scales;
float min_original_scale=10.0; // minimum x/y/z component of the scales in the linkset
float max_original_scale=0.0; // minimum x/y/z component of the scales in the linkset
float min_rescale_factor;
float max_rescale_factor;
integer listener;
  
scanLinkset()
{
   vector link_pos;
   vector link_scale;
   integer total_links=llGetNumberOfPrims();
   integer link;
   link_positions=[];
   link_scales=[];
 
   for(link=1; link<=total_links; link++)
   {
       link_pos=llList2Vector(llGetLinkPrimitiveParams(link,[PRIM_POSITION]),0);
       link_scale=llList2Vector(llGetLinkPrimitiveParams(link,[PRIM_SIZE]),0);
 
       // determine the minimum and maximum prim scales in the linkset, 
       //   so that rescaling doesn't fail due to prim scale limitations
       //   NOTE: the full linkability rules are _not_ checked by this script:
       //   http://wiki.secondlife.com/wiki/Linkability_Rules 
       if(link_scale.x<min_original_scale) min_original_scale=link_scale.x;
       else if(link_scale.x>max_original_scale) max_original_scale=link_scale.x;
       if(link_scale.y<min_original_scale) min_original_scale=link_scale.y;
       else if(link_scale.y>max_original_scale) max_original_scale=link_scale.y;
       if(link_scale.z<min_original_scale) min_original_scale=link_scale.z;
       else if(link_scale.z>max_original_scale) max_original_scale=link_scale.z;
 
       link_scales+=[link_scale];
       link_positions+=[(link_pos-llGetRootPosition())/llGetRootRotation()];
 
   }
 
   max_rescale_factor=MAX_DIMENSION/max_original_scale;
   min_rescale_factor=MIN_DIMENSION/min_original_scale;
}
 
rescaleLinkset(float scale)
{
   integer link;
   vector pos_to_set;
   vector scale_to_set;
   integer total_links=llGetListLength(link_positions);
 
   for(link=1; link<=total_links; link++)
   {
       scale_to_set=llList2Vector(link_scales,link-1)*scale;
       pos_to_set=llList2Vector(link_positions,link-1)*scale;
 
       // don't move the root prim
       if(link==1) llSetLinkPrimitiveParamsFast(link,[PRIM_SIZE,scale_to_set]);
       else
       {
           llSetLinkPrimitiveParamsFast(link,[PRIM_POSITION,pos_to_set,PRIM_SIZE,scale_to_set]);
       }
   }
}
 
init()
{
   llListenRemove(listener);
   listener=llListen(LISTEN_CHANNEL,"",llGetOwner(),"");
   llOwnerSay("Say the scale that you want in channel "+(string)LISTEN_CHANNEL
    +" chat (e.g. \"/"+(string)LISTEN_CHANNEL+" 0.8\"), relative to the original scale.");
   llOwnerSay("The allowed rescaling range for this object is "
    +(string)min_rescale_factor+" - "+(string)max_rescale_factor);
}
 
default
{
   state_entry()
   {
        scanLinkset();
        init();
   }
 
   on_rez(integer reznum)
   {
       init();
   }
 
 
   listen(integer channel, string name, key id, string msg)
   {
       float scale=(float)msg;
 
       if(scale<min_rescale_factor*0.999)
       {
           llOwnerSay("ERROR: Cannot rescale to "+(string)scale
            +". The minimum allowed rescale factor for this object is "+(string)min_rescale_factor);
       }
       else if(scale>max_rescale_factor/0.999)
       {
           llOwnerSay("ERROR: Cannot rescale to "+(string)scale
            +". The maximum allowed rescale factor for this object is "+(string)max_rescale_factor);
       }
       else
       {
           integer total_links=llGetListLength(link_positions);
           llOwnerSay("Setting "+(string)total_links+" prims to scale "+(string)scale+"..");
           rescaleLinkset(scale);
           llOwnerSay("..done");
       }
   }
}