Simple Array insertitem problem


#1

Here’s my code:
Trying to figure out how to change an item in an existing array.
It keeps giving me an error “insertItem wanted 3, got 2”…and I’m too simple to figure it out.

Thanks in advance!

try(destroyDialog Temp_Array)catch()
– NOTE – MESH OJBECT MUST BE SELECTED IN IN VERTEX MODE TO WORK…
– Start Create Rollout Interface
rollout Temp_Array “Append Array Test” width:260 height:100
(
button ‘Create_Array_MD’ “Create Multi Dim Array” pos:[65,20] height:24 toolTip:“Track selected vertex.” align:#left

button 'Append_Test' "Append / Insert" pos:[80,55]  height:16 toolTip:"Track selected vertex." align:#left	
	

Global Indx = 1  -- Index of an array in Maxscript must start at 1...not 0
Global Cfrm = 0  -- Temp Frame Number		
Global Cvtx = 100  -- Temp vertex number
Global Sfrm = 10  -- starting frame of selection
Global Efrm = 20  -- ending frame of selection
Global Lck = "X" -- numbers locked or not
Global NumItems = 7  --  Number of data items in each 'line' 
Global AnimRangeStart = 0 -- Starting Frame Number To Process
Global AnimRangeEnd = 10 -- End Frame Number To Process
Global Range =1

Global dimCount = 1  -- used to test counting / size of arrays
Global myMultiDimArray=#(#(),#()) -- empty multi dimensional array named 'myMultiDimArray'
		
on Create_Array_MD pressed do
(
	clearlistener()
	-- Initialize count of array
	Range = (AnimRangeEnd +1)-AnimRangeStart    -- 0 to 100 = 101 frames.... 
	myMultiDimArray = #()
	myMultiDimArray[Range] = 0    --initialize a 100 elements array in memory
	for i = 1 to 100 do
	myMultiDimArray[i] = random 1 Range    --assign to predefined array	
	
	sliderTime = 0f
	Indx = 1
	dimCount = 1

	for r = AnimRangeStart to AnimRangeEnd do  -- loop from 0 to total items in range	
	(
		Cfrm = sliderTime as string
		Cfrm = trimright Cfrm "f"
		Cfrm = Cfrm as integer	-- get Current Frame
		
		Cvtx = 100+Cfrm-- 'get Current Vertex' - changing value here for testing
		
		Sfrm = Sfrm -- get first frame of vertex pos
		Efrm = Efrm -- get last frame when vertex pos	
		
		-- Lck will be determined by interface
		-- dimCount is preset befor for loop, and increments
		
		if r == AnimRangeStart do  -- First Write to Array -- Array Count = 1, but Frame (Animation Range), could be 0
		(
			myMultiDimArray=#(#(Indx,Cfrm,Cvtx,Sfrm,Efrm,Lck,dimCount))
		)
		
		if r > AnimRangeStart do  -- All other Writes
		(
			if r == 90 then -- temp test to change Lck item
			(
				Lck = "Y"
				append myMultiDimArray #(Indx,Cfrm,Cvtx,Sfrm,Efrm,Lck,dimCount)
				Lck = "N"
				
			)
			else -- Normal action
			(
				append myMultiDimArray #(Indx,Cfrm,Cvtx,Sfrm,Efrm,Lck,dimCount)	
			)

		)
		
	Indx += 1  --  This could be dimCount I suppose...
	sliderTime += 1
	dimCount += 1
		
	)		
	print myMultiDimArray -- Test

– ACCSESS ONE ITEM FOR TEST
dimCountTemp = myMultiDimArray.count – simple counter to check final array size…
print “-----------------------------------”
print ("Dim Count is: " + dimCountTemp as string)
print “requesting Index item myMultiDimArray[10] [7] - Answer should be 10”
print ("GETTING : " + myMultiDimArray[10] [7] as string)

	print "----------------------------------------------"
	print ("1: New Array Count: " + myMultiDimArray.count as string)
	print "----------------------------------------------"		
)

– Start Export MD Array to Data File
on Append_Test pressed do
(
try
(
print “Trying to Change 7th item in Index 10 to 33 ------------------”

		insertitem 33 myMultiDimArray[10] [7]
		
	)
	catch
	(
		print "OOPS..., Error on insertitem."
	)	

– ACCSESS ONE ITEM FOR TEST
dimCountTemp = myMultiDimArray.count – should not have changed since we didn’t change the array at all
print “-----------------------------------”
print ("Dim Count is: " + dimCountTemp as string)
print “requesting Index item myMultiDimArray[10] [7] - Old item was 10, New Answer should be 33”
print ("GETTING : " + myMultiDimArray[10] [7] as string)

)

)
– Create Rollout
createdialog Temp_Array


