Notifications
Clear all

[Closed] Problem with attach loop

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 [x][3]), 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

while ThreeArray.count >1 do
  (
  holdingArray = #()
  x=2
  while x <= ThreeArray.count do 
 (
 
 if ThreeArray[x][3] == ThreeArray[1][3] then
 ( 
 Polyop.attach (ThreeArray[1][1]) (ThreeArray[x][1])
 append  holdingArray (x)
 print ThreeArray[x][3]
 )
 x += 1
 )
 sort holdingArray
 print holdingarray
 print "Done with Array!"
 
 for z in holdingArray do deleteitem ThreeArray (holdingArray[holdingArray.count+1-z])
 deleteitem ThreeArray 1
 )
1 Reply

I must be that the expression (holdingArray[holdingArray.count+1-z]) is returning 0.

while ThreeArray.count >1 do
  (
  holdingArray = #()
  x=2
  while x <= ThreeArray.count do 
 (
 
 if ThreeArray[x][3] == ThreeArray[1][3] then
 ( 
 Polyop.attach (ThreeArray[1][1]) (ThreeArray[x][1])
 append  holdingArray (x)
 print ThreeArray[x][3]
 )
 x += 1
 )
 sort holdingArray
 print holdingarray
 print "Done with Array!"
 
 for z in holdingArray do deleteitem ThreeArray (holdingArray[holdingArray.count+1-z])
 deleteitem ThreeArray 1
 )

I can’t really test this without your scene but it looks like you need to replace for z in holdingArray with for z = 1 to holdingArray.count.

while ThreeArray.count >1 do
(
	holdingArray = #()
	x=2
	while x <= ThreeArray.count do 
	(
		if ThreeArray[x][3] == ThreeArray[1][3] then
		( 
			Polyop.attach (ThreeArray[1][1]) (ThreeArray[x][1])
			append  holdingArray (x)
			print ThreeArray[x][3]
		)
		x += 1
	)
	sort holdingArray
	print holdingarray
	print "Done with Array!"

	for z = 1 to holdingArray.count do
	(
		deleteitem ThreeArray (holdingArray[holdingArray.count+1-z])
	)
	deleteitem ThreeArray 1
)