This next article is a bit more about scripting and programming in general and less about MEL specifically. It's about using procedures to take care of basic functions in order to speed up tasks you perform regularly in your scripts.
Let say for example you want to print out some data. In MEL and most languages I've programmed with, when you print or echo something it will not create a new line the next time you print. So if you print the phrase "Hello World" twice in a row, it will look like this: "HelloWorldHelloWorld".
So, a quick and easy way to do this is to add a newline. To do this in MEL, you use the characters "\n". So your print statement looks like this:
print "Hello World\n" ;
print "Hello World\n" ;
// Result:
// Hello World
// Hello World
//
// Without \n you'll get:
// Result: HelloWorldHelloWorld
When you need to add a newline character onto every single thing you print it can get time-consuming. Especially when you need to concatenate it with a variable which in turn you then need to add parenthesis around your entire statement, it's very annoying. We know every millisecond counts right? So let's save ourselves some time and build a custom print statement.
// **********************************************************
// Faster way to print
global proc jgPrint (string $print) {
print ($print+"\n") ;
}
// Now I can simply call:
jgPrint "Hello World!" ;
// And it adds the newline for me.
Slightly unrelated, but another benefit to this method is that you can print a concatenated int or float with a string argument using this method which you can't with a normal print statement.
So there we've created a small procedure that will save us lots of time down the road. Essentially we're creating tools to help perform tasks faster than we could without them, and filling in gaps with tools that the default language doesn't provide or where the default commands are lacking in one way or another.
There are an infinite number of ways you can use these. I'm not going to provide all the code for you since it's a good learning experience to write these on your own...but here are a couple examples of some procs I use on a daily basis.
- Append A Single String Argument To A String Array
- Read A Text File And Return It As A String Array
- Create A Set Driven Key
- Make An Attribute On An Object
- Edit An Attribute's Values
- Pass An Array Into A String Argument
No comments:
Post a Comment