PDA

View Full Version : format to file with ""


3rd Dimentia
02-01-2008, 11:27 AM
I need to send the name of an object out to a text file with the "" intact around the string.
.
At the moment I have the line

format "objectName = %\n" objectName to:file

which produces
objectName = Small
in the text file.

What I'm after is this
objectName = "Small"
How would I go about this? It's got me a bit stumped.

Thanks,
Cg.

Pjanssen
02-01-2008, 12:01 PM
format "objectName = \"%\"\n" objectName to:file

Try that :)

PEN
02-01-2008, 12:30 PM
Just wondering why you need this. When a file is read in, it is read as a string. These means that all data written to a file will have "" around them once read in.

JHN
02-01-2008, 01:21 PM
...skip this

Pjanssen
02-01-2008, 07:52 PM
Just wondering why you need this. When a file is read in, it is read as a string. These means that all data written to a file will have "" around them once read in.
It could be anything parsing the file, so it can just as well be required syntax.
And of course there's a difference between what you suggest:
"objectName = Small"
and what he's after:
objectName = "Small"

MoonDoggie
02-01-2008, 09:26 PM
What paul is saying is that it's always
"objectName = Small" anyways. It's always a string, so adding additional quote brackets is serving no purpose.

-Colin

PEN
02-01-2008, 09:34 PM
I would normaly use something like filterString str "=" and then get the two parts and do somthing with it. That way I can test both parts to make sure that it is what I want.

3rd Dimentia
02-01-2008, 10:46 PM
I need to do that cause I'm writing a script that will write a script that wil rebuild poly objects. I'm writing a way for me to take models from say max 9 to max 8. So I want to be able to run this script on a poly object and have it write out a script that when run in another version of max it will rebuild the poly object with all the cool max stuff that other export formats won't support like ALL the UV channels, vert colours etc. I am relatively new to maxscript so I may be doing some stuff awkwardly.

Here's my script as it stands now. It's far from finshed but the base is working but it's also really slow to rebuild big objects as it is. I'm going to test whether writing an array into the built script and using a for loop instead of writing every line out will make it run faster. Any suggestions are very welcome.


clearlistener()

if (selection.count == 1) and (superclassof $ == geometryclass) and (classof $ != TargetObject) then
(
outName = ("C:\\PolyExport.ms")
outFile = createfile outName
tempObj = snapshot $
convertTo tempObj PolyMeshObject
vertCount = polyop.getnumVerts tempObj
polyCount = polyop.getNumFaces tempObj
objectName = $.name

format "clearlistener()\n" to:outFile
-- format "objectName = %\n" objectName to:outFile
format "newPoly = editable_mesh()\n" to:outFile
format "convertTo newPoly PolyMeshObject\n" to:outFile

for i = 1 to vertCount do
(
vert = polyop.getvert tempObj i
format "polyop.createvert newPoly %\n" vert to:outFile

)
for i = 1 to polyCount do
(
poly = polyop.getFaceVerts tempObj i
format "polyop.createPolygon newPoly %\n" poly to:outFile

)
close outFile
delete tempObj
)
else
(
messagebox "Select one geometry object to export" title:"Selection Error"
)

kees
02-01-2008, 11:07 PM
so adding additional quote brackets is serving no purpose.


In some cases, yes.

But not in all cases.
For example XML requires the data to be in quotes:

<NODE NAME="paulNeale" TYPE="TD">
</NODE>

If you don't add the additional quotes, things go very wrong.
Pjanssen's suggestion is the correct way to solve the problem when you need additional quotes.


Take care,

RustyKnight
02-03-2008, 06:51 AM
I'd have to agree with moondoggie and Paul on this way, although kees is absolutly correct. Seen as the output is actually a script, then cg needs this approach. He's not just dumping data, but he's also creating a "self run" script to re-compile the objects...

I think Bobo did something simular sometime ago, might be worth looking at...

Shane

3rd Dimentia
02-03-2008, 09:24 AM
The one that Bobo wrote, are you meaning the BFF (back from five) that has since changed to Bobo's file format?

Gravey
02-03-2008, 09:38 AM
The one that Bobo wrote, are you meaning the BFF (back from five) that has since changed to Bobo's file format?Yes I believe that's the one Shane's talking about. I recently checked out the code for it and it really helped to get me on the right track for a script i've been writing for work. I'd say definatly take a look if you havn't already

RustyKnight
02-03-2008, 11:13 AM
The one that Bobo wrote, are you meaning the BFF (back from five) that has since changed to Bobo's file format?Yeah, that's the one. I tried it last year to go from 9 to 8, but it didn't quite meet our needs, but it looked really interesting!

Well worth a look into to help out with any problem areas you might have.

Shane

