Find Me on

Contact:

Riyadh, Saudi Arabia

Tel: +966 533 238 935

Alaa@vfxarabia.co

Skype: 

Copyright © 2014 - 2019 VFXArabia.

Maxscript tutorial: Print vs Format

I did a small research regarding the differences between Format and print commands in 3ds max script, and concluded that the main difference is the quotation sign "" in their results.

 

Print is simple, straight forward to use and it's very helpful when debugging. To make work it just needs a string (which is a normal sentence between two quotations "" ) or variable, so the command will look like:

Note:

In this tutorial you will learn how to write to an external file, but what about reading from that file. To know how check out this tutorial:

Reading from an external text file....

print "I love maxscript"

"I love maxscript"

Print $.pos -- this will print the position of the selected object

[10,10,0]

incase you want to mix between values like $.pos and string you will have to convert the position to a string using (as string) command after the value as follows:

print ("objPos="+ $.pos as string)

Result:

"objPos=[10,10,0]"

And if you want to print a dialog where each line will contain a sentence then we have to add (\n) after each one :

print "How are you today?\n I'm fine, thanx\n"

Result:

"How are you today?

 I'm fine, thanx

"

Everything is great till now, but print command has some limitations and you will face this when for example you wanna write to an external file (*.ms or *.txt) (I will provide a complete example about writing to an external file at the end of the tutorial, for now just focus on the print command) lets say you want to write a the following code to an ms file:

print ("mergemaxfile @\"" + @"D:\temp\test.max" + "\"")

the result would be:
"mergemaxfile @"D:\temp\test.max ""

which is useless, you cannot execute this line because it's a string
we want this line but without the quotation sign "".

Or another scenario where you wanna export objects data like name, position and rotation to txt file:

objName = $.name as string

objPos = $.pos as string

objRot = $.rotation as string

 

print ("name:" + objName + " pos:" + objPos + " rot:" + objRot )

Result:

"name:Point01 pos:[5,5,5] rot:(quat 0 0 0 1)"

Again with quotations.

 

So how to get rid of them. Well it's easy, by using the Format command instead. We can use it with a simple string:

format "How are you today?\n I'm fine, thanx\n"

Result:

How are you today?

 I'm fine, thanx

The same result as print command but without the quotations.

and if we want to print values like $.pos or make it a bit complex and introduce a mix of strings and values then we have to use something called string formatting argument. Don't be intimidated by the name, it's a small string before the values that we want to print that will make the Format command work properly.

Check the following:

format "%" ( $.pos) -- Just make sure only one object selected in the scene

[10,10,0]

format "object position= %" ( $.pos)

object position=[10,10,0]

format "name:% pos:% rot:%" $.name $.pos $.rotation

name:Box001 pos:[10,10,0] rot:(quat 0 0 0 1)

In simple words it's printing the first block ( the string ) while replacing each % sign with a variable or value from the next block. And it's doing this in order where the first % is gonna be replaced by $.name (in the third code) as in the pic.

And if you want each result to be on a seperate line, then use \n after each like:

format "name:%\n pos:%\n rot:%\n " $.name $.pos $.rotation

Result:

name:Box001

 pos:[10,10 ,0]

 rot:(quat 0 0 0 1)

Another thing let's say you want the result to have quotation "", let's add it in the example above to the name of the object. We can achieve this by adding \" before and after the name and since the name is represented by % so it will be \"%\":

format "name:\"%\"\n pos:%\n rot:%\n " $.name $.pos $.rotation

Result:

name:"Box001"

 pos:[10,10 ,0]

 rot:(quat 0 0 0 1)

Finally lets use maxscript to create an ms file and write inside it using Print and Format and see the difference:

-- Print example first make sure the directory exist before proceeding

Printfile=@"D:\temp\Print.ms" -- the file we are creating print.ms

Outfile = createfile Printfile -- createfile command will create the file for us

print ("Box()") to:Outfile -- write to the file using print method

close Outfile -- you have to close it before continuing or else you won't be able to edit it later

 

Result:

now open the created file and you will find a string inside it:

"Box()"

-- Format example just replace print by format "%"

Formatfile=@"D:\temp\Format.ms" 

Outfile = createfile Formatfile

format "%" ("Box()") to:Outfile 

close Outfile

 

Result:

now open the created file and you will find a command that will create a box

Box()

 

to take it one step further you can test it using:

filein Formatfile -- this will open the script and run it and you should see a box

That's it for this tutorial, I hope you enjoyed it and didn't find it boring specially I tried to make it as colorful as possible. Regards ^_^