save and re-using objects rotation



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 07/02/2009 at 16:41, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   10.1 
    Platform:   Windows  ;   
    Language(s) :   C.O.F.F.E.E  ;

    ---------
    im not good with coffee (this would be my first try at it)

    i made a script that finds the objects rotation HPB and prints it.

    what i want to do now is to store that data and be able to call it back later.

    eg.. i want to have a button that sets the object HPB coordinates to "0,0,0".
    then if i press the button again it will set the object back to its last HPB coordinates (when i pressed the button the first time)

    here is the start of the code

    > var obj = doc->FindObject("Cube"); \> var m = obj->GetMg();                  \> var rot = obj->GetRotation(); \> obj->SetMg(m); \> obj->SetRotation(vector(0.0,0.0,0.0)); \> println("Global rot = ", rot); \>



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 07/02/2009 at 17:12, xxxxxxxx wrote:

    Be careful. First, GetRotation() returns the 'local' rotation of the object whereas 'm' is the 'global' matrix (global rotation). You don't need to obj->GetMg() and obj->SetMg() while using GetRotation().

    Is this script in a COFFEE tag or a command plugin? It will make a difference where the script is on what you can easily do or how to do it. For a COFFEE 'plugin', it is trivial: simply store the rotation vector in rot as you have done and use it to reset the rotation when the button is pressed: obj->SetRotation(rot);

    The problem with a script tag is that it is run constantly. Your Cube would have its rotation set to vector(0.0..) no matter what you try to do.

    You set a state variable to know whether to zero or reset the rotation:

    // initialize it somewhere like Init()
    var reset = false;
    ...
    // when the button is pressed
    if (reset == true)
    {
    obj->SetRotation(rot);
    reset = false;
    }
    else
    {
    obj->SetRotation(Vector(0.0,0.0,0.0));
    reset = true;
    }

    More information would be helpful.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 07/02/2009 at 18:04, xxxxxxxx wrote:

    that is closer to what i want but there is a part missing.

    ill try to explain what i want

    eg..
    i rotate a cube 90 degrees in H rotation
    i execute the code (the cube resets to 0,0,0)
    the following is the part the code doesn't do
    i want to then execute the code again and have it reset to the rotation before i executed the code the first time

    (maybe this need to be 2 separate scripts to work?)

    your code that you post does print the first coordinates
    but it doesnt reset the object(cube) back to those coordinates when i execute again (it just stays as 0,0,0)

    thanks for your help



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 07/02/2009 at 18:36, xxxxxxxx wrote:

    The reason that won't work as expected is that each time you run the code, everything in the code is reinitialized - including the rotation that you are trying to store.

    One way around this is instead to create a Tag plugin and store the rotation (and create the button) there. Now it is simply a matter of using the tag (and the object is now explicit to the object on which the tag is set: tag->GetObject()).

    A COFFEE Command plugin should be capable of doing the same but you'll have to juggle any number of objects or limit it to one particular one.

    This will not work in a script (Script Manager, COFFEE tag, or XPresso COFFEE node). The reason is that none of these can 'store' data internally between invocations.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 08/02/2009 at 07:19, xxxxxxxx wrote:

    You can have this as a start to look at.
    Use the code in a coffee expression tag and add three Userdata boleans on the tag.
    The memory variable is placed globally, before the main().

    If you need/want to store anything inside a object directly you must
    get a unique ID from PluginCafe and use that ID as your memory.
    This way you could use a script or commandplugin instead of a tag.

    And as Robert pointed out, this is "only" the local rotation.
    If you need global rotation you need to use Matrix (Mg).

    Cheers
    Lennart

    > \> GetFirstTag(obj,type) // Our function for getting our tag \> { \> var tag = obj->GetFirstTag(); \> while (tag){ \> if (tag->GetType() == type) return tag; \> tag = tag->GetNext(); \> } \> return NULL; \> } \> \> \> var memory; // This is our memory for store and restore as a Global variable \> \> main(doc,op) \> { \> \> var tag     = GetFirstTag(op,Tcoffeeexpression); // Get our tag \> var zero    = tag#ID_USERDATA:1; // Get our "Zeroing" Userdata bolean \> var store   = tag#ID_USERDATA:2; // Get our "Store" Userdata bolean \> var restore = tag#ID_USERDATA:3; // Get our "Restore" (read) Userdata bolean \> \> var oprot   = op->GetRotation(); // Get the curent op rotation (local rotation) \> \> if(!memory) memory = oprot; // If no memory, fill it with current rotation \> \> if(store == 1) // Load memory with current rotation \> { \> memory = oprot; \> tag#ID_USERDATA:2 = 0; // Set the bolean Userdata back to "0". (Off) \> EventAdd(); \> } \> \> if(restore == 1) // Set the rotation from memory \> { \> op->SetRotation(memory); \> tag#ID_USERDATA:3 = 0; // Set the bolean Userdata back to "0". (Off) \> EventAdd(); \> } \> \> if(zero == 1) // Set the op rotation to zero \> { \> op->SetRotation(vector(0)); // Yes, you can write it this way with a single "0" vector \> tag#ID_USERDATA:1 = 0; // Set the bolean Userdata back to "0". (Off) \> EventAdd(); \> } \> \> } \>



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 08/02/2009 at 17:32, xxxxxxxx wrote:

    excellent.

    i wanted this code so when im animating something i can zero out rotation, move the object then restore the rotation back.

    it does exactly what i wanted.

    just something i want to do next is have it so it only rotates the axis (just like when you use the "Use Object Axis Tool")
    is there a command to do that?

    thanks for your help


Log in to reply