scripted/simpleImageFile.py

scripted/simpleImageFile.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 # Simple Image File plugin. This plugin registers a new image
42 # file format against file extension ".moo". Loading any ".moo"
43 # image file will produce a procedurally generated colour
44 # spectrum including values outside 0 to 1.
45 #
46 # Usage:
47 # Run the script:
48 #
49 # import maya.cmds
50 # maya.cmds.loadPlugin("simpleImageFile.py")
51 # # Create a poly plane
52 # # Assign a shader to it
53 # # Assign a file texture to the shader
54 # # Make a copy of a project/images file and call it test.moo
55 # # Assign the test.moo image to the file texture
56 # # Turn on hardware texturing
57 
58 import maya.OpenMaya as OpenMaya
59 import maya.OpenMayaMPx as OpenMayaMPx
60 import maya.OpenMayaRender as OpenMayaRender
61 
62 import sys
63 
64 kImagePluginName = "spSimpleImageFile"
65 
66 # Use MGLFunctionTable to make openGL calls
67 glRenderer = OpenMayaRender.MHardwareRenderer.theRenderer()
68 glFT = glRenderer.glFunctionTable()
69 
70 
71 # Class definition
72 class SimpleImageFile(OpenMayaMPx.MPxImageFile):
73 
74  # init
75  def __init__(self):
76  OpenMayaMPx.MPxImageFile.__init__(self)
77 
78  #
79  # DESCRIPTION:
80  # Configure the image characteristics. A real image file
81  # format plugin would extract these values from the image
82  # file header.
83  #
84  #################################################################
85  def open( self, pathname, info ):
86 
87  if info is not None:
88  info.width( 512 )
89  info.height( 512 )
90  info.channels( 3 )
91  info.pixelType( OpenMaya.MImage.kFloat )
92 
93  # Only necessary if your format defines a native
94  # hardware texture loader
95  info.hardwareType( OpenMaya.MImageFileInfo.kHwTexture2D)
96 
97  #
98  # DESCRIPTION:
99  # Load the contents of this image file into an MImage. A real
100  # file format plugin would extract the pixel data from the image
101  # file here.
102  #
103  #################################################################
104  def load( self, image, idx ):
105  width = 512
106  height = 512
107 
108  # Create a floating point image and fill it with
109  # a pretty rainbow test image.
110  #
111  image.create( width, height, 3, OpenMaya.MImage.kFloat )
112  self.populateTestImage( image.floatPixels(), width, height )
113 
114  #
115  # DESCRIPTION:
116  # Load the contents of this image file into an OpenGL texture. A
117  # real file format plugin would extract the pixel data from the
118  # image file here.
119  #
120  #################################################################
121  def glLoad( self, info, imageNumber ):
122  w = info.width()
123  h = info.height()
124 
125  # Create a floating point image and fill it with
126  # a pretty rainbow test image.
127  #
128  image = OpenMaya.MImage()
129  image.create( w, h, 3, OpenMaya.MImage.kFloat )
130  self.populateTestImage( image.floatPixels(), w, h )
131 
132  # Now load it into OpenGL as a floating point image
133  glFT.glTexImage2D( OpenMayaRender.MGL_TEXTURE_2D, 0, \
134  OpenMayaRender.MGL_RGB, w, h, 0, OpenMayaRender.MGL_RGB, \
135  OpenMayaRender.MGL_FLOAT, pixelsPtr )
136 
137 
138  #
139  # DESCRIPTION:
140  # Helper method to populate our procedural test image
141  #
142  #################################################################
143  def populateTestImage( self, pixels, w, h ):
144  #
145  rainbowScale = 4.0
146  index = 0
147  for y in range( 0, h ):
148  g = rainbowScale * y / float(h)
149  for x in range( 0, w ):
150  r = rainbowScale * x / float(w)
151  OpenMaya.MScriptUtil.setFloatArray( pixels, index, r )
152  index+=1
153  OpenMaya.MScriptUtil.setFloatArray( pixels, index, g )
154  index+=1
155  b = rainbowScale * 1.5 - r - g
156  OpenMaya.MScriptUtil.setFloatArray( pixels, index, b )
157  index+=1
158 
159 # creator
160 def creator():
161  return OpenMayaMPx.asMPxPtr( SimpleImageFile() )
162 
163 
164 # initialize plugin
165 def initializePlugin( mObject ):
166  mPlugin = OpenMayaMPx.MFnPlugin(mObject, "Autodesk", "1.0", "Any")
167  #
168  extensions = ["moo"]
169  try:
170  mPlugin.registerImageFile( kImagePluginName, creator, extensions )
171  except:
172  sys.stderr.write( "Failed to register image plugin: %s" % kImagePluginName )
173  raise
174 
175 # uninitialize plugin
176 def uninitializePlugin( mObject ):
177  mPlugin = OpenMayaMPx.MFnPlugin( mObject )
178  try:
179  mPlugin.deregisterImageFile( kImagePluginName )
180  except:
181  sys.stderr.write( "Failed to deregister image: %s" % kImagePluginName )
182  raise