Difference between revisions of "Open Prim Animator/Import-Export"

From Second Life Wiki
Jump to navigation Jump to search
(comments for names property.)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
<lsl>
<source lang="lsl2">
//!  @brief Animation export script for http://wiki.secondlife.com/wiki/Open_Prim_Animator
//!  @brief Animation export script for http://wiki.secondlife.com/wiki/Open_Prim_Animator
/**
/**
Line 14: Line 14:
*/
*/
list names;
list names;
//!  place-holder for all prim positions
list pos;
list pos;
//!  place-holder for all prim rotations
list rot;
list rot;
//!  place-holder for all prim scales
list size;
list size;


//!  used to communicate with the core OPA script to trigger an import.
key import = "6b78fcc8-e147-4105-99a6-ff19b4bf559d";
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";
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){
export_string(string _export_string){
     integer s = 249;
     integer s = 249;
Line 32: Line 42:
}
}


//!  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){
string truncate_float(float foo){
     if(foo == 0.0){
     if(foo == 0.0){
Line 48: Line 63:
}
}


//!  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){
string truncate_vector(vector foo){
     if(foo == ZERO_VECTOR){
     if(foo == ZERO_VECTOR){
Line 59: Line 76:
}
}


//!  unlike (string)foo, truncate_rotation() attempts to "minify" the string so keep the size of the export document down.
string truncate_rotation(rotation foo){
string truncate_rotation(rotation foo){
     if(foo == ZERO_ROTATION){
     if(foo == ZERO_ROTATION){
Line 74: Line 92:
     link_message(integer s, integer n, string m, key k){
     link_message(integer s, integer n, string m, key k){
         if(k == export){
         if(k == export){
             integer i;
             integer i;
             integer j;
             integer j;
             if(n == 1){
 
             if(n == 1){ // if n == 1, we're parsing link names
                 names = llParseString2List(m,["|"],[]);
                 names = llParseString2List(m,["|"],[]);
             }else if(n == 2){
             }else if(n == 2){ // if n == 2, we're parsing position vectors
                 pos  = llParseString2List(m,["|"],[]);
                 pos  = llParseString2List(m,["|"],[]);
                 j=llGetListLength(pos);
                 j=llGetListLength(pos);
Line 84: Line 104:
                     pos = llListReplaceList(pos,[(vector)llList2String(pos,i)],i,i);
                     pos = llListReplaceList(pos,[(vector)llList2String(pos,i)],i,i);
                 }
                 }
             }else if(n == 3){
             }else if(n == 3){ // if n == 3, we're parsing rotations
                 rot  = llParseString2List(m,["|"],[]);
                 rot  = llParseString2List(m,["|"],[]);
                 j=llGetListLength(rot);
                 j=llGetListLength(rot);
Line 90: Line 110:
                     rot = llListReplaceList(rot,[(rotation)llList2String(rot,i)],i,i);
                     rot = llListReplaceList(rot,[(rotation)llList2String(rot,i)],i,i);
                 }
                 }
             }else if(n == 4){
             }else if(n == 4){ // if n == 4, we're parsing prim sizes
                 size  = llParseString2List(m,["|"],[]);
                 size  = llParseString2List(m,["|"],[]);
                 j=llGetListLength(size);
                 j=llGetListLength(size);
Line 97: Line 117:
                 }
                 }
             }
             }
             if(names != [] && pos != [] && rot != [] && size != []){
 
             if(names != [] && pos != [] && rot != [] && size != []){ // start truncating the lists once they're all populated


                 llOwnerSay("Exporting:");
                 llOwnerSay("Exporting:");
Line 105: Line 126:
                 list bar = [];
                 list bar = [];
                 j= llGetListLength(pos);
                 j= llGetListLength(pos);
                 for(i=0;i<j;++i){
                 for(i=0;i<j;++i){ // truncate the
                     bar += truncate_vector(llList2Vector(pos,i));
                     bar += truncate_vector(llList2Vector(pos,i)); // minify each position vector
                 }
                 }
                 export_string("pos:" + llDumpList2String(bar,"|"));
                 export_string("pos:" + llDumpList2String(bar,"|")); // export minified position vectors to chat
 
                 bar = [];
                 bar = [];
                 for(i=0;i<j;++i){
                 for(i=0;i<j;++i){
                     bar += truncate_rotation(llList2Rot(rot,i));
                     bar += truncate_rotation(llList2Rot(rot,i)); // minify each rotation
                 }
                 }
                 export_string("rot:" + llDumpList2String(bar,"|"));
                 export_string("rot:" + llDumpList2String(bar,"|")); // export minified rotations to chat
 
                 bar = [];
                 bar = [];
                 for(i=0;i<j;++i){
                 for(i=0;i<j;++i){
                     bar += truncate_vector(llList2Vector(size,i));
                     bar += truncate_vector(llList2Vector(size,i)); // minify each size vector
                 }
                 }
                 export_string("size:" + llDumpList2String(bar,"|"));
                 export_string("size:" + llDumpList2String(bar,"|")); // export minified sizes to chat
                 bar = [];
 
                 llMessageLinked(LINK_THIS,0,"XDmenu",NULL_KEY);
                 bar = []; // we're about to reset the script so this might be superfluous
                 llResetScript();
                 llMessageLinked(LINK_THIS,0,"XDmenu",NULL_KEY); // re-open the menu
                 llResetScript();  
             }
             }
         }else if(k == import && n == -1){
         }else if(k == import && n == -1){
Line 135: Line 159:
     }
     }
}
}
</lsl>
</source>


[[Category:Open Prim Animator]]
[[Category:Open Prim Animator|Import/Export]]

Latest revision as of 15:00, 26 February 2015

//!  @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();
        }
    }
}