Notifications
Clear all

[Closed] Now load data from text file with Maxscript

Hi, i’m new in this forum and i need help for a simple script. I use 3dstudio good but i never utilized maxscipt.
I need a scipt that load many Box from a text file like this:

 +20.6378326  +26.4747047  +17.2715569   2   2   2   2  36   2  -1  -1   2   2  30  37   2  -1  -1   1  -1   -3.1699963   +1.9398565  -11.1652737  +35.3927727  +30.2766495  +68.8456573 
   +11.0808983  +16.9177704   +7.7146225   2   2   2  -1  -1   2  -1  -1   2   2  -1  -1   2  -1  -1   2   0   -3.1699963   +2.7963781  -11.1652737  +22.2553501  +30.2766495  +25.3791275 
   +20.6378326  +26.4747047  -20.9561806   2   2   2   4  26   2  27  -1   2   2  28  29   2  -1  -1   1  -1   -5.8604488   +1.9398489  -50.6473846  +27.7138195  +57.4468918   +2.2601328 
   +11.0808983  +16.9177704  -30.5131149   2   2   2  -1  -1   2  -1  -1   2   2  -1  -1   2  -1  -1   2   1   -2.8320866   +2.3425775  -49.0498123  +21.4371834  +28.8870106  -13.9253283 
   -17.5899048  +26.4747047  +17.2715569   2   2   2  -1  34   2  -1  -1   2   2   6  35   2  -1  -1   1  -1  -33.1839447   -0.2257872   -7.5961504   +4.7825007  +25.1738129  +69.5140076 
    -8.0329704  +16.9177704   +7.7146225   2   2   2  -1  -1   2  -1  -1   2   2  -1  -1   2  -1  -1   2   2   -9.2442427   +1.9398508   -7.5961504   +4.7825007  +17.2004242  +17.7136402 

The numbers 1°-2°-3° are the coordinates XYZ of center of box.
The numbers 18°-19°-20° are the coordinates XYZ of maximum vertices
The numbers 21°-22°-23° are the coordinates XYZ of minimum vertices
The other numbers aren’t necessary
The center is the Pivot position, not the center of object.

Can anyone help me

5 Replies

The “+” characters in the data add a bit of a wrinkle and I’m not sure if I’ve interpreted what you’re doing with the pivot correctly, but this should get you going.


   fn fixPlus str =
   (
   	if str[1] == "+" then str = replace str 1 1 ""
   	str
   )
   
   fn toPoint3 arr startIndex =
   (
   	local p3 = [0, 0, 0]
   	p3.x = fixPlus arr[startIndex] as float
   	p3.y = fixPlus arr[startIndex+1] as float
   	p3.z = fixPlus arr[startIndex+2] as float
   	p3
   )
   
   local f = openFile "C:\	emp\\boxes.txt" mode:"r"
   
   while not EOF f do
   (
   	local thisLine = filterString (readLine f) " "
   	local newPivot = toPoint3 thisLine 1
   	local newMax = toPoint3 thisLine 21
   	local newMin = toPoint3 thisLine 24
   	local b = box()
   	b.pivot = b.center
   	b.pos = (newMax + newMin) / 2.0
   	b.width = abs (newMax.x - newMin.x)
   	b.length = abs (newMax.y - newMin.y)
   	b.height = abs (newMax.z - newMin.z)
   	b.pivot = newPivot
   )
   
   close f
   

Cheers,

Drea

VERY THANKS!

Ops, I have a little problem:
the gameX = 3ds X
the gameZ = 3ds Y
the gameY = 3ds Z

for exemple i look this data:

+20.6378326  +26.4747047  +17.2715569   2   2   2   2  36   2  -1  -1   2   2  30  37   2  -1  -1   1  -1   -3.1699963   +1.9398565  -11.1652737  +35.3927727  +30.2766495  +68.8456573 

in 3dstudio i create a Spline with 2 vertices:
1 vertices: x=-3.169 z= 1.93 y= -11.165
2 vertices x= 35.392 z= 30.276 y= 68.845
it’s diagonal of box1
i utilized this modified script for (max9):

  fn fixPlus str =
     (
  	   if str[1] == "+" then str = replace str 1 1 ""
  	   str
     )
     
     fn toPoint3 arr startIndex =
     (
  	   local p3 = [0, 0, 0]
  	   p3.x = fixPlus arr[startIndex] as float
  	   p3.y = fixPlus arr[startIndex+1] as float
  	   p3.z = fixPlus arr[startIndex+2] as float
  	   p3
     )
     f = openfile ("ascii.txt")
  	if f == undefined do
  	(
  	local fname
  	MessageBox "Could not find ascii.dat. Please point locate the file"
  	fname = getOpenFileName caption:"Locate ascii.txt" filename:"ascii.txt"
  	if fname != undefined then 
  		f = openFile fname
  	)
  
  	if f != undefined then   
     while not EOF f do
     (
  	   local thisLine = filterString (readLine f) " "
  	   local newPivot = toPoint3 thisLine 1
  	   local newMax = toPoint3 thisLine 21
  	   local newMin = toPoint3 thisLine 24
  	   local b = box()
  	   b.pos.x = (newMin.x - newMax.x) / 2.0
  	b.pos.y = (newMax.z - newMax.z) / 2.0
  	b.pos.z = (newMin.y - newMax.y) / 2.0
  	   b.width = abs (newMax.x - newMin.x)
  	   b.length = abs (newMax.z - newMin.z)
  	   b.height = abs (newMax.y - newMin.y)
  	b.pivot = b.center
  	   b.pivot = newPivot
     )
     close f
  

