[Closed] [wish] dlx for fit function
I have a wish to the sdk-heads out there:
I ported this “fit function” from python to maxscript:
fn fit v oldmin oldmax newmin newmax clamp:true = (
if clamp == true then (
case of (
(v < oldmin): return newmin
(v > oldmax): return newmax
)
)
scale = ( v as float - oldmin) / (oldmax - oldmin)
if newmin < newmax then (
return newmin + scale * (newmax - newmin)
)else(
return newmin - scale * (newmin - newmax)
)
)
Chances are high that this is a standard maxscript function I just didn’t find in the help.
But if this is not a native function, could some one build a dlx for this?
I know I should take a look at the SDK anyway, and there a enough samples in this forum to get this done by c+±n00b-like-me. So if nobody answers it is likely I drown in c++.
Is there another way to “easily” deploy such functions?
Should I embed it into a CA and call it there?
thank you.
Georg
Just copy the .MS file into \Stdplugs\StdScripts folder where all shipping non-native MAXScript functions are installed. Alternatively, you can copy an .MS file into any folder listed in the Plugins paths, OR ANY SUB-DIR of such folder (MAXScript scans folders recursively, while Max loads DLLs only from the path listed in plugins.ini). This way, one can create a real plugin, install it and put some related MAXScript files in a subfolder called \Scripts and get them run on startup when the plugin is loading…
Btw, chances are your code is very slow – it uses lots of RETURN statements. Check out this thread:
http://forums.cgsociety.org/showthread.php?f=98&t=458965
I tested your code. For 100000 iterations with clamp:true, it executed in 41266 ms. On the same machine, the following variation of the code finished in 265 ms:
fn fit v oldmin oldmax newmin newmax clamp:true = (
if clamp then
(
if (v < oldmin) then
newmin
else
if (v > oldmax) then
newmax
else
(
scale = ( v as float - oldmin) / (oldmax - oldmin)
if newmin < newmax then
newmin + scale * (newmax - newmin)
else
newmin - scale * (newmin - newmax)
)
)
else
(
scale = ( v as float - oldmin) / (oldmax - oldmin)
if newmin < newmax then
newmin + scale * (newmax - newmin)
else
newmin - scale * (newmin - newmax)
)
)
With clamp:false, both versions of the script execute in the same time – 375 ms (because the RETURN statements at the end of the code are optimized away by MAXScript).
So in the default case of clamp:true, the optimized version is 155 times faster (but does not look that great as it is longer). If speed is more important than looks and you have to call this function thousands or millions of times, use the optimized one.
Bobo,
thank you.
Just copy the .MS file into \Stdplugs\StdScripts folder
I knew that. I just forgot that deploying a dlx is “same same but different”.
Btw, chances are your code is very slow - it uses lots of RETURN statements.
Ja Sir .
As a frequent reader of maxScript help I know the best parts by heart:
[color=white]Do not use return, break, exit or continue[/color]
I was so happy I found this snippet, I didn’t care of return statements and just changed what was needed to get it running.
What’s more important to me and is also a subject in the thread you refer to:
How to handle missing language constructs like “else if”?
I see you changed the “case of” into nested if-statements.
For the sake of completeness here is the link to the orignal snippet:
http://homepage.mac.com/andykopra/pdm/tutorials/shapes.html
Georg