Collapse Animation using Max script?

Collapse Animation using Max script?

niels2HFLZ
Explorer Explorer
2,203 Views
12 Replies
Message 1 of 13

Collapse Animation using Max script?

niels2HFLZ
Explorer
Explorer

Heya!

For my project I need to manually bake all objects that have constraints and controllers!

The Conversion Tools layout under Motion > Motion paths is very usefull for this:

Schermafdruk 2023-03-21 15.00.40.png

But it's very bothersome to navigate..

Everytime you click on the Motion Paths tab it wants to create the all of the visual lines in the vieport.. and with a lot of objects moving around in a big file just hangs for 5 minutes straight.

Also having to manually type in the range is also not ideal.

 

I was just wondering, if at all, it was possible to acces these functions via script!

 

So I can for example just make a BAKE button on a toolbar that collapses the pos, rot and scale of the selected objects and automatically sets the sample range to the current active time time range.

 

Thanks for your time!

0 Likes
Accepted solutions (1)
2,204 Views
12 Replies
Replies (12)
Message 2 of 13

istan
Advisor
Advisor
Accepted solution

Since all controllers, their calculations and the time/animation-control is scriptable - yes, this is possible. Btw, also possible from C++.

0 Likes
Message 3 of 13

chengshunvfx
Explorer
Explorer

Ran into this problem as well.

 

Is there a solution for this in Maxscript or Python?

 

I used a script to record the transforms of bones and set them back as controllers frame by frame. But it is much slower than the built-in Collapse Transform function when the number of bones is about 500 and the timeline is 100 frames.

 

Are there any docs on Maxscript or SDK for Collapse Transform?

0 Likes
Message 4 of 13

MartinBeh
Advisor
Advisor

Maybe this is useful?

https://scripts.breidt.net/#mbCollapse

Martin B   EESignature
→ please 'Like' posts that are helpful; if a post answers your question please click the "Accept Solution" button.
0 Likes
Message 5 of 13

denisT.MaxDoctor
Advisor
Advisor

@chengshunvfx wrote:

 

I used a script to record the transforms of bones and set them back as controllers frame by frame. But it is much slower than the built-in Collapse Transform function when the number of bones is about 500 and the timeline is 100 frames.

 


How slow is your script? Please provide numbers for 500 baked bones at a 0-100 interval.

0 Likes
Message 6 of 13

denisT.MaxDoctor
Advisor
Advisor

 

delete objects


pp = for k=1 to 500 collect (point())
with animate on at time 100 (move pp [100,0,0]; rotate pp (angleaxis 45 z_axis))

 
(
	disablerefmsgs() 
	animate on, redraw off
	(
		for p in pp do
		(
			tms = for t = animationrange.start to animationrange.end collect (at time t p.transform)  
			k = 0
			for t = animationrange.start to animationrange.end collect (at time t p.transform = tms[k += 1]) 
		)
	)
	enablerefmsgs()
	for p in pp do notifydependents p
)

 

 

This snippet demonstrates that straightforward MXS "key baking" takes about a sec for 500 nodes on the 0-100 interval. 
To do everything correctly you also must sort nodes by hierarchy before the baking.

If you need to do it even faster, you can perform all read and write transforms in one pass of the animation range time loop. This will speed things up considerably.

0 Likes
Message 7 of 13

chengshunvfx
Explorer
Explorer

The script I used is very similar to the one you mentioned.

It works correctly, but too slow.

0 Likes
Message 8 of 13

chengshunvfx
Explorer
Explorer

About 10-15 minutes in total. 

 

The scene is a complex rig. Skinning bones are constrained by Biped bones.

I want to bake the animation frames of the skinning bones and export them to Maya

0 Likes
Message 9 of 13

denisT.MaxDoctor
Advisor
Advisor

the MCR script doesn't do the job correctly.  

0 Likes
Message 10 of 13

denisT.MaxDoctor
Advisor
Advisor

@chengshunvfx wrote:

About 10-15 minutes in total. 

 

The scene is a complex rig. Skinning bones are constrained by Biped bones.

