how to insert a gradiet with step interperation?



  • hello!
    i want to insert a gradient with step interperation.
    but this code"grad.SetData(c4d.GRADIENT_INTERPOLATION, c4d.GRADIENT_INTERPOLATION_NONE)"seems don't work.
    i want to know where do i made a mistake?
    this is my code:

    import c4d
    from c4d import gui
    
    def main():
        newMat=c4d.BaseMaterial(c4d.Mmaterial)             #create a new material
        newShader=c4d.BaseShader(c4d.Xgradient)            #create a new gradient shader 
        newShader[c4d.SLA_GRADIENT_TYPE]=2001              #change type to 2D-v 
    
        grad=newShader[c4d.SLA_GRADIENT_GRADIENT]       #get the gradient gui
        grad.SetData(c4d.GRADIENT_INTERPOLATION, c4d.GRADIENT_INTERPOLATION_NONE) #Change the interpolation
    
        newShader[c4d.SLA_GRADIENT_GRADIENT]=grad           #reassigned the gradient
        newMat[c4d.MATERIAL_COLOR_SHADER]=newShader         #gradient to color channel
        newMat.InsertShader(newShader)                      #insert shader
    
        doc.InsertMaterial(newMat)                          #insert material
    
        newMat.Message(c4d.MSG_UPDATE )
        newMat.Update( True, True )
        c4d.EventAdd()
    
    
    if __name__=='__main__':
        main()
    

  • Global Moderator

    Hi, @milkliu, unfortunately, the documentation was not updated to R20 change of the Gradient class.
    But this will be done for the next release.

    With that's said, now as you may have seen, Interpolation are stored per knot and not global to the whole gradient. So after R20 Cinema4D have a GRADIENT_KNOT BaseContainer which holds all knots information.
    Here a full example to use it.

    import c4d
    
    def main():
        # Create a new material
        newMat = c4d.BaseMaterial(c4d.Mmaterial)
        
        # Create a new gradient shader 
        newShader = c4d.BaseShader(c4d.Xgradient)
        
        # Change type to 2D-v 
        newShader[c4d.SLA_GRADIENT_TYPE] = c4d.SLA_GRADIENT_TYPE_2D_V
    
        # Get the Gradient gui
        grad = newShader[c4d.SLA_GRADIENT_GRADIENT]
        
        # Get the BaseContainer for all Knots
        bcAllKnots = grad.GetData(c4d.GRADIENT_KNOT)
        
        # Iterate over the knots and define their interpolations to linear
        for k, knotBc in bcAllKnots:
            knotBc[c4d.GRADIENTKNOT_INTERPOLATION] = c4d.GRADIENT_INTERPOLATION_NONE
            
        # Set the position of the second knot to 0,5
        # We use GetIndexData/SetIndexData because all knot basecontainer get 0 as Id.
        knotBc = bcAllKnots.GetIndexData(1)
        knotBc[c4d.GRADIENTKNOT_POSITION] = 0.5
        bcAllKnots.SetIndexData(1, knotBc)
        
        # Then we push back our modification to the basecontainer stored in the Gradient Gui
        grad.SetData(c4d.GRADIENT_KNOT, bcAllKnots)
        
        # Reassigned the gradient
        newShader[c4d.SLA_GRADIENT_GRADIENT] = grad
        
        # Gradient to color channel
        newMat[c4d.MATERIAL_COLOR_SHADER] = newShader
        
        # Insert shader into the mat
        newMat.InsertShader(newShader)
    
        # Insert material in the doc
        doc.InsertMaterial(newMat)
    
        # Update the document
        c4d.EventAdd()
    
    if __name__=='__main__':
        main()
    

    If you have any question, please let me know.
    Cheers,
    Maxime.



  • yeah! I got it!
    Thank you very much!