Sample the color of a vertex



  • Back in releases prior to R20, I have a set of code that would sample the color that a polygonal object had at any vertex, assuming it had a single material and a UVW tag assigned to it.
    This was all I required: a polygonal object with a material tag and a UVW tag.
    But now, in R20, that code (that was quite long) is not working anymore.
    And I can't seem to make it work, no matter what I do.
    Is there any R20 code that would get me the result I need?



  • Just to be clear, you sampled the mapped material color per vertex, not the color of the vertex attribute (aka known as vertex color)?



  • Yes, exactly.
    I had two sets of code that sampled the mapped material per vertex.
    One would sample the color using a modified version of a snipped code from Remo, and another one would calculate the texture with an internal method to bake the texture.
    Neither one seems to be working now, in R20.


  • Global Moderator

    Hi rui_mac, thanks for reaching us.

    With regard to your support request, in R20 I confirm that nothing has changed compared to R19 on how a shader is sampled in Cinema 4D API.

    Again without having any evidence of where your code is failing or what kind of behavior your code is exhibiting is actually helpless to me.

    Something simple like this

    import c4d
    from c4d import gui
    # Welcome to the world of Python
    
    
    # Main function
    def main():
        if op is None:
            return
    
        # get the current TextureTag / Material / UVWTag
        currentTextureTag = op.GetTag(c4d.Ttexture)
        currentMaterial =  currentTextureTag[c4d.TEXTURETAG_MATERIAL]
        currentUVWTag = op.GetTag(c4d.Tuvw)    
        # get the shader associated to the color slot
        shd = currentMaterial[c4d.MATERIAL_COLOR_SHADER]
        # init via  InitRenderStruct()
        initRS = c4d.modules.render.InitRenderStruct()
        c4d.INITRENDERRESULT_OK == shd.InitRender(initRS)
        chanData = c4d.modules.render.ChannelData()
        
        # loop over the data in the UVW set to retrieve the color
        for i in xrange(currentUVWTag.GetDataCount()):
            uvwdict = currentUVWTag.GetSlow(i)
            chanData.p =  uvwdict["a"]
            col = shd.Sample(chanData)
            print  uvwdict["a"], "/", col
            chanData.p =  uvwdict["b"]
            col = shd.Sample(chanData)
            print  uvwdict["b"], "/", col
            chanData.p =  uvwdict["c"]
            col = shd.Sample(chanData)
            print  uvwdict["c"], "/", col
            chanData.p =  uvwdict["d"]
            col = shd.Sample(chanData)
            print  uvwdict["d"], "/", col
    
        # free the allocated resources
        shd.FreeRender()
    
    # Execute main()
    if __name__=='__main__':
        main()
    

    Does at least the above script works for you? Can you provide further details on your code?

    Cheers, Riccardo



  • It seems quite simple, reading your code.
    However, that is python and I'm doing it in c++.
    I was trying to attach the old code I was using, but the uploader only allows for images.
    However, I'm now trying to make more generic, by internally baking the material, as it allows for more complex material assignments to objects.
    I'm still struggling but I will try to solve it myself, before coming back here ;-)