MaterialX release distributions include Python sub-modules for editing documents, code generation, and rendering. The sub-modules are wrappers for C++ libraries also shipped with releases.
For content on this site the minimum requirement is MaterialX version 1.38.7 is and Python 3.9.
A Visual Studio Code environment has been chosen to provide tutorials and utilities written in Python. Published content can be directly accessed in .
The following videos and corresponding text instructions can be followed to set up an appropriate environment.
Using MaterialX version 1.38.8 or laterAs of version 1.38.8, MaterialX Python package can be downloaded and install from PyPi.
|
Using MaterialX versions prior to 1.38.8For users who wish to use earlier versions they can:
|
The basic requirements are:
⌘/Ctrl+Shift+P
can be used to run the command
Python: Create Environment
.
virtual environment>
(versus a
Conda
environment).
We will use a simple example file called hello_materialx.py. Add this file to your workspace and attempt to run it.
This will result in an import error if MaterialX is not installed yet.
import MaterialX as mx
ModuleNotFoundError: No module named 'MaterialX'
To install the MaterialX package into the virtual environment a
VSCode workspace terminal (1) can be opened and
pip
used to install from
python -m pip install .
pip install MaterialX
This will add the MaterialX modules to the virtual environment under
.venv/Lib/site-packages.
The modules installed will depend on the ones found in the package location.
Other dependent packages can be added using pip
or other means
as necessary.
(1) A security alert may show up when running from the terminal. If so, follow the the instructions provided to to set up appropriate permissions
Importing MaterialX
will include all sub-modules. This differs from
C++ where individual sub-modules can be loaded.
Shown below is how to import MaterialX with the module name mx.
import MaterialX as mx
At a minimum a document must be created as all operations are based on documents.
To test that the environment works properly the test example creates a document and checks the version of MaterialX.
doc = mx.createDocument()
print('Hello MaterialX (Version %s)' % doc.getVersionString())
To do any useful operations, the standard MaterialX libraries need to be loaded. The load_libraries.py example shows this minimal setup.
import MaterialX as mx
# Create library document and load in default data libraries
stdlib = mx.createDocument()
libFiles = mx.loadLibraries(mx.getDefaultDataLibraryFolders(), mx.getDefaultDataSearchPath(), stdlib)
# Create main document and import the library document
doc = mx.createDocument()
doc.importLibrary(stdlib)
print('Loaded library file: %d' % len(libFiles))
Note that the "standard" libraries are in the
libraries
folder in
the Python package location. The key interfaces used are:
loadLibraries
to load in the standard library definitions.
importLibrary
to import the libraries
into a working / main document.From here, the learning notebooks can be used to discover how to do something useful with MaterialX.
A series of Jupyter learning notebooks are regularly being added and updated. User can clone or copy the repo to run locally, or as of MaterialX 1.38.8, remotely using `Github Codespaces` or `Google Colab`.
See the Hosting page for more details.
Any contribution requests or comments can be posted as pull requests on .
It is recommended to use the MaterialX packages which are available via PyPi which are available starting with version 1.38.8.
For those which may not have access to the execution environment or simply wish to read the notebook pre-evaluated, the results of executing the notebooks are provided in the Examples page. These pages are generated directly from the sample notebooks.
Available Notebooks
A Python file (with markdown), a notebook and a link to content on Github
is given below.
Note that it is possible to start with the Python
itself and convert to a notebook in VSCode or export from a notebook
to Python.
Notebook | Raw Python (py) | Raw Notebook (ipynb) | Notebook in Github |
---|
The notebook files can be read in to Visual Studio Code to run or modify as desired.
Below is an example of the "Basics" notebook after execution inside of VSCode.
Below is a snapshot of Python and Markdown with meta data tags within a Python generated from a notebook.