Solved Assign materials to Objects using Python Un-subscribe from this thread

Hello!
I would like to automate the replacement of materials loaded in Alembic.

How can I change the attached python code to replace the material with a material with the same name as the one applied in Alembic?

I would appreciate your advice.

I am new to the forum, so please forgive my unfamiliarity.

import re
import c4d

def main():
  doc = c4d.documents.GetActiveDocument()
  oplist = doc.GetObjects()
  if not oplist: return
  matlist = doc.GetMaterials()
  if not matlist: return
  if oplist:
      for op in oplist:
        opname = op.GetName()       
        mat = doc.SearchMaterial(opname)
        if not mat:
          for mat in matlist:
              mname = mat.GetName()
        mname = mat.GetName()
        match = re.match(opname, mname)
        if match:
          textag = c4d.TextureTag()
          textag.SetMaterial(mat)
          textag[c4d.TEXTURETAG_PROJECTION]=c4d.TEXTURETAG_PROJECTION_UVW
          op.InsertTag(textag)
  c4d.EventAdd()
if __name__==‘__main__’:
  main()

hi,

welcome to the forum.

I would change the fact that if no material is found with the right name, we can just continue the loop with the next object.
This will add a new material tag to the alembic object and link the tag to the material. That is exactly what you want to do?

import re
import c4d

def main():
  doc = c4d.documents.GetActiveDocument()
  oplist = doc.GetObjects()
  if not oplist: 
    return
  # Check if there is at leat one material in the document otherwise return 
  mat = doc.GetFirstMaterial()
  if not mat: 
    return

  # we already checked if oplist is not empty we do not need it twice
  for op in oplist:
    opname = op.GetName()
    mat = doc.SearchMaterial(opname)
    # If there is no material with the same name, we can continue the loop to the next object
    if not mat:
      continue
      
    mname = mat.GetName()
    match = re.match(opname, mname)
    if match:
      textag = c4d.TextureTag()
      textag.SetMaterial(mat)
      textag[c4d.TEXTURETAG_PROJECTION]=c4d.TEXTURETAG_PROJECTION_UVW
      op.InsertTag(textag)
  c4d.EventAdd()
  
if __name__== '__main__':
  main()

Cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer

hi,

welcome to the forum.

I would change the fact that if no material is found with the right name, we can just continue the loop with the next object.
This will add a new material tag to the alembic object and link the tag to the material. That is exactly what you want to do?

import re
import c4d

def main():
  doc = c4d.documents.GetActiveDocument()
  oplist = doc.GetObjects()
  if not oplist: 
    return
  # Check if there is at leat one material in the document otherwise return 
  mat = doc.GetFirstMaterial()
  if not mat: 
    return

  # we already checked if oplist is not empty we do not need it twice
  for op in oplist:
    opname = op.GetName()
    mat = doc.SearchMaterial(opname)
    # If there is no material with the same name, we can continue the loop to the next object
    if not mat:
      continue
      
    mname = mat.GetName()
    match = re.match(opname, mname)
    if match:
      textag = c4d.TextureTag()
      textag.SetMaterial(mat)
      textag[c4d.TEXTURETAG_PROJECTION]=c4d.TEXTURETAG_PROJECTION_UVW
      op.InsertTag(textag)
  c4d.EventAdd()
  
if __name__== '__main__':
  main()

Cheers,
Manuel

MAXON SDK Specialist

MAXON Registered Developer

Hello @Tomoya,

without any further questions or postings, we will consider this thread as solved by Friday the 4th, February 2022.

Thank you for your understanding,
Ferdinand

MAXON SDK Specialist
developers.maxon.net