Notifications
Clear all

[Closed] Exporting vertex data from Cloth modifier

I would like to export cloth animation data from the cloth modifier to file, I need to extract/export the vertex data for each keyframe in order to write a vertex shader to animate cloth in a game engine.

Specifically, I guess I need to know the structure of the arrays under the cloth modifier in the object structure. The Max chm-style documentation is not detailed enough on this. Any tips anyone?

  • thanks
2 Replies

If I’ve understood correctly, what you want is export the geometry data for each frame so later, in a vertex shader, you can do vertex tweening to reproduce the cloth animation.

In MAXScript you could do something like this:

fn exportCloth obj file = (

	local msh = snapshotAsMesh obj

	format "numVerts %
" msh.numVerts to: file
	format "numTVerts %
" (getNumTVerts msh) to: file
	format "numFaces %

" msh.numFaces to: file

	-- Write face data
	for curFace = 1 to msh.numFaces do (
		local face = getFace msh curFace
		format "face % ( % % % )
" curFace (face.x as Integer) (face.y as Integer) (face.z as Integer) to: file
	)

	format "
" to: file

	-- Write texture face data
	for curTVFace = 1 to msh.numFaces do (
		local tvFace = getTVFace msh curTVFace
		format "tvface % ( % % % )
" curTVFace (tvFace.x as Integer) (tvFace.y as Integer) (tvFace.z as Integer) to: file
	)
	
	format "
" to: file
	
	-- Write texture coordinates data
	for curTVert = 1 to (getNumTVerts msh) do (
		local tVert = getTVert msh curTVert
		format "tvert % ( % % )
" curTVert (tVert.x as Integer) (tVert.y as Integer) to: file
	)
	
	delete msh

	-- For each frame...
	for curFrame = animationRange.start to animationRange.end do (

		format "
frame %
" curFrame to: file
		at time curFrame (
			local msh = snapshotAsMesh obj
			
			-- Write vertex data
			for curVert = 1 to msh.numVerts do (
				local vertex = getVert msh curVert
				format "	vert % ( % % % )
" curVert vertex.x vertex.y vertex.z to: file
			)
			
			delete msh
		)
	)
)

fileName = getSaveFileName caption:"Save cloth data" types:"Cloth data (*.cloth)|*.cloth"
if fileName != undefined do (
	local file = createFile fileName
	if file != undefined do	(
		exportCloth $ file
		close file
	)
)

Keep in mind that this script export the raw geometry data. You’ll have to reprocess it in order to use it in Direct3D/OpenGL.

Hope that helps.

Thanks a lot, HalfVector, this looks like just what I need I’ll try it out and report back how it works. I guess I’ll post the shader code when its done too, might be useful to someone else…

  • A