PDA

View Full Version : Exporting objects position to text file


Andrey777
03-01-2006, 12:01 PM
Hello.
I've been trying to write a simple script that will print (to a text file or at least listener) positions (world coordinates) of selected object. So far i have not succeded (not even close). Can anybody help with that or at least point in a right direction.

Thanks in advance for help
Best Regards
Andrew Skibinski

handiklap
03-01-2006, 12:59 PM
Try this:

-----code-----

outputFile = createFile "output.txt"

for obj in $ do
(
print obj.pos to:outputFile
)

close outputFile

-----/code-----


God the code tag sucks. I can't figure out how to make text a consistent size (see, look doesn't my edit look nice?)...anyway that will create "output.txt" in your max root. If you wanted to, for example, format it like:

objectName,objectPosition

you would modify the loop content like this:

print (obj.name + "," + obj.pos) to:outputFile

Andrey777
03-01-2006, 01:52 PM
Thanks for the code, I just knew it must be really simple
I came up with something like this:

------CODE------

outputFile = createFile "c:\\output.txt"

for obj in $ do

(
posx = (obj.pos.x as string)
posy = (obj.pos.y as string)
posz = (obj.pos.z as string)

string = obj.name + " : X:" + posx + " Y:" + posy + " Z:" + posz
print string to:outputFile



)

close outputFile
------CODE------

It makes the whole file more clear and readable (also makes it easier to copy&paste position values).
However it adds " at the beggining and at the end of the each line, like this:
"SmallGun_Base_7 : X:78.6413 Y:-450.398 Z:-113.457"
"SmallGun_Muzzle_7 : X:78.6413 Y:-450.398 Z:-113.457"

Is it possible to get rid of those " " ?

handiklap
03-01-2006, 02:19 PM
ok figured out you should be using "format" instead of just "print" like:


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


the "%" formats the arguments following the input string. each consecutive use of "%" uses each consecutive argument respectively, and the "\n" is an escape character that acts like a carriage return...oh god i just used the term carriage return (from the days of typewriters)...it's a new line fyi

Andrey777
03-01-2006, 02:53 PM
Thanks for help :)

j-man
03-06-2006, 11:21 AM
Hi,

It occured to me that maybe you should be making a CSV file, so maybe later you could write a file to import these values back into Max.


obj=selection as array
for o in obj do format "\n%,%,%,%" o.name o.pos.x o.pos.y o.pos.z to:outputfile


Also, notice how I careate an array with the current selection. It's bad practice to use $ inside a script since it may change during script operation.

