MaxScript: Reading Custom Info from a File


#1

Hi guys! Another simple doubt for you, monsters of CG.
:beer:

I just want to read some lines from a simple text file, where every line contains a position and rotation data to apply to an previously selected object(like a BOX1, for example)

the lines represent every frame and will be like(position(X Y Z) rotation(X Y Z):

0.0 0.0 0.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0 5.0 0.0
0.0 2.0 0.0 0.0 10.0 0.0
.
.
.
go on!

so, the situation is: the objet that will receive this info (every line is a single frame) will go up rotation along Y axis.

please help!

Bobo, where are you?!?!
:blush:

thank you, thank you and thank you
:bowdown:


#2

Here is a basic script I made for you. just call it, and use the object you want animated as a parameter. It will just start at frame 0, and keyframe every frame with the info from your file.

As long as your file is set up pos x, y, z, rotation x, y, z, it should work for you. Try it out.

And by the way: this should really have been in the maxscript forum.

Edit:made a mistake in setting the rotation. Fixed now.


#3

Thank you Flipped. I’m almost getting there. Anyway, I really didnt realize what modification you did in the script about the rotation thing.

Now, i am stuck. I cant associate the file values in the rotation!

heres my snippet:


		with animate on (
			try (
				while not eof f2 AND not errorState do (
					--Based on 6 items per line...will give screwy results if that is not the case
					inputData = readLine f2 
					messagebox inputData
					inputArray = filterString inputData "|"

					-- Position
					messagebox inputArray[1]
					at time frameNumber targetObj.pos = (execute inputArray[1])
					--Rotation
					messagebox inputArray[2]
					at time frameNumber targetObj.rotation = (execute inputArray[2])

					frameNumber = frameNumber + 1
				)
			) catch ()
		)


and here is the data:
[17.04,8.68,1.17]|[0.61,0.73,0.22]
[17.19,8.76,1.04]|[0.63,0.71,0.22]
[17.34,8.84,0.90]|[0.64,0.70,0.22]
[17.49,8.92,0.77]|[0.65,0.68,0.23]
[17.63,9.00,0.63]|[0.67,0.66,0.23]
[17.78,9.08,0.49]|[0.68,0.65,0.23]
[17.93,9.17,0.35]|[0.69,0.63,0.23]
[18.08,9.25,0.21]|[0.70,0.61,0.24]
[18.22,9.33,0.06]|[0.72,0.59,0.24]

every thing reads fine, but when the script try to associate the rotation value, just stop!

where did all go wrong?
:surprised


#4

Not sure what went wrong with mine, or what the format of your file rotations are, but try this instead then:

--Set the animation keyframes of an object from a text file
--If the object has keyframes already outside the range the file has, they will be kept
fn setAnimToObject object = (
	disableSceneRedraw()
	errorState = false
	undo "Animate From File" on (
	
		--Get the file with the animation sequence...try to contain errors
		f = getOpenFileName types:"Text(*.txt)|*.txt|All files|*.*|"
		print f
		if f == undefined do (print "No File Chosen"; errorState = true)
		f2 = openFile f mode:"rt"
		if f2 == undefined do (print "File Not Found"; errorState = true)
		--Use info from file to animate object. Starts on frame 0
		frameNumber = 0;
		with animate on (
			try (
				while not eof f2 AND not errorState do (
					--Based on 6 items per line...will give screwy results if that is not the case
					inputData = readLine f2 
					--messagebox inputData
					inputArray = filterString inputData "|"

					-- Position
					--messagebox inputArray[1]
					at time frameNumber object.pos = (execute inputArray[1])
					--Rotation
					--messagebox inputArray[2]
					temp = execute (inputArray[2] )
					at time frameNumber object.rotation = (eulerAngles temp.x temp.y temp.z) as quat
					frameNumber = frameNumber + 1
				)
			) catch ()
		)
			
		)
	enableSceneRedraw()
)	
	
	

Have fun


#5

Gelero -
In your initial post you wrote that the values in the text file would be like:

0.0 0.0 0.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0 5.0 0.0
0.0 2.0 0.0 0.0 10.0 0.0



But Flipped_Normal’s script appears to be looking for pipe-delimited, bracketed x y z sequences ( [x1,y1,z1]|[x2,y2,z2] ). Since you seem to have had some degree of success with his script, I have assumed that this is the actual data format you’re using.

Here is my rudimentary attempt at this script: AnimateFromFile.mzp. Simply drag the .mzp file into a Max viewport or run from the Maxscript menu to install. Once installed, a new command will be available in the “RHTools” category; you must assign this command to a button, quad, menu, or keyboard shortcut to use it.

Please note that there is currently no error-checking in this script. If the text file is in any format that differs from what is expected the script will crash gracelessly;)

