Decimal places

Decimal places

GeoffKornfeld
Advocate Advocate
2,050 Views
15 Replies
Message 1 of 16

Decimal places

GeoffKornfeld
Advocate
Advocate
I need to create some text that automatically changes with the angle of an object, so I modified the script in the Maxscript tutorials to the following:

TM=exposetm name:"ExposeAngle"
t=text name:"ControlledText"
t.baseobject.renderable=true
theCtrl = float_script()
theCtrl.addNode "TheText" t
theCtrl.addNode "ExposeAngle" TM
theCtrl.SetExpression "TheText.text = ExposeAngle.localEulerY as string\n0"


It works great -except that I don't want any decimals. Is there a way to do round the numbers?

Thanks!
0 Likes
2,051 Views
15 Replies
Replies (15)
Message 2 of 16

Anonymous
Not applicable
Do a FindString to search for "." the decimal point. That will give you a position in the numeric string to truncate at that position.
0 Likes
Message 3 of 16

Anonymous
Not applicable
pi as integer --> 3
floor pi --> 3.0
ceil pi --> 4.0
0 Likes
Message 4 of 16

Anonymous
Not applicable
Hi, I´m runing a script that give me all the xyz coordinates of an sphere trajectory, the data give to many decimals, but I need something similar to Yoyo.
Need just 2 decimals, how do achieve this?

This is my line script

for t = 0 to 100 do at time t format "frame#: % pos: %\n" t $.pos

This is the result I´m getting

frame#: 0 pos:
frame#: 1 pos:
frame#: 2 pos:
frame#: 3 pos:
frame#: 4 pos:
frame#: 5 pos:

What can I sum to the script to give just to decimals in each axis

Thanks in advance

Pitu
0 Likes
Message 5 of 16

Anonymous
Not applicable
for t = 0 to 100 do at time t
format "frame#: % pos: \n" t \
$.pos.x as integer $.pos.y as integer $.pos.z as integer
0 Likes
Message 6 of 16

Anonymous
Not applicable
Hi Anubis, thanks for the quick reply.

I used the code you give me and the results was this

0
1027
0

The results are in cms, while I´m using meters in the 3dmax as units, how i figuret out to convert to meters.

The results of my first code give me the format: frame#: 0 pos: , how I can keep this format?

Thanks again for the reply

Pitu
0 Likes
Message 7 of 16

Anonymous
Not applicable
I wrote the example code above on-the-fly, and I miss to close in brackets the convertions, i.e:
-- example pos:
$.pos -->>
format "pos:%\n" $.pos -->> pos:
$.pos.x as integer -->> 12

-- correct:
format "pos:\n" ($.pos.x as integer) ($.pos.y as integer) ($.pos.z as integer)
-->> pos:

Also the "as integer" just convert the Float to Integer and s'd not do anything else (i.e. cant convert meters to cms).
0 Likes
Message 8 of 16

Anonymous
Not applicable
Anubis,

I tested the new code, works perfect. Yours replies were a lot of help.

Thanks again

Pitu
0 Likes
Message 9 of 16

PiXeL_MoNKeY
Collaborator
Collaborator
You can also use formattedprint to define how a float value is printed.

-Eric
0 Likes
Message 10 of 16

Anonymous
Not applicable
PiXel , I don´t know exactly what you mean?

You say that I could replace integer with float?

I don´t know why but the script still give as result

frame:0 pos:, while my system units is metric in 3d max and my coordinates are 0 on x, 5 meters on y and 2,4 meters on Z.

I think it´s a convetion problem, but I tried some stuff without any luck.

Pitu
0 Likes
Message 11 of 16

Steve_Curley
Mentor
Mentor
Formatted print is the Maxscript equivalent to the C printf functions - allows you to specify the width, number of decimals etc. Look it up in the Maxscript help - it's all there.

And I also think you need to step back and look at this logically. You have some numbers - they are just numbers - nothing more, nothing less. Only when those numbers refer to something specific do they take on any real meaning.
"1" could be a mm or a mile - without reference to something else it is just "1 something".

Secondly, read up on the System Units and the Display Units. The System Units Setup dialog does not mention "metric" therefore I suspect you are looking at the Display Units (which does mention metric) and confusing the two - again, read the (main Max) help as this is all in there and it is very important you understand the relationship between the 2 because getting it wrong can cause real problems down the line. Your values are clearly a factor of 10 out - 24 versus 2.4.

And finally, you said "and 2,4 meters on Z" - having windows set to a locale which uses a comma decimal separator causes problems - I would advise you to change it to one which uses the full stop (period).

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 12 of 16

Anonymous
Not applicable
Following Maxscript built-in functions could help in you case :

<code>
units.formatValue <float>
and
units.decodeValue <string>
</code>

but first at all, follow Steve advice, it's very important to understand the concept of System Units and Display Units.
0 Likes
Message 13 of 16

Anonymous
Not applicable
Steve,

I read the system and display units, I understand the diference, thanks for the tip. That is done.

The comma in the 2,4 meters on z was a mistake, I meant 2.4 meters.

What I´m still can´t figured out is to get the results with 2 decimals, I tried severals ways with fomatted print

#2f (meaning 2 decimals), but didn´t work, I´m sure it´s wrong, but no idea how to write it down

Could help me out on how to add to my code the expression for resulting in to decimals values.

here is the code:


for t = 0 to 100 do at time t format "frame:% pos:\n" t ($.pos.x as integer) ($.pos.y as integer) ($.pos.z as integer)

amarhys,

maybe I have to replace integer with some other expression like float, but still don´t know the decimal part.

thanks in advance.

Pitu
0 Likes
Message 14 of 16

Steve_Curley
Mentor
Mentor
First off - read this thread from the top. It is clearly about printing integer values and NOT about printing decimal places. In future it would be much better to start your own thread rather than hanging your questions onto the end of someone else's thread.

(
for t = 1 to 15 do
(
at time t
format "Frame % pos:\n" t \
(formattedprint $.pos.x format:"1.2f") \
(formattedprint $.pos.y format:"1.2f") \
(formattedprint $.pos.z format:"1.2f")
)
)
or

(
for t = 1 to 15 do
(
tp = #()
at time t
for p = 1 to 3 do
tp

= (formattedprint $.pos

format:"1.2f")
format "Frame % pos:\n" t tp tp tp
)
)


Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 15 of 16

Anonymous
Not applicable
Steve,

I miss undestood the thread, I thought it was for decimal places, as said in the name of the thread.

For futures threads I will start my own`s.

Anyway, I tested your code, works great.

Thanks it was lot of help
0 Likes
Message 16 of 16

Steve_Curley
Mentor
Mentor
This is one of the problems when the Topic and the Contents don't agree - you need to read the whole thread before replying, especially if adding a "supplementary question", which is why it's often safer to start a new thread, but do search first as many questions have already been answered.

I'm glad the code helped. Formatted print is useful but a bit of a pain as you can only format a single number at a time, hence the 3 almost identical lines in the first example and the extra loop in the second.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes