Toggle debug print in Maxscript

Printing out a debug spew is very useful when scripting, but often you end up with tons of print statements littering your code. Printing debug is often one of the slowest things in max (or any language, instead of just doing the actual work you are sending signal through hardware, but I digress). Often you’ll only want to turn toggle on the debug logging when you’ve got an issue.

A while ago I started using a more clever system by setting a variable debugprint = true at the top of my scripts. Then when I was using the debug spew I’d use:

if debugprint == true then print "My debug stuff"

Matt (who I work with) came up with a much more elegant solution. Much like my debugprint = true variable he created debugPrintVal, and instead of querying the state of it for every print, he simple used a new function, debugPrint:

fn debugPrint printStr =
(
    if debugPrintVal == true do
        (
            Print printStr
        )
)

So, instead of using :

if debugprint == true then print "My debug stuff"

We can now used the much cleaner version:

debugprint "My debug stuff"

4 Responses to “Toggle debug print in Maxscript”

  1. imnotmatthonest Says:

    That’s a really good idea!

  2. Robert Hexter Says:

    I implemented a similar system that indents processes as they run it was mel..
    This would log effectively stack traces, you could use the dInits to trip a debug dump of current state if you went a certain direction in code you weren’t expecting.. made working with me infinitely quicker.

    Using a combination of:
    dInit
    dStart
    dPrint
    dPrintArray
    dEnd
    dReset

    would give you:

    {
        *************We have lighting data to add ************
        Contents of: $cmds
        			lightingChunkGroupVis ( 1, "ff_interior_lightingData" )
        			lightingChunkGroupVis ( 0, "ff_interior_lightingData" )
        createDialOutGui
        {
            Frame_ff_interior_lighting
        }
        createLevelChunkButton
        {
            Creating button for:
            					x_FrostFire --> ff_interior_sh
            					parentUi --> ff_interior_lighting_column
            ff_interior_sh_visCheckBox
            chunkButtonStates
            {
                ff_interior_sh
            }
        }
        lightingDataUiVisability
        {
            currentWorkSpace
            {
            }
            getSceneRefList
            {
                in getAtgiLevelReferenceNodes
                {
                }
            }
            levelDialOutStates
            {
                lighting->
            }
            setLightingChunkVis
            {
                getLightingChunkReferences
                {
                    $chunk -->
                    Contents of: $lightingChunks
                    getSceneRefList
                    {
                        in getAtgiLevelReferenceNodes
                        {
                        }
                    }
                }
                Contents of: $allLightingChunks
                			ff_interior_sh
                $lchunk ->ff_interior_sh
                $visCheckBox ->ff_interior_sh_visCheckBox
                showChunk
                {
                    whichNode
                    {
                    }
                }
            }
    
  3. Robert Hexter Says:

    Of course if your site maintained the indentation the dump at the bottom would be tabbed as the code stepped in and out of procs

  4. Rick Says:

    Cheers Robert! I did a quick edit on your comment to make the code easier to read by wrapping it <pre>

Thoughts on this?