Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Recent
    • Tags
    • Users
    • Login

    setting keys for gradient

    Scheduled Pinned Locked Moved PYTHON Development
    14 Posts 0 Posters 1.4k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H Offline
      Helper
      last edited by

      On 16/09/2013 at 23:07, xxxxxxxx wrote:

      Hi,

      We need to set keys for a shader effector's gradient.

      We can successfully change the gradient with the following code, but don't know how to key it.

      import c4d
      from c4d import gui
      from c4d.modules import mograph as mo

      def main() :

      shader = op[c4d.ID_MG_SHADER_SHADER]
          shader[c4d.SLA_GRADIENT_CYCLE] = True
          shader[c4d.SLA_GRADIENT_TURBULENCE]=.30

      gradient = shader[c4d.SLA_GRADIENT_GRADIENT] 
          #knot = gradient.GetKnot(1)
          
          gradient.FlushKnots()
          
          knot_1 = gradient.InsertKnot(col=c4d.Vector(20/256.0 , 40/256.0, 200/256.0), pos=0.0,index=0)
          knot_2 = gradient.InsertKnot(col=c4d.Vector(55/256.0 , 200/256.0, 127/256.0), pos=0.5,index=1)
          knot_3 = gradient.InsertKnot(col=c4d.Vector(75/256.0 , 230/256.0, 150/256.0), pos=1.0,index=2)

      gradient.SetData(c4d.GRADIENT_INTERPOLATION, c4d.GRADIENT_INTERPOLATION_SMOOTHKNOT)
          shader[c4d.SLA_GRADIENT_GRADIENT] = gradient     
         
          c4d.EventAdd()

      if __name__=='__main__':
          main()

      I have to also key colors on lights, which we can successfully accomplish with something like the following code, and I've been trying to model the DescIDs on the lights for the gradients.  When I key the gradient by hand, I see that the gradient includes r,g.b tracks for each knot, so I think I need to create r,g,b tracks for each knot, but I am not sure how to construct the DescID and DescLevels for this by code, nor can't seem to find this information anywhere. The code below shows one of the many attempts to start that process. I am clearly tripping up on the DescID and DescLevels.

      Any help or direction would be greatly appreciated.

      Thanks.

      import c4d
      from c4d.modules import mograph as mo

      def CreateKey(obj,id,value,frame) :
          
          if not obj.GetDocument() : raise Exception, "object must be in a document"

      track=obj.FindCTrack(id)

      if not track:

      track = c4d.CTrack(obj,id)
              obj.InsertTrackSorted(track)

      curve=track.GetCurve()
          key=curve.AddKey(c4d.BaseTime(frame,doc.GetFps()))

      if type(value)==int or type(value)==float:
              
              key["key"].SetValue(curve,value)
              
          else:
              
              key["key"].SetGeData(curve,value)
       
      def main() :
          
          r = float(64) / 255.0 #values to be read from database 
          g = float(12) / 255.0
          b = float(123) / 255.0
          
          #if op is the light
          #CreateKey(op, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0)), r, 0)
          #CreateKey(op, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0)), g, 0) 
          #CreateKey(op, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0)), b, 0)
          
          #if op is the shader effector
          CreateKey(op, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT,c4d.CUSTOMDATATYPE_GRADIENT,0), c4d.DescLevel(1), c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0)), r, 50)
          CreateKey(op, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT,c4d.CUSTOMDATATYPE_GRADIENT,0), c4d.DescLevel(1), c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0)), g, 50)
          CreateKey(op, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT,c4d.CUSTOMDATATYPE_GRADIENT,0), c4d.DescLevel(1), c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0)), b, 50)

      c4d.EventAdd()
          
          
              
       
      if __name__=='__main__':
          main()

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 17/09/2013 at 02:58, xxxxxxxx wrote:

        Actually, I see that setting a key for the gradient on the shader effector object by hand doesn't hold its values. I am using r15. If I set one key by hand; it shows a red dotl but when I set another key, it shows a gold dot. I can see the keys in the timeline window, but not the values.

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 17/09/2013 at 03:01, xxxxxxxx wrote:

          However, the hand-keying of the shader effector's gradient works just fine in r13.

          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            On 17/09/2013 at 06:38, xxxxxxxx wrote:

            I am again not 100% sure what you are asking for, but :

            1. The IDs for CustomDataType DescLevels are on top of each CustomDataType info page in the SDK.
            The default IDs can be found at the DescLevel page of course. So a gradient DescLevel would be:

            > myDescLevel = c4d.DescLevel(someID, c4d.CUSTOMDATATYPE_GRADIENT)

            2. To be interpolated over time every ckey value has to be a floating point value / be composed
            of floating point subchannel values. You can create keys for a Ctrack created with the DescLevel
            myDescLevel as described in 1, but there will be then no interpolation, the value will jump when
            the time reaches a new key frame.
            If you want to interpolate the gradient gradually you have basically to keyframe all the floating 
            point values that make up your gradient data (multiple knots with their r,g,b values, intensity and 
            so on) - as you did already suggest yourself.

            edit : just to be clear, for manually constructing the subvalue DescLevels you have to look them
            up. The fastest way would be a small script.

                for track in op.GetCTracks() :
                    print track
                    print track.GetDescriptionID()
            

            Happy rendering,
            Ferdinand

            1 Reply Last reply Reply Quote 0
            • H Offline
              Helper
              last edited by

              On 17/09/2013 at 14:51, xxxxxxxx wrote:

              Hi Ferdinand,

              Thank you for your response.

              I am still missing something.

              When I pull the IDs, I get the following list:

              <c4d.BaseObject object called 'Shader/Shader' with ID 1018561 at 0x126ac8110>
              <c4d.BaseShader object called 'Gradient/Gradient' with ID 1011100 at 0x126ac85d0>
              <c4d.CTrack object called 'Gradient . Interpolation/Track' with ID 5350 at 0x126ac83d0>
              (1000, 15, 1000481)
              <c4d.CTrack object called 'Gradient . [1] Color . R/Track' with ID 5350 at 0x126ac8130>
              (1000, 19, 23)
              <c4d.CTrack object called 'Gradient . [1] Color . G/Track' with ID 5350 at 0x126ac88b0>
              (1001, 19, 23)
              <c4d.CTrack object called 'Gradient . [1] Color . B/Track' with ID 5350 at 0x126ac8550>
              (1002, 19, 23)
              <c4d.CTrack object called 'Gradient . [1] Intensity/Track' with ID 5350 at 0x126ac85b0>
              (10101, 19, 1000481)
              <c4d.CTrack object called 'Gradient . [1] Position/Track' with ID 5350 at 0x126ac8250>
              (10102, 19, 1000481)
              <c4d.CTrack object called 'Gradient . [1] Bias/Track' with ID 5350 at 0x126ac80b0>
              (10103, 19, 1000481)
              <c4d.CTrack object called 'Gradient . [2] Color . R/Track' with ID 5350 at 0x126ac8430>
              (1000, 19, 23)
              <c4d.CTrack object called 'Gradient . [2] Color . G/Track' with ID 5350 at 0x126ac8910>
              (1001, 19, 23)
              <c4d.CTrack object called 'Gradient . [2] Color . B/Track' with ID 5350 at 0x126ac8290>
              (1002, 19, 23)
              <c4d.CTrack object called 'Gradient . [2] Intensity/Track' with ID 5350 at 0x126ac8510>
              (10201, 19, 1000481)
              <c4d.CTrack object called 'Gradient . [2] Position/Track' with ID 5350 at 0x126ac8c50>
              (10202, 19, 1000481)
              <c4d.CTrack object called 'Gradient . [2] Bias/Track' with ID 5350 at 0x126ac8b30>
              (10203, 19, 1000481)
              <c4d.CTrack object called 'Gradient . [3] Color . R/Track' with ID 5350 at 0x126ac8590>
              (1000, 19, 23)
              <c4d.CTrack object called 'Gradient . [3] Color . G/Track' with ID 5350 at 0x126ac87f0>
              (1001, 19, 23)
              <c4d.CTrack object called 'Gradient . [3] Color . B/Track' with ID 5350 at 0x126ac8c10>
              (1002, 19, 23)
              <c4d.CTrack object called 'Gradient . [3] Intensity/Track' with ID 5350 at 0x126ac8f30>
              (10301, 19, 1000481)
              <c4d.CTrack object called 'Gradient . [3] Position/Track' with ID 5350 at 0x126ac84f0>
              (10302, 19, 1000481)
              <c4d.CTrack object called 'Gradient . [3] Bias/Track' with ID 5350 at 0x126ac8150>
              (10303, 19, 1000481)
              <c4d.CTrack object called 'Gradient/Track' with ID 5350 at 0x126ac8190>
              (1007, 1000481, 1011100)
              <c4d.CTrack object called 'Gradient/Track' with ID 5350 at 0x126ac85f0>
              (1007, 1000481, 1011100)

              When I look at these IDs, I am not sure how the three different knots are differentiated for keying: They have the same three r, g, b IDs (1000, 19, 23), (1001, 19, 23), (1002, 19, 23).

              I also am not sure how to build the complete DescID from these descriptions.

              I know that the DescID is a collection of one or more DescLevels, though I think I read that there are a maximum of three. In the following experiment, for instance, I am assuming that there are two DescLevels: one for the main object, and one for the R channel of the colors for the knots.

              CreateKey(shader, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT,0), c4d.DescLevel(1000,19,23)), 1.0,10) #R channel

              But how would the system know this DescID pertains to only the first knot and not the second or third? I see in the name that there is this designation "Gradient . [3]"  that shows they are different, but where would this go in the DescID?

              None of my experiments are holding values. (I end up with gold dots beside the gradient field, not a red dot. Or when I try to insert something like DescLevel(c4d.SLA_GRADIENT_GRADIENT.[3]), I of course get errors.)

              I actually don't need to interpolate between graidents, so in one experiment, I assumed the following would set the key, but it doesn't work, either.

              CreateKey(shader, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT,0)), gradient, 50)

              Thanks again for all your help. Any comments are greatly appreciated.

              1 Reply Last reply Reply Quote 0
              • H Offline
                Helper
                last edited by

                On 17/09/2013 at 21:58, xxxxxxxx wrote:

                Hi,

                please use [.code] tags to make your postings more readable. I am still not quite sure what is 
                not working for you. I wrote a small example for creating an intensity and a color.r track for the 
                first gradient knot in the c4d light object.visibility.color gradient. The Code is working for me (I 
                do get a red dot). The approach is the one as described above, I simply used the console and 
                Agent Ransack to unfold the DescLevels. I did convert some of the integer ids into their enum 
                repr to make it more readable. 10100 is the id for the color vector, 10101 for the intensity.

                import c4d
                from c4d import gui
                #Welcome to the world of Python
                  
                  
                def main() :
                    dlevel_visgrad = c4d.DescLevel(c4d.LIGHT_VISIBILITY_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT, 0)
                    did_intensity = c4d.DescID(dlevel_visgrad, c4d.DescLevel(10101, c4d.DTYPE_REAL, c4d.CUSTOMDATATYPE_GRADIENT))
                    did_colr = c4d.DescID(dlevel_visgrad, c4d.DescLevel(10100, c4d.DTYPE_COLOR, 0), c4d.DescLevel(c4d.VECTOR_X, c4d.DTYPE_REAL, 23))
                    ctrack_intens = c4d.CTrack(op, did_intensity)
                    ctrack_colr = c4d.CTrack(op, did_colr)
                    dic_intens = ctrack_intens.GetCurve().AddKey(doc.GetTime())
                    dic_colr = ctrack_colr.GetCurve().AddKey(doc.GetTime())
                    dic_intens['key'].SetValue(ctrack_intens.GetCurve(), 0.25)
                    dic_colr['key'].SetValue(ctrack_colr.GetCurve(), 0.50)
                    op.InsertTrackSorted(ctrack_intens)
                    op.InsertTrackSorted(ctrack_colr)
                    c4d.EventAdd()
                    
                if __name__=='__main__':
                    main()
                
                1 Reply Last reply Reply Quote 0
                • H Offline
                  Helper
                  last edited by

                  On 17/09/2013 at 21:58, xxxxxxxx wrote:

                  Hi Ferdinand,

                  Thanks for your example. As you can see in the following code, I have a similar structure for the DescID—two DescLevels—but it doesn't work. Nor do I see how to set the different colors on different knots. Seems like there should be three DescLevels, though I have no idea what the second one would be.

                  The following testing code does everything except work. LOL

                  The file has a light and an effector shader in the object manager.

                  Thanks again for your help.

                    
                  import c4d  
                  from c4d.modules import mograph as mo  
                    
                  def CreateKey(obj,id,value,frame) :  
                      
                    if not obj.GetDocument() : raise Exception, "object must be in a document"  
                    
                    track=obj.FindCTrack(id)  
                    
                    if not track:  
                    
                        track = c4d.CTrack(obj,id)  
                        obj.InsertTrackSorted(track)  
                    
                    curve=track.GetCurve()  
                    key=curve.AddKey(c4d.BaseTime(frame,doc.GetFps()))  
                    
                    if type(value)==int or type(value)==float:  
                          
                        key["key"].SetValue(curve,value)  
                          
                    else:  
                          
                        key["key"].SetGeData(curve,value)  
                          
                    
                  def GetNextObject(obj) :  
                    if obj==None:  
                        return None  
                     
                    if obj.GetDown() :  
                        return obj.GetDown()  
                     
                    while not obj.GetNext() and obj.GetUp() :  
                        obj = obj.GetUp()  
                     
                    return obj.GetNext()  
                    
                     
                  def main() :  
                      
                    obj = doc.GetFirstObject()  
                    if obj==None: return  
                    
                    descid = obj.GetType()      
                    while obj:  
                        descid = obj.GetType()  
                    
                        if descid == 5102: #If it is a light  
                            lightObj = obj  
                        if descid == 1018561:  
                            shaderObj = obj  
                              
                        obj = GetNextObject(obj)  
                          
                          
                    r = float(64) / 255.0 #values to be read from database   
                    g = float(12) / 255.0  
                    b = float(123) / 255.0  
                      
                    #key the light: This produces a gold dot in r15, a red dot in r13  
                    CreateKey(lightObj, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0)), r, 0)  
                    CreateKey(lightObj, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0)), g, 0)   
                    CreateKey(lightObj, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0)), b, 0)   
                      
                    gradient = shaderObj[c4d.SLA_GRADIENT_GRADIENT] #take the object as it is already constructed for testing  
                    shader = shaderObj.GetFirstShader()  
                      
                    print shaderObj  
                    print shader  
                        
                    #print out the IDs for reference  
                    for track in shader.GetCTracks() :  
                        print track  
                        print track.GetDescriptionID()  
                          
                    #key the shader effector: This produces a gold circle in both r13 and r15. It also doesn't differentiate which knot it is setting the color for  
                    CreateKey(shader, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT,0), c4d.DescLevel(1000,19,23)), 0.0,10)  
                    CreateKey(shader, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT,0), c4d.DescLevel(1001,19,23)), 0.0,10)  
                    CreateKey(shader, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT,0), c4d.DescLevel(1002,19,23)), 0.0,10)  
                    
                    #This is an example of trying to differentiate which knot to set  
                    #CreateKey(shader, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT.[1], c4d.CUSTOMDATATYPE_GRADIENT,0), c4d.DescLevel(1000,19,23)), 1.0,10)  
                    #CreateKey(shader, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT.[1], c4d.CUSTOMDATATYPE_GRADIENT,0), c4d.DescLevel(1001,19,23)), 0.0,10)  
                    #CreateKey(shader, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT.[1], c4d.CUSTOMDATATYPE_GRADIENT,0), c4d.DescLevel(1002,19,23)), 0.0,10)  
                    
                    #example for keying the whole gradient, without its subchannels. It produces a gold dot in both r13 and r15  
                    #CreateKey(shader, c4d.DescID(c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT,0)), gradient, 50)  
                    c4d.EventAdd()  
                      
                       
                     
                  if __name__=='__main__':  
                    main()  
                  
                  1 Reply Last reply Reply Quote 0
                  • H Offline
                    Helper
                    last edited by

                    On 17/09/2013 at 22:16, xxxxxxxx wrote:

                    Hi Ferdinand,

                    I ran your sample code, which, interestingly, produced a gold dot in r15 and a red one in r13. I wonder if I've been struggling because of a bug. . .

                    1 Reply Last reply Reply Quote 0
                    • H Offline
                      Helper
                      last edited by

                      On 17/09/2013 at 22:34, xxxxxxxx wrote:

                      The DescID for the vector has to be a three level DescID. I am on R14, to lazy to switch to R15.
                      About the red and golden dots : Do you have the timeline open / pressed play ? If not, it might 
                      be an update problem.

                      Here is the same snippet a bit cleaner than the first one, it might show better why you do
                      need three DescLevels to access the red channel in a vector inside a custom datataype.

                      import c4d
                      from c4d import gui
                      #Welcome to the world of Python
                        
                        
                      def main() :
                          # the DescLevels we need
                          dlvl_grad = c4d.DescLevel(c4d.LIGHT_VISIBILITY_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT, 0)
                          dlvl_vecx = c4d.DescLevel(c4d.VECTOR_X, c4d.DTYPE_REAL, 0) # x is the red channel
                          dlvl_color = c4d.DescLevel(10100, c4d.DTYPE_COLOR, c4d.CUSTOMDATATYPE_GRADIENT)
                          dlvl_inten = c4d.DescLevel(10101, c4d.DTYPE_REAL, c4d.CUSTOMDATATYPE_GRADIENT)
                          
                          # the two DescIDs to build the CTracks
                          did_col_r = c4d.DescID(dlvl_grad, dlvl_color, dlvl_vecx)
                          did_inten = c4d.DescID(dlvl_grad, dlvl_inten)
                          
                          # the rest should be obvious
                          ctrack_col_r = c4d.CTrack(op, did_col_r)
                          ctrack_intens = c4d.CTrack(op, did_inten)
                          
                          dic_col_r = ctrack_col_r.GetCurve().AddKey(doc.GetTime())
                          dic_intens = ctrack_intens.GetCurve().AddKey(doc.GetTime())
                          
                          dic_col_r['key'].SetValue(ctrack_col_r.GetCurve(), 0.75)
                          dic_intens['key'].SetValue(ctrack_intens.GetCurve(), 0.25)
                          
                          op.InsertTrackSorted(ctrack_col_r)
                          op.InsertTrackSorted(ctrack_intens)
                          
                          # I do not know the proper message to invoke a gui_update for setting ctracks programmatically.
                          # You have either to open the Timeline manager or start the document playback to update the
                          # values, so that they are shown correctly in the AM.
                          op.Message(c4d.MSG_CHANGE)
                          op.Message(c4d.MSG_DESCRIPTION_CHECKUPDATE)
                          c4d.EventAdd()
                          
                      if __name__=='__main__':
                          main()
                      
                      1 Reply Last reply Reply Quote 0
                      • H Offline
                        Helper
                        last edited by

                        On 17/09/2013 at 23:06, xxxxxxxx wrote:

                        Hi Ferdinand,

                        Thank you for your next clear example.

                        I thought I'd need three DescLevels, at least.

                        I see a distinction new to me: the third element of the last two DescLevel (c4d.CUSTOMDATATYPE_GRADIENT)—the origin—refers to a previous DescLevel datatype. I see it, but not sure what this means and how you knew to do this. I can not seem to find any definition of what the origin means in the DescLevel structure. Looks like it might be the link from one DescLevel to the next?

                          
                            dlvl_grad = c4d.DescLevel(c4d.LIGHT_VISIBILITY_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT, 0)
                            dlvl_vecx = c4d.DescLevel(c4d.VECTOR_X, c4d.DTYPE_REAL, 0) # x is the red channel
                            dlvl_color = c4d.DescLevel(10100, c4d.DTYPE_COLOR, c4d.CUSTOMDATATYPE_GRADIENT)
                            dlvl_inten = c4d.DescLevel(10101, c4d.DTYPE_REAL, c4d.CUSTOMDATATYPE_GRADIENT)
                          
                        

                        I also don't yet see how this structure accommodates a gradient with three knots. In the Timeline, there is a folder that holds the three r, g, b tracks for each knot, another "level" in the organizing structure. There must be some mechanism for separating these tracks via the DescID and DescLevel classes.

                        And thank you for the reference to the file examiner software, which is also new to me. I am on a mac, but I found  equivalent software and will use it to look at the files.

                        Also, I did try several ways to update the display to see the values in r15, but none worked.  Even if I try to key the gradient by command-clicking in r15, I also get a gold dot, so something is off. I will send a note to Maxon about this. Given the number of attempts I've made to get the correct incantation for the DescIDs, I may have already solved the issue without knowing it due to a bug.

                        I will study your example and re-read everything.

                        Once again, thank you for your generous help.

                        Robert

                        1 Reply Last reply Reply Quote 0
                        • H Offline
                          Helper
                          last edited by

                          On 18/09/2013 at 01:09, xxxxxxxx wrote:

                          Hi,

                          Originally posted by xxxxxxxx

                          I see a distinction new to me: the third element of the last two DescLevel (c4d.CUSTOMDATATYPE_GRADIENT)—the origin—refers to a previous DescLevel datatype. I see it, but not sure what this means and how you knew to do this. I can not seem to find any definition of what the origin means in the DescLevel structure. Looks like it might be the link from one DescLevel to the next?

                          Nah, normally you do pass zero for t_creator. I did pass CUSTOMDATATYPE_GRADIENT,
                          because the DescLevel printed to the console was doing it that way, but the code does also 
                          work with a 0.

                          <c4d.CTrack object called 'Gradient . Interpolation/Track' with ID 5350 at 0x126ac83d0>
                          (1000, 15, 1000481)

                          1000481 is CUSTOMDATATYPE_GRADIENT, 15 is the DTYPE_REAL and the 1000ish number is some 
                          id in  the data type.

                          Originally posted by xxxxxxxx

                          I also don't yet see how this structure accommodates a gradient with three knots. In the Timeline, there is a folder that holds the three r, g, b tracks for each knot, another "level" in the organizing structure. There must be some mechanism for separating these tracks via the DescID and DescLevel classes.

                          The format is of course dynamic, there is not a fixed set of ids because you could have dozens
                          of knots. You can see the structure in your own console listing :

                          <c4d.CTrack object called 'Gradient . [2] Intensity/Track' with ID 5350 at 0x126ac8510>
                          (10201, 19, 1000481)
                          <c4d.CTrack object called 'Gradient . [3] Intensity/Track' with ID 5350 at 0x126ac8f30>
                          (10301, 19, 1000481)

                          Each knot has its own id range from n + 0 to n + 100. The general structure remains the same, so 
                          that 10n00 is always the color and 10n01 is always the intensity. The r,g,b values get a folder
                          because they are parented to that c4d.Vector DescLevel (the 2nd DescLevel in the triple). If
                          you do know the c4d api a bit better you can tell alone from looking at (1000, 19, 23) that
                          that DescLevel is not sufficient to identify an element, as all the plugin IDs are at least 7
                          digits long, while sub ids (which only do have a meaning in combination with their enclosing
                          plugin id) are usually 4-5 digits long.

                          I cannot help you with your R15 problem, as I do not have R15 installed. I can only say that 
                          DescLevels are a rather bumpy road in python (at least for me). In R14 DTYPE_BUTTON and 
                          DTYPE_FILENAME went rogue which could lead to freezes and crashes in extreme cases. Sadly 
                          these fundamental problems have not been fixed.

                          Happy rendering,
                          Ferdinand

                          1 Reply Last reply Reply Quote 0
                          • H Offline
                            Helper
                            last edited by

                            On 18/09/2013 at 04:26, xxxxxxxx wrote:

                            Thank you. I have the basic code working, even in r15.

                            The most helpful piece was explaining the second DescLevel and how it parents the last ones. I suppose we would see this pattern often.

                            Again, thanks for your generosity.

                            Here is the working code, should someone reference it.

                              
                              
                            import c4d  
                            from c4d.modules import mograph as mo  
                              
                            def CreateKey(obj,id,value,frame) :  
                                
                              if not obj.GetDocument() : raise Exception, "object must be in a document"  
                              
                              track=obj.FindCTrack(id)  
                              
                              if not track:  
                              
                                  track = c4d.CTrack(obj,id)  
                                  obj.InsertTrackSorted(track)  
                              
                              curve=track.GetCurve()  
                              key=curve.AddKey(c4d.BaseTime(frame,doc.GetFps()))  
                              
                              if type(value)==int or type(value)==float:  
                                    
                                  key["key"].SetValue(curve,value)  
                                    
                              else:  
                                    
                                  key["key"].SetGeData(curve,value)  
                                    
                              
                            def GetNextObject(obj) :  
                              if obj==None:  
                                  return None  
                               
                              if obj.GetDown() :  
                                  return obj.GetDown()  
                               
                              while not obj.GetNext() and obj.GetUp() :  
                                  obj = obj.GetUp()  
                               
                              return obj.GetNext()  
                              
                               
                            def main() :  
                                
                              obj = doc.GetFirstObject()  
                              if obj==None: return  
                              
                              descid = obj.GetType()      
                              while obj:  
                                  descid = obj.GetType()  
                              
                                  if descid == 5102: #If it is a light  
                                      lightObj = obj  
                                  if descid == 1018561:  
                                      shaderObj = obj  
                                        
                                  obj = GetNextObject(obj)  
                                    
                                    
                              r = float(64) / 255.0 #values to be read from database   
                              g = float(12) / 255.0  
                              b = float(123) / 255.0  
                                
                              #key the light: This produces a gold dot in r15, a red dot in r13  
                              CreateKey(lightObj, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0)), r, 0)  
                              CreateKey(lightObj, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0)), g, 0)   
                              CreateKey(lightObj, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0)), b, 0)   
                                
                              gradient = shaderObj[c4d.SLA_GRADIENT_GRADIENT] #take the object as it is already constructed for testing  
                              shader = shaderObj.GetFirstShader()  
                                
                              print shaderObj  
                              print shader  
                                  
                              #print out the IDs for reference  
                              for track in shader.GetCTracks() :  
                                  print track  
                                  print track.GetDescriptionID()  
                                    
                                  # the DescLevels we need  
                              dlvl_grad = c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT, 0)  
                              dlvl_color_k1 = c4d.DescLevel(10100, c4d.DTYPE_COLOR, c4d.CUSTOMDATATYPE_GRADIENT)  
                              dlvl_color_k2 = c4d.DescLevel(10200, c4d.DTYPE_COLOR, c4d.CUSTOMDATATYPE_GRADIENT)  
                              dlvl_color_k3 = c4d.DescLevel(10300, c4d.DTYPE_COLOR, c4d.CUSTOMDATATYPE_GRADIENT)  
                              dlvl_vec_r = c4d.DescLevel(c4d.VECTOR_X, c4d.DTYPE_REAL, 0) # x is the red channel  
                              dlvl_vec_g = c4d.DescLevel(c4d.VECTOR_Y, c4d.DTYPE_REAL, 0) # y is the green channel  
                              dlvl_vec_b = c4d.DescLevel(c4d.VECTOR_Z, c4d.DTYPE_REAL, 0) # z is the blue channel  
                                
                              did_r_k1 = c4d.DescID(dlvl_grad, dlvl_color_k1, dlvl_vec_r)  
                              did_g_k1 = c4d.DescID(dlvl_grad, dlvl_color_k1, dlvl_vec_g)  
                              did_b_k1 = c4d.DescID(dlvl_grad, dlvl_color_k1, dlvl_vec_b)  
                              
                              did_r_k2 = c4d.DescID(dlvl_grad, dlvl_color_k2, dlvl_vec_r)  
                              did_g_k2 = c4d.DescID(dlvl_grad, dlvl_color_k2, dlvl_vec_g)  
                              did_b_k2 = c4d.DescID(dlvl_grad, dlvl_color_k2, dlvl_vec_b)  
                              
                              did_r_k3 = c4d.DescID(dlvl_grad, dlvl_color_k3, dlvl_vec_r)  
                              did_g_k3 = c4d.DescID(dlvl_grad, dlvl_color_k3, dlvl_vec_g)  
                              did_b_k3 = c4d.DescID(dlvl_grad, dlvl_color_k3, dlvl_vec_b)  
                                
                              
                              #key the shader effector GRADIENT  
                                
                              #First gradient and knots 1, 2, and 3 at first keyframe  
                              CreateKey(shader, did_r_k1, 1.0,10)  
                              CreateKey(shader, did_g_k1, 0.0,10)  
                              CreateKey(shader, did_b_k1, 0.0,10)  
                                
                              CreateKey(shader, did_r_k2, 1.0,10)  
                              CreateKey(shader, did_g_k2, 1.0,10)  
                              CreateKey(shader, did_b_k2, 0.0,10)  
                              
                              CreateKey(shader, did_r_k3, 1.0,10)  
                              CreateKey(shader, did_g_k3, 0.0,10)  
                              CreateKey(shader, did_b_k3, 1.0,10)  
                                
                              #second gradient and knots 1, 2, and 3 at second keyframe  
                              CreateKey(shader, did_r_k1, 0.5,20)  
                              CreateKey(shader, did_g_k1, 1.0,20)  
                              CreateKey(shader, did_b_k1, 0.5,20)  
                                
                              CreateKey(shader, did_r_k2, 0.2,20)  
                              CreateKey(shader, did_g_k2, 1.0,20)  
                              CreateKey(shader, did_b_k2, 0.2,20)  
                              
                              CreateKey(shader, did_r_k3, 1.0,20)  
                              CreateKey(shader, did_g_k3, 0.7,20)  
                              CreateKey(shader, did_b_k3, 1.0,20)  
                              
                              c4d.EventAdd()  
                                
                            
                            1 Reply Last reply Reply Quote 0
                            • H Offline
                              Helper
                              last edited by

                              On 18/09/2013 at 11:14, xxxxxxxx wrote:

                              1 Reply Last reply Reply Quote 0
                              • H Offline
                                Helper
                                last edited by

                                On 18/09/2013 at 11:15, xxxxxxxx wrote:

                                I see some of the code didn't copy over. Here is the complete code:

                                  
                                import c4d  
                                from c4d.modules import mograph as mo  
                                  
                                def CreateKey(obj,id,value,frame) :  
                                    
                                  if not obj.GetDocument() : raise Exception, "object must be in a document"  
                                  
                                  track=obj.FindCTrack(id)  
                                  
                                  if not track:  
                                  
                                      track = c4d.CTrack(obj,id)  
                                      obj.InsertTrackSorted(track)  
                                  
                                  curve=track.GetCurve()  
                                  key=curve.AddKey(c4d.BaseTime(frame,doc.GetFps()))  
                                  
                                  if type(value)==int or type(value)==float:  
                                        
                                      key["key"].SetValue(curve,value)  
                                        
                                  else:  
                                        
                                      key["key"].SetGeData(curve,value)  
                                        
                                  
                                def GetNextObject(obj) :  
                                  if obj==None:  
                                      return None  
                                   
                                  if obj.GetDown() :  
                                      return obj.GetDown()  
                                   
                                  while not obj.GetNext() and obj.GetUp() :  
                                      obj = obj.GetUp()  
                                   
                                  return obj.GetNext()  
                                  
                                   
                                def main() :  
                                    
                                  obj = doc.GetFirstObject()  
                                  if obj==None: return  
                                  
                                  descid = obj.GetType()      
                                  while obj:  
                                      descid = obj.GetType()  
                                  
                                      if descid == 5102: #If it is a light  
                                          lightObj = obj  
                                      if descid == 1018561:  
                                          shaderObj = obj  
                                            
                                      obj = GetNextObject(obj)  
                                        
                                  
                                        
                                  r = float(64) / 255.0 #values to be read from database   
                                  g = float(12) / 255.0  
                                  b = float(123) / 255.0  
                                    
                                  #key the light: This produces a gold dot in r15, a red dot in r13  
                                  CreateKey(lightObj, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_X,c4d.DTYPE_REAL,0)), r, 0)  
                                  CreateKey(lightObj, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_Y,c4d.DTYPE_REAL,0)), g, 0)   
                                  CreateKey(lightObj, c4d.DescID(c4d.DescLevel(c4d.LIGHT_COLOR,c4d.DTYPE_COLOR,0), c4d.DescLevel(c4d.VECTOR_Z,c4d.DTYPE_REAL,0)), b, 0)   
                                    
                                  gradient = shaderObj[c4d.SLA_GRADIENT_GRADIENT] #take the object as it is already constructed for testing  
                                  shader = shaderObj.GetFirstShader()  
                                    
                                  print shaderObj  
                                  print shader  
                                      
                                  #print out the IDs for reference  
                                  for track in shader.GetCTracks() :  
                                      print track  
                                      print track.GetDescriptionID()  
                                        
                                      # the DescLevels we need  
                                  dlvl_grad = c4d.DescLevel(c4d.SLA_GRADIENT_GRADIENT, c4d.CUSTOMDATATYPE_GRADIENT, 0)  
                                  dlvl_color_k1 = c4d.DescLevel(10100, c4d.DTYPE_COLOR, c4d.CUSTOMDATATYPE_GRADIENT)  
                                  dlvl_color_k2 = c4d.DescLevel(10200, c4d.DTYPE_COLOR, c4d.CUSTOMDATATYPE_GRADIENT)  
                                  dlvl_color_k3 = c4d.DescLevel(10300, c4d.DTYPE_COLOR, c4d.CUSTOMDATATYPE_GRADIENT)  
                                  dlvl_vec_r = c4d.DescLevel(c4d.VECTOR_X, c4d.DTYPE_REAL, 0) # x is the red channel  
                                  dlvl_vec_g = c4d.DescLevel(c4d.VECTOR_Y, c4d.DTYPE_REAL, 0) # y is the green channel  
                                  dlvl_vec_b = c4d.DescLevel(c4d.VECTOR_Z, c4d.DTYPE_REAL, 0) # z is the blue channel  
                                    
                                  did_r_k1 = c4d.DescID(dlvl_grad, dlvl_color_k1, dlvl_vec_r)  
                                  did_g_k1 = c4d.DescID(dlvl_grad, dlvl_color_k1, dlvl_vec_g)  
                                  did_b_k1 = c4d.DescID(dlvl_grad, dlvl_color_k1, dlvl_vec_b)  
                                  
                                  did_r_k2 = c4d.DescID(dlvl_grad, dlvl_color_k2, dlvl_vec_r)  
                                  did_g_k2 = c4d.DescID(dlvl_grad, dlvl_color_k2, dlvl_vec_g)  
                                  did_b_k2 = c4d.DescID(dlvl_grad, dlvl_color_k2, dlvl_vec_b)  
                                  
                                  did_r_k3 = c4d.DescID(dlvl_grad, dlvl_color_k3, dlvl_vec_r)  
                                  did_g_k3 = c4d.DescID(dlvl_grad, dlvl_color_k3, dlvl_vec_g)  
                                  did_b_k3 = c4d.DescID(dlvl_grad, dlvl_color_k3, dlvl_vec_b)  
                                    
                                  
                                  #key the shader effector GRADIENT  
                                    
                                  #First gradient and knots 1, 2, and 3 at first keyframe  
                                  CreateKey(shader, did_r_k1, 1.0,10)  
                                  CreateKey(shader, did_g_k1, 0.0,10)  
                                  CreateKey(shader, did_b_k1, 0.0,10)  
                                    
                                  CreateKey(shader, did_r_k2, 1.0,10)  
                                  CreateKey(shader, did_g_k2, 1.0,10)  
                                  CreateKey(shader, did_b_k2, 0.0,10)  
                                  
                                  CreateKey(shader, did_r_k3, 1.0,10)  
                                  CreateKey(shader, did_g_k3, 0.0,10)  
                                  CreateKey(shader, did_b_k3, 1.0,10)  
                                    
                                  #second gradient and knots 1, 2, and 3 at second keyframe  
                                  CreateKey(shader, did_r_k1, 0.5,20)  
                                  CreateKey(shader, did_g_k1, 1.0,20)  
                                  CreateKey(shader, did_b_k1, 0.5,20)  
                                    
                                  CreateKey(shader, did_r_k2, 0.2,20)  
                                  CreateKey(shader, did_g_k2, 1.0,20)  
                                  CreateKey(shader, did_b_k2, 0.2,20)  
                                  
                                  CreateKey(shader, did_r_k3, 1.0,20)  
                                  CreateKey(shader, did_g_k3, 0.7,20)  
                                  CreateKey(shader, did_b_k3, 1.0,20)  
                                  
                                  c4d.EventAdd()  
                                    
                                if __name__=='__main__':  
                                  main()  
                                
                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post