Tuesday, 25 February 2014

Maxscript: Resizing text to fit within a boundary

I was asked the other day if I could make a way of resizing text inside 3dsMax to fit the width of an object; in this case a 2D rectangle primitive, so that the rectangle could be positioned and then different language texts could be resized to fit within the predetermined area. As you may know, the text object inside 3dsMax constantly expands as you write text and there is no way of constraining it to a certain width.

Here is the final script in action:


Like most things, first I figured out how I could make a system that did this, then I created a maxscript so other people could operate it and I didn't have to perform the setup each time.

So what needs to be done?

You want to take the width of the text (textWidth) and scale it by a number such that it equals the width of the target object (targetWidth)

You can do this by:

targetWidth/textWidth = scaleFactor
finds the mutiple needed to get the textWidth to be the same value as the targetWidth



textWidth * scaleFactor
makes the textWidth the same value as the targetWidth by multiplying by the scaleFactor

So how can we perform this in 3dsMax?

First we need the width of the targetWidth. After a quick Google, I found you can get the boundary box width of an object through an Expose Transform tool. Although you could also use the width value of the rectangle, this method means you could apply it to a different shape easily if need be.


Now we need the textWidth. Again I can use an Expose Transform.

So if I have for example, text that was 200 wide that needed to fit within a space that is 100 wide, we would get:

100/200 = 0.5
targetWidth/textWidth = scaleFactor

We can check this. If we take 200 and multiply by 0.5 we get 100 which is correct.

As I don't necessarily want to affect the scale of the text, the scale factor would also work with the size parameter, as it is merely a multiple of what needs to be applied.

This is the final maxscript I created for the function.


Finally add a simple UI and you have a nice little script:


No comments:

Post a Comment