Community
3ds Max Programming
Welcome to Autodesk’s 3ds Max Forums. Share your knowledge, ask questions, and explore popular 3ds Max SDK, Maxscript and Python topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Need Maxscript help reading values from a csv

8 REPLIES 8
Reply
Message 1 of 9
jpblower
3848 Views, 8 Replies

Need Maxscript help reading values from a csv

So here's my code.  I need to read in values from a *.csv and make an object move up and down while rotating.  Essentially I'm trying to have a box bob up and down on a sinusoildal wave created from another program. 

 

(
 AssyFile = openfile "C:\Documents\3dsMaxDesign\Scripts\\MotionTest.csv"
 set animate on 
 if (AssyFile != undefined) then
 (
  skiptonextline AssyFile

  select $Box001
  while not eof AssyFile do
  (
   TransformName = readdelimitedstring AssyFile ","'
   XRot = readValue AssyFile
   ZTrans = readValue AssyFile    
   sliderTime = 0
   max set key keys 
   sliderTime = sliderTime + 15
   move $ [0,0,ZTrans]
   rotate $ (angleaxis XRot [1,0,0])
  ) 
 )
 else
  (
   messageBox "Open Failed" title:"Error!"
  )
)

 

My spread sheet reads like this

TransformNameXRotZTrans
1101
2101
3101
4101
5101
600
7-10-1
8-10-1
9-10-1
10-10-1
11-10-1
1200

 

I get this error

 

--Syntax error: at end of script, expected end of literal

--In line:   )

 

I've no idea what this means or what's wrong.  Any help?

8 REPLIES 8
Message 2 of 9
Steve_Curley
in reply to: jpblower

Well, if that is the actual csv file then there are no commas in it, yet you're
readdelimitedstring AssyFile ","'
which has 2 issues. The trailing single quote, and the fact that you're asking it to look for a comma when there aren't any. Also, as that first field is a number you don't need readDelimited anyway, just read a temporary variable as you've done with XRot and ZTrans.
You don't need to select the Box in order to set keys on it. Neither do you need "max set keys" - just move it and rotate it.
You're resetting SliderTime to 0, then adding 15 to it every time around the loop. Result - all the keys will overwrite each other on frame 15 resulting in just one movement.

Max Design has different default settings for animation. Customise > Preferences > Animation (tab) > Auto Key Default Frame (section) > On and 0 for this to work as expected.

And a tip for pathnames - be consistent, and use single forward slashes (a la unix/linux) rather the one (or 2) backslashes. Those will bite you eventually - the forward slashes just work.

(
local st = 1
local theBox = $Box001
AssyFile = openfile "C:/Documents/3dsMaxDesign/Scripts/MotionTest.csv"
if (AssyFile != undefined) then
	(
	skiptonextline AssyFile
	with animate on
		(
		while not eof AssyFile do
			(
			TransformName = readvalue AssyFile
			XRot = readValue AssyFile
			ZTrans = readValue AssyFile 
--			format "tn=% X=% Z=%\n" TransformName XRot ZTrans
			at time (st * 15) 
				(
				move theBox [0, 0, ZTrans]
				rotate theBox (angleaxis XRot [1,0,0])
				)
			st += 1
			) 
		)
	)
else
	(
	messageBox "Open Failed" title:"Error!"
	)
)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

Message 3 of 9
jpblower
in reply to: jpblower

Thanks a bunch, I'll try to get that working.  As for the csv I copied it from excel, hence the lack of commas. 

Message 4 of 9
jpblower
in reply to: Steve_Curley

First it worked great, then I couldn't get the file to read. 

 

I tried

 

AssyFile = openfile "C:\Scripts\\Motion.csv"

 

AssyFile = openfile "C:\Scripts\Motion.csv"

 

AssyFile = openfile "C:/Scripts/Motion.csv"

 

but I keep getting a read fail error.  What gives?

Message 5 of 9
jpblower
in reply to: jpblower

Sorry it was reading all 3 ways before I closed max and reopened.  Now it won't read any.

 

Message 6 of 9
Steve_Curley
in reply to: jpblower

Permissions? Does the file actually exist in that location?
The only thing I see wrong with the code (my fault) is that there's no close of the file. This can cause the number of available file handles to reduce every time you run the script. Windows does have a finite number of handles for open files - maybe a reboot will fix it?
			st += 1
			) 
		)
	close AssyFile -- missing this line
	)
else

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

Message 7 of 9
jpblower
in reply to: Steve_Curley

Yeah the file is there.  I had it in several locations because I got the same error when using the path you see.  So I moved it into what I thought was the simplest location.  It read fine and when I closed the scene and reopened it suddenly couldn't find the file.  Now I'm trying it on another computer and get the same error.  No clue why it can't find the file.  I'm using windows 7 and max design 2014 if that makes a difference.

Message 8 of 9
jpblower
in reply to: Steve_Curley

I just got the file to read on a network drive.  My guess is there's something wrong w/ reading the file directly when it wants to read something like MyComputer>Documents etc.  I'm not sure what to input to fix this but right now it's working if I use a network drive.  However when I try to work at home I'm lost as to what to do.

Message 9 of 9
Steve_Curley
in reply to: jpblower

Is it actually failing - giving you the "Open Failed" messagebox? I've tried it repeatedly here:-
AssyFile = openfile "C:/Documents and settings/Administrator/My Documents/3dsmax/MotionTest.csv"
Load a test scene, run the script, load the scene, run the script... no file errors of any kind. Max/Max Design shouldn't make a difference, W7 vs XP might, though as I don't have a W7 installation yet I can't test that.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report