Python API 2.0 Reference: scripted/simpleImageFile.py

scripted/simpleImageFile.py
1 from __future__ import division
2 #-
3 # ==========================================================================
4 # Copyright (C) 1995 - 2006 Autodesk, Inc. and/or its licensors. All
5 # rights reserved.
6 #
7 # The coded instructions, statements, computer programs, and/or related
8 # material (collectively the "Data") in these files contain unpublished
9 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
10 # licensors, which is protected by U.S. and Canadian federal copyright
11 # law and by international treaties.
12 #
13 # The Data is provided for use exclusively by You. You have the right
14 # to use, modify, and incorporate this Data into other products for
15 # purposes authorized by the Autodesk software license agreement,
16 # without fee.
17 #
18 # The copyright notices in the Software and this entire statement,
19 # including the above license grant, this restriction and the
20 # following disclaimer, must be included in all copies of the
21 # Software, in whole or in part, and all derivative works of
22 # the Software, unless such copies or derivative works are solely
23 # in the form of machine-executable object code generated by a
24 # source language processor.
25 #
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
27 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
28 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
29 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
30 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
31 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
32 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
33 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
34 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
35 # OR PROBABILITY OF SUCH DAMAGES.
36 #
37 # ==========================================================================
38 #+
39 
40 ########################################################################
41 # DESCRIPTION:
42 #
43 # Produces a simple image file plug-in.
44 #
45 # This plug-in registers a new image file format against file extension ".moo".
46 # Loading any ".moo" image file will produce a procedurally generated colour
47 # spectrum including values outside of 0 to 1.
48 #
49 # Usage:
50 # (1) Run the script:
51 #
52 # import maya.cmds
53 # maya.cmds.loadPlugin("simpleImageFile.py")
54 #
55 # (2) Perform the following:
56 #
57 # - Create a poly plane.
58 # - Assign a shader to it.
59 # - Assign a file texture to the shader.
60 # - Make a copy of a project/image file and name it test.moo.
61 # - Assign the test.moo image to the file texture.
62 # - Turn on hardware texturing.
63 # - Render the scene.
64 #
65 ########################################################################
66 
67 from builtins import range
68 import maya.OpenMaya as OpenMaya
69 import maya.OpenMayaMPx as OpenMayaMPx
70 import maya.OpenMayaRender as OpenMayaRender
71 
72 import sys
73 
74 kImagePluginName = "spSimpleImageFile"
75 
76 # Use MGLFunctionTable to make openGL calls
77 glRenderer = OpenMayaRender.MHardwareRenderer.theRenderer()
78 glFT = glRenderer.glFunctionTable()
79 
80 
81 # Class definition
82 class SimpleImageFile(OpenMayaMPx.MPxImageFile):
83 
84  # init
85  def __init__(self):
86  OpenMayaMPx.MPxImageFile.__init__(self)
87 
88  #
89  # DESCRIPTION:
90  # Configure the image characteristics. A real image file
91  # format plugin would extract these values from the image
92  # file header.
93  #
94  #################################################################
95  def open( self, pathname, info ):
96 
97  if info is not None:
98  info.width( 512 )
99  info.height( 512 )
100  info.channels( 3 )
101  info.pixelType( OpenMaya.MImage.kFloat )
102 
103  # Only necessary if your format defines a native
104  # hardware texture loader
105  info.hardwareType( OpenMaya.MImageFileInfo.kHwTexture2D)
106 
107  #
108  # DESCRIPTION:
109  # Load the contents of this image file into an MImage. A real
110  # file format plugin would extract the pixel data from the image
111  # file here.
112  #
113  #################################################################
114  def load( self, image, idx ):
115  width = 512
116  height = 512
117 
118  # Create a floating point image and fill it with
119  # a pretty rainbow test image.
120  #
121  image.create( width, height, 3, OpenMaya.MImage.kFloat )
122  self.populateTestImage( image.floatPixels(), width, height )
123 
124  #
125  # DESCRIPTION:
126  # Load the contents of this image file into an OpenGL texture. A
127  # real file format plugin would extract the pixel data from the
128  # image file here.
129  #
130  #################################################################
131  def glLoad( self, info, imageNumber ):
132  w = info.width()
133  h = info.height()
134 
135  # Create a floating point image and fill it with
136  # a pretty rainbow test image.
137  #
138  image = OpenMaya.MImage()
139  image.create( w, h, 3, OpenMaya.MImage.kFloat )
140  self.populateTestImage( image.floatPixels(), w, h )
141 
142  # Now load it into OpenGL as a floating point image
143  glFT.glTexImage2D( OpenMayaRender.MGL_TEXTURE_2D, 0, \
144  OpenMayaRender.MGL_RGB, w, h, 0, OpenMayaRender.MGL_RGB, \
145  OpenMayaRender.MGL_FLOAT, pixelsPtr )
146 
147 
148  #
149  # DESCRIPTION:
150  # Helper method to populate our procedural test image
151  #
152  #################################################################
153  def populateTestImage( self, pixels, w, h ):
154  #
155  rainbowScale = 4.0
156  index = 0
157  for y in range( 0, h ):
158  g = rainbowScale * y / float(h)
159  for x in range( 0, w ):
160  r = rainbowScale * x / float(w)
161  OpenMaya.MScriptUtil.setFloatArray( pixels, index, r )
162  index+=1
163  OpenMaya.MScriptUtil.setFloatArray( pixels, index, g )
164  index+=1
165  b = rainbowScale * 1.5 - r - g
166  OpenMaya.MScriptUtil.setFloatArray( pixels, index, b )
167  index+=1
168 
169 # creator
170 def creator():
171  return OpenMayaMPx.asMPxPtr( SimpleImageFile() )
172 
173 
174 # initialize plugin
175 def initializePlugin( mObject ):
176  mPlugin = OpenMayaMPx.MFnPlugin(mObject, "Autodesk", "1.0", "Any")
177  #
178  extensions = ["moo"]
179  try:
180  mPlugin.registerImageFile( kImagePluginName, creator, extensions )
181  except:
182  sys.stderr.write( "Failed to register image plugin: %s" % kImagePluginName )
183  raise
184 
185 # uninitialize plugin
186 def uninitializePlugin( mObject ):
187  mPlugin = OpenMayaMPx.MFnPlugin( mObject )
188  try:
189  mPlugin.deregisterImageFile( kImagePluginName )
190  except:
191  sys.stderr.write( "Failed to deregister image: %s" % kImagePluginName )
192  raise