Open Prim Animator/Import-Export

From Second Life Wiki
Jump to navigation Jump to search
//!  @brief Animation export script for http://wiki.secondlife.com/wiki/Open_Prim_Animator
/**
*    @author SignpostMarv Martin
*/

//!  Storing the names in exports allows us to remap links according to their current state
/**
*    we want to store the names because there are circumstances where the link order can change-
*      TPV object import/export
*      accidentally delinking objects etc.
*    Once an import has been parsed, the script checks it against the current object state to avoid the wrong prims being animated.
*    Since OPA doesn't animate prim cut/twist etc., this is essential to avoid any messed up animations.
*/
list names;

//!  place-holder for all prim positions
list pos;

//!  place-holder for all prim rotations
list rot;

//!  place-holder for all prim scales
list size;

//!  used to communicate with the core OPA script to trigger an import.
key import = "6b78fcc8-e147-4105-99a6-ff19b4bf559d";

//!  used to communicate with the core OPA script to trigger an export.
key export = "7c2ca168-2b64-4836-8727-8e62b78dbd44";

//!  spits out the export document to chat so the user can copypasta to a notecard to allow the animation frames to be persisted.
export_string(string _export_string){
    integer s = 249;
    while(llStringLength(_export_string) > s){
        llOwnerSay("\n" + llGetSubString(_export_string,0,(s-1)));
        _export_string = llGetSubString(_export_string,s,-1);
    }
    if(llStringLength(_export_string) > 0){
        llOwnerSay("\n" + _export_string);
    }
}

//!  unlike (string)foo, truncate_float() attempts to "minify" the string so keep the size of the export document down.
/**
*    1.0 would be converted to "1"
*    0.1 would be converted to ".1"
*/
string truncate_float(float foo){
    if(foo == 0.0){
        return "0";
    }else if(foo == (float)((integer)foo)){
        return (string)((integer)foo);
    }
    string bar = (string)foo;
    while(llGetSubString(bar,-1,-1) == "0"){
        bar = llGetSubString(bar,0,-2);   
    }
    if(llGetSubString(bar,-1,-1) == "."){
        bar = llGetSubString(bar,0,-2);
    }
    return bar;
}

//!  unlike (string)foo, truncate_vector() attempts to "minify" the string so keep the size of the export document down.
//!  @see truncate_float()
string truncate_vector(vector foo){
    if(foo == ZERO_VECTOR){
        return "<0,0,0>";
    }
    string bar = "<";
    bar += truncate_float(foo.x) + ",";
    bar += truncate_float(foo.y) + ",";
    bar += truncate_float(foo.z) + ">";
    return bar;
}

//!  unlike (string)foo, truncate_rotation() attempts to "minify" the string so keep the size of the export document down.
string truncate_rotation(rotation foo){
    if(foo == ZERO_ROTATION){
        return "<0,0,0,1>";
    }
    string bar = "<";
    bar += truncate_float(foo.x) + ",";
    bar += truncate_float(foo.y) + ",";
    bar += truncate_float(foo.z) + ",";
    bar += truncate_float(foo.s) + ">";
    return bar;
}

default{
    link_message(integer s, integer n, string m, key k){
        if(k == export){

            integer i;
            integer j;

            if(n == 1){ // if n == 1, we're parsing link names
                names = llParseString2List(m,["|"],[]);
            }else if(n == 2){ // if n == 2, we're parsing position vectors
                pos   = llParseString2List(m,["|"],[]);
                j=llGetListLength(pos);
                for(i=0;i<j;++i){
                    pos = llListReplaceList(pos,[(vector)llList2String(pos,i)],i,i);
                }
            }else if(n == 3){ // if n == 3, we're parsing rotations
                rot   = llParseString2List(m,["|"],[]);
                j=llGetListLength(rot);
                for(i=0;i<j;++i){
                    rot = llListReplaceList(rot,[(rotation)llList2String(rot,i)],i,i);
                }
            }else if(n == 4){ // if n == 4, we're parsing prim sizes
                size  = llParseString2List(m,["|"],[]);
                j=llGetListLength(size);
                for(i=0;i<j;++i){
                    size = llListReplaceList(size,[(vector)llList2String(size,i)],i,i);
                }
            }

            if(names != [] && pos != [] && rot != [] && size != []){ // start truncating the lists once they're all populated

                llOwnerSay("Exporting:");
                export_string("rootscale:" + truncate_vector(llGetScale()));
                export_string("names:" + llDumpList2String(names,"|"));
                
                list bar = [];
                j= llGetListLength(pos);
                for(i=0;i<j;++i){ // truncate the 
                    bar += truncate_vector(llList2Vector(pos,i)); // minify each position vector
                }
                export_string("pos:" + llDumpList2String(bar,"|")); // export minified position vectors to chat

                bar = [];
                for(i=0;i<j;++i){
                    bar += truncate_rotation(llList2Rot(rot,i));  // minify each rotation
                }
                export_string("rot:" + llDumpList2String(bar,"|")); // export minified rotations to chat

                bar = [];
                for(i=0;i<j;++i){
                    bar += truncate_vector(llList2Vector(size,i)); // minify each size vector
                }
                export_string("size:" + llDumpList2String(bar,"|")); // export minified sizes to chat

                bar = []; // we're about to reset the script so this might be superfluous
                llMessageLinked(LINK_THIS,0,"XDmenu",NULL_KEY); // re-open the menu
                llResetScript(); 
            }
        }else if(k == import && n == -1){
            llOwnerSay("importing");
            integer i;
            integer j=llGetListLength(pos);
            for(i=0;i<j;++i){
                llMessageLinked(s,i,llDumpList2String([llList2Vector(pos,i),llList2Rot(rot,i),llList2Vector(size,i)],"|"),import);
            }
            llMessageLinked(LINK_THIS,0,"XDmenu",NULL_KEY);
            llResetScript();
        }
    }
}