[Closed] Precise position export with MaxScript
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?
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
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?
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
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
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
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
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
Sorry for bumping old topic, but
recently I got a chance to actually use this script in my projects, and I stumbled across weird rotation export from 3ds Max. I thought it export values in radians, but apparently it’s not.
So for example, rotation value of 30 degrees exported as 0.25881904, but rotation value of 60 degress will be exported as 0.5, while 120 degrees will be exported as 0.86602545. And this behavior actually rendered whole script useless – even if positions are correcly exported, values of rotations are messed up all across the output.
It will be more useful for me, if script just output degrees values as numbers (30, 60, 120, etc degrees) and then later I manually divide each value to 57,2958. How can I modify script to output degrees as numbers or at least in radians?
Edit: using this script I was able to make a modification, so now script outputs positions as hex and rotations in radians. However, I cannot no more output rotation value as hex…
outputFile = createFile "c:\output.txt"
for obj in $ do
(
rot = quatToEuler obj.rotation
xrot = degToRad rot.x
yrot = degToRad rot.y
zrot = degToRad rot.z
format "% X_pos:% Y_pos:% Z_pos:% X_rot:% Y_rot:% Z_rot:%
" obj.name (formattedPrint (bit.floatAsInt obj.pos.x) format:"#X") (formattedPrint (bit.floatAsInt obj.pos.y) format:"#X") (formattedPrint (bit.floatAsInt obj.pos.z) format:"#X") xrot yrot zrot to:outputFile
)
close outputFile
if I try to use this (formattedPrint (bit.floatAsInt yrot) format:"#X")
it only outputs number, not hex value.