The power of regular expressions

loocas | 3ds Max,dotNET,maxscript,opinions,technical | Tuesday, January 10th, 2012

I don’t think I have to praise regular expressions here, however, I wanted to point out one extremely useful case where regular expressions were pretty much the single most useful, fastest and not so obvious choice in my 3ds Max pipeline.

The thing with 3ds Max is that regular expressions are foreign to MAXScripters and they don’t usually use them. I too am more used to regex in Python or IronPython than MAXScript. However, since we do have access to .NET in MAXScript, we can use its Regex class inside MAXScript.

Why I’m mentioning this and why could it be useful to you? I bumped into a little issue with my pipeline’s handling of rendered files. They assume to be exactly the same as I set them up in 3ds Max, which is logical and correct. However, since I started using Deadline’s SMTD script for submitting my files to the render farm, which takes care of handling the path remapping and storing, it also accidentally took care of letter casing. So, in the end, my render files were being saved all upper cased: “\\SERVER\PROJECT\RENDERS\ABC.EXR” instead of what I set in the Render Dialog: “\\SERVER\Project\Renders\ABC.exr”. The reason was simple, I used simple MAXScript substituteString() method to re-map my local paths to my server, UNC, paths and I converted everything to upper case just in case I got a mismatch:

substituteString (toUpper srcPath) @"D:" @"\\RAMMSTEIN\__UNMANAGED_PROJECTS__"

This wouldn’t be such an issue in a Windows environment, however, when you start dealing with path and file names in Python and you start mixing up casing, you can easily get burnt.

So, the only option left in Python would be to use the re module and search or replace strings based on regular expression matching. In MAXScript, however, we don’t natively have a re module and its methods, but luckily for us, we can use .NET, so that the previous method written in MAXScript using regex would look like this:

regex = dotNetClass "System.Text.RegularExpressions.Regex"
regex.Replace srcPath @"^(D[:])" @"\\RAMMSTEIN\__UNMANAGED_PROJECTS__"

This replaces the capital letter D in the source path srcPath with the UNC server path exactly the same way as the substituteString method above. However, since this is a regular expression engine you’re using, you can modify the search in order to match any casing, should that be your choice.

There are two ways of doing that. The ultimate way I chose in the end was to modify the search pattern to @”^([D|d][:])”, which matches both “D:” and “d:” in the source path and then replaces it with the UNC path at the end.

The other option you can use, should you choose to, is the RegexOptions Enumeration argument at the very end of the method call. If you want to ignore casing, just use this:

regex = dotNetClass "System.Text.RegularExpressions.Regex"
regexOpt = dotNetClass "System.Text.RegularExpressions.RegexOptions"
regex.Replace @"d:\testProject\projFolder\something.exr" @"^(D[:])"
@"\\MyServer\__myProjects__" regexOpt.IgnoreCase

Tadaaa! It’s as simple as that. :) No path splitting, no case testing, just a single line of a regular expression call (not counting the class call) and you’re done. Besides you can use this at a much greater scale with far more complex regex search strings for anything you like directly inside MAXScript.

Note that I’m replacing a D: drive letter in the source paths since that is my projects local drive. You can use whatever you want, hell, you can replace any and all drive letters in one regex call, if you want:



  1. This is very useful snippet. The power of .net is amazing and at the same time, its’ simple to use in combination with mxs. I wonder when Autodesk will integrate Phyton to work in the same way like Maybe one sunny day but who knows :)

    Comment by Branko — March 4, 2013 @ 18:01

  2. Hey, Branko, well, until Autodesk does so, we had to implement IronPython on our own, because, quite frankly, in modern pipelines, there is no way you can work without Python in one form or another. :)

    Comment by loocas — March 9, 2013 @ 12:12

  3. Pretty cool to know that you can do that, I used RegEx to parse out the network path stored in a string. Old ass tool, but friggin’ powerful! Thanks for the tip!

    Comment by Camerajunkie — March 29, 2014 @ 05:46

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress | Theme by Roy Tanck