I want to bake the animation frames of the skinning bones and export them to Maya


built-in Collapse Transform doesn't collapse Biped bones!

0 Likes
Message 11 of 13

denisT.MaxDoctor
Advisor
Advisor

@chengshunvfx wrote:


I want to bake the animation frames of the skinning bones and export them to Maya


To export animation to Maya you need only FBX import/export with baking animation key-per-frame

Message 12 of 13

chengshunvfx
Explorer
Explorer

It works.

Thanks Denis

0 Likes
Message 13 of 13

in_human
Enthusiast
Enthusiast
macroScript AnimBake category:"IN_tools"
(
global bakeobjArr, baketransArr, bakenameArr, opaArr, fnumArr
----------------------------------------------------------------------------------
rollout storeAnim "Bake Animation v1.33 JSON edition"
 (
  group "Time"
   (
    spinner startT "Start: " range:[-9999999,9999999,animationRange.start] type:#integer fieldWidth:45 align:#center across:2
	spinner endT " End: " range:[-9999999,9999999,animationRange.end] type:#integer fieldWidth:45 align:#center
   )
  checkbox controlCheck "assign controllers" align:#center
  button store "Store" width:100
  button restore "Restore" width:100 enabled:false
  label nodeFind "Find node by:"
  checkButton handleButton "Handle" across:2 align:#center width:90 checked:true
  checkButton nameButton "Name"  width:90
  label stateString "ready"
  on handleButton changed state do
   (
    nameButton.state = not state
   )
  on nameButton changed state do
   (
    handleButton.state = not state
   )
  on store pressed do
   (
    bakeobjArr = #()
	bakenameArr = #()
	baketransArr = #()
	opaArr = #()
	fnumArr = #()
	--max time start
	--max time forward
	--max time back
	if $!=undefined do
	 (
      for i in selection do
	   (
 	    append bakeobjArr i.handle
	    append bakenameArr i.name
	    append baketransArr #()
		append opaArr #()
		append fnumArr #()
	   )

      for j=startT.value to endT.value do
	   (
        --max time forward
		for i=1 to selection.count do
	     (
		  --slidertime = j
		  at time (j as time) append baketransArr[i] selection[i].transform
		  --at time (j as time) append opaArr[i] selection[i].visibility.controller.value
		  --if (custattributes.count selection[i])==1 do at time (j as time) append fnumArr[i] selection[i].fnum
		  stateString.text = j as string
	     )
	   ) 
	  stateString.text = "stored"
	  restore.enabled = true
     )
   )
  on restore pressed do undo "Bake animation" on
   (
	for i=1 to bakeobjArr.count do
	 (
	  if handleButton.state then curObj = maxOps.getNodeByHandle bakeobjArr[i]
	                        else curObj = getNodeByName bakenameArr[i]
	  if (curObj != undefined) and controlCheck.state do
	   (
	    curObj.controller = transform_script()
		curObj.controller = prs()
	    curObj.position.controller = linear_position()
	    curObj.rotation.controller = tcb_rotation()
	    curObj.scale.controller = linear_scale()
	   )
	 )
    --slidertime = startT.value
	for j=startT.value to (baketransArr[1].count + startT.value - 1) do
	 (
	  for i=1 to bakeobjArr.count do
	  with animate on
	   (
	    if handleButton.state then curObj = maxOps.getNodeByHandle bakeobjArr[i]
	                          else curObj = getNodeByName bakenameArr[i]
		if curObj != undefined do
			(
				at time j curObj.transform = baketransArr[i][j + 1 - startT.value]
				--animate on curObj.visibility.controller.value = opaArr[i][j]
				--if ((custAttributes.count curobj)==1) do animate on curObj.fnum = fnumArr[i][j]
			)
		--format "%\n" ((startT.value+j-1) as time)
	    stateString.text = j as string
	   )
	  --slidertime += 1
	 )
	stateString.text = "stored"
   )
 )
---------------------------------------------------------------------
createDialog storeAnim width:280
)
0 Likes