/maya-mock

Mock for Autodesk Maya cmds and pymel API

Primary LanguagePythonMIT LicenseMIT

maya_mock

Actions codecov Code style: black

A mock for Autodesk Maya and it's cmds and pymel API.

Originally developed to ease testing of omtk2.

Installation

You can install the project with all it's dependencies with pip. Navigate to the root directory and run:

pip install .

Testing

To run the tests you'll need to install tox.

pip install tox

Then assuming you have python-2, python-3 and maya-2017 on your system, you can run all checks with:

tox

Tox tips

You can run individual checks with the -e flag. See the tox.ini file for the available targets.

tox -e unit-py2.7

You can also pass additional arguments to your tests with the -- flag. For example, here's how to run a single test named test_node_shape_transform_melobject in python-2.7:

tox -e unit-py2.7 -- -k test_node_shape_transform_melobject

Usage

You can create a session and interact with it directly.

import maya_mock
session = maya_mock.MockedSession()
session.create_node('transform', name='test')

You can also interact with a session via a binding like cmds or pymel.

import maya_mock
session = maya_mock.MockedSession()
cmds = maya_mock.MockedCmdsSession(session)
cmds.createNode('transform', name='test')
import maya_mock
session = maya_mock.MockedSession()
pymel = maya_mock.MockedPymelSession(session)
pymel.createNode('transform', name='test')

Schemas

A schema is a snapshot of a Maya version.

You can generate a search from a maya session with this command:

from maya_mock import MockedSessionSchema
schema = MockedSessionSchema.generate()

You can also use generate a schema from anywhere with this simple script:

./generate ./schemas/test_schema.json

By default, a mocked session instance is schema-less. This mean that a scene will be truly empty and no node type are registered. Off course, a Maya installation is not as minimal, some objects are pre-registered and an empty scene is not empty.

When initializing a session, you can provide the schema to use:

from maya_mock import MockedSession, MockedSessionSchema
schema = MockedSessionSchema.from_json_file('/path/to.json')
session = MockedSession(schema=schema)

Decorators

You also have access to decorators that will temporary expose a binding. This is usefull if you have want to use the mock with already existing code.

These decorators will temporary patch sys.modules so that any import on maya.cmds or pymel will import the mock.

import maya_mock
from maya_mock.decorators import mock_cmds, mock_pymel

session = maya_mock.MockedSession()

with mock_cmds(session) as cmds:
    pass  # Some code


with mock_pymel(session) as pymel:
    pass  # Some code

Contributing

Any contributions are welcome in the form of a PR!