but the pivot don’t mach with x=20.637 z=26.474 y=17.271 ( i think it’s the center of box but the (b.center.x , b.center.y(z) , b.center.z(y)
and the box it’s in the correct position like image:

Can you help me ?, I think it’s symple for you but i never utilized maxscript (only C++)

There’s a couple of reasons it’s not working for you.

 First, in your position calculation, you're subtracting the maximum value from the minimum to get the average. You should be adding them instead. Also, for the pos.y calculation you have (newMax - newMax) / 2.0. That will always return 0.0.
 
 Secondly, You're setting the position of the box before you move the pivot to the centre of the box. When Max creates a box, it places the pivot point in the middle of the bottom face rather than in the centre of the box. As the pivot defines the position of an object, it needs to be centred to the box before the position is set.
 
 Here's a version that does what you're after.
fn fixPlus str =
     (
     	if str[1] == "+" then str = replace str 1 1 ""
     	str
     )
     
     fn toPoint3 arr startIndex =
     (
     	local p3 = [0, 0, 0]
     	p3.x = fixPlus arr[startIndex] as float
     	-- Better to convert from left to right-handed here
     	p3.y = fixPlus arr[startIndex+2] as float
     	p3.z = fixPlus arr[startIndex+1] as float
     	p3
     )
     
     local f = openfile "ascii.txt" mode:"r"
    
     if f == undefined do
     (
     	-- local fname -- Don't need to pre-define variables in maxscript
     	MessageBox "Could not find ascii.dat. Please point locate the file"
     	local fname = getOpenFileName caption:"Locate ascii.txt" filename:"ascii.txt"
     	if fname != undefined then
     	(
     		f = openFile fname mode:"r"
     	)
     )
     
     if f != undefined then
     (
     	while not EOF f do
     	(
     		local thisLine = filterString (readLine f) " "
     		local newPivot = toPoint3 thisLine 1
     		local newMin = toPoint3 thisLine 21
     		local newMax = toPoint3 thisLine 24
     		local b = box()
     		b.width = abs (newMax.x - newMin.x)
     		b.length = abs (newMax.y - newMin.y)
     		b.height = abs (newMax.z - newMin.z)
     		b.pivot = b.center
     		b.pos = (newMax + newMin) / 2.0
     		b.pivot = newPivot
     	)
     	
     	close f
     )

Cheers,

Drea

IT WORK CORRECTLY !!!
but your script make this error:

so i changed with:

fn fixPlus str =
  	 (
  		 if str[1] == "+" then str = replace str 1 1 ""
  		 str
  	 )
  	 
  	 fn toPoint3 arr startIndex =
  	 (
  		 local p3 = [0, 0, 0]
  		 p3.x = fixPlus arr[startIndex] as float
  		 -- Better to convert from left to right-handed here
  		 p3.y = fixPlus arr[startIndex+2] as float
  		 p3.z = fixPlus arr[startIndex+1] as float
  		 p3
  	 )
  	
  
  	 f = openfile "ascii.txt" mode:"r"
  	
  	 if f == undefined do
  	 (
  		 -- local fname -- Don't need to pre-define variables in maxscript
  		 MessageBox "Could not find ascii.dat. Please point locate the file"
  		 local fname = getOpenFileName caption:"Locate ascii.txt" filename:"ascii.txt"
  		 if fname != undefined then
  		 (
  			 f = openFile fname mode:"r"
  		 )
  	 )
  	 
  	 if f != undefined then
  	 (
  		 while not EOF f do
  		 (
  			 local thisLine = filterString (readLine f) " "
  			 local newPivot = toPoint3 thisLine 1
  			 local newMin = toPoint3 thisLine 21
  			 local newMax = toPoint3 thisLine 24
  			 local b = box()
  			 b.width = abs (newMax.x - newMin.x)
  			 b.length = abs (newMax.y - newMin.y)
  			 b.height = abs (newMax.z - newMin.z)
  			 b.pivot = b.center
  			 b.pos = (newMax + newMin) / 2.0
  			 b.pivot = newPivot
  		 )
  		 
  		 close f
  	 )