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 # import maya
40 # maya.cmds.loadPlugin("customNodeFileTranslator.py")
41 
42 import math, sys
43 
44 import maya.OpenMaya as OpenMaya
45 import maya.OpenMayaMPx as OpenMayaMPx
46 
47 kPluginTranslatorTypeName = "spCustomNodeTranslator"
48 
49 # Node definition
50 class customNodeTranslator(OpenMayaMPx.MPxFileTranslator):
51  def __init__(self):
52  OpenMayaMPx.MPxFileTranslator.__init__(self)
53  def haveWriteMethod(self):
54  return True
55  def haveReadMethod(self):
56  return True
57  def filter(self):
58  return "*.spcnt"
59  def defaultExtension(self):
60  return "spcnt"
61  def writer( self, fileObject, optionString, accessMode ):
62  #
63  try:
64  fullName = fileObject.fullName()
65  fileHandle = open(fullName,"w")
66  fileHandle.write("# Simple text file of custom node information\n")
67 
68  iterator=OpenMaya.MItDependencyNodes()
69  while not iterator.isDone():
70  object = iterator.thisNode()
71  #
72  try:
73  dnFn = OpenMaya.MFnDependencyNode( object )
74  userNode = dnFn.userNode()
75  if ( not( userNode == None ) ):
76  line = "# custom node: " + dnFn.name() + "\n"
77  fileHandle.write(line)
78  except:
79  pass
80  iterator.next()
81  fileHandle.close()
82  except:
83  sys.stderr.write( "Failed to write file information\n")
84  raise
85  def processLine( self, lineStr ):
86  # Normally do parsing here. Simple example will only
87  # print out the line.
88  print "read <%s>" % lineStr
89  def reader( self, fileObject, optionString, accessMode ):
90  #
91  try:
92  fullName = fileObject.fullName()
93  fileHandle = open(fullName,"r")
94  for line in fileHandle:
95  # print line
96  self.processLine( line )
97  # print "1"
98  fileHandle.close()
99  except:
100  sys.stderr.write( "Failed to read file information\n")
101  raise
102 
103 
104 # creator
105 def translatorCreator():
106  return OpenMayaMPx.asMPxPtr( customNodeTranslator() )
107 
108 # initialize the script plug-in
109 def initializePlugin(mobject):
110  mplugin = OpenMayaMPx.MFnPlugin(mobject)
111  try:
112  mplugin.registerFileTranslator( kPluginTranslatorTypeName, None, translatorCreator )
113  except:
114  sys.stderr.write( "Failed to register translator: %s" % kPluginTranslatorTypeName )
115  raise
116 
117 # uninitialize the script plug-in
118 def uninitializePlugin(mobject):
119  mplugin = OpenMayaMPx.MFnPlugin(mobject)
120  try:
121  mplugin.deregisterFileTranslator( kPluginTranslatorTypeName )
122  except:
123  sys.stderr.write( "Failed to deregister translator: %s" % kPluginTranslatorTypeName )
124  raise
125