Let me know how it works out:thumbsup:

RH


#6

Hey LF, nice to see you here. Always nice to see one of the scripting gurus :bowdown:

The problems I have with the script are not that it doesn’t work, but I am not sure how the rotation was saved. It is not just the angle (always really low numbers), and multiplying by 100 does not look quite right to me. I will have to try yours, and hope it does the trick.

And BTW, the style of input was changed in the third post. You can use that for test data I guess. It is a piece from the whole sequence (which is like 1300 frames long!)


#7

I was going to leave this one to you, F_N, but Gelero sent a PM and invited me to poke into it. Your script seemed fine; the only thing I noticed was that you were setting position before rotation - you’ll usually want to do it the other way around. I went a slightly different route by using the input values to construct a transform matrix, then assigning it to the .transform of the object. I noticed the small rotation values and figured that they might be radians, but I gave the option of whether to interpret them as degrees or as radians (it was this ambiguity that led to the decision to create a UI)

RH


#8

Ahh, radians! I should have thought of that, looking at the numbers :rolleyes:

Just a question. Why would setting the position before the rotation make a difference? I am fairly new to MAXScript too, so little details like that still escape me.

And feel free to poke around. The more hands the better :thumbsup:


#9

I’m actually not sure why, technically, rotation should come first. I just know that when you set the rotation of an object in Maxscript its position often changes as well (maybe the rotation occurs around some world axis, rather than the local pivot?). Setting the position after the rotation ensures that the object will be always end up where it is intended. I suppose it doesn’t matter in your script, really, because of the way that keyframes are being created. I still like to follow this rule of thumb, though, as a matter of style and consistency. Your script, your call;)

RH


#10

Thanks very much! You are right that it doesn’t matter in this script, but it is always good to know things like that. Someday I will know everything. Someday
:smiley:


#11

Oh, and by the way, I tried installing your script, but it gave me a ‘cannot open file’ error, with the path to the stdscripts folder (no file on the end, just folder). Not sure why, but it doesn’t work for me.


#12

I threw that installer together hastily based on a version from one of my other scripts. I really didn’t do any testing:blush:

I’ve now updated the .mzp with a simplified installer that should work properly. Thanks for catching that!

RH


#13

Hey guys!!!
Eric, thanks for stick around suppporting me and learning with me.
:beer:
LF, really thank you for answer my request. Really really! :buttrock:

Well. Yesterday, I come up with some tests. I have noticed the error on your script, but just fixed and I can run it.

The result is ALMOST what is expect. Let me put the thing together here.

This data(3rd post) is actually generated by the neoengine, using his physics simulation. I just drop a cube in the floor, and he bounces around. Based on several parameters, the result animation can be very different.

Anyway, sometimes the cube just SPINS in the air after bounce on the floor. Getting the rotation info of the cube(which is a quaternion varable) I have noticed the little values. And from my little point of view, means nothing to me. :blush:

And come on, if a cube spins in the air, every axis value would EXPLODES! Then I really can’t understand the magic behind quaternions and radians, right now
 :blush:

Sooooo, i changed the engine to export the absolute rotation value for every axis(degrees).

If the cube spins completely on the X, so the value is up to 360. But i got stuck in some C++ converting routine.
Anyway, that is another history.

I guess I am gettting there!

I will post the results!
:smiley:


#14

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.