I'm working on a script, and I need the OldP = selection in an array to keep its values so I can re-call the objects positions later and move them back to where they used to be. But for some reason that variable gets changed to their new pos after I move them.....
Does anyone know what I'm doing wrong??
global oldP = selection as array
local cnt = 0
for obj in selection where selection.count > 0 do
(
--stores objects as array so we can get restore there positions later
cnt += 1
print oldP[cnt].position
--sets the selections pivots to the averaged center point, and moves them to 0,0,0 as a group average.
selection.pivot = selection.center
selection.position = [0,0,selection.center[3]]
)
)
else
(
local cnt = 0
for obj in selection where selection.count > 0 do
(
cnt += 1
obj.position = oldP[cnt].position
print oldP[cnt]
--obj.pivot = obj.position
)
I would really appreciate any input.
M
Replies
//plugs in programming keyboard... I am not sure if pos will be referenced here instead of copies (like simple values) so thats why I deep copied them. Later in your script you just reference to them using a for i=.. loop instead of the one I used so that you can use: posArray where i is the iterator.
If I get it right you want to temporarily shift all selected objects to an average plane along the Z axis, print their positions and later shift them back to their original positions. Does it make sense? Could you post a complete block of code like the whole if..else?
When you do oldP = (selection as array) each item in there is a reference to the actual object in the scene. When you change the pivot and position of each object using Selection.pivot = ... you are changing the same object that is stored in oldP.
I wrote something that demonstrates how to use a struct to store and restore data using your code posted, I'm pretty sure this is the effect you were going for.
To use this select your object in the scene and call the function: storeData &objectData
& is a symbol that tells maxscript to pass by reference instead of by value. For more information on pass by reference and pass by value you can look here http://cplus.about.com/od/glossar1/g/passbyrefdefn.htm
To simplify it though, passing by reference tells the function to not make a copy of the variable and edit it directly, pass by value, which is the default for most maxscript variables, will make and use a copy of the variable passed in. Keep in mind that not all variables passed into a function are being passed by value, object handles are an example of a variable that is always passed by reference.
Ok, got a little off tangent there, but the second part of script is a function called: restoreData. To restore your objects to their original pivot and position call the function: restoreData objectData
Notice, as we are not editing the variable there is no reason to pass this by reference.
example:
The script idea was asked by a fellow artist, if I could make something to take a selection of object(s), and temporarily move them to 0,0,0, or in general around that point, then move them back to where they were when the button is deactivated.
I'll checkout what u guys have posted and see what works best for what I need. But I'll keep all the info. u guys supplied for further use/research
Edit:
Ok...So I think what render has might work fine, it's simple enough, although I get the error after copying the first objects position "unable to convert:[-2.31057,-9.35394,0] to type: Array
mgrd: Your's doesn't quite work how I was planning, but it did move each object to 0,0,0. I was planning to move the whole selection. But the issue with the code was that when I did move back, it moved back the object pivot, but not the mesh itself :S.
Pilflik: I tried yours as well, and maybe I didn't call them right... but I evaluated both of those lines, then moved the objects, and printed out the oldPos, and it said 0,0,0.
So after testing it, like u mentioned, I have to call that dialog to get it to properly work? Maybe I'll just have a button my toolbar create/destroy that little button then to make sure it all works as expected.
thank you very much
You don't have to worry about it though, I'll figure it out... I hope, ha. I just want to do that so the selection doesn't sit halfway through the grid in there z pos.
Edit: Got that working, so all is sweetness . Now to polish up a couple last things on my tools