Created
February 6, 2023 17:58
-
-
Save tbttfox/501ea3c32e1179f554cbee2c82b4f6b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import os | |
import shutil | |
import time | |
from maya import cmds | |
RELEASE_TYPES = ["Debug", "RelWithDebInfo", "Release"] | |
def reloadPlugin( | |
folder, | |
plugName, | |
releaseType=None, | |
openFile=None, | |
openFunc=None, | |
srcFmt="mayabuild_{year}/{releaseType}", | |
dstFmt="loaded/{releaseType}_{year}", | |
src=None, | |
dst=None, | |
overrides=None, | |
): | |
""" | |
Close the current file, make a copy of the .mll file you specify, and load it | |
taking into account the current maya version | |
Parameters | |
---------- | |
folder: str | |
The base folder to look for the plugin. Use the `srcFmt` and `dstFmt` to | |
find the actual plugins | |
plugName: str | |
The name of the plugin, without the trailing '.mll' | |
releaseType: str (optional) | |
The releaseType from (Debug, RelWithDebInfo, Release). If this is not specified | |
all three types are checked, and the latest one is loaded | |
openFile: str (optional) | |
If specified, open this file once the plugin is reloaded | |
openFunc: callable (optional) | |
If specified, run this function once the plugin is reloaded | |
src: str (optional) | |
If provided, override the the source .mll file to copy | |
dst: str (optional) | |
If provided, Copy the source to this location to load from | |
overrides: dict (optional) | |
Provide any extra keyword arguments for the srcFmt or dstFmt | |
""" | |
overrides = overrides or {} | |
year = cmds.about(version=1) | |
if src is None: | |
if releaseType is None: | |
possSrc = [] | |
times = [] | |
rts = [] | |
for rt in RELEASE_TYPES: | |
src = os.path.join( | |
folder, srcFmt.format(releaseType=rt, year=year, plugName=plugName, **overrides) | |
) | |
path = os.path.join(src, "{0}.mll".format(plugName)) | |
if not os.path.isfile(path): | |
continue | |
possSrc.append(src) | |
times.append(os.path.getmtime(path)) | |
rts.append(rt) | |
if not possSrc: | |
print("Checking Sources:", possSrc) | |
raise IOError("Cannot find a valid .mll plugin") | |
idx = times.index(max(times)) | |
releaseType = rts[idx] | |
src = possSrc[idx] | |
else: | |
src = os.path.join( | |
folder, srcFmt.format(releaseType=releaseType, year=year, **overrides) | |
) | |
if dst is None: | |
dst = os.path.join(folder, "loaded", "{0}_{1}".format(releaseType, year)) | |
# Apparently maya 2020 has a problem with keeping the .mll files loaded | |
# So instead of overwriting, I have to create new folders for the loaded plugins | |
dpar = os.path.dirname(dst) | |
if not os.path.isdir(dpar): | |
os.makedirs(dpar) | |
for i in range(200): | |
newdst = "{0}_{1}".format(dst, i) | |
if not os.path.isdir(newdst): | |
os.makedirs(newdst) | |
dst = newdst | |
break | |
else: | |
raise RuntimeError("You've made over 200 iterations. Clean up after yourself") | |
cmds.file(force=True, newFile=True) | |
cmds.unloadPlugin("{0}.mll".format(plugName), force=True) | |
time.sleep(0.5) | |
print("Copying from", src) | |
toLoad = [] | |
for fn in os.listdir(src): | |
p = os.path.join(src, fn) | |
if p.endswith(".mll"): | |
toLoad.append(os.path.join(dst, fn)) | |
shutil.copy(p, dst) | |
if p.endswith((".cl", ".ilk", ".lib", ".exp", ".pdb")): | |
shutil.copy(p, dst) | |
clPath = os.path.join(folder, "{0}.cl".format(plugName)) | |
if os.path.isfile(clPath): | |
shutil.copy(clPath, dst) | |
for mll in toLoad: | |
print("Loading Plugin", mll) | |
cmds.loadPlugin(mll) | |
if openFile is not None: | |
typ = "mayaBinary" | |
if openFile[-1] == "a": | |
typ = "mayaAscii" | |
cmds.file(openFile, force=1, typ=typ, o=1) | |
if openFunc is not None: | |
openFunc() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment