Different materials to cube sides in Python

  • On 07/05/2017 at 03:38, xxxxxxxx wrote:


    I have some cubes (created with a Python script) and I would like to set 6 different colors to all of the sides of them.

    I know how to do this manually, but I have plenty of cubes (some thousands) so i'd like to automate the process: create the cube -> make it editable -> select the poligion on the side -> apply texture -> repeat this with all the sides.

    I absolutely don't have any ideas how this can be done in Python, any help would be greatly appreciated.

  • On 10/05/2017 at 10:49, xxxxxxxx wrote:

    Hope it's make sense for you

    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)
            #add tag to the scene
            doc.AddUndo(c4d.UNDOTYPE_NEW, 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()
        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)
            #assign random color
            mat[c4d.MATERIAL_COLOR_COLOR] = c4d.Vector(random.random(), random.random(), random.random())
            doc.AddUndo(c4d.UNDOTYPE_NEW, 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:
            texture_tag = c4d.BaseTag(c4d.Ttexture)
            texture_tag[c4d.TEXTURETAG_MATERIAL] = mat
            texture_tag[c4d.TEXTURETAG_RESTRICTION] = tag.GetName()
            doc.AddUndo(c4d.UNDOTYPE_NEW, texture_tag)
    def main() :
        if not isinstance(op, c4d.PolygonObject) :
        #create 6 random materials
        #create and assign selection tag
        tags = create_poly_selection(op)
        if not assign_selection(op, tags) :
        #create texture tag into the obj
        create_texture_tag(op, tags)
    if __name__=='__main__':

  • On 15/05/2017 at 05:44, xxxxxxxx wrote:

    Hi Gergely,

    welcome to the Plugin Café forums 🙂

    I think, gr4ph0s answer more or less solves most of your questions, with maybe just creation of cubes and make editable not answered, yet. For the "make editable" part I suggest to take a look at SendModelingCommand() in conjunction with MCOMMAND_MAKEEDITABLE.

  • On 23/06/2017 at 15:09, xxxxxxxx wrote:

    I'm trying to do a similar thing but create a selection tag based on a manual selection but am stumped on how to assign the selection to the tag.

    selection = op.GetPolygonS()
    primaryTag = op.MakeTag(c4d.Tpolygonselection)

    I feel like there should be a command to add my selection to the tag but after two hours I'm still stumped.  Any help is appreciated.

  • On 26/06/2017 at 07:43, xxxxxxxx wrote:

    Found my missing command.  Posting here in case anybody else has to go digging for this.

    import c4d
    from c4d import gui
    #Welcome to the world of Python
    primaryTag = op.MakeTag(c4d.Tpolygonselection) # adds a selection tag to your selected object
    primaryTag_bs = primaryTag.GetBaseSelect() # get the selection container from the tag so it can be accessed.
    op.GetPolygonS().CopyTo(primaryTag_bs) # copy your live polygon selection to the selection container
    primaryTag.SetName("primary") # give your new selection tag a name

  • On 26/06/2017 at 07:45, xxxxxxxx wrote:

    Glad you found a solution yourself, I was just about to post a similar answer.

  • On 26/06/2017 at 07:51, xxxxxxxx wrote:

    It's confusing at times because "Set" is used in so many places that "Copy" never enters my mind.  Finally found an old thread that had my missing line and then it all made sense.  Tough search as I kept looking for things with "Set".

    thanks for looking at it.

Log in to reply