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

    Ternary Operator in Python

    PYTHON Development
    0
    8
    740
    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
      Helper
      last edited by

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

      On 26/07/2011 at 11:51, xxxxxxxx wrote:

      Hey guys,
      I can't seem to figure out the proper syntax python needs run ternary conditions.

      Here's an example that works in Coffee:

      main(doc,op)  
      {  
       var distance = 1.5; // the larger the number the farther the travel   
       var pos = op->GetAbsPos() ? vector(0,0,0) : vector(0,100,0) * distance;  
       op->SetAbsPos(pos);  
      }
      

      I can't seem to get this same code to work in python:

      import c4d  
      def main() :  
        distance = 1.5 # the larger the number the farther the travel   
        pos = op.GetAbsPos() if c4d.Vector(0,0,0) else c4d.Vector(0,100,0) * distance  
        op.SetAbsPos(pos)  
        c4d.EventAdd()  
        
      if __name__=='__main__':  
        main()
      

      What am I doing wrong?

      -ScottA

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

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

        On 26/07/2011 at 12:20, xxxxxxxx wrote:

        See this thread on StackOverflow

        Your example works for me.

        Btw, what do you want to reach with that tenary conditiom ? It seems senseless to me.

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

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

          On 26/07/2011 at 13:14, xxxxxxxx wrote:

          I'm trying to toggle things with it.
          The coffee example I posted toggles an object's Y position.

          I've looked at lots of examples. But I can only get a one way result in python.
          Here's an example of it only working in one direction. Which is not what I want:

          import c4d  
          from c4d import gui  
            
          def main() :  
            pos = op.GetAbsPos() if op.GetAbsPos==c4d.Vector(0,0,0) else c4d.Vector(0,100,0)  
            op.SetAbsPos(pos)  
            c4d.EventAdd()  
            
          if __name__=='__main__':  
            main()
          

          I can't find any examples on how to use it as a toggle.

          -ScottA

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

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

            On 26/07/2011 at 13:57, xxxxxxxx wrote:

            I don't understand what you are trying to achive.
            (I'm also not quite sure if the coffee ternary conditiom has the same structure ?)

            a if b else c means Give a if b is True, if not, give c. and is the same as

            if b:   
            a   
            else:   
            c
            

            Translating your code would mean the following:

            Set 'pos' to op.GetAbsPos if Vector(0) is True, if not, set 'pos' to Vector(0,100,0) * distance.
            Using a constant as 'b' is senseless. (Vector(0) is a constant)

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

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

              On 26/07/2011 at 14:23, xxxxxxxx wrote:

              I don't know how else to explain it.
              My coffee example does what I want. I'm simply looking for the python version of it.

              -ScottA

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

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

                On 26/07/2011 at 14:39, xxxxxxxx wrote:

                condition ? value if True : value if False Is this right for the coffee version ? (cant test it)
                I yes, you are doing it wrong in python.
                value if True if condition else v alue if False

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

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

                  On 27/07/2011 at 09:26, xxxxxxxx wrote:

                  Hy Scott,

                  here is a solution:

                    
                  import c4d  
                  def main() :  
                    distance = 1.5 # the larger the number the farther the travel   
                    pos =  c4d.Vector(0,100,0) * distance if (op.GetAbsPos() == c4d.Vector(0,0,0)) else c4d.Vector(0,0,0)  
                    op.SetAbsPos(pos)  
                    c4d.EventAdd()  
                    
                  if __name__=='__main__':  
                    main()  
                  

                  The reason for your confusion comes from the fact, that COFFEE has some "additional" information when it comes to comparisons. In the ternary operator you ask COFFEE to compare a vector to the state "is defined". COFFEE *knows... actually, a strange assumption* that only a zero-vector is (seen as) undefined. So the question for COFFEE is:

                  op->GetAbsPos() is defined ?

                  This will return 0 only for the zero-vector. That is the reason why it worked so far in COFFEE. In Python, this does not make sense. Python will just look if the variable if the condition is not 0 or None (ie. defined, which it always is, when given a vector).

                  Cheers,
                  maxx

                  Edit: explanation wasn't that accurate, up to date ...

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

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

                    On 27/07/2011 at 10:34, xxxxxxxx wrote:

                    Thanks a lot Maxx.🍺
                    That's exactly what I was searching for.

                    -ScottA

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