View Full Version : Writing out an image using the API

01 January 2008, 07:10 AM

I'm trying to use Python and the API to write out an imagefile (that i want to draw myself).
Somehow I cannot pass my pixel array to the MImage object - I don't know why it is not working. Whatever I pass it results in 1's only. Here is my code:

import random
import maya.OpenMaya as om

img = om.MImage()


#make some test data
for i in range(1,10000) :
c= random.randint(1, 255)
pix+= [c,c,c,1]
#print pix

#Pass to MImage as a Pointer
p = om.MScriptUtil()
img.setPixels(p.asUcharPtr(), 100, 100)

#Reading for testing
r = om.MScriptUtil()
ret = img.pixels()
for i in range(1,40000): ga.append(r.getUcharArrayItem(ret,i))
print ga # prints out a bunch of 1's


Thanks for any hints - its the first time i'm using something from the api, maybe i got something wrong..


01 January 2008, 03:40 PM
hmm...i really don't know phyton and i haven't started with the api yet. but i think that your pointer isn't treated correctly.

#make some test data
for i in range(1,10000) :
c= random.randint(1, 255)
pix+= [c,c,c,1]
#print pix

don't you need to end the for-loop? well that may be the phyton syntax i don't know. what i suggest is that you change the line:
pix+=[c, c, c, 1] to this pix+=[c, c, c, c]

if the output i still 1 then forget my post :D
but if there is another output then your problem is at this line:
img.setPixels(p.asUcharPtr(), 100, 100)

because maybe setPixels wants as first parameter a pointer to a one dimensional array.

as i already said i don't really know a lot about this stuff but try the change i mentioned above and if the output changes my idea may be correct.

CGTalk Moderation
01 January 2008, 03:40 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.