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, reinstall our package and restart 3ds Max.

startup.py

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:

python.execute ("def _python_startup():\n" +
    "    import pkg_resources\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" +
    "            fcn = entrypt.load()\n" +
    "            try:\n" +
    "                fcn()\n" +
    "            except:\n" +
    "                pass\n" +
    "_python_startup()\n" +
    "del _python_startup")

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.