[Closed] Array problem – What do I do wrong 🙁 ???
Hi,
I’m working on my newbe script that will transfer selected vertices by random amount in random directions.
…I can’t figure out what I do wrong.
The way I want that to work requires to generate a number of random target point for each selected vertex.
Let’s say I have 3 vertices so I generate an array with 3 random destination points.
#([1,23,6], [21,7,77], [9,51,14])
All worked fine till I decided to add a possibility to turn on/off X, Y and Z transformations.
I’m turning an axis off by killing one of the axis values for every random point.
#([1,23,0], [21,7,0], [9,51,0])
…this way z axis gets no offset
Before I kill the values I do a copy of the array with another name so I switch the axis back on by bringing in the original points.
This part does not work. It looks like the script kills the values in both arrays but in my understanding it id not supposed to?
What do I do wrong ?!
Here is the part of the script:
macroScript RandomizeVerts
category:“MDScripts”
toolTip:””
(
local TheVerts
local TheRandomValuesXYZ
local TheRandomValues
--gets selected verts
TheVerts = polyop.getVertSelection $ as array
--generates random 3point values - as many as selected verts
fn TheRandomValuesXYZFunction =
(
TheRandomValuesXYZ = for o = 1 to TheVerts.count collect ([(random -1.0 1.0),(random -1.0 1.0),(random -1.0 1.0)])
)
TheRandomValuesXYZFunction()
--makes copy of the array
fn TheRandomValuesFunction =
(
TheRandomValues = for o in TheRandomValuesXYZ collect o
)
TheRandomValuesFunction()
-----------------------------------------------
-- ROLLOUT
-----------------------------------------------
Rollout RandomizeVertsRollout "Randomize Vertices"
(
button zoff "z off"
button zon "z on"
Button TheDice "Dice"
-- re-generates the random 3point value
on TheDice pressed do
(
TheRandomValuesXYZFunction()
TheRandomValuesFunction()
for o in TheRandomValuesXYZ do print o -- prints new points
)
--switches OFF the Z axis
on zoff pressed do
(
for o in TheRandomValues do o.z = 0
for o in TheRandomValues do print o -- prints the points with z=0
)
-- SUPPOSE TO BRING IN BACK THE Z AXIS BY CALLING IN THE ORIGINAL RANDOM VALUES (TheRandomValuesXYZ)
--WHICH SUPPOSE TO REMAIN UNCHANGED
on zon pressed do
(
TheRandomValuesFunction()
for o in TheRandomValues do print o -- prints the points with z=0 :(:(:(
for o in TheRandomValuesXYZ do print o -- prints the points with z=0 :(:(:(
)
)
Createdialog RandomizeVertsRollout
Some value types in maxscript require you to explicitly copy the original variable to create an independent copy, such as matrix3, point2, point3, array, etc.
a = [10,10,10]
[10,10,10]
b = a
[10,10,10]
b.z = 0
0
a
[10,10,[b][color=orange]0[/b]][/color]
The line b = a does not create a copy, instead it returns a pointer to the same point3 value. So changing a property of any of these (a or b) will change both.
a = [10,10,10]
[10,10,10]
b = [b]copy[/b] a
[10,10,10]
b.z = 0
0
a
[10,10,[b][color=orange]10[/b]][/color]
As you can see, the copy method creates an independent copy and any changes to either a or b no longer affect the other.
On a side note, wouldn’t it be simpler to generate the random values on the fly, instead of copying and reusing them each time?
Martijn
Thank you Martijn,
This is exactly what I felt but I did not know how to make it a copy. Thanks a milion.
Also thanks for the note but I have imagined a certain functionality for the script and I’m trying to finish it this way also for sake of learning
Cheers