Last active
December 14, 2022 22:04
-
-
Save azrafe7/3596af2b9caa1867bdca25502992a52b to your computer and use it in GitHub Desktop.
protect maya files
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
| # Credits to Farsheed Ashouri (https://www.highend3d.com/maya/script/protect-for-maya) | |
| # Code adapted and improved from there | |
| import maya.cmds as cmds | |
| import zlib | |
| import base64 | |
| import hashlib | |
| # SETUP | |
| password = 'letMeIn' # un-hashed password | |
| passwordHash = hashlib.sha512(password).hexdigest() | |
| importScriptName = 'DBLE' | |
| protectScriptName = 'cameraConfigurationScriptNode' | |
| # actual payload to run (it will be encrypted) | |
| codeToEncrypt = ''' | |
| import maya.cmds as cmds | |
| import hashlib | |
| try: | |
| pwd = raw_input() | |
| if hashlib.sha512(pwd).hexdigest() == '{0}': | |
| pass | |
| else: | |
| cmds.file(new=1, f=1) | |
| except: | |
| cmds.file(new=1, f=1) | |
| '''.format(passwordHash) | |
| encryptedCode = base64.b64encode(zlib.compress(codeToEncrypt)) | |
| # SCRIPT NODES | |
| # remove script nodes if already present | |
| for scriptNode in cmds.ls('*', type='script'): | |
| # (Maya adds a uiConfigurationScriptNode and a sceneConfigurationScriptNode, skip those) | |
| if scriptNode in ['uiConfigurationScriptNode', 'sceneConfigurationScriptNode']: | |
| continue | |
| print "Removing script node '" + scriptNode + "'..." | |
| cmds.lockNode(scriptNode, lock=False) | |
| cmds.delete(scriptNode) | |
| # node that imports modules and obfuscates function names | |
| print "Adding script node '{0}'...".format(importScriptName) | |
| scriptNode = cmds.createNode('script', name=importScriptName) | |
| command = ''' | |
| #import maya.cmds as cmds | |
| from zlib import decompress as _zDec_ # | |
| from base64 import b64decode as _decParams_ # | |
| def _configureExCamera(cam):exec(cam) # exemplify cam config | |
| # Create a camera and get the shape name. | |
| #cameraName = cmds.camera() | |
| #cameraShape = cameraName[1] | |
| # Get the focal length of the camera. | |
| #focalLength = cmds.camera(cameraShape, q=True, fl=True) | |
| # Change the film fit type. | |
| #cmds.camera( cameraShape, e=True, ff='overscan' ) | |
| ''' | |
| cmds.setAttr(importScriptName + ".before", 'python("' + command.strip().replace("\n", "\\n") + '");', type="string") | |
| cmds.setAttr(importScriptName + ".before", lock=True) | |
| cmds.setAttr(importScriptName + ".sourceType", 0) # 0 - Mel, 1 - Python | |
| cmds.setAttr(importScriptName + ".sourceType", lock=True) | |
| cmds.setAttr(importScriptName + ".scriptType", 1) | |
| cmds.setAttr(importScriptName + ".scriptType", lock=True) | |
| cmds.select(clear=True) | |
| cmds.lockNode(scriptNode) | |
| # node that decrypts and executes the payload | |
| print "Adding script node '{0}'...".format(protectScriptName) | |
| scriptNode = cmds.createNode('script', name=protectScriptName) | |
| command = '''_configureExCamera(_zDec_(_decParams_("{0}")))'''.format(encryptedCode) | |
| cmds.setAttr(protectScriptName + ".before", command, type="string") | |
| cmds.setAttr(protectScriptName + ".before", lock=True) | |
| cmds.setAttr(protectScriptName + ".sourceType", 1) # 0 - Mel, 1 - Python | |
| cmds.setAttr(protectScriptName + ".sourceType", lock=True) | |
| cmds.setAttr(protectScriptName + ".scriptType", 1) | |
| cmds.setAttr(protectScriptName + ".scriptType", lock=True) | |
| cmds.select(clear=True) | |
| cmds.lockNode(scriptNode) | |
| # now save the file as Maya binary, and try reopening it | |
| print "Script nodes added (password: '{0}', hash: '{1}')".format(password, passwordHash) | |
| cmds.ls("*", type="script") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment