PDA

View Full Version : Loop trough all objest and print transformation


Krystman
05-25-2008, 10:41 AM
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

Guiniture
05-25-2008, 01:02 PM
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?

Gravey
05-25-2008, 01:23 PM
OR...

for o in objects do
(
-- insert your code here
)

Krystman
05-25-2008, 01:23 PM
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
...

)

Bobo
05-25-2008, 03:33 PM
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 %\n" o.name --output the object name
format "Position.X = %\n" o.pos.x --output the position...
format "Position.Y = %\n" o.pos.y
format "Position.Z = %\n" o.pos.z
--and so on
)

Krystman
05-25-2008, 03:50 PM
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\"?>\n" to:output_file
format "<synths>\n\n" to:output_file
for o in objects do
(
format "<synth\n" to:output_file
format "\tx=\"%\"\n" o.pos.x to:output_file
format "\ty=\"%\"\n" o.pos.y to:output_file
format "\tz=\"%\"\n" o.pos.z to:output_file
format "\trx=\"%\"\n" o.rotation.x_rotation to:output_file
format "\try=\"%\"\n" o.rotation.y_rotation to:output_file
format "\trz=\"%\"\n" o.rotation.z_rotation to:output_file
format "\tshrink=\"%\"\n" o.scale.x to:output_file
format "\t>%</synth>\n\n" o.name to:output_file
)
format "</synths>" to:output_file
close output_file
edit output_name
)


Thank you, guys! You rock! :buttrock:

thatoneguy
05-26-2008, 06:08 PM
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.

PEN
05-26-2008, 07:01 PM
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:\\test.xml"
edit "C:\\test.xml"

CGTalk Moderation
05-26-2008, 07:01 PM
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.