Incremental Moves

SloshSlosh Posts: 2,391
edited December 1969 in Technical Help (nuts n bolts)

Does anyone know how to write/create a pose that moves an object an incremental amount in one direction? For example, you have a cube at position 0,0 (center) and you want to move it 10 units to the right. Then you want to move it 10 more units to the right, or even 10 units backward. How do you write that into a script? When I use a parameter dial to move the object 10 units, then save it as a pose, applying the new pose always puts it back to the 10 unit mark. I tried making sure that only the x-translation was checked before saving the pose, but the problem is that the pose treats the 10 as absolute, not additive.

I am sure this is going to involve establishing a variable for position x, the having a function similar to the following:

b = b (current position on x axis)
c = b + 10 (new position on x axis)
set position = c, y, z

This is for a freebie I am planning to distribute that allows multiple copies of an object in the scene, but each needs to be a specific distance apart so that an object spanning between them will fit perfectly.

Comments

  • adamr001adamr001 Posts: 1,322
    edited December 1969

    You'd need to use a script. Read in the existing coordinates, do the math, apply the new values to translation x,y and z.

    If I can find the time today I might look at it, but crazy busy at the moment so not sure if I'll get to it or not.

  • JaderailJaderail Posts: 0
    edited December 1969

    I have this Info running around some place. I create Pose controls for Modular sets with scripts for easy set up. I'll find the Base example and post the code if Adam does not beat me to it.

  • JaderailJaderail Posts: 0
    edited April 2013

    Here is a Basic example of a move Script. I'm not sure when I found this but I'm sure it was from the old Forums.
    EDIT The Proper indenting did not Post But I think it will still work properly.

    BIG EDIT! will update in few with proper code.

    Post edited by Jaderail on
  • SloshSlosh Posts: 2,391
    edited December 1969

    Jaderail said:
    Here is a Basic example of a move Script. I'm not sure when I found this but I'm sure it was from the old Forums.
    EDIT The Proper indenting did not Post But I think it will still work properly.

    BIG EDIT! will update in few with proper code.

    Oh, you both are lifesavers! I used to do some programming in Visual Basic, so I get the gist of most of it, but of course there will be differences. Jaderail, would you consider a quick beta of my freebie, once I implement this code? I don't want to get specific here, not yet anyway.

  • SloshSlosh Posts: 2,391
    edited December 1969

    adamr001 said:
    You'd need to use a script. Read in the existing coordinates, do the math, apply the new values to translation x,y and z.

    If I can find the time today I might look at it, but crazy busy at the moment so not sure if I'll get to it or not.

    Adam, I have several of your scripts in my content directory already. I was hoping you would reply, and you did not disappoint! I am really looking forward to getting this done, since it will also be a learning experience for a bigger product I am working on. Modular and whatnot. Jaderail is putting something together for me, as well, since he does some modular work.

  • adamr001adamr001 Posts: 1,322
    edited April 2013

    I thought about going through the whole shebang of making a GUI prompt to prompt for translation value but that seems unnecessary (why not just use the parameters tab, yanno?)

    Anyway, try this:

    //QND Move it move it move it script 
    
    var g_nXOffset = 10; //set this value to whatever you want to translate on X
    var g_nYOffset = 0; //set this value to whatever you want to translate on Y
    var g_nZOffset = 0; //set this value to whaever you want to translate on Z
    var g_ncounter;
    var g_oSelected;
    
    function MoveIt( g_oSelected ){
     var l_nXtran; // funciton local variables
     var l_nYtran;
     var l_nZtran;
     if (g_oSelected == undefined) return -1; // bail out!  Error!  Error!
     l_nXtran = g_oSelected.getXPosControl().getValue();
     l_nYtran = g_oSelected.getYPosControl().getValue();
     l_nZtran = g_oSelected.getZPosControl().getValue();
     g_oSelected.getXPosControl().setValue(l_nXtran + g_nXOffset);
     g_oSelected.getYPosControl().setValue(l_nYtran + g_nYOffset);
     g_oSelected.getZPosControl().setValue(l_nZtran + g_nZOffset);
    }
    
    g_ncounter = Scene.getNumSelectedNodes(); // get the number of selected objects.
    
    for (g_ncounter; g_ncounter--;){ // loop through each object
     MoveIt(Scene.getSelectedNode(g_ncounter)); // move each object
     }
     
    
    Post edited by adamr001 on
  • JaderailJaderail Posts: 0
    edited December 1969

    Slosh said:
    Jaderail said:
    Here is a Basic example of a move Script. I'm not sure when I found this but I'm sure it was from the old Forums.
    EDIT The Proper indenting did not Post But I think it will still work properly.

    BIG EDIT! will update in few with proper code.

    Oh, you both are lifesavers! I used to do some programming in Visual Basic, so I get the gist of most of it, but of course there will be differences. Jaderail, would you consider a quick beta of my freebie, once I implement this code? I don't want to get specific here, not yet anyway. Be more than happy to Beta for you. Just drop me a PM.

  • SloshSlosh Posts: 2,391
    edited April 2013

    adamr001 said:
    I thought about going through the whole shebang of making a GUI prompt to prompt for translation value but that seems unnecessary (why not just use the parameters tab, yanno?)

    Are you okay with me using this code as is (minus the funny text like Move it, move it, move it :) )? It totally makes sense, I was just unsure about naming conventions for variables. Also, if I had gone through the trouble of trying to write it myself, I would have forgotten to include the other two axes, thereby hitting an error again and again, beating my head to a pulp against the wall.

    The main reason for doing this instead of relying on parameter dials is because if the person wants to put 15 of these in the scene, it would be a pain to line up each one, and the align tab would not help in this case. Of course, logic tells us once you figure out the distance of the first one, the rest should just be a matter of typing in the same value for each new instance. But reading through the forum, it is quite clear that "click this icon" is much preferred over "do this math" and a lot of people would not think to remember the distance between each one. I included one of those empty poses to tell the user that the distance between two is 141.20, but most ignore those, I think.

    Which leads to another question... to use instancing or not? I am not 100% clear on the difference between an instance and simply putting another figure into the scene. But, I will read up on that

    Of course, the ideal scenario, which I will endeavor to write on another project, would be a GUI that prompt for how many instances and in what configuration, then the script lays the whole thing out for them. But I haven't done any kind of coding in about 12 years, so my brain is a little foggy on most of that.

    Post edited by Slosh on
  • adamr001adamr001 Posts: 1,322
    edited December 1969

    Sure, use the code. There are only a few ways to do this and all of them involve the same root functionality. Variable names are variable names. You could make them anything you want. I like to prefix mine. g_ for global, l_ for local, n for number, o for object, etc. after that it's just a reasonably descriptive name. The function name can also be just about anything (so long as you don't try and use an existing function name, that can get ugly) so feel free to personalize it. the // stuff is just comments.

    You may want to look at adding undo functionality around the main for loop if you want the action to be part of the undo stack.

    Basically there are three undo actions...

    beginUndo();
    cancelUndo();
    acceptUndo('some text here');

    You need a begin and an accept, cancel is optional.

  • adamr001adamr001 Posts: 1,322
    edited December 1969

    RichardH pointed out a few mistakes in the original code (not fatal ones, but mistakes nonetheless)... I've corrected them in the original code posting. Please double check that you have the current version.

  • SloshSlosh Posts: 2,391
    edited December 1969

    adamr001 said:
    RichardH pointed out a few mistakes in the original code (not fatal ones, but mistakes nonetheless)... I've corrected them in the original code posting. Please double check that you have the current version.

    Will do, thanks. I'm glad I haven't had the chance to try the other yet, I would have probably gotten frustrated. That being said, I would have worked it out, which is how I learned coding in the first place.

  • adamr001adamr001 Posts: 1,322
    edited December 1969

    The code worked, I wouldn't have posted it without that, but I was accidentally using undeclared variables. I'd declared locals but created new globals on the fly. LOL. Fortunately (?) DAZ Script lets you do that, but it's bad practice.

Sign In or Register to comment.