PEN
02-03-2008, 11:32 AM
Ah so you want a runable script then you will need the quotes if you are just going to execute the whole thing. I thought that you were looking to store data in a file. And Kees, I agree that if you are writting an XML then you have to have the quotes. Better to just use the dotNet object to write the XML for you so that you don't have to do all the formatting your self though.

3rd Dimentia
02-03-2008, 08:12 PM
Yeah to start with I just want to export polygon objects to an executable maxscript with all the possibilities intact like all UV's, vert colours, material ID's etc... Then I'll move onto modifiers and see if I can get my script to be able to write scripts to rebuild objects with stacks.

U.S.S. Speed
02-04-2008, 12:03 AM
I didn't read the whole thread...

But I did have to export XML code to a file from MaxScript, which required me to put quotes in a string.

So, in case you need String = "MyString" and export it exactly that way in a file, then you do

LongString = "String = \"MyString\" "
LongString += "<xml>id:\"MyId\"<\xml>"

Any \" is reading a " string and not as a MaxScript quote.
Like that I managed to export a long Excel sheet in XML from a 3DMax scene, compiling a lot of stuff like polycount, vertex count, even texture weight in memory and doing automatic little nice Excel graph from MaxScript.

PEN
02-04-2008, 10:59 AM
"system.xml" will get you every thing that you would need in reading and writting xml files without having to worry about quotes.

U.S.S. Speed
02-04-2008, 03:27 PM
So far I found little to no example of a DotNet System.XML in MaxScript. Of course it works, but can't find example script.

Aside, from this page : http://www.functionx.com/vcsharp/xml/Lesson06.htm it appears somehow problematic to return XML value between quotes. One of their example of subchild is to store the hole subchild group as a string... Which bring us back to our original problem of having quotes in a string.

Aside, I'm not sure which system is the fastest. Using system.xml or just parsing the xml you want... To me, it seams that both way would require almost the same ammount of script.

Pjanssen
02-04-2008, 05:45 PM
If the dotnet xml system is giving you problems, then perhaps consider writing your own XML classes/struct in maxscript.
That's what I did, not too much work, and you've got full control.

PEN
02-04-2008, 05:49 PM
Not really as you can get all elements as an array. You can create and set properties and don't have to worry about double quotes as it will do it for you. You can get an element in a file and insert elements into it. As was suggested to me, if you use xml.write it is probably faster them xml.xmldocument how ever it isn't as flexable I don't think.

The code below will write out a new xml doc with properties for the objects in the scene. The xml doc is sorted by hierarchy and can be read that way.


dotnet.loadAssembly "system.xml.dll"
xml=dotNetObject "system.xml.xmlDocument"
showProperties root
showMethods root
root=xml.CreateElement "root"
xml.AppendChild root

fn sceneToXml obj root=
(
node=xml.CreateElement obj.name
node.SetAttribute "Object" obj.name
node.SetAttribute "wireColor" (obj.wireColor as string)
node.SetAttribute "transform" (obj.transform as string)
node.SetAttribute "classOf" ((classOf obj) as string)
root.AppendChild node
for x in obj.children do
(
sceneToXml x node
)

)

for x in objects do
(
if x.parent==undefined then
(
sceneToXml x root
)
)
xml.save "C:\\test.xml"
edit "C:\\test.xml"

U.S.S. Speed
02-04-2008, 06:35 PM
Quite cool and quite useful. Thanks.
Gonna bookmark this page.

However, there is time where you can't avoid the need of quotes... Like the XML header of a Excel files.

PEN
02-04-2008, 06:59 PM
Very true, I wasn't trying to say you didn't need to.

Now writes out all modifiers and keys. Doesn't handle tangents and other key settings how ever.


dotnet.loadAssembly "system.xml.dll"
xml=dotNetObject "system.xml.xmlDocument"
-- showProperties root
-- showMethods root
root=xml.CreateElement "root"
xml.AppendChild root

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 "object" (sub.object 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 root
)
)
xml.save "C:\\test.xml"
edit "C:\\test.xml"

U.S.S. Speed
02-04-2008, 07:07 PM
Yeah, like that you could convert a whole scene with everything inside into XML.

PEN
02-04-2008, 07:23 PM
Well you would not get everything unfortinitly. There are many parameters that you just can't get at and that would stop the scene from loading the way that it was created. Also you would have to take into consideration ever controller and what it might be dependent on. For instance what I have posted here doesn't take into account if an object, modifier or controller is an instance of another. Or what the expression is in a wire parameter.

So it would take a lot of doing to even get close to reproducing the file. BFF is as close as it has come so far and Im sure Bobo could make it just that much better each version of Max but it will always miss things that you just can't get at. Lofts for instance can't be created in Max script so you would have to colapse them when writting it out.

It would be fairly easy to write your own exporter that stores extra information like modifiers.

CGTalk Moderation
02-04-2008, 07:23 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.