scripted/customNodeFileTranslator.py

scripted/customNodeFileTranslator.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 import math, sys
62 
63 import maya.OpenMaya as OpenMaya
64 import maya.OpenMayaMPx as OpenMayaMPx
65 
66 kPluginTranslatorTypeName = "spCustomNodeTranslator"
67 
68 # Node definition
69 class customNodeTranslator(OpenMayaMPx.MPxFileTranslator):
70  def __init__(self):
71  OpenMayaMPx.MPxFileTranslator.__init__(self)
72  def haveWriteMethod(self):
73  return True
74  def haveReadMethod(self):
75  return True
76  def filter(self):
77  return "*.spcnt"
78  def defaultExtension(self):
79  return "spcnt"
80  def writer( self, fileObject, optionString, accessMode ):
81  #
82  try:
83  fullName = fileObject.resolvedFullName()
84  fileHandle = open(fullName,"w")
85  fileHandle.write("# Simple text file of custom node information\n")
86 
88  while not iterator.isDone():
89  object = iterator.thisNode()
90  #
91  try:
92  dnFn = OpenMaya.MFnDependencyNode( object )
93  userNode = dnFn.userNode()
94  if ( not( userNode == None ) ):
95  line = "# custom node: " + dnFn.name() + "\n"
96  fileHandle.write(line)
97  except:
98  pass
99  iterator.next()
100  fileHandle.close()
101  except:
102  sys.stderr.write( "Failed to write file information\n")
103  raise
104  def processLine( self, lineStr ):
105  # Normally do parsing here. Simple example will only
106  # print out the line.
107  print "read <%s>" % lineStr
108  def reader( self, fileObject, optionString, accessMode ):
109  #
110  try:
111  fullName = fileObject.resolvedFullName()
112  fileHandle = open(fullName,"r")
113  for line in fileHandle:
114  # print line
115  self.processLine( line )
116  # print "1"
117  fileHandle.close()
118  except:
119  sys.stderr.write( "Failed to read file information\n")
120  raise
121 
122 
123 # creator
124 def translatorCreator():
125  return OpenMayaMPx.asMPxPtr( customNodeTranslator() )
126 
127 # initialize the script plug-in
128 def initializePlugin(mobject):
129  mplugin = OpenMayaMPx.MFnPlugin(mobject)
130  try:
131  mplugin.registerFileTranslator( kPluginTranslatorTypeName, None, translatorCreator )
132  except:
133  sys.stderr.write( "Failed to register translator: %s" % kPluginTranslatorTypeName )
134  raise
135 
136 # uninitialize the script plug-in
137 def uninitializePlugin(mobject):
138  mplugin = OpenMayaMPx.MFnPlugin(mobject)
139  try:
140  mplugin.deregisterFileTranslator( kPluginTranslatorTypeName )
141  except:
142  sys.stderr.write( "Failed to deregister translator: %s" % kPluginTranslatorTypeName )
143  raise
144