Tutorial 4 - Automatically launching our tool when max starts

In this tutorial we demonstrate how to make a package run during the startup of max.

Adding 3dsMax.startup to our package

Reference

Add the following entry to the setup.py file:

    entry_points={'3dsMax': 'startup=pyramid:startup'},

and rewrite our init.py as:

name = "pyramid"
import pyramid.ui
def startup():
    """Create and show the dialog during 3ds Max startup, provided that
    the pyramid component is present in the pip loading environment at that
    moment."""
    dialog = pyramid.ui.PyMaxDialog()
    dialog.show()

To make this work we need to close 3ds Max, add a script to the startup directory, and restart 3ds Max.

pystartup.ms

For the above code to work we need to have a pystartup.ms file in our \<MaxRoot\>/scripts/Startup directory. Scripts in this directory are automatically run during the startup of 3ds Max.

This file should contain the following MAXScript code:

if isProperty python "execute" then (
    python.execute ("def _python_startup():\n" +
    "    try:\n" +
    "        import pkg_resources\n" +
    "    except ImportError:\n" +
    "        print('startup Python modules require pip to be installed.')\n" +
    "        return\n" +    
    "    for dist in pkg_resources.working_set: \n" +
    "        entrypt = pkg_resources.get_entry_info(dist, '3dsMax', 'startup')\n" +
    "        if not (entrypt is None):\n" +
    "            try:\n" +
    "                fcn = entrypt.load()\n" +
    "                fcn()\n" +
    "            except:\n" +
    "                print('skipped package startup for {}, startup not working'.format(dist))\n" +
    "_python_startup()\n" +
    "del _python_startup")
)

When you re-start 3ds Max, you should see the pyramid dialog.

The advantage of this approach is that python startup scripts are discovered in the active python environment, and therefore work well with virtualenv. We scan the available packages, look for ones that have the 3dsMax startup entry point, and run this entry point.