January 08, 2015

Dynamo: Text Padding Nodes

I made an interesting discovery the other day, just as I was finishing up my article on zero-padding for an integer-to-string conversion. And while I do not regret the effort I put into developing the custom nodes to achieve that result, as I am still very much in learning mode, and that gave me a task in which I had some interest, I would be remiss if I did not pass along this "discovery".

In the Dynamo out-of-the-box Library, there are two nodes that, similar to my custom node, will "pad" a string to a specified length, using the character (or characters) of your choice. These can be found under Core > String > Actions, and are called PadLeft and PadRight.

In the image above, the same input is provided to each of these nodes, and the newWidth input is provided with a list of values, resulting in a list of results, so you can see the effects. The str input is "ABC" and a multi-character string, "wxyz" is used for the padChars input. As you can see, the PadLeft node adds padding characters to the left side of the string; the PadRight node adds padding characters to the right side of the string. Just like my custom node, if the length of the initial string is the same or greater than the newWidth value, the original string is passed through unmodified.

Unlike my custom node, you can specify any string to be used for padding. If the padding string has more than one character, then Dynamo uses the first character of the string when one character is required for padding, the first two characters when two characters are required, and so on. If the required number of characters for padding exceeds the length of the padChars string, as many instances of the full string plus a partial string with as many characters starting with the first as needed to bring the final string to the specified length will be used.

The Screencast below shows how the PadLeft node can be used to do the same thing as my custom node.

The lesson to be learned here is, prior to embarking on a programming task in Dynamo, take a careful look at what is provided out-of-the-box (as well as in "packages" of custom nodes that can be downloaded) to see if some or all of what you need to do is already available.

2015-11-13 UPDATE: In Dynamo 0.8.2, the ToString node is now called String from Object.

No comments: