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.
-~----------~----~----~----~------~----~------~--~---