1
$\begingroup$

I've been working with Blender 2.79 for some time now, and I'm in the process of writing a plugin for it. I've mostly figured out writing a custom Operator class that adds itself to the "File > Import" list of model importers.

Now I need to add a custom image file importer to load a custom texture format. Is this something I can script for Blender? And if so, does anyone have any pointers as to where I should start?

$\endgroup$
1
  • 1
    $\begingroup$ Perhaps look into the Import Images as Planes addon to see how they worked that out. If you look in the packaged addons that you can turn on in Blender, you will find it there in Import/Export. $\endgroup$ Commented Jan 10, 2018 at 3:46

1 Answer 1

1
$\begingroup$

Thanks to @CraigDJones's comment on the Planes addon, and lots of digging around, I've narrowed down how to custom-import images.

First, as with any extension importing anything, it's necessary to write an Operator class for handling the file windows. As I am extremely new, this is something I'm still in the process of figuring out. However, operators do seem to be pretty general, as they do a large number of things. For starters, Here's Blender's Operators - bpy.ops reference.

For importing images, I discovered it's possible to instantiate a new image via the bpy.data.images.new(name,width,height) method. Then from there, pixels can be easily accessed and changed via the image_object.pixels[] array, which indexes the pixels data from left to right, then bottom to top as clamp (0.0 to 1.0) values. Each pixel has four values in this array: R, G, B, and A. Here's a simple example:

image_object = bpy.data.images.new("New image",1024,1024)
image_object.pixels[0:3] = (0.5,0.5,0.5) # R,G,B - changes bottom left pixel

# makes entire image half-transparent red
for i in range(0,len(image_object.pixels), 4):
    image_object.pixels[i:i+4] = (1,0,0,0.5)

EDIT:
A note of caution: So after a little effort, I was able to write a script for importing the image type I'm working with. For those doing the same, though, pixel operations on the image pixel data itself, e.g. image_object.pixels[...] = ..., are very slow. My script took about 20mins to import an image because it was directly copying each pixel to the image_object's pixel data. But after I made it copy to a separate buffer, and then dump everything over, e.g. image_object.pixels[0:] = another_buffer it went lightning fast.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .