Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware 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

    Reorder gradient knots by position

    Cinema 4D SDK
    c++ sdk
    2
    3
    483
    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.
    • mfersaouiM
      mfersaoui
      last edited by mfersaoui

      Hello,
      I'm searching a method to get gradient Knot by position not by index, the problem with gradient->GetKnot(i) is that when user change the knots position and knots order, the knot index stay the same for each knot.

      I had create the following python function to get knot by position and I sorted them from lower to higher knot position.

      def GetKnotByPos(gradient, index): 
          KnotDict = {}
          
          knotCount = gradient.GetKnotCount()
          
          for i in range(knotCount) :
              KnotDict[gradient.GetKnot(i)['pos']] = gradient.GetKnot(i)
          
          # dictionary’s keys select.
          Ks = list(KnotDict.keys())
          Ks.sort()
          return KnotDict[Ks[index]]['col']
      
      # Use
      gradient = op[c4d.MYOBJECT_GRADIENT]
      for index in range(gradient.GetKnotCount()):
          color = GetKnotByPos(gradient, index)
      
      

      I'm searching how to do the same thing but using c++.

      const Int32 knotCount = gradient->GetKnotCount();
      for (Int32 i = 0; i < knotCount; i++)
      {
      	GradientKnot knot = gradient->GetKnot(i);
      	const Vector color = knot.col;
      
      	result += String::VectorToString(color) + "\n";
      }
      
      1 Reply Last reply Reply Quote 0
      • S
        s_bach
        last edited by

        Hello,

        you can use a maxon::BaseArray to store elements in a list. You can use maxon::BaseSort to define a class that is able to sort the elements of such a BaseArray.

        In the case of GradientKnots, it could look like this:

        // custom BaseSort based class
        class GradientSort : public maxon::BaseSort<GradientSort, maxon::BASESORTFLAGS::NONE>
        {
        public:
          static inline Bool LessThan(GradientKnot a, GradientKnot b)
          {
            return a.pos < b.pos;
          }
        };
        
        const Int32 knotCount = gradient->GetKnotCount();
        
        // prepare array
        maxon::BaseArray<GradientKnot> knots;
        knots.EnsureCapacity(knotCount) iferr_return;
        
        // fill array
        for (Int32 i = 0; i < knotCount; i++)
        {
          GradientKnot knot = gradient->GetKnot(i);
          knots.Append(knot) iferr_return;
        }
        
        // sort the elements stored in the array
        GradientSort sort;
        sort.Sort(knots);
        
        // check order
        for (GradientKnot& knot : knots)
        {
          DiagnosticOutput("Pos: @", knot.pos);
        }
        

        best wishes,
        Sebastian

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        mfersaouiM 1 Reply Last reply Reply Quote 2
        • mfersaouiM
          mfersaoui @s_bach
          last edited by mfersaoui

          @s_bach

          Hi Sebastian,
          Perfect, thank you very much.

          Best regards,
          Mustapha

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