Problem with attach loop

Problem with attach loop

Anonymous
Not applicable
644 Views
3 Replies
Message 1 of 4

Problem with attach loop

Anonymous
Not applicable
So I'm really new to maxscript, and I'm assuming this'll be super easy for someone to figure out. I have a function that goes through a multidimensional array of polys looking for objects with the same name tag (in index ), then it attaches them, stores the position in a new array, then goes through that array and deletes the elements from the original array. This process continues until there are no more elements in the original array.

I keep getting an error on the final for loop, when trying to remove the original array elements.
Error is:

-- Error occurred in z loop; filename: C:\---.ms; position: 1532; line: 48
-- Frame:
-- z: 316
-- called in anonymous codeblock; filename: C:\---.ms; position: 1571; line: 48
-- Frame:
-- holdingArray: #(2, 3, 4, 5, 6, 7, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, ...)
-- x: 359
-- Runtime error: array index must be positive number, got: 0

Code:

  while ThreeArray.count >1 do
(
holdingArray = #()
x=2
while x <= ThreeArray.count do
(

if ThreeArray == ThreeArray then
(
Polyop.attach (ThreeArray) (ThreeArray)
append holdingArray (x)
print ThreeArray
)
x += 1
)
sort holdingArray
print holdingarray
print "Done with Array!"

for z in holdingArray do deleteitem ThreeArray (holdingArray)
deleteitem ThreeArray 1
)
0 Likes
645 Views
3 Replies
Replies (3)
Message 2 of 4

Steve_Curley
Mentor
Mentor
Ignoring the error for a moment (as I believe it's a result of the way you're trying to do it, and unnecessary as well). Please correct me if I'm wrong, but I think this is all that the script is really trying to do.

Starting at the bottom of the array, check all the other elements and if they're the same attach the later one to the current one. In a nutshell - that's it - but repeated for whatever is left in the array.
Right? or wrong?

If that's right, then there is a much simpler, less convoluted way to deal with the situation which doesn't require maintaining a second array then using that to remove elements already dealt with.

Bearing in mind that I do not have your original data to work with, I've had to construct the original ThreeArray manually (the first 2 elements of each row are just placeholders for the real data).
I've added a 4th element (which it not required - it's only there to show how many objects are being "collected" into that row of the array) - the single line of code which refers to element 4 can be removed from the code.
I don't know whether you need the array after the main processing is complete, but if you do then the final section of code cleans it up so it will only contain rows which still have un-attached objects in them. You can remove that if you don't need to do further work on the array.
The print lines are just debugs to show what is happening.
You will have to uncomment the attach line - without the real data that line would cause an error.

(
ThreeArray = #(
#(0,0,9,1),
#(0,0,6,1),
#(0,0,9,1),
#(0,0,9,1),
#(0,0,5,1),
#(0,0,6,1),
#(0,0,4,1),
#(0,0,6,1)
)

clearListener()
print "Original Array"
print ThreeArray

for outerLoop = 1 to ThreeArray.count - 1 do
(
if ThreeArray != undefined then
(
for innerLoop = outerLoop + 1 to ThreeArray.count do
(
if ThreeArray != undefined then
(
if ThreeArray == ThreeArray then
(
-- Polyop.attach ThreeArray ThreeArray
ThreeArray += 1
ThreeArray = undefined
)
)
)
)
)

print "After processing"
print ThreeArray

for x = ThreeArray.count to 1 by -1 do
if ThreeArray == undefined then
deleteItem ThreeArray x

print "After removing redundant elements"
print ThreeArray
)

The results should be as follows - you can see at each stage what the results are and relate them back to what is (was) in the original array.

"Original Array"
#(0, 0, 9, 1)
#(0, 0, 6, 1)
#(0, 0, 9, 1)
#(0, 0, 9, 1)
#(0, 0, 5, 1)
#(0, 0, 6, 1)
#(0, 0, 4, 1)
#(0, 0, 6, 1)
"After processing"
#(0, 0, 9, 3)
#(0, 0, 6, 3)
#(0, 0, undefined, 1)
#(0, 0, undefined, 1)
#(0, 0, 5, 1)
#(0, 0, undefined, 1)
#(0, 0, 4, 1)
#(0, 0, undefined, 1)
"After removing redundant elements"
#(0, 0, 9, 3)
#(0, 0, 6, 3)
#(0, 0, 5, 1)
#(0, 0, 4, 1)
OK

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 4

Anonymous
Not applicable
Thanks a bunch, that works great! And you're right, it's a much cleaner and less convoluted way of accomplishing what I was aiming for. Thanks for showing me a better way to go about it.
0 Likes
Message 4 of 4

Steve_Curley
Mentor
Mentor
You're welcome - consider it an early Chrismas present 😉

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes