Tutorial 2 - Adding NumPy, an external component

In this tutorial we add the NumPy module to the pyramid project created in Tutorial 1. This tutorial requires that the previous one is completed.

The code of the completed tutorial can be found at the end of this topic for reference.

Installing NumPy

We will, in another tutorial, package our pyramid component as a pip package and this will allow us to declare its dependencies and automatically install them.

But at this point the pyramid project is just a bunch of files in a directory and we need to install its dependencies manually.

First, install numpy by running: .\python.exe -m pip install numpy in your 3ds Max Python installation

(full details here.)

A Note on Pip

The installation of pip for 3ds Max is explained here.

When pip is installed with --user, it ends up in /Users/myusername/AppData/Roaming/Python/Python39/Scripts/pip so when we say that after installing pip we want to make sure we use this version by using pip in a path that looks like this:

/Users/myusername/AppData/Roaming/Python/Python39/Scripts/pip install --user something

If we don't use the complete path, there can be other pip installations on the system and pip install could invoke one of these and either fail or end up at a different location.

When packages are installed with --user for the Python3 3ds Max interpreter, they should end up in /Users/myusername/AppData/Roaming/Python/Python39/site-packages otherwise it will be impossible to import them.

Ultimately what's important is to use the right pip, the one installed with 3ds Max.

Using NumPy in our Project

We will add this to graphics.py:

Note: what we do here with numpy is not very useful, the goal is to keep the sample very small.
"""
    Provide the graphic functionality for the pryamid tool.
"""
from pymxs import runtime as rt
import numpy as np

def make_pyramid_mesh(side=20.0):
    '''Construct a pyramid from vertices and faces.'''
    halfside = side / 2.0

    npa = np.array([
        [0.0, 0.0, side],
        [-halfside, -halfside, 0.0],
        [-halfside, halfside, 0.0],
        [halfside, 0.0, 0.0]])

    pyr = rt.mesh(
        vertices=list(map(lambda l: rt.point3(*l.tolist()), npa)),
        faces=[
            rt.point3(1, 2, 3),
            rt.point3(1, 3, 4),
            rt.point3(1, 4, 2),
            rt.point3(2, 3, 4),
        ])
    rt.redrawViews()
    return pyr

Load the tutorial (If we closed 3ds Max between the two tutorials)

Enter these lines on the listener - all lines need to be entered as separate lines followed by Ctrl+Enter.

import sys
sys.path += [ "d:\\sources\\hacking\\python\\pyramid" ]
import pyramid.ui

Or if the code is already imported from the previous tutorial:

importlib.reload(pyramid.graphics)
importlib.reload(pyramid.ui)

Run the tutorial

dialog = pyramid.ui.PyMaxDialog()
dialog.show()

We have now modified our pyramid sample to use NumPy. In the next tutorial we will show how to package it with pip.

Full Code

graphics.py:


"""
    Provide the graphic functionality for the pryamid tool.
"""
from pymxs import runtime as rt
import numpy as np

def make_pyramid_mesh(side=20.0):
    '''Construct a pyramid from vertices and faces.'''
    halfside = side / 2.0

    npa = np.array([
        [0.0, 0.0, side],
        [-halfside, -halfside, 0.0],
        [-halfside, halfside, 0.0],
        [halfside, 0.0, 0.0]])

    pyr = rt.mesh(
        vertices=list(map(lambda l: rt.point3(*l.tolist()), npa)),
        faces=[
            rt.point3(1, 2, 3),
            rt.point3(1, 3, 4),
            rt.point3(1, 4, 2),
            rt.point3(2, 3, 4),
        ])
    rt.redrawViews()
    return pyr