Look at www.scriptspot.com (http://www.scriptspot.com) for import / export scripts, I'm sure thing kind of thing has been done before.

J.

joconnell
03-06-2006, 11:27 AM
Here's a basic one.

Export:

rollout ExportKeys "Export Keyframes"
(
Button SetOutFile "Set output Filename" align:#right width:150
Button ExportAnim "Export Animation" align:#right width:150

on setoutfile pressed do
(
Global OutFile = getSaveFileName caption:"Choose the output filename" filename:"h:\\Databases\\"
SetOutFile.text = OutFile
)

Fn WriteAnimData Keyarray destfile =
(
for k = 1 to Keyarray.count do
(
Format "%," Keyarray[k].time to:DestFile
Format "%," Keyarray[k].value to:DestFile
Format "%," Keyarray[k].intangenttype to:DestFile
Format "%," Keyarray[k].intangent to:DestFile
Format "%," Keyarray[k].outTangentType to:DestFile
Format "%," Keyarray[k].outTangent to:DestFile
Format "%," Keyarray[k].inTangentLength to:DestFile
Format "%," Keyarray[k].outTangentLength to:DestFile
)
)


on ExportAnim pressed do
(

MyOutFile = OutFile
AnimFile = CreateFile MyOutFile

MyObjects = Selection as array

format "%\n" MyObjects.count to:AnimFile

for i = 1 to myObjects.count do
(

XposKeys = MyObjects[i].position.x_position.controller.keys
YPosKeys = MyObjects[i].position.y_position.controller.keys
ZPosKeys = MyObjects[i].position.Z_position.controller.keys

XrotKeys = MyObjects[i].rotation.x_rotation.controller.keys
YrotKeys = MyObjects[i].rotation.y_rotation.controller.keys
ZrotKeys = MyObjects[i].rotation.z_rotation.controller.keys



-- Object Name

format "%\n" MyObjects[i].name to:AnimFile

-- Key Counts

format "%\n" XposKeys.count to:AnimFile
format "%\n" YposKeys.count to:AnimFile
format "%\n" ZposKeys.count to:AnimFile

format "%\n" XrotKeys.count to:AnimFile
format "%\n" YrotKeys.count to:AnimFile
format "%\n" ZrotKeys.count to:AnimFile

-- Output keys

WriteAnimData XposKeys AnimFile
WriteAnimData YposKeys AnimFile
WriteAnimData ZposKeys AnimFile

WriteAnimData XrotKeys AnimFile
WriteAnimData YrotKeys AnimFile
WriteAnimData ZrotKeys AnimFile

Format "\n" "" to:AnimFile

)

Close AnimFile
)
)

CreateDialog ExportKeys width:250

And the corrosponding import:

Rollout ImportExportKeys "Import / Export Keyframes"
(
Group "Import Animation"
(
Button importAnim "Import Animation" align:#right width:150
)

Group "Export Animation"
(
Button SetOutFile "Set output Filename" align:#right width:150
Button ExportAnim "Export Animation" align:#right width:150
)

Fn ReadAnimData TargetKeys KeyCount sourcefile =
(
for k = 1 to KeyCount do
(
TempKeyTime = readValue sourcefile
addNewKey TargetKeys TempKeyTime
TempKeyValue = readValue sourcefile
TargetKeys[k].value = TempKeyValue
TempInType = readValue sourcefile
TargetKeys[k].intangenttype = TempInType
TempIn = readValue sourcefile
TargetKeys[k].intangent = TempIn
TempOutType = readValue sourcefile
TargetKeys[k].Outtangenttype = TempoutType
TempOut = readValue sourcefile
TargetKeys[k].Outtangent = Tempout
TempInLength = readValue sourcefile
TargetKeys[k].inTangentLength = TempInLength
TempOutLength = readValue sourcefile
TargetKeys[k].OutTangentLength = TempOutLength
)
)

Fn WriteAnimData Keyarray destfile =
(
for k = 1 to Keyarray.count do
(
Format "%," Keyarray[k].time to:DestFile
Format "%," Keyarray[k].value to:DestFile
Format "%," Keyarray[k].intangenttype to:DestFile
Format "%," Keyarray[k].intangent to:DestFile
Format "%," Keyarray[k].outTangentType to:DestFile
Format "%," Keyarray[k].outTangent to:DestFile
Format "%," Keyarray[k].inTangentLength to:DestFile
Format "%," Keyarray[k].outTangentLength to:DestFile
)
)

on importanim pressed do
(
Global MyInFile = getOpenFileName caption:"Choose an animation file:" filename:"H://Databases//"

MyFile = OpenFile MyInFile

ObjCount = readline MyFile as integer

For i = 1 to objcount do
(
SourceObj = readline MyFile as string

TargetObj = execute ("$" + SourceObj)

TargetXCont = TargetObj.position.x_position.controller.keys
TargetYCont = TargetObj.position.y_position.controller.keys
TargetZCont = TargetObj.position.z_position.controller.keys

TargetXRotCont = TargetObj.rotation.x_rotation.controller.keys
TargetyRotCont = TargetObj.rotation.y_rotation.controller.keys
TargetzRotCont = TargetObj.rotation.z_rotation.controller.keys

XKeyCount = readline MyFile as integer
YKeyCount = readline MyFile as integer
ZKeyCount = readline MyFile as integer

XRotKeyCount = readline MyFile as integer
YRotKeyCount = readline MyFile as integer
ZRotKeyCount = readline MyFile as integer

ReadAnimData TargetXCont XKeyCount MyFile
ReadAnimData TargetYCont YKeyCount MyFile
ReadAnimData TargetZCont ZKeyCount MyFile
ReadAnimData TargetXRotCont XRotKeyCount MyFile
ReadAnimData TargetYRotCont YRotKeyCount MyFile
ReadAnimData TargetZRotCont ZRotKeyCount MyFile
)

)

on setoutfile pressed do
(
Global OutFile = getSaveFileName caption:"Choose the output filename" filename:"h:\\Databases\\"
SetOutFile.text = OutFile
)

on ExportAnim pressed do
(

MyOutFile = OutFile
AnimFile = CreateFile MyOutFile

MyObjects = Selection as array

format "%\n" MyObjects.count to:AnimFile

for i = 1 to myObjects.count do
(

XposKeys = MyObjects[i].position.x_position.controller.keys
YPosKeys = MyObjects[i].position.y_position.controller.keys
ZPosKeys = MyObjects[i].position.Z_position.controller.keys

XrotKeys = MyObjects[i].rotation.x_rotation.controller.keys
YrotKeys = MyObjects[i].rotation.y_rotation.controller.keys
ZrotKeys = MyObjects[i].rotation.z_rotation.controller.keys



-- Object Name

format "%\n" MyObjects[i].name to:AnimFile

-- Key Counts

format "%\n" XposKeys.count to:AnimFile
format "%\n" YposKeys.count to:AnimFile
format "%\n" ZposKeys.count to:AnimFile

format "%\n" XrotKeys.count to:AnimFile
format "%\n" YrotKeys.count to:AnimFile
format "%\n" ZrotKeys.count to:AnimFile

-- Output keys

WriteAnimData XposKeys AnimFile
WriteAnimData YposKeys AnimFile
WriteAnimData ZposKeys AnimFile

WriteAnimData XrotKeys AnimFile
WriteAnimData YrotKeys AnimFile
WriteAnimData ZrotKeys AnimFile

Format "\n" "" to:AnimFile

)

Close AnimFile
)

)

CreateDialog ImportExportKeys Width:250

It's only working on simple things right now - no list controllers etc but it might be of help.

CGTalk Moderation
03-06-2006, 11:27 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.