Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C4D create unique material per face
import c4d
import random
#create a selection for each polygon
def create_poly_selection(obj):
poly_count = obj.GetPolygonCount()
tags = list()
for i in xrange(poly_count):
#create the tag and name it by poly_id
tag = c4d.SelectionTag(c4d.Tpolygonselection)
tag.SetName(str(i))
#add tag to the scene
doc.AddUndo(c4d.UNDOTYPE_NEW, tag)
obj.InsertTag(tag)
tags.append(tag)
return tags
#assign 1 poly_id
def assign_selection(obj, tags_list):
poly_count = obj.GetPolygonCount()
if len(tags_list) != poly_count:
return False
for i, tag in enumerate(tags_list):
bs = tag.GetBaseSelect()
bs.Select(i)
return True
#create materials for a given obj
def create_materials(number=6):
mats = list()
for i in xrange(number):
mat = c4d.BaseMaterial(c4d.Mmaterial)
mat.SetName(str(i))
#assign random color
mat[c4d.MATERIAL_COLOR_COLOR] = c4d.Vector(random.random(), random.random(), random.random())
doc.AddUndo(c4d.UNDOTYPE_NEW, mat)
doc.InsertMaterial(mat)
mats.append(mat)
return mats
#create texture tag where selection tag and material name are identical
def create_texture_tag(obj, tags):
for tag in tags:
name = tag.GetName()
mat = doc.SearchMaterial(name)
if not mat:
continue
texture_tag = c4d.BaseTag(c4d.Ttexture)
texture_tag.SetName(name)
texture_tag[c4d.TEXTURETAG_MATERIAL] = mat
texture_tag[c4d.TEXTURETAG_RESTRICTION] = tag.GetName()
texture_tag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW
doc.AddUndo(c4d.UNDOTYPE_NEW, texture_tag)
obj.InsertTag(texture_tag)
def main():
if not isinstance(op, c4d.PolygonObject):
return
doc.StartUndo()
#create 6 random materials
create_materials()
#create and assign selection tag
tags = create_poly_selection(op)
if not assign_selection(op, tags):
return
#create texture tag into the obj
create_texture_tag(op, tags)
doc.EndUndo()
c4d.EventAdd()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment