Notifications
Clear all
[Closed] Round number by increment?
Nov 01, 2013 5:21 pm
Is there a way with maxscript to round a number to say the nearest 90 (ie, 47.26 becomes 90, 302.9 becomes 270, etc.)?
7 Replies
Nov 01, 2013 5:21 pm
this will do the trick as long as val is positive, rnd is even and an integer
fn roundto val rnd = ((val as integer + rnd/2)/ rnd * rnd)
Nov 01, 2013 5:21 pm
Thanks for the help guys. I ended up using your method Klunk for a small script that snaps the viewport viewing angle (like shift in ZBrush):
macroScript snapViewportAngle
category: "GregsScripts"
(
--Needs: get viewport focal point, rotate from focal point to put view camera into position.
viewAngle = (viewport.getTM()) as eulerAngles
fn roundto val rndto = ((val as integer + rndto/2)/ rndto * rndto)
newAngle = #()
if (viewAngle.x != 0) then
(
if (viewAngle.x > 0) then (newAngle[1] = roundto viewAngle.x 90)
else if (viewAngle.x < 0) do
(
buff = viewAngle.x * -1
tmp = roundto buff 90
newAngle[1] = tmp * -1
)
)
if (viewAngle.y != 0) then
(
if (viewAngle.y > 0) then (newAngle[2] = roundto viewAngle.y 90)
else if (viewAngle.y < 0) do
(
buff = viewAngle.y * -1
tmp = roundto buff 90
newAngle[2] = tmp * -1
)
)
if (viewAngle.z != 0) then
(
if (viewAngle.z > 0) then (newAngle[3] = roundto viewAngle.z 90)
else if (viewAngle.z < 0) do
(
buff = viewAngle.z * -1
tmp = roundto buff 90
newAngle[3] = tmp * -1
)
)
finalAngle = (eulerAngles newAngle[1] newAngle[2] newAngle[3])
viewport.setTM (finalAngle as matrix3)
actionMan.executeAction 0 "311" -- Tools: Zoom Extents All Selected
)
Nov 01, 2013 5:21 pm
A dotnet method too, for the sake of completeness
math = dotnetclass "System.Math"
roundVal = 90
angleVal = 256
roundVal * (math.Round(angleVal / (roundVal as float)) as integer)
1 Reply
woohoo!!! sometimes MXS is better!
fn dotnetRoundFloat0 d pre:0.001 =
(
((dotnetclass "System.Math").Round (d/pre))*pre
)
-- !
dotnetmath = dotnetclass "System.Math"
fn dotnetRoundFloat1 d pre:0.001 =
(
(dotnetmath.Round (d/pre))*pre
)
-- !!
round = (dotnetclass "System.Math").Round
fn dotnetRoundFloat2 d pre:0.001 =
(
(round (d/pre))*pre
)
-- !!!
fn roundFloat d pre:0.001 =
(
d /= pre
(if (d - (v1 = floor d)) > ((v2 = ceil d) - d) then v2 else v1)*pre
)
(
v
iter = 10000
t1 = timestamp()
m1 = heapfree
for k=1 to iter do v = dotnetRoundFloat0 pi pre:0.001
format "net0 > % = time:% memory:%
" v (timestamp() - t1) (m1 - heapfree)
t1 = timestamp()
m1 = heapfree
for k=1 to iter do v = dotnetRoundFloat1 pi pre:0.001
format "net1 > % = time:% memory:%
" v (timestamp() - t1) (m1 - heapfree)
t1 = timestamp()
m1 = heapfree
for k=1 to iter do v = dotnetRoundFloat2 pi pre:0.001
format "net2 > % = time:% memory:%
" v (timestamp() - t1) (m1 - heapfree)
t1 = timestamp()
m1 = heapfree
for k=1 to iter do v = roundFloat pi pre:0.001
format "mxs! > % = time:% memory:%
" v (timestamp() - t1) (m1 - heapfree)
)
Nov 01, 2013 5:21 pm
BAM! MXS ROCKS on the numberwang.
All we need is the stats from the same op in python and we can party like it’s 1999.34563