Using Base64 encoding in 3dsMax
If you have ever received an email and instead of your normal information for pharmaceutical-related special offers and personal member enhancement, you get a jumble of nonsense, you’re probably already aware of what a Base64 encoded string looks like. Email clients use MIME to transfer messages and attachments, and one way to break up things like images so that it can be sent is Base64 encoding.
Despite looking like the sound you make when trapping your plums in the fridge door, Base64 encoded strings can be used to represent images and sounds and deployed with scripts to avoid the need for external linked dependencies.
DotNet Provides some easy methods to do this within the framework, so here are some functions for converting images to Base64 within 3dsmax below.
fn ConvertImageToBase64String filename = ( if (doesfileexist filename) do ( memstream = dotnetobject "System.IO.MemoryStream" ImgLoaded = ImageClass.fromfile filename ImgLoaded.save memstream ImgLoaded.rawformat Base64string = ConvertClass.ToBase64String (memstream.ToArray()) memstream.close() return Base64String ) ) fn ConvertBase64StringToImage string = ( bytearr = convertclass.FromBase64String string memstream = dotnetobject "System.IO.MemoryStream" bytearr DecodedImg = ImageClass.fromstream memstream memstream.close() return DecodedImg )
I’ve added these into a utility with a few extras (namely functions to convert and play Wav files using Base64 encoding) You can download this script at the bottom of the page.
Grimlock says “SSdtIGdvbm5hIG9wZW4gYSBjYW4gb2YgV0hPT1BBU1Mgb24geW91IQ==”
If if you have ever read the “Bitmap Values” topic in the MXSHelp, you will be aware of this script –
b=selectbitmap() -- open image file browser bname="bitmap_"+(getfilenamefile b.filename) -- build name from filename w=b.width -- get properties of bitmap h=b.height format "----------nfn load_% = (n" bname -- start defining function format "local %=bitmap % %n" bname w h -- create bitmap in function -- write out a function that unpacks an integer into a pixel color format "fn unpack val = for p in val collect (r=p/256^2; g=p/256-r*256; b=mod p 256; color r g b)n" for r=0 to h-1 do -- for each row in the bitmap -- have function write the column of pixels to the bitmap ( format "setpixels % [0,%] (unpack #(" bname r pixels=getpixels b [0,r] w -- read in the column of pixels for c=1 to w do -- loop through each pixel ( p=pixels[c] -- get the pixel -- pack the pixel into an integer and write it out format "%" (((p.r as integer)*256+(p.g as integer))*256+(p.b as integer)) if c != w then -- if not at end of data format ", " -- write a comma else format "))n" -- else close out the line ) ) format "return %n" bname -- function returns the bitmap format ")n----------n" -- finish off function definition )
Base64 is a DotNet method of performing the same thing, and instead of returning a max bitmap, it returns a Dotnet image.
This could probably benefit from being moved into a dedicated dotnet assembly, as I found with the color control a few weeks back, similar functions are much slower within max. Therefore if you are converting large images you might find the UI snagged up for a while.
Finally, to convert text to and from Base64, here’s a great site I found that will do it for you! –
8 Comments + Add Comment
Got anything to say? Go ahead and leave a comment!
Want to know about updates the moment they happen? Subscribe to LoneRobot.net
- SliderMan – Does everything that a slider can
- Grabbing The Material Preview
- The Path of the Righteous
- I Don’t Need Regular Expressions
- Kinetic Energy
- What we are today comes from our thoughts of yesterday
- Autodesk Webinar
- Two Clicks From Amsterdam
- WindowBox Replanted
- SpeechBot – A handy script to load and save morpher keys