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