[Closed] Loop trough all objest and print transformation
Hello,
I'm trying to get into MaxScript and as always, the beginning is the hardest. I want to create a script that loops through all objects in a scene and simply prints out information about their transoformation in the console window.
Now the printing is easy. I guess I would simply use the pos, rotation and scale properties
print Node.pos.x
print Node.pos.y
print Node.pos.z
print Node.rotation.x_rotation
print Node.rotation.y_rotation
print Node.rotation.z_rotation
print Node.scale.x
print Node.scale.y
print Node.scale.z
Right?
But how do I loop trough ALL of the objects in a scene? I know this is a noob question but I had a hard time finding an answer and it seems to me like something a pro could answer in just a few seconds. So please help. :D
if you select all the objects into an array first, you can easily loop through them using a script like this:
--select all objects
objs = getcurrentselection()
for i in objs do
(
print i.pos
...
)
hope thats helpful?
Yes! It is, thank you!
Searching some more about getcurrentselection() i found this:
allObjects = objects as array
for i = 1 to allObjects.count do
(
print allObjects[i].pos.x
...
)
This is a bit convoluted.
If you are not changing the objects while looping through them (in other words, not creating new objects or changing the parenting), you can use the ‘objects’ keyword to access them all directly.
for o in objects do
(
format "Object %
" o.name --output the object name
format "Position.X = %
" o.pos.x --output the position...
format "Position.Y = %
" o.pos.y
format "Position.Z = %
" o.pos.z
--and so on
)
Ah! Going trough the array was a bit easier to understand at first. But now I get how the less convoluted way works!
By the way, here is my final code. It goes through all the objects in the scene and outputs transoformation data into a custom XML file I can then read with a different program, for example Flash.
output_name = getSaveFileName caption:"XML File" types:"XML (*.xml)|*.xml|All Files (*.*)|*.*|"
if output_name != undefined then
(
output_file = createfile output_name
format "<?xml version=\"1.0\"?>
" to:output_file
format "<synths>
" to:output_file
for o in objects do
(
format "<synth
" to:output_file
format " x=\"%\"
" o.pos.x to:output_file
format " y=\"%\"
" o.pos.y to:output_file
format " z=\"%\"
" o.pos.z to:output_file
format " rx=\"%\"
" o.rotation.x_rotation to:output_file
format " ry=\"%\"
" o.rotation.y_rotation to:output_file
format " rz=\"%\"
" o.rotation.z_rotation to:output_file
format " shrink=\"%\"
" o.scale.x to:output_file
format " >%</synth>
" o.name to:output_file
)
format "</synths>" to:output_file
close output_file
edit output_name
)
Thank you, guys! You rock! :buttrock:
While you’re learning maxscript here’s an idea for something else to learn…
Now make it a function where you define an output file of your choice in the function call and an optional set of objects (which defaults to all objects if none is defined by the user).
Example:
ObjectArray = (Selection as Array)
XMLPath = "C:\\XMLFiles\\XMLFile.xml"
XMLObjectFunction Objects:ObjectArray Output:XMLPath --ouputs selection
or
XMLObjectFunction Output:XMLPath -- outputs all objects in scene.
This might be of help to you…
/*
Doesn't support Ca defs, material trees, key tangent types, mesh generation and much more.
*/
dotnet.loadAssembly "system.xml.dll"
xml=dotNetObject "system.xml.xmlDocument"
-- showProperties root
-- showMethods root
root=xml.CreateElement "root"
xml.AppendChild root
root.SetAttribute "release" ((maxversion())[1] as string)
root.SetAttribute "API" ((maxversion())[2] as string)
root.SetAttribute "SDK" ((maxversion())[3] as string)
sceneDescription=xml.CreateElement "sceneDescription"
root.AppendChild sceneDescription
objectDescription=xml.CreateElement "objectDescription"
root.AppendChild objectDescription
clearListener()
fn formatKeys keys root=
(
for k in keys do
(
node=xml.CreateElement "key"
node.SetAttribute "time" (k.time as string)
node.SetAttribute "value" (k.value as string)
root.AppendChild node
)
)
fn recurseNumSubs sub root=
(
node=xml.CreateElement "subAnim"
node.SetAttribute "name" sub.name
node.SetAttribute "classOf" ((classOf sub) as string)
node.SetAttribute "superClassOf" ((superClassOf sub) as string)
node.SetAttribute "value" (sub.value as string)
node.SetAttribute "index" (sub.index as string)
node.SetAttribute "isAnimated" (sub.controller.isAnimated as string)
node.SetAttribute "controller" (sub.controller as string)
if sub.controller!=undefined and sub.controller.keyable then
(
formatKeys sub.controller.keys node
)
root.AppendChild node
for i = 1 to sub.numsubs do
(
recurseNumSubs sub[i] node
)
)
fn formatModifiers mod root=
(
node=xml.CreateElement "Modifier"
node.SetAttribute "name" mod.name
node.SetAttribute "classOf" ((classOf mod) as string)
node.SetAttribute "superClassOf" ((superClassOf mod) as string)
root.AppendChild node
)
fn sceneToXml obj root=
(
node=xml.CreateElement "Object"
node.SetAttribute "Name" obj.name
node.SetAttribute "wireColor" (obj.wireColor as string)
node.SetAttribute "transform" (obj.transform as string)
node.SetAttribute "classOf" ((classOf obj.baseObject) as string)
node.SetAttribute "superClassOf" ((superClassOf obj.baseObject) as string)
node.SetAttribute "material" ((obj.material) as string)
for mod in obj.modifiers do
(
formatModifiers mod node
)
for i = 1 to obj.numsubs do
(
recurseNumSubs obj[i] node
)
root.AppendChild node
for x in obj.children do
(
sceneToXml x node
)
)
for x in objects do
(
if x.parent==undefined then
(
sceneToXml x objectDescription
)
)
xml.save "C:\ est.xml"
edit "C:\ est.xml"