PDA

View Full Version : Copy array (shallow copy)


Himbeertoni
03-02-2008, 11:00 PM
Hi,
i want to have a copy of an array but not of the objects that it contains (one array should be sorted but not the other one). In fact i want just what the copy <array> function seem to do. But that returns "OK" so i cant use it like that: newArray = copy oldArray. If i add that [#noMap] command it just throws another error. i found another thread for that problem but it does not solve it http://forums.cgsociety.org/showthread.php?t=421078 (http://forums.cgsociety.org/showthread.php?t=421078)
Edit: It worked with the methods described in that thread. i'd still like to know how to use the "copy" command
(http://forums.cgsociety.org/showthread.php?t=421078)

Moosley
03-03-2008, 01:56 PM
The copy <array> seems to be the most pointless function ever. Why is it there, what does it do? In what situation would you use it ?

U.S.S. Speed
03-04-2008, 01:36 AM
Ok, I'm just shooting something I remember from the back of my head with no sure thing at all...

Is it the case of using

NewArray = deepcopy OtherArray


If that's not that specific case, just ignore this post.

RobGalanakis
03-04-2008, 05:28 AM
The copy <array> seems to be the most pointless function ever. Why is it there, what does it do? In what situation would you use it ?

It has to do with pointers.

Array1 = #(1, 2, 3)
Array2 = Array1
so: both Array1 and Array2 are the same, but more than that, they 'point' to the same data
append Array1 4
then: Array2 = #(1, 2, 3, 4)

If we did Array2 = copy Array1, however, Array2 would not be pointing to the same data as Array1, and thus would still be equal to #(1, 2, 3)

Moosley
03-04-2008, 08:35 AM
Yeah, I get that much, but I mean the actual function:

copy #(1,2,3)

returns 'OK'

so doing array1 = copy #(1,2,3) is useless. Or am i missing something there?

U.S.S. Speed
03-04-2008, 01:07 PM
Ok... I checked back my old scripts, and yeah...

used :


NewArray = deepcopy OldArray


It "duplicate" the whole data into a new array. So the new and old array has no link at all, but has virtually the same data.
Changing one array, like sorting it, won't change the second.

Moosley
03-04-2008, 01:19 PM
deepcopy is fine, but just 'copy' is still the odd one.

U.S.S. Speed
03-04-2008, 01:27 PM
Yeah... No. I haven't found any use to copy alone.
As if I remember well Array1 = Array2 does kinda the same thing.

RobGalanakis
03-04-2008, 02:47 PM
Yeah, I get that much, but I mean the actual function:

copy #(1,2,3)

returns 'OK'

so doing array1 = copy #(1,2,3) is useless. Or am i missing something there?

Yes, that function as you used it is useless. Because that is not the point of the function. Its intended use is as I stated, and as you both seem to understand.

Yeah... No. I haven't found any use to copy alone.
As if I remember well Array1 = Array2 does kinda the same thing.
As I just stated below, (Array1 = Array2) != (Array1 = copy Array2)

U.S.S. Speed
03-04-2008, 03:03 PM
Yes, that function as you used it is useless. Because that is not the point of the function. Its intended use is as I stated, and as you both seem to understand.


As I just stated below, (Array1 = Array2) != (Array1 = copy Array2)

I tought some people said it clearly...


A1 = #(1,2,3)
return #(1,2,3)
A2 = A1
return #(1,2,3)
A3 = copy A1
return OK
A4 = deepcopy A1
return #(1,2,3)
A3
return OK
A3 == OK
return True
append A2 4
return #(1,2,3,4)
A1
return #(1,2,3,4)
A2
return #(1,2,3,4)
A4
return #(1,2,3)


A2 = A1 point at the same data. One change to one will change the other.
A3 = copy A1 return always OK. What is the use of this?
A4 = deepcopy A1 copy every value of the array into a new one. Duplicating the data.

So, what is the point of the function COPY?

Bobo
03-04-2008, 03:41 PM
So, what is the point of the function COPY?

There is no point.
When MAXScript was developed during Max 1.0 days, each value type added to it implemented certain methods. I suspect that the copy() for arrays was added for no practical reason, but because all other values exposed it. Thus, if you would write code that performs copy() on various data values, having no copy() for arrays would require special case handling as it would probably throw "no copy method for #(1,2,3)". Of course, returning OK is awful and will cause another problem, but I am just trying to figure out the motivation to put the copy method into a Array Value class to start with.
So let's assume for now it was a case of (pun intended) "copy and paste" from other value class implementations and not really meant to do anything. Just like setNormal() which is even more pointless as Max recalculates vertex normals on the fly...

Take it as a historical peculiarity and don't use it.

You can also use a for loop to make the top-level elements of an array unique:

a = #(1,2,3)
-->#(1,2,3)
b = for i in a collect i
-->#(1,2,3)
a[3] = 10
-->10
b
-->#(1,2,3)

This will NOT perform a deep copy though, only a shallow copy:

a = #(1,2,#(4,5,6))
-->#(1,2,#(4,5,6))
b = for i in a collect i
-->#(1,2,#(4,5,6))
a[1] = 42
-->42
b[1]
-->1
a[3][1] = 100
-->100
b[3][1]
-->100

As you can see, the sub-array in the 3rd element was instanced, while the first and second element were disconnected. It would require sub-loops to disconnect nested arrays, so deepcopy() would be better in this case.

Deepcopy() was added in Max 9, so for 9, 2008 and 2009 development it is the way to go.

RobGalanakis
03-04-2008, 05:24 PM
Thanks Bobo and sorry USS Speed. However, copy does seem to work with #noMap as I think was stated earlier.

Moosley
03-05-2008, 10:01 AM
There is no point.


Suspicion Confirmed :)

CGTalk Moderation
03-05-2008, 10:01 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.