Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add new setup to allow use nox command launch maya standalone #35

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ run_pycharm.bat

coverage.xml
.zip
tests/virus/_virus/
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ MAYA_UMBRELLA_LOG_LEVEL
修改杀毒后文件的备份文件夹名称, 默认是`_virus`
比如:
你文件路径是 `c:/your/path/file.ma`
那么备份文件路径是 `c:/your/path/_maya_umbrella/file.ma`
那么备份文件路径是 `c:/your/path/_virus/file.ma`
```shell
MAYA_UMBRELLA_BACKUP_FOLDER_NAME
```
Expand Down Expand Up @@ -144,7 +144,7 @@ api = MayaVirusDefender()
print(api.get_unfixed_references())
```

批量修复文件
批量修复文件, 通过正则表达式
```python
from maya_umbrella import MayaVirusScanner

Expand All @@ -153,6 +153,16 @@ print(api.scan_files_from_pattern("your/path/*.m[ab]"))

```

# 案例
如果你想要快速通过maya standalone去批量清理maya文件,可以`下载`或者`git clone`当前`main`分支的工程,
根据上面指引设置好开发环境
通过`nox`命令去启动maya standalone环境,maya版本根据你当前本地安装的maya为准,比如你本地安装了`2018`,
那么就是 `nox -s maya-2018-s`
下面的语法是启动一个maya-2020的环境去动态从`c:/test`文件夹下去查杀病毒
```shell
nox -s maya-2020-s -- c:/test/*.m[ab]
```

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
Expand Down
11 changes: 11 additions & 0 deletions maya_umbrella/maya_funs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import maya.api.OpenMaya as om
import maya.cmds as cmds
import maya.mel as mel
import maya.standalone as maya_standalone
except ImportError:
# Backward compatibility to support test in uinstalled maya.
try:
Expand All @@ -21,8 +22,10 @@
cmds = MagicMock()
om = MagicMock()
mel = MagicMock()
maya_standalone = MagicMock()

# Import built-in modules
from contextlib import contextmanager
from functools import wraps


Expand Down Expand Up @@ -99,6 +102,7 @@ def block_prompt(func):
Returns:
function: The decorated function.
"""

@wraps(func)
def wrap(*args, **kwargs):
# Grabs the initial value.
Expand Down Expand Up @@ -134,3 +138,10 @@ def save_as_file(file_name):
"""
cmds.file(rename=file_name)
cmds.file(s=True, f=True)


@contextmanager
def maya_standalone_context():
maya_standalone.initialize()
yield cmds
maya_standalone.uninitialize()
24 changes: 21 additions & 3 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def _setup_maya(maya_version):
"""Set up the appropriate Maya version for testing."""
try:
import winreg # noqa: F401
import winreg
except ImportError:
return {}
try:
Expand Down Expand Up @@ -190,6 +190,22 @@ def dynamic_session(session: nox.Session):
)


def add_dynamic_maya_standalone_session(maya_version, mayapy, command):
session_name = f"maya-{maya_version}-s"

@nox.session(name=session_name, python=False)
def dynamic_session(session: nox.Session):
parser = argparse.ArgumentParser(prog=f"nox -s maya-{maya_version}-s")
parser.add_argument("pattern", type=str)
args = parser.parse_args(session.posargs)
session.run(
mayapy,
command,
args.pattern,
env={"PYTHONPATH": ROOT},
)


# Dynamic to set up nox sessions for Maya 2018-2026.
# For example, to run tests for Maya 2018, run:
# nox -s maya-2018
Expand All @@ -200,6 +216,8 @@ def dynamic_session(session: nox.Session):
maya_python = os.path.join(maya_setup["bin_root"], "mayapy.exe")
test_runner = os.path.join(ROOT, "tests", "_test_runner.py")
add_dynamic_maya_test_session(maya_version, maya_python, test_runner)
standalone_runner = os.path.join(ROOT, "run_maya_standalone.py")
add_dynamic_maya_standalone_session(maya_version, maya_python, standalone_runner)


@nox.session(name="make-zip")
Expand Down Expand Up @@ -237,6 +255,6 @@ def make_install_zip(session: nox.Session):
for root, _, files in os.walk(build_root):
for file in files:
zip_obj.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file),
os.path.join(build_root, '.')))
os.path.relpath(os.path.join(root, file),
os.path.join(build_root, ".")))
print("Saving to %s" % zip_file)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ exclude = [
"site-packages",
"venv",
"dev",
"maya_umbrella/_vendor/",
"noxfile.py",
"maya_umbrella/_vendor/"
]
line-length = 120

Expand Down
9 changes: 9 additions & 0 deletions run_maya_standalone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from maya_umbrella.maya_funs import maya_standalone_context
from maya_umbrella import MayaVirusScanner
import sys

pattern = sys.argv[-1]
print("Current pattern: {}".format(pattern))
with maya_standalone_context() as cmds:
api = MayaVirusScanner()
api.scan_files_from_pattern(pattern)