Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created October 19, 2017 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRoy/38ca6118c848091ef1c14229d5b4fad4 to your computer and use it in GitHub Desktop.
Save BigRoy/38ca6118c848091ef1c14229d5b4fad4 to your computer and use it in GitHub Desktop.
This is a custom MPxFileTranslator that can be loaded as plug-in into Maya. With this loaded as plug-in you can reference any `.txt` file on disk. To make this work save a `.ma` scene next to the text file with the same name and reference the txt file. What should happen is that the `.ma` should be loaded three times within a single reference no…
"""This is a custom MPxFileTranslator that can be loaded as plug-in into Maya.
With this loaded as plug-in you can reference any `.txt` file on disk.
To make this work save a `.ma` scene next to the text file with the same name
and reference the txt file. What should happen is that the `.ma` should be
loaded three times within a single reference node in Maya.
Note: it crashes in Maya 2018 when saving the scene and then reopening the scene.
"""
import maya.OpenMayaMPx as omMPx
import maya.cmds as cmds
import os
import logging
log = logging.getLogger("CustomTranslator")
class CustomTranslator(omMPx.MPxFileTranslator):
"""Custom MPxFileTranslator
Reference a .txt file that is next to a .ma file.
Then this should reference the .ma file three times
as if it's the .txt file.
"""
typeName = "custom"
extension = "txt"
def __init__(self):
omMPx.MPxFileTranslator.__init__(self)
def haveReadMethod(self):
return True
def haveNamespaceSupport(self):
return True
def defaultExtension(self):
return self.extension
def filter(self):
return "*.{0}".format(self.extension)
@classmethod
def creator(cls):
return omMPx.asMPxPtr(cls())
def identifyFile(self, fileObject, buffer, bufferLen):
# Ensure filename ends with extension
fname = fileObject.resolvedName()
ext = ".{0}".format(self.extension)
if fname.endswith(ext):
return self.kIsMyFileType
log.info("Not my file type")
return self.kNotMyFileType
def reader(self, fileObject, optionString, accessMode):
fullPath = fileObject.resolvedFullName()
ma_path = fullPath[:-(len(self.extension) + 1)] + ".ma"
# Reference the other file 3 times (for testing)
cmds.file(ma_path, i=True, namespace="a", returnNewNodes=True)
cmds.file(ma_path, i=True, namespace="b", returnNewNodes=True)
cmds.file(ma_path, i=True, namespace="c", returnNewNodes=True)
return True
def initializePlugin(obj):
plugin = omMPx.MFnPlugin(obj, "Roy Nieterau", "0.1")
# Register
try:
plugin.registerFileTranslator(
JSONTranslator.typeName,
None,
JSONTranslator.creator,
None,
None,
True
)
except RuntimeError, e:
mc.warning("Can't register plug-in node "
"{0}: {1}".format(JSONTranslator.typeName, e))
def uninitializePlugin(obj):
plugin = omMPx.MFnPlugin(obj)
# Deregister
try:
plugin.deregisterFileTranslator(
JSONTranslator.typeName
)
except RuntimeError, e:
cmds.warning("Can't deregister plug-in node "
"{0}: {1}".format(JSONTranslator.typeName, e))
@david-rhodes
Copy link

I stumbled across this while researching MPxFileTranslators and was wondering if you ever filed this as a bug with Autodesk? Did you find any viable workarounds? Did this work in previous versions of Maya?

I'd really love to use File Translators with our referencing pipeline, but this crash makes that difficult. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment