# How to get an associated Material?

**URL:** https://forum.highend3d.com/t/how-to-get-an-associated-material/6690
**Category:** Maya
**Tags:** mel
**Created:** [November 28, 2001, 4:37pm UTC](https://forum.highend3d.com/t/how-to-get-an-associated-material/6690 "2001-11-28T16:37:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![adreamtraveler](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@adreamtraveler](https://forum.highend3d.com/u/adreamtraveler)
#### Post date: [November 28, 2001, 4:37pm UTC](https://forum.highend3d.com/t/how-to-get-an-associated-material/6690/1 "2001-11-28T16:37:57Z")

</div>

Hi,

ok, I am quite new to writing Plugins for Maya and now I am stuck: I am currently writing an exporter for Polygon-Meshes. Exporting the Vertices and so on is no problem, but I don

---

<div class="post-metadata">

### Author: ![glocker](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/g/a9adbd/32.png) [@glocker](https://forum.highend3d.com/u/glocker)
#### Post date: [November 28, 2001, 5:13pm UTC](https://forum.highend3d.com/t/how-to-get-an-associated-material/6690/2 "2001-11-28T17:13:46Z")

</div>

search for getConnectedShaders or getConnectedSetsAndMembers on this forum. You should find several postings with source code.

g-

---

<div class="post-metadata">

### Author: ![adreamtraveler](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@adreamtraveler](https://forum.highend3d.com/u/adreamtraveler)
#### Post date: [November 28, 2001, 10:35pm UTC](https://forum.highend3d.com/t/how-to-get-an-associated-material/6690/3 "2001-11-28T22:35:51Z")

</div>

Thanks, that helped a lot. However, I still wonder how to find out which Filetexture is connected to which Colorchannel.

Any ideas

Michael

---

<div class="post-metadata">

### Author: ![rob-bateman](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/r/4491bb/32.png) [@rob-bateman](https://forum.highend3d.com/u/rob-bateman)
#### Post date: [November 29, 2001, 3:24pm UTC](https://forum.highend3d.com/t/how-to-get-an-associated-material/6690/4 "2001-11-29T15:24:31Z")

</div>

This is the way I do it, (More or less, doing this from memory so you might have to check a couple of things, but it should be more or less correct)

// obj is the MObject representing the shader....  
MFnDependecyNode fnDep( obj );

// get the attribute plug  
MPlug colorPlug = fnDep.findPlug( "color" );  
MPlugArray connections;  
MString filename;

colorPlug.connectedTo( connections,true,true );

// check each connection for a file texture.  
for( unsigned int i=0;i ( **_less than_** )connections.length();i++)  
{  
 // get the node of this connection - replace squiggly brackets with suare ones (arrrgggh html)   
 MObject node = connections{i}.node();

```
  // is it a file texture node?
  if( node.apiType == MFn::kFileTexture )
  {
       MFnDependencyNode fnNode( node );

       // find the fileTextureName attribute
       MPlug fileNamePlug = fnNode.findPlug("ftn");

       // get the filename
       fileNamePlug.getValue( filename );

       // stop looping
       break;
  }
```

}

// if you've got Maya 4.01, you can go on and use MImage to get the texture data...  
MImag image;  
unsigned int width,height,depth;

// load texture file  
image.readFromFile( filename );

// get texture dimensions  
image.getSize( width, height );

// get bytes per pixel.  
depth = image.depth;

// alwas 32bit RGBA, no matter what the depth is (annoyingly)  
unsigned char \*pData = image.pixels;

// do something

// release image data  
image.release();

// sorry about the less than sign thing, cant put it in cos of annoying html tags

---

<div class="post-metadata">

### Author: ![adreamtraveler](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/a/e79b87/32.png) [@adreamtraveler](https://forum.highend3d.com/u/adreamtraveler)
#### Post date: [November 29, 2001, 6:22pm UTC](https://forum.highend3d.com/t/how-to-get-an-associated-material/6690/5 "2001-11-29T18:22:32Z")

</div>

Thanks, I will try it out soon.

Michael
