Python API 2.0 Reference
python/api1/py1CustomNodeFileTranslator.py
1 #-
2 # ==========================================================================
3 # Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All
4 # rights reserved.
5 #
6 # The coded instructions, statements, computer programs, and/or related
7 # material (collectively the "Data") in these files contain unpublished
8 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
9 # licensors, which is protected by U.S. and Canadian federal copyright
10 # law and by international treaties.
11 #
12 # The Data is provided for use exclusively by You. You have the right
13 # to use, modify, and incorporate this Data into other products for
14 # purposes authorized by the Autodesk software license agreement,
15 # without fee.
16 #
17 # The copyright notices in the Software and this entire statement,
18 # including the above license grant, this restriction and the
19 # following disclaimer, must be included in all copies of the
20 # Software, in whole or in part, and all derivative works of
21 # the Software, unless such copies or derivative works are solely
22 # in the form of machine-executable object code generated by a
23 # source language processor.
24 #
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
26 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
27 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
28 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
29 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
30 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
31 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
32 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
33 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
34 # OR PROBABILITY OF SUCH DAMAGES.
35 #
36 # ==========================================================================
37 #+
38 
39 ########################################################################
40 # DESCRIPTION:
41 #
42 # Produces the file translator "spCustomNodeTranslator".
43 #
44 # This example implements a simple input and output file translator. The writer
45 # functionality of the translator searches the Maya scene for proxy nodes and
46 # writes out the name information to the file. The reader functionality of the
47 # translator simply reads the file in and displays each line.
48 #
49 # To use this file translator, first load the plug-in, then create some proxy nodes
50 # such as spSineNode. Then, use File > Export All to save the proxy node information
51 # by selecting the "Files of Type" option "spCustomNodeTranslator".
52 #
53 # You can select File > Import to read the information back in. When the file is read
54 # back in, output will be sent to the History panel of the Script Editor.
55 #
56 ########################################################################
57 
58 # import maya
59 # maya.cmds.loadPlugin("customNodeFileTranslator.py")
60 
61 from builtins import next
62 import math, sys
63 
64 import maya.OpenMaya as OpenMaya
65 import maya.OpenMayaMPx as OpenMayaMPx
66 
67 kPluginTranslatorTypeName = "spCustomNodeTranslator"
68 
69 # Node definition
70 class customNodeTranslator(OpenMayaMPx.MPxFileTranslator):
71  def __init__(self):
72  OpenMayaMPx.MPxFileTranslator.__init__(self)
73  def haveWriteMethod(self):
74  return True
75  def haveReadMethod(self):
76  return True
77  def filter(self):
78  return "*.spcnt"
79  def defaultExtension(self):
80  return "spcnt"
81  def writer( self, fileObject, optionString, accessMode ):
82  #
83  try:
84  fullName = fileObject.resolvedFullName()
85  fileHandle = open(fullName,"w")
86  fileHandle.write("# Simple text file of custom node information\n")
87 
89  while not iterator.isDone():
90  object = iterator.thisNode()
91  #
92  try:
93  dnFn = OpenMaya.MFnDependencyNode( object )
94  userNode = dnFn.userNode()
95  if ( not( userNode == None ) ):
96  line = "# custom node: " + dnFn.name() + "\n"
97  fileHandle.write(line)
98  except:
99  pass
100  next(iterator)
101  fileHandle.close()
102  except:
103  sys.stderr.write( "Failed to write file information\n")
104  raise
105  def processLine( self, lineStr ):
106  # Normally do parsing here. Simple example will only
107  # print out the line.
108  print("read <%s>" % lineStr)
109  def reader( self, fileObject, optionString, accessMode ):
110  #
111  try:
112  fullName = fileObject.resolvedFullName()
113  fileHandle = open(fullName,"r")
114  for line in fileHandle:
115  # print line
116  self.processLine( line )
117  # print "1"
118  fileHandle.close()
119  except:
120  sys.stderr.write( "Failed to read file information\n")
121  raise
122 
123 
124 # creator
125 def translatorCreator():
126  return OpenMayaMPx.asMPxPtr( customNodeTranslator() )
127 
128 # initialize the script plug-in
129 def initializePlugin(mobject):
130  mplugin = OpenMayaMPx.MFnPlugin(mobject)
131  try:
132  mplugin.registerFileTranslator( kPluginTranslatorTypeName, None, translatorCreator )
133  except:
134  sys.stderr.write( "Failed to register translator: %s" % kPluginTranslatorTypeName )
135  raise
136 
137 # uninitialize the script plug-in
138 def uninitializePlugin(mobject):
139  mplugin = OpenMayaMPx.MFnPlugin(mobject)
140  try:
141  mplugin.deregisterFileTranslator( kPluginTranslatorTypeName )
142  except:
143  sys.stderr.write( "Failed to deregister translator: %s" % kPluginTranslatorTypeName )
144  raise
145