Notifications
Clear all

[Closed] how to put this to an array

hi everybody
here it is script to get number of keys of an animated object and get the time of the keys
but to add the result into an array i have a problem
any help is appreciated

(
 	--get the keys number
 	key_num = numkeys $.position.controller
 	keyarr=#()
 	
 	for i = 1 to key_num do 
 	(
 
 		(
 		--get the keys' time
 		AA=(getkeytime $.position.controller i as integer )/TicksPerFrame
 		print AA
 		keyarr= #()
 		oo=for j in AA do (append j keyarr )
 		)
 	)
 )
11 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0
fn collectKeyTimes controller sorted:on = if isController controller and numKeys controller > -1 do
(
	if sorted do sortKeys controller 
	for k in controller.keys collect k.time.frame
)
collectKeyTimes $.pos.controller
$.position.controller.keys

Is an array with all the keys related to the position controller. You can access their time and a few other things :

for key in $.position.controller.keys do print key.time

[doc]( http://forums.cgsociety.org/ )


fn getKeyNum node =
(
	keyarr=#()
	key_num = numkeys node.position.controller
	
	while key_num > 0 do 
	(
		append keyarr (getkeytime node.position.controller key_num)
		key_num -=1
	)
	sort keyarr
)

getKeyNum selection[1]

Thanks for the reply ,i am a beginner in maxscript sorry for multiply question
i didn’t well undrestand the above script so it cause me another question

here it is


 	fn getKeyNum node =
 	(
 		keyarr=#()
 		key_num = numkeys node.position.controller
 		
 		while key_num > 0 do 
 		(
 			append keyarr (getkeytime node.position.controller key_num)
 			key_num -=1
 		)
 		sort keyarr
 	)
 Arr=getKeyNum $		-- it turns   #(0f, 7f, 13f, 26f)
 	
 lastElement =  arr[arr.count]		--it turns 26f
 
  oo = for i in  Arr do
 	(
 	newarr=#()
 	oo = (i+lastElement	)
 		print oo
 	)
 		
 for i in newarr do(slidertime= i)
 

again i can’t put it in an array

@k4noe : There is no need to collect key times in an array and use a sort, since all keys are already sorted and stored in the controller.keys array. If you want to collect only the times and not the keys :

keyTimes = for key in $.position.controller.keys collect key.time

@ScriptLover : You have to reorganize your code and learn some of the basics :

 
  oo = for i in  Arr do
(
	      newarr=#()
	  oo = (i+lastElement)
	  print oo
)

This newarr shouldn’t be instantiated in the for loop. It’s not evend used.
If you want to grab all the elements with a for loop you can use the “collect” instead of “do”
If you just want to append a variable to an array use “append”

append array var

Again, the documentation is here to help you

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

actually this is not true:
check yourself:

delete objects
b = box()
addnewkey b.pos.controller[1] 1
addnewkey b.pos.controller[1] 10
b.pos.controller[1].keys[1].time = 20
print b.pos.controller[1].keys

but there is a controller method – sortkeys

sortkeys b.pos.controller[1]
print b.pos.controller[1].keys

Thank you all

Max’s Key arrays are frustrating to use (surprise!)

s = sphere()
animate on (
	at time 0 ( s.pos = [0,0,0] )
	at time 10 (s.pos = [10,0,0] )
	at time 20 (s.pos = [20,0,0] )
	at time 30 (s.pos = [30,0,0] )
)
c = s[#transform][#position][#x_position].controller
k2 = c.keys[2]
k2.value -- returns 10
i = getkeyIndex c 20
k3 = getKey c i
k3.time = 5
sortkeys c
k2.value -- returns 20: key value is a reference to the maxKeyArray index, not a MaxKey value

It’s best not to hold onto the key values beyond a single operation.

Damn controllers. Thanks for the tip Denis & Rotu.
I’m going to have to double check all my code now…

Thanks for all your reply
i hope i didn’t miss a very basic rule again.

here the try to get all the keys in pos rot and scale of an animated object

Arr=#($.pos.controller ,$.rotation.controller,$.scale.controller )
  
  AllkeysTime =
  	for i in Arr collect
  	(
  	fn collectKeyTimes i sorted:on = if isController i and numKeys i > -1 do
  		(
  			if sorted do sortKeys i  --controller2
  			for k in i.keys collect k.time.frame
  		)
  		collectKeyTimes i
  	)
  	
  cntArray=#()	--creat an empty array
  for i in AllkeysTime do ( join cntArray i 	) --join each controller keys to the Finalarr
  
  FinallArray = makeUniqueArray cntArray

Here is my first question, how to make these number sort?
not knowing the answer, I found a page to find the last key of an animationrange and here it is

	
  ----To get the last keys in a animation range
  sortkeys $.controller
  arr1 = $.position.controller.keys
  arr2 = $.Rotation.controller.keys
  arr3 = $.scale.controller.keys
  --arr4 =
  endtime = 0
  for i = 1 to 3 do
  (	
  	if (execute ("arr" + i as string + ".count")>0) do
  	(
  		key_time = (execute ("arr" + i as string + "[arr" + i as string + ".count]"+".time"))
  		if key_time > endtime then
  		endtime = key_time
  	)
  )
  format "endtime = %
" endtime

–So the result
FinallArray
Endtime

Now another question i like to say for n time of the num, we sum Endtime with FinallArray element and resultingarray get sum with Endtime


  fn AdEnTime Tharray lastkey = (for i in Tharray collect ( i + lastkey))
  
  AdEnTime FinallArray  Endtime
  
  num=2
  for i = 1 to num collect
      (
      Theplus = AdEnTime FinallArray  Endtime
      Thefinal =AdEnTime Theplus  Endtime
      )
  

but it just repeat 2 times

Thanks in advance

For the first question i think Sort is not bad

	Arr = #(0.0, 7.0, 9.0, 14.0, 27.0, 12.0)
      	Sort Arr
      ---result
      	#(0.0, 7.0, 9.0, 12.0, 14.0, 27.0)
  and for the second question 
Arr = #(0.0, 7.0, 9.0, 14.0, 27.0, 12.0)
 sortArr = sort Arr
 Endtime=Arr[Arr.count]
 num = 4
   
 ResultArray = for i = 1 to num collect
   (
 	arr = for i in arr collect (i +Endtime)
   )
 

–and if you need all the element of resultArray which is a Multidimensional array here and was my goal to make this post here:

 
  for j=1 to resultArray.count do
 	(
 		for i in resultArray[j] do (print i)
 	)

This time thanks God .