Accessing image layers

From The Foundry MODO SDK wiki
Jump to: navigation, search


This example command goes through all image layers (as found in exr or psd files) and reads the first pixel


#include <lxidef.h>
#include <lx_item.hpp>
#include <lx_plugin.hpp>
#include <lx_log.hpp>
#include <lx_image.hpp>
#include <lxu_command.hpp>
#include <vector>
#include <string>
static char *SERVER_NAME             = "imagetest";
static char *SERVER_USERNAME         = "Image Test";
static char *LOG_NAME                = "imagetest";
class CCmdImage : public CLxBasicCommand
	static LXtTagInfoDesc           descInfo[];
	int basic_CmdFlags() LXx_OVERRIDE {
		return LXfCMD_UNDO;
	void cmd_Execute(unsigned int flags) LXx_OVERRIDE {
		CLxLoc_SceneService svc;
		CLxSceneSelection sceneSelection;
		CLxUser_Scene scene;
		CLxUser_Item item;
		LXtItemType imgLayerType;
		uint count, t;
		svc.ItemTypeLookup(LXsITYPE_IMAGELAYER, &imgLayerType);
		scene.ItemCount(imgLayerType, &count);
		for(t=0; t< count; t++)
			scene.GetItem (imgLayerType,t,item);
			if(item.IsA (imgLayerType))
				CLxUser_ChannelRead read;
				read.from (item, 0.0);
				CLxUser_ImageFilter filter;
				read.Object (item, LXsICHAN_VIDEOCLIP_IMAGESTACK, filter);
				bool test = filter.test();
				// This will fill the metrics structure so that we can read the image dimensions from it
				LXtImageMetrics metrics;
				CLxUser_ImageFilterMetrics myMetrics (filter);
				CLxUser_Image layimg;
				filter.Generate (metrics.maxRes[0], metrics.maxRes[1], 0, layimg);
				test = layimg.test();
				LXtPixelFormat format = layimg.Format();
				if (format == LXiIMV_RGBA)
					unsigned char pixel[4];
					LxResult result = layimg.GetPixel (0, 0, format, pixel);
LXtTagInfoDesc CCmdImage::descInfo[] = {
void initialize()
	CLxGenericPolymorph *srv = new CLxPolymorph<CCmdImage>;
	srv->AddInterface(new CLxIfc_Command    <CCmdImage>);
	srv->AddInterface(new CLxIfc_StaticDesc <CCmdImage>);
	lx::AddServer(SERVER_NAME, srv);