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
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Accessing Lines in Sketch and Toon with Python

    Scheduled Pinned Locked Moved PYTHON Development
    3 Posts 0 Posters 963 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 27/07/2018 at 09:44, xxxxxxxx wrote:

      Hello,
      I would like to randomize each line/stroke's color for Sketch and Toon.

      I'm taking a look through the Modifiers in the Sketch and Toon material and I'm trying to wrap my head around the Python expression. When I change the color there, it changes the color of a pixel in the stroke, not the color of the entire stroke's line.

      If I use this in the Editor:
      def main() :
          return c4d.Vector(randint(0, 255) / 256.0, randint(0, 255) / 256.0, randint(0, 255) / 256.0)

      I get a line of colored noise:

      Can anyone help me randomize the stroke color please? Thank you!

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

        On 31/07/2018 at 06:27, xxxxxxxx wrote:

        Hi Blastframe, thanks for writing us.

        With regard to your question, first of all I warmly recommend to have a look at the Command section in this Cinema 4D Help page describing the Sketch and Toon outline color. There you'll be provided with the list of global variables that can be used inside the Python script to properly control the script execution.

        That said, using PntA and PntB, you can use the following code snippet to generate pseudo-random color per-stroke

        import c4d  
        #Welcome to the world of Python  
          
        POINT_OFFSET = 10000  
          
        # Given two points, calculates a pseudo-unique stroke index   
        # NOTE: the uniqueness depends on the offset value which should   
        # be greater than the maximum vertices point count
        def CalcIndex(a, b) :  
          p = 0  
          if a > b:  
              p = a * POINT_OFFSET + b    
          else:  
              p = b * POINT_OFFSET + a  
          return p  
          
        # Given an index and a component value, a pseudo-random number is returned
        # NOTE: the number returned is constant across frames and/or render buckets  
        def myrand(p, comp) :  
          f = float(p) / (256.0 + comp)  
          rand = f - int(f)  
          return rand  
          
        def main() :  
          p = CalcIndex(PntA, PntB)  
          return c4d.Vector(myrand(p, 0), myrand(p, 1), myrand(p, 2))  
        
        

        Beside the code, it's relevant to state that using a standard random generator - like randint() -, since the execution of the scripts happens per bucket and per frame you could end up in a stroke made of as many color as the number of buckets the stroke is rendered by. By using instead a pseudo-random function, which is in this case based on the index of the segment, you can end up with a consistent coloring across buckets and frames.

        Definitively the pseudo-number generator provided above is pretty simple but as a proof of concept it works as expected.

        Best, Riccardo

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

          On 31/07/2018 at 23:07, xxxxxxxx wrote:

          This was exactly what I was seeking. Thank you very much, Riccardo!

          Originally posted by xxxxxxxx

          Hi Blastframe, thanks for writing us.

          With regard to your question, first of all I warmly recommend to have a look at the Command section in this Cinema 4D Help page describing the Sketch and Toon outline color. There you'll be provided with the list of global variables that can be used inside the Python script to properly control the script execution.

          That said, using PntA and PntB, you can use the following code snippet to generate pseudo-random color per-stroke

          import c4d  
          #Welcome to the world of Python  
           
          POINT_OFFSET = 10000  
           
          # Given two points, calculates a pseudo-unique stroke index   
          # NOTE: the uniqueness depends on the offset value which should   
          # be greater than the maximum vertices point count
          def CalcIndex(a, b) :  
            p = 0  
            if a > b:  
                p = a * POINT_OFFSET + b    
            else:  
                p = b * POINT_OFFSET + a  
            return p  
           
          # Given an index and a component value, a pseudo-random number is returned
          # NOTE: the number returned is constant across frames and/or render buckets  
          def myrand(p, comp) :  
            f = float(p) / (256.0 + comp)  
            rand = f - int(f)  
            return rand  
           
          def main() :  
            p = CalcIndex(PntA, PntB)  
            return c4d.Vector(myrand(p, 0), myrand(p, 1), myrand(p, 2))  
          
          

          Beside the code, it's relevant to state that using a standard random generator - like randint() -, since the execution of the scripts happens per bucket and per frame you could end up in a stroke made of as many color as the number of buckets the stroke is rendered by. By using instead a pseudo-random function, which is in this case based on the index of the segment, you can end up with a consistent coloring across buckets and frames.

          Definitively the pseudo-number generator provided above is pretty simple but as a proof of concept it works as expected.

          Best, Riccardo

          1 Reply Last reply Reply Quote 0
          • First post
            Last post