Editing Material Gradients in R13



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 21/02/2012 at 11:55, xxxxxxxx wrote:

    I've managed to convert most of my materials C++ code to python. But I'm having trouble with two things.
    1.- I can't figure out how to write the SetData() function in python
    2.- I can't figure out how to change the gradient's interpolation mode

    #2000 = 2D-U  
    #2001 = 2D-V  
    #2002 = 2D-Diagonal  
    #2003 = 2D-Radial  
    #2004 = 2D-Circular  
    #2005 = 2D-Box  
    #2006 = 2D-Star  
    #2007 = 2D-FourCorner  
    #2008 = 3D-Linear  
    #2009 = 3D-Cylindrical  
    #2010 = 3D-Spherical  
      
    import c4d  
    from c4d import gui  
    from c4d.modules import render  
      
    def main() :  
      mat = doc.GetFirstMaterial()  
      color = c4d.DescLevel(c4d.MATERIAL_COLOR_COLOR, c4d.DTYPE_COLOR)#The material's color channel  
      shdr = mat[c4d.MATERIAL_COLOR_SHADER]  #The shader inside the "texture" option  
      shdr[c4d.SLA_GRADIENT_TYPE]= 2000      #The "Type" option  
      shdr[c4d.SLA_GRADIENT_CYCLE] = True    #The "Cycle" option  
      shdr[c4d.SLA_GRADIENT_TURBULENCE]=0    #The "Turbulence" option  
      shdr[c4d.SLA_GRADIENT_SPACE]= 2021     #The "Space" option  
      
      
      grad = shdr[c4d.SLA_GRADIENT_GRADIENT] #The gradient GUI  
      count = grad.GetKnotCount()            #The number of gradient knots  
        
      k1 = grad.GetKnot(0)                   #Gets the first knot  
      k2 = grad.GetKnot(1)                   #Gets the second knot  
        
      k1['pos'] = 0.1                        #Changes the first knot's position in memory only  
      k2['pos'] = 0.8                        #Changes the second knot's position in memory only  
      k1['col'] = c4d.Vector(0.5, 0.1, 0.0)  #Changes the first knot's color in memory only  
      k2['col'] = c4d.Vector(1.0 , 1.0, 1.0) #Changes the second knot's color in memory only      
      
      #grad.SetKnot(0,k1)<-------Crashes!!!  
      
      #grad[c4d.GRADIENT_INTERPOLATION]<----Wrong!!...How do you change this option?  
      
      mat.Message(c4d.MSG_UPDATE )  
      mat.Update( True, True )        
      
      c4d.EventAdd()  
      
    if __name__=='__main__':  
      main()
    

    Can anyone shed a light on these two things?

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 22/02/2012 at 01:27, xxxxxxxx wrote:

    It's only possible to get / set the gradient data with GetData() and SetData(). So to retrieve the gradient interpolation we have to call:

    gradient.GetData(c4d.GRADIENT_INTERPOLATION)
    

    Which is c4d.GRADIENT_INTERPOLATION_SMOOTHKNOT by default (see customgui_gradient.h).
    Also SLA_GRADIENT_TYPE and SLA_GRADIENT_SPACE enums are defined in xs**gradient.res (** is la, don't know why this is replaced).



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 22/02/2012 at 08:22, xxxxxxxx wrote:

    Hi Yannick,

    Something strange is going on.
    I cannot change the interpolation option using your code either:

    import c4d  
    from c4d import gui  
    def main() :  
      mat = doc.GetFirstMaterial()  
      color = c4d.DescLevel(c4d.MATERIAL_COLOR_COLOR, c4d.DTYPE_COLOR)#The material's color channel  
      shdr = mat[c4d.MATERIAL_COLOR_SHADER]  #The shader inside the "texture" option  
      shdr[c4d.SLA_GRADIENT_TYPE]= 2000      #The "Type" option  
      shdr[c4d.SLA_GRADIENT_CYCLE] = True    #The "Cycle" option  
      shdr[c4d.SLA_GRADIENT_TURBULENCE]=0    #The "Turbulence" option  
      shdr[c4d.SLA_GRADIENT_SPACE]= 2021     #The "Space" option  
      
      
      grad = shdr[c4d.SLA_GRADIENT_GRADIENT] #The gradient GUI  
      count = grad.GetKnotCount()            #The number of gradient knots  
        
      k1 = grad.GetKnot(0)                   #Gets the first knot  
      k2 = grad.GetKnot(1)                   #Gets the second knot  
        
      k1['pos'] = 0.1                        #Changes the first knot's position in memory only  
      k2['pos'] = 0.8                        #Changes the second knot's position in memory only  
      k1['col'] = c4d.Vector(0.5, 0.1, 0.0)  #Changes the first knot's color in memory only  
      k2['col'] = c4d.Vector(1.0 , 1.0, 1.0) #Changes the second knot's color in memory only      
      
      #grad.SetKnot(0,k1)<-------Crashes!!!  
      
      grad.SetData(c4d.GRADIENT_INTERPOLATION, c4d.GRADIENT_INTERPOLATION_SMOOTHKNOT)#<---Does Not work!!!  
      print grad.GetData(c4d.GRADIENT_INTERPOLATION) #<---Always results in 2..It never changes!!!  
      
      mat.Message(c4d.MSG_UPDATE )  
      mat.Update( True, True )        
      
      c4d.EventAdd()  
      
    if __name__=='__main__':  
      main()
    

    I even tried the C++ way too:
    grad.SetData(c4d.GRADIENT_INTERPOLATION, c4d.GRADIENTSUBCHANNEL_INTERPOLATION_SMOOTHKNOT)

    And what about SetKnot()?
    I cannot get that function to work. Even when I use it in it's most basic form:
    -grad.SetKnot(0,['pos'] = 1.5)
    -grad.SetKnot(0,pos = 1.5)
    -etc...
    I've tried a bunch of different things. But I cannot figure out the proper way to write this function either.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 22/02/2012 at 12:52, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

     
    grad.SetData(c4d.GRADIENT_INTERPOLATION, c4d.GRADIENT_INTERPOLATION_SMOOTHKNOT)#<---Does Not work!!!  
    print grad.GetData(c4d.GRADIENT_INTERPOLATION) #<---Always results in 2..It never changes!!!  
    

    GRADIENT_INTERPOLATION data value is GRADIENT_INTERPOLATION_SMOOTHKNOT by default which value is 2 so this is normal 🙂.
    Try to set the gradient interpolation to GRADIENT_INTERPOLATION_LINEARKNOT which value is 3 and you'll see that it changes.

    Originally posted by xxxxxxxx

    And what about SetKnot()?
    I cannot get that function to work. Even when I use it in it's most basic form:
    -grad.SetKnot(0,['pos'] = 1.5)
    -grad.SetKnot(0,pos = 1.5)
    -etc...
    I've tried a bunch of different things. But I cannot figure out the proper way to write this function either.

    I contacted the developers on this issue.



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 22/02/2012 at 13:47, xxxxxxxx wrote:

    Yes.This will change it..... But in memory only:
    grad.SetData(c4d.GRADIENT_INTERPOLATION, c4d.GRADIENT_INTERPOLATION_SMOOTHKNOT)
    If I change that attribute by hand to something else. The attribute itself does not change. And I'm already using C4D.Event() and c4d.MSG_UPDATE in my code. Making this useless until I know how to actually execute the changes that are stored in memory.

    Being able to use the SetKnots() is actually a lot more important to me.
    Thank you for taking it to the developers. And as always...Thanks for your efforts.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 23/02/2012 at 02:09, xxxxxxxx wrote:

    To edit a Gradient data (knots, interpolation, mode etc.) it needs to be reassigned. So your code would be:

    grad = shdr[c4d.SLA_GRADIENT_GRADIENT] # Get the existing gradient data
      
    grad.FlushKnots()
    grad.InsertKnot(col=c4d.Vector(0.5, 0.1, 0.0), pos=0.1)
    grad.InsertKnot(col=c4d.Vector(1.0 , 1.0, 1.0), pos=0.8)
      
    shdr[c4d.SLA_GRADIENT_GRADIENT] = grad # Assign the edited gradient data so it's updated by C4D
    


  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 23/02/2012 at 09:02, xxxxxxxx wrote:

    Ahhaaaa!!!
    shdr[c4d.SLA_GRADIENT_GRADIENT] = grad <--That's the sucker I was looking for. 👍

    Now I can get everything to update properly.
    Here is a full working example in case anyone needs it:

    import c4d  
      
    def main() :  
      
      # First we get the material we want to work with  
      
      mat = doc.GetFirstMaterial()           #Gets the first material in the materials manager  
      color = c4d.DescLevel(c4d.MATERIAL_COLOR_COLOR, c4d.DTYPE_COLOR)#The material's color channel  
      
      
      # Now lets grab the gradient(also known as "Xlsagradient") that is already there in the color channel  
      # We are not creating that gradient with code in this example..So it needs to be there already  
      
      shdr = mat[c4d.MATERIAL_COLOR_SHADER]  #The shader inside the "texture" option  
      shdr[c4d.SLA_GRADIENT_TYPE]= 2000      #The "Type" option...ID's are found in the xlsagradient.h file  
      shdr[c4d.SLA_GRADIENT_CYCLE] = True    #The "Cycle" option  
      shdr[c4d.SLA_GRADIENT_TURBULENCE]=0    #The "Turbulence" option  
      shdr[c4d.SLA_GRADIENT_SPACE]= 2021     #The "Space" option...ID's are found in the xlsagradient.h file  
      
      
      # Now lets drill into the gradient shader to control the knots, interpolation, and color bar  
      
      grad = shdr[c4d.SLA_GRADIENT_GRADIENT] #Get the gradient GUI and assign it to a variable   
      grad.FlushKnots()                      #Delete all existing knots  
      k1 = grad.InsertKnot(col=c4d.Vector(0.5, 0.1, 0.0), pos=0.0,index = 0)  #Creates the first knot  
      k2 = grad.InsertKnot(col=c4d.Vector(1.0 , 1.0, 1.0), pos=1.0,index= 1)  #Creates the second knot  
      
      k1 = grad.GetKnot(k1)  #Get the first knot so we can read it's dictionary values  
      k1pos = k1['pos']      #The position of the knot from the dict value 'pos'  
      k1col = k1['col']      #The color of the knot from the dict value 'col'  
            
      k2 = grad.GetKnot(k2)  #Get the second knot so we can read it's dictionary values  
      k2pos = k2['pos']      #The position of the knot from the dict value 'pos'  
      k2col = k2['col']      #The color of the knot from the dict value 'col'  
        
      k3 = grad.InsertKnot(col=k1col+k2col/2, pos=k1pos+k2pos/2) #Create a new knot using the other knot's values  
      
      
      
    # Now lets change the position of the first knot's handle(diamond shape attribute) to this new value  
      
      k1pos = 0.3              
      grad.SetKnot(index=0,pos=k1pos)        #Set the handle to this new value  
      
      grad.SetData(c4d.GRADIENT_INTERPOLATION, c4d.GRADIENT_INTERPOLATION_CUBICKNOT) #Change the interpolation  
      
      shdr[c4d.SLA_GRADIENT_GRADIENT] = grad #Update the gradient data we changed  
      mat.Message(c4d.MSG_UPDATE )           #Update the material changes  
      mat.Update( True, True )               #Update the material's icon image        
      
      c4d.EventAdd()                         #Update C4D so things update quickly  
      
    if __name__=='__main__':  
      main()
    

    Thanks so much Yannick.🍺
    ScottA



  • On 27/04/2016 at 09:04, xxxxxxxx wrote:

    Thanks a lot guys !!!!!!!!!!!!!!!!!!!!!!


Log in to reply