#2

insertItem() requires 3 arguments. <value><array><integer>. You are only giving it two arguments. SHould be something like:

insertItem 33 myMultiDimArray 10

Or, since it is multidimensional, make an array of your new values ([1] as 33 and whatever the second value is supposed to be as [2]) and put:

insertItem(myNewArray,myMultiDimArray,10)


#3

Am I making a mistake by having a multi dimensional array here?
My goal is to store data of a,b,c,d,e,f etc. on a per frame basis.
When something changes at frame “2”, then I want to access the ‘3rd’ position of that data (101 in this case), and only change that.

Here’s a sample of my array printout as it stands now:
I did this as a multi dimensional array. Was that even necessary?

Sorry…I really have a blind spot with arrays…

#(1, 0, 100, 10, 20, “X”, 1)
#(2, 1, 101, 10, 20, “X”, 2)
#(3, 2, 102, 10, 20, “X”, 3)
#(4, 3, 103, 10, 20, “X”, 4)
#(5, 4, 104, 10, 20, “X”, 5)
#(6, 5, 105, 10, 20, “X”, 6)
#(7, 6, 106, 10, 20, “X”, 7)
#(8, 7, 107, 10, 20, “X”, 8)
#(9, 8, 108, 10, 20, “X”, 9)
#(10, 9, 109, 10, 20, “X”, 10)
#(11, 10, 110, 10, 20, “X”, 11)


#4

Depends on what data you are storing. Looking over your earlier code, it looks like you are storing an index and a value. If this is the case, then yes, the multidimensional array is unnecessary as the array will already have an index value. Could be overcomplicating things.

But, if you really need that Y/N data in there, then you can use a multidimensional array.


#5

Sorry, my bad, you seem to be trying to give it three arguments here

In this line, the third argument should be an integer. The brackets are possibly what is throwing it off.


#6

That last suggestion worked…sort of.

I am looking at using some “Y” “N” data, but that could always be replaces with integers…

After trying:
insertitem 33 myMultiDimArray[10] 7

I get this printout. It inserted the ‘33’, but didn’t overwrite the '10"
That sort of makes sense using ‘insertitem’.
is there a way to replace the item…not just insert in front of it?

#(1, 0, 100, 10, 20, “X”, 1)
#(2, 1, 101, 10, 20, “X”, 2)
#(3, 2, 102, 10, 20, “X”, 3)
#(4, 3, 103, 10, 20, “X”, 4)
#(5, 4, 104, 10, 20, “X”, 5)
#(6, 5, 105, 10, 20, “X”, 6)
#(7, 6, 106, 10, 20, “X”, 7)
#(8, 7, 107, 10, 20, “X”, 8)
#(9, 8, 108, 10, 20, “X”, 9)
#(10, 9, 109, 10, 20, “X”, 33, 10)
#(11, 10, 110, 10, 20, “X”, 11


#7

Should be able to just replace it.

newDataArray = “build new array to replace sub array at [10]”
myMultiDimensionalArray[10] = newDataArray


#8

Suggest downloading MaxScript documentation. Will help a lot. Then you can find answers if I’m offline. :wink:

https://www.autodesk.com/developer-network/platform-technologies/me-sdk-docs-2020


#9

I’ll take a look.
Thanks for you help!


#10

No prob!


#11

For what it’s worth…you had the right info in your last post.

I just didn’t check the name of the array you had in your example vs. mine. Made the change, and all is working!

Thanks again…


#12

Sure. I know what it’s like when you are sitting there banging your head against the wall and you just can’t figure out what it is that you are doing wrong, but you know it’s something simple…

I was lucky to have someone sitting next to me while was learning to script who could help me out.

Serious though, having the scripting reference open while you are writing your code is so helpful. You may already be doing that, but sometimes it is just that slog through to find the right property or method. Although, in this case, it did not explicitly say how to replace data. You kinda had to know that you just use = as the assignment operator.

Again, glad to help.