Export X Z Y position of objects to CSV


#1

Hello!

I’m trying to export 10,000 + object positions to a CSV file so our devs can import in this “point cloud” to a real time app.

Anyway, I found this script - Exporting objects position to text file and I’ve attempted to modify it to -


  -------------------------------------------
  -- ExportAsCVS.ms
  --  exports the currently selected models
  --  height, width, length (in that order)
  --
  --  Version: 0.1
  --  Tested using 3ds max v8.0
  --
  -------------------------------------------
  
  global theFileStream
  
  -------------------------------------------
  -- START
  -------------------------------------------
  
  fn exportAs fileName = (
  	-- create file
  	theFileStream = createFile fileName;
  
  	if theFileStream != undefined then (
  		-- file ok to write
  		selection = getCurrentSelection()
  
  		if selection.count > 0 then (
  			-- write header
  			printToFile ("x, y, z, Pieces, Name")
  			-- write data
  			for obj in selection do (
  				 _name = obj.name
  				 _xpos = obj.pos.x as string
  				 _ypos = obj.pos.y as string
  				 _zpos = obj.pos.z as string
 				 -- assuming pieces is always 1
 				 pieces = "1"
  				 -- write to file
  				 printToFile (_xpos+","+_ypos","+_zpos","+_pieces","+_name)
  			)
  		) else (
  			-- error
  			printToFile ("Error: Nothing Selected!")
  		)
  	)
  
  	-- close
  	close theFileStream;
  )
  
  fn printToFile theString t:0 = (
  	-- auto tab (empty string)
  	tabString = "												   ";
  	tabString = substring tabString 1 (t*4);
  	-- print
  	format "%%
" tabString theString to:theFileStream;
  )
  
  fn floorToNDecimals theNumber N:2 = (
  	return floor( ((pow 10 N) * theNumber) ) / (pow 10 N)
  )
  
  -------------------------------------------
  -- RUN
  -------------------------------------------
  
  exportAs("2.csv");
  
  -------------------------------------------
  -- END

However only the header is included in the CSV. The original file also doesn’t fully work, when I first ran the script it would only add the first object selected to the CSV.
However when I run the script now, the Maxscript window highlights in grey the line “theFileStream = createFile fileName;” and doesn’t actually give an error.

I’m guessing the code is 15 years old, so that’s probably why it’s not running, but can anyone shine a light on this and point me in the right direction please?

Thanks,
Dean


#2

i think you need to move printToFile to above exportAs in the file as the function would need to be “seen” before it is called


#3

thanks for the reply.

even if i re-order that function, it still doesn’t work. the CSV only shows the header.

Capture

any ideas?


#4
(
	fn floorToNDecimals theNumber N:2 = ( floor( ((pow 10 N) * theNumber) ) / (pow 10 N) )
	ss = stringstream ""
	format "x, y, z, Pieces, Name\n" to:ss

	for obj in selection do
	(
		format "%,%,%,1,%\n" (floorToNDecimals obj.pos.x) (floorToNDecimals obj.pos.y) (floorToNDecimals obj.pos.z) obj.Name to:ss
	)

	(dotnetclass "system.io.file").writeAllText path_to_csv (ss as string)
)
)

#5

Thanks for your reply!

However I’m a bit lost as to where this should fit in the script, and what is replaces exactly.

Any chance you could elaborate please?

Thanks!


#6

just replace path_to_csv with your path to csv like “C:\temp.csv” and run it
it will save positions and names of selected objects


#7

Thanks! I have it working.

Is it possible for the export data to include the decimal places? I think it’s just rounding to whole number currently.


#8

I’ve edited the script above now it should work.


#9

Amazing! Thank you so much!!