Source code for cgl.plugins.maya.setup
import os
from cgl.core.config.edit import edit_user_config
from cgl.core.config.query import get_user_dir, AlchemyConfigManager
from cgl.core.utils.general import cgl_copy
from pathlib import Path
CFG = AlchemyConfigManager()
[docs]
def copy_setup():
"""
Copies the usersetup.py script from the local code location to the maya documents dir
"""
from cgl.core.config.query import get_user_dir
user_profile = Path(get_user_dir())
code_root = CFG.get_code_root()
user_setup_path = Path(code_root) / "cgl"/ "plugins"/ "maya" / "userSetup.py"
maya_scripts_folder = user_profile / "maya" / "scripts"
print("Copying CGL UserSetup.py to {}".format(maya_scripts_folder))
maya_scripts_path = maya_scripts_folder / "userSetup.py"
if not maya_scripts_folder.exists():
maya_scripts_folder.mkdir()
if not maya_scripts_path.exists():
print(
"Copying userSetup.py to maya scripts folder: {}".format(maya_scripts_path)
)
cgl_copy(user_setup_path.as_posix(), maya_scripts_path.as_posix())
# TODO - we should add a button that would copy this on demand inside maya.
[docs]
def get_mayapy_path(version=None):
"""
Returns the mayapy path for the given maya version
"""
maya_version = version
program_files = os.getenv("PROGRAMFILES")
mayapy_path = os.path.join(
program_files, "Autodesk", f"Maya{maya_version}", "bin", "mayapy.exe"
)
if os.path.exists(mayapy_path):
return mayapy_path
else:
raise Exception(f"Maya version: {maya_version} not found at {mayapy_path}")
[docs]
def get_maya_versions():
"""
Returns the maya versions available on the system
"""
program_files = os.getenv("PROGRAMFILES")
maya_versions = []
for folder in os.listdir(os.path.join(program_files, "Autodesk")):
if "Maya" in folder:
# try to match for a 4 digit number, if it's there extract it
if folder[-4:].isdigit():
year = folder[-4:]
if year not in maya_versions:
maya_versions.append(year)
return maya_versions
[docs]
def install_requirements(version=2024):
"""
Installs the requirements.txt file in the magic_browser folder using mayapy.
"""
print("Installing Maya requirements")
maya_version = version
program_files = os.getenv("PROGRAMFILES")
code_root = CFG.get_code_root()
mayapy_path = os.path.join(
program_files, "Autodesk", f"Maya{maya_version}", "bin", "mayapy.exe"
)
if os.path.exists(mayapy_path):
requirements_path = os.path.join(
code_root,
"cgl",
"plugins",
"maya",
"maya_setup",
f"{version}requirements.txt",
)
print(requirements_path)
if not os.path.exists(requirements_path):
requirements_path = os.path.join(
code_root, "requirements", f"{version}requirements.txt"
)
os.chdir(code_root)
command = f'"{mayapy_path}" -m pip install -r {requirements_path}'
print(command)
os.system(command)
else:
raise Exception(f"Maya version: {maya_version} not found at {mayapy_path}")
[docs]
def write_env_file(version=None):
"""
UPdates the maya.env file with the magic_browser code root and config root
Returns:
"""
if version:
user_profile = get_user_dir()
# get the documents folder path from windows
code_root = os.path.join(
user_profile, "Documents", "magic_browser", f"Maya{version}"
)
config_root = os.path.join(code_root, "config")
env_folder = os.path.join(user_profile, "Documents", "maya", version)
if not os.path.exists(env_folder):
os.makedirs(env_folder)
env_file_path = os.path.join(env_folder, "Maya.env")
print("Editing env file at: {}".format(env_file_path))
with open(env_file_path, "w") as f:
f.write(f"PYTHONPATH= {code_root};{config_root}")
f.close()
edit_user_config(["software", f"Maya {version}"], env_file_path)
[docs]
def update_user_config(version=None):
"""
UPdates the user config file with the path to the maya env file and the year:
user_config()['software']['maya'][version]['maya_env_file'] = maya_env_file
Returns:
"""
maya_version = version.split()[-1]
user_profile = os.getenv("USERPROFILE")
env_folder = os.path.join(user_profile, "Documents", "maya", maya_version)
env_file_path = os.path.join(env_folder, "Maya.env")
# user_config = ProjectConfig.getLastConfig().user_config
# user_config["software"][maya_version]["maya_env_file"] = env_file_path
edit_user_config(["software", version.lower()], env_file_path)
[docs]
def setup_env():
"""
Sets up the maya python path to contain the alchemy_build_path if it exists, or the alchemy_dev_path if it exists
Returns:
"""
import sys
alchemy_dev_path = CFG.code_root
if alchemy_dev_path and os.path.exists(alchemy_dev_path):
sys.path.append(alchemy_dev_path)
[docs]
def clean_env():
"""
Cleans the maya python path
"""
import sys
user_profile = os.getenv("USERPROFILE")
alchemy_dev_path = CFG.code_root
if alchemy_dev_path and os.path.exists(alchemy_dev_path):
for path_ in sys.path:
if path_ == alchemy_dev_path:
sys.path.remove(path_)
[docs]
def setup_maya(version):
install_requirements(version=version)
# copy_setup()
if __name__ == "__main__":
# mayas = get_maya_versions()
# print(mayas)
setup_maya(version="2026")
# install_requirements(version=2025)
# print(get_user_dir())