Help with Formatting Text output from a Script Controller to have leading zeros

Help with Formatting Text output from a Script Controller to have leading zeros

peterjung
Enthusiast Enthusiast
2,053 Views
11 Replies
Message 1 of 12

Help with Formatting Text output from a Script Controller to have leading zeros

peterjung
Enthusiast
Enthusiast

I am trying to animate an LED style 7 segment numerical display and I am almost there based on this excellent tutorial found here:

 

https://youtu.be/A6V5DE2GKZE

 

However I am struggling to get the text output correctly formatted.  I want the output always to have three numerical places with leading zeros when required – i.e.

 

001

002

003

 

Etc.

 

I found a tip to use formattedprint in an old thread on CGTalk but I guess I am not applying this correctly.  The expression I have in the Script Controller is as follows:

 

TextRamp.text = ($ET1.distance as integer)as string

formattedprint TextRamp.text format:"03d"

0

 

But this has no effect on the format of my variable Text.Ramp.text.

 

I would be very grateful if anyone can help me out with this as I know practically nothing about Max Script!

 

Thanks in advance… 😊

0 Likes
Accepted solutions (2)
2,054 Views
11 Replies
Replies (11)
Message 2 of 12

peterjung
Enthusiast
Enthusiast

Well, I have made some kind of progress I guess, I have worked out that I need to assign the variable to the output of the formattedprint command – so I now have:

 

TextRamp.text = ($ET1.distance as integer)as string

TextRamp.text = formattedprint TextRamp.text format:"03d"

 

However, this is giving me some weird results…

 

ET1 is an ExposeTransformHelper object which I have set up to look at two dummy objects and ET1.distance is the distance between the two objects.

 

I want to use this distance value to drive the LED display using TextRamp.text

 

If I just have the first line from above, then it the value of TextRamp.text is displayed correctly (but not in the format I am wanting – i.e. 3 digit with leading zeros when applicable)

 

However, when I add the second line in an attempt to format the TextRamp.text I get a weird large random negative value?

 

From the help it states that:

 

“The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed (subject to the precision specification).”

 

So this weird value is getting written to the variable TextRamp.text but I cannot figure out why? 

 

Further, each time I click the “Evaluate” button the displayed value changes even though though the distance between the two dummy objects has not changed?  So, the second line is calculating something, but I cannot figure out what is going on?

 

The attached files show each case – I would really appreciate if anyone can give me some pointers on where I am going wrong?

0 Likes
Message 3 of 12

peterjung
Enthusiast
Enthusiast
Accepted solution

Eureka!  Ok, I figured this out…..

 

My original script was feeding in the variable as text into the formattedprint function so it was evaluating and formatting each text character individually.

 

If I initially create a variable as in integer to capture the value of $ET1.distance and feed this to the formattedprint function then it all works!

 

Here is the modified script – I also included a small while loop so the display will roll over back to three zeros after it reaches 999.

 

i = $ET1.distance as integer

while i > 1000 do

i = i - 1000

TextRamp.text = formattedprint i format:"03d"

print $ET1.distance

0

 

Works great now!  I uploaded a screencast to illustrate the application if anyone is interested….

 

0 Likes
Message 4 of 12

peterjung
Enthusiast
Enthusiast

Something happened and the screencast link disappeared... here it is again:

 

https://www.screencast.com/t/tYnOcIihff

 

 

0 Likes
Message 5 of 12

denisT.MaxDoctor
Advisor
Advisor

the numbers on the device shift ... this should not be

0 Likes
Message 6 of 12

denisT.MaxDoctor
Advisor
Advisor
Accepted solution

@peterjung wrote:

Eureka!  Ok, I figured this out…..

Here is the modified script – I also included a small while loop so the display will roll over back to three zeros after it reaches 999.

 

i = $ET1.distance as integer

while i > 1000 do

i = i - 1000

TextRamp.text = formattedprint i format:"03d"

print $ET1.distance

0

 

Works great now! 


formattedprint (int(mod (dist + 0.5) 1000)) format:"03d"
0 Likes
Message 7 of 12

peterjung
Enthusiast
Enthusiast

Do you mean the small left / right movement when it changes through certain numbers… yea... not sure why that is happening?  I guess it may be related to the font... maybe I can think about treating each digit individually as a separate object which may address this issue?

0 Likes
Message 8 of 12

peterjung
Enthusiast
Enthusiast

Thank you Denis T…. an infinitely more elegant solution!  Appreciate the input! 😊

0 Likes
Message 9 of 12

peterjung
Enthusiast
Enthusiast

You know I thought I could live with the glitch on the text…  but after Denis T pointed this out it just bugged me more and more… so I have just been digging into this…

 

So it seems to be an issue with the characters ‘6’ and ‘7’ as when these characters are displayed the whole text object seems to move in the left & right direction.  I checked carefully and the pivot for the text object itself does not move so I though it must be a problem in the font file.

 

The font I used (DSEG7 from https://www.keshikan.net/fonts-e.html) looks like it has a small problem.

 

After a couple of quick YouTube video lessons I was able to figure out how to edit this font using something called Glyphr Studio.  I had to change the origin position and the width of the ‘6’ and ‘7’ characters and then re-export this font.

 

This seems to have fixed the issue!!  Every day’s a school day 😊

 

https://www.screencast.com/t/etKLIezy

0 Likes
Message 10 of 12

peterjung
Enthusiast
Enthusiast

EDIT - the post above should refer to the characters '5' and '6' as the problem with the original font file

0 Likes
Message 11 of 12

denisT.MaxDoctor
Advisor
Advisor

@peterjung wrote:

The font I used (DSEG7 from https://www.keshikan.net/fonts-e.html) looks like it has a small problem.

 

This seems to have fixed the issue!!

 

https://www.screencast.com/t/etKLIezy


Oh! I thought I was wrong in my experience ... and the problem with the Text splineshape can be solved by adjusting the TTF file ...

I regret this and confirm that it is not possible. Text splineshape problem CANNOT be solved this way.

you can see this in your video at time ~0:45, where the leading 0 switches to 1. Where everything shifts as I expected ...

 


 

 

0 Likes
Message 12 of 12

denisT.MaxDoctor
Advisor
Advisor

It's an "old school device" that can't shift it's own digital screen as far as I remember ... ‌‌;)

0 Likes