Precise position export with MaxScript


#1

Hello, I wrote a script to export coordinates of objects to text file:

outputFile = createFile "c:\output.txt"

for obj in $ do
(
format "% X:% Y:% Z:%
" obj.name obj.pos.x obj.pos.y obj.pos.z to:outputFile
)

close outputFile

However it export coordinates not precisely, for example an object with coordinates X:-20531.262 gets exported as X:-20531.2
How to edit script to make it export exast position number as shown in 3ds max?


#2

max use single number to save data , it’s only 5 number are faithful ,that’s why you get the number longer than 5 will be limited
if you really need the incredible part , you can get by mod obj.pos.x 1
that 's just an approximate value , and there 's no accuracy value


#3

thanks for suggestion! I edited script to be like this:

outputFile = createFile "c:\output.txt"

for obj in $ do
(
format "% X_pos:% Y_pos:% Z_pos:%
" obj.name (mod obj.pos.x 1) (mod obj.pos.y 1) (mod obj.pos.z 1) to:outputFile
)

close outputFile

However with this output now omits values, so instead of X:-20531.262 I am getting X:-0.262, any way to fix this?


#4

use formattedPrint


#5

I always thought formattedPrint print any where ,as it won’t print in loop

formattedPrint obj.pos format:"1.11g"
it’s maybe your need, mod was to get the fraction part


#6

Thanks guys, I edited script to be like this:

outputFile = createFile "c:\output.txt"

for obj in $ do
(
format "% X_pos:% Y_pos:% Z_pos:%
" obj.name (formattedPrint obj.pos.x format:"1.11g") (formattedPrint obj.pos.y format:"1.11g") (formattedPrint obj.pos.z format:"1.11g") to:outputFile
)

close outputFile

and now it does its magic


#7

formattedPrint too many times may slower the script , use filterstring to split a complete point3 value could reduce your calculative time
filterstring (formattedPrint obj.pos format:"1.11g") "[]," from it you will get all values , while use twice the time


#8

Thanks, this is really helpful

I have one more question though (this one might warrant separate topic, but I will just write it here; might open new one if its against rules or needed)

Basically, I need to have those values represented as hex values to be able to patch binary file with them later. I tried to use Microsoft Excel to convert floats to hex, but format is not the one I need, and none of available VBA scirpts seems to do the job. Since formattedPrint seems to be able to export values in hex, how can I do this?

This calculator provides format suitable for work:

what I tried in MaxScript is this command:
(formattedPrint obj.pos.x format:"#X")
But for seemingly no reason it does not output hex properly and just writes weird values like “X_pos:014000000000” in output.txt


#9

maxscript has bit struct with useful methods
bit.floatAsInt <float>
bit.intAsHex <integer>
more info you can find in the mxs reference

( dotnetobject "system.single" my_float_val ).ToString "X8" should work as well