Getting material assignment

On 08/11/2013 at 12:15, xxxxxxxx wrote:

Using python how can I get a list of objects in the assignment tab of a material

This is where I'm at:

myMat = doc.GetActiveMaterial()
    Olink = myMat[c4d.MATERIAL_PAGE_ASSIGNMENT]
    print Olink

However it just returns None.

Any help is appreciated!

On 08/11/2013 at 21:32, xxxxxxxx wrote:

Hi,
Man, Iam glad you posted that cause i had problems in r13 with the ### python sdk , I spent tree days trying to make MatAssignData.GetObject(index, doc) in r13 sdk to work aand by luck I think I read R15 SDK and FOUND  MatAssignData.ObjectFromIndex(doc, Index) which is WORKING, that was I thought at first, but it only returns the texture tag only.

so here the code, the trick is to use c4d.ID_MATERIALASSIGNMENTS not c4d.MATERIAL_PAGE_ASSIGNMENT.

import c4d
from c4d import *
#HOPE SOMEONE WILL EXPLAIN!!!!
def main() :
    myDoc=documents.GetActiveDocument()
    myMat = doc.GetActiveMaterial()
    
    Olink = myMat[c4d.ID_MATERIALASSIGNMENTS]
    myCount = Olink.GetObjectCount()
    for i in xrange(myCount) :
        print i, " : ", Olink.ObjectFromIndex(myDoc,i)

If you are a C++, you can try to port this code to python
BaseMaterial *mat = doc->GetActiveMaterial();
if(!mat) return TRUE;
GeData d;
if(mat->GetParameter(DescLevel(ID_MATERIALASSIGNMENTS),d,0))
{
     MatAssignData *mad = (MatAssignData* )d.GetCustomDataType(CUSTOMDATATYPE_MATASSIGN);
     if(mad)     GePrint("found it");
     GePrint(LongToString(mad->GetObjectCount())+" objects");
}

Have a nice week-end!

On 09/11/2013 at 08:24, xxxxxxxx wrote:

**Python version
**

  
#This script lists all the objects that are listed in a materials "Assignment" option  
  
import c4d  
def main() :  
  
  myMat = doc.GetActiveMaterial()  
    
  ObjLink = myMat[c4d.ID_MATERIALASSIGNMENTS]   #The object Assignment option in the material  
  
  myCount = ObjLink.GetObjectCount()  
  for i in xrange(myCount) :  
      tag = ObjLink.ObjectFromIndex(doc,i)      #Gets the tag that's on the object  
      obj = tag.GetObject().GetName()           #Gets the object the texture tag is attached to  
      print obj, tag  
                
if __name__=='__main__':  
  main()

**

C++ version
**

//This is an example of getting all the objects that are listed in the material's Assignment link list  
  
#include "customgui_matassign.h"  
  
  BaseMaterial *mat = doc->GetActiveMaterial();  
  if(!mat) return TRUE;  
  
  GeData d;  
  if(mat->GetParameter(DescLevel(ID_MATERIALASSIGNMENTS), d, DESCFLAGS_GET_0))  
  {  
       MatAssignData *mad = (MatAssignData* )d.GetCustomDataType(CUSTOMDATATYPE_MATASSIGN);  
  
       LONG count = mad->GetObjectCount();  //The number of objects assigned this material  
       for(LONG i=0; i<count ; i++)  
       {  
           BaseTag *tag = (BaseTag* )mad->ObjectFromIndex(doc,i);  //Gets the tag that's on the object  
           BaseObject *obj = tag->GetObject();                    //Gets the object the texture tag is attached to  
           String objName = obj->GetName();                       //Gets the names of the objects in the material's Assignment list  
           GePrint(objName);  
       }  
  }

-ScottA

On 09/11/2013 at 13:22, xxxxxxxx wrote:

@ScottA,

stupid Me, yes that the way to solve it.
But according to sdk, ObjectFromIndex(doc,i)  must return object list no tag list.

So Thanks a lot for submitting your solution.

On 11/11/2013 at 19:18, xxxxxxxx wrote:

Thanks for the help guys! This got me what I needed. Also I found out this is the same code that you should use to get any data from an inexclusion list.

Thanks again!

On 27/02/2017 at 08:41, xxxxxxxx wrote:

I need the same thing in XPresso(python node). I am new in python.
I need to get a list of objects from the assignment tab of a material.

On 28/02/2017 at 02:12, xxxxxxxx wrote:

Originally posted by xxxxxxxx

I need the same thing in XPresso(python node). I am new in python.
I need to get a list of objects from the assignment tab of a material.

Hi and Welcome to the forums!

Why do you need to access a material assignment from a Python XPresso node?
It's not recommended to change the active document from such a node.
A Python XPresso node should only manipulate its input data to produce its output(s).

Please create a new topic if you have more questions.