Dean, thank you so much for the information! Very clearly written. I came to that same conclusion and have written the code that can read pixel values at given x,y points using MImage.
The problem I'm having now is a little more (i hope) basic. I posted it in another thread so I'll wait and see if it gets answered there. It's me having trouble retrieving the filename attribute I gavre my node as a readable string. Or more pointedly, getting the readable string value from an MString object. I seem to recall reading that MString is not needed in the maya python api? If this is the case, how do I initialize string attribute types in a node? And then retrieve them in my compute? Thanks again! -John On Sat, Feb 21, 2009 at 8:16 AM, Dean Edmonds <[email protected]>wrote: > > On Thu, Feb 19, 2009 at 17:43, [email protected] <[email protected]> > wrote: > > > > I'm attempting to write my first custom node. The node needs to be > > able to take an image file (or sequence, but we can start with 1) as > > input, read some specific pixel values, and make these available as > > outputs. > > > > I've read through most of the documentation and I've got a good idea > > of the direction I'm going and the bits of the API I'll need. I just > > didn't want to get started on the wrong foot with the input. > > > > A file node already has everything I need as far as input goes... can > > read in an image or sequence of images, already has the AE template > > for this, etc. so I feel like it would be best to start with this as > > my nodes input?? > > > > So my scripted plugin would generate a blank file node attached to it > > already. Is this logical? Is this bad practice? > > A node is a black box which calculates output values from its input > values. If you want to use a node for something you should ask > yourself which of its outputs you are interested in. > > The 'file' node is a shading node which treats an image file as a > texture. During rendering the renderer sets the node's 'uvCoord' > attribute to the (u, v) coordinates of the texture that it is > interested in and then reads the resulting color, transparency, etc, > from the file node's various output attributes. > > In other words, the file node provides information about the single > point in the image which applies to the surface point currently being > sampled by the renderer. So if that's what your node needs, then it > makes sense to use a file node for that. > > But if you just want to read a bunch of pixels from an image file then > forget about the 'file' node. Instead, use MImage to open the file and > retrieve its pixels: > > import maya.OpenMaya as om > > image = om.MImage() > image.readFromFile(filename) > pixelPtr = image.pixels() > > At this point 'pixelPtr' holds a pointer to an array of bytes > ('unsigned char' or 'uchar' in C++-lingo) containing pixel > information. Since Python doesn't understand pointers, you'll need to > use the special MScriptUtil class to retrieve the elements of the > array: > > pixelIndex = pixelNumber * 4 > red = om.MScriptUtil.getUcharArrayItem(pixelPtr, pixelIndex) > green = om.MScriptUtil.getUcharArrayItem(pixelPtr, pixelIndex+1) > blue = om.MScriptUtil.getUcharArrayItem(pixelPtr, pixelIndex+2) > alpha = om.MScriptUtil.getUcharArrayItem(pixelPtr, pixelIndex+3) > > That code assumes that the image is in RGBA format. There are methods > on MImage that will let you check that an other properties of the > image. > > -- > -deane > > > > --~--~---------~--~----~------------~-------~--~----~ Yours, Maya-Python Club Team. -~----------~----~----~----~------~----~------~--~---
