Float * DPoint or Point not working

edited October 2017 in Python scripting

Hi everyone. This is my first post. I'd like to collaborate on the project. But right now I am working from a Mac and I still haven't figured out a way to build for Macs so I could test code changes.

I found a bug in the Geometry API. The * override is not working. I get an error from python saying 'float' and 'DPoint' are not compatible operands of '*'. In my script, I fixed that manually with the following code in the beginning.


def mul(p, factor): newx, newy = factor * p.x, factor * p.y return pya.DPoint(newx, newy) pya.DPoint.__mul__ = mul pya.DPoint.__rmul__ = mul

Thomas

Comments

  • edited October 2017

    Hi Thomas,

    thanks for this hint.

    The other way is supported - "Point*Float" is allowed. "Float*Point" would be a method on "Float" in Ruby. Basically that's possible, but I don't like monkey-patching basic types too much. So I did not consider this way.

    So essentially, it's a matter of taste whether you want to write "5*DPoint" or just "DPoint*5". The result is the same.

    Matthias

  • edited August 2018

    This came full-circle. =) I actually submitted a patch that implements __rmul__.

    It is generally safe, because python checks the __mul__ method of the left operand first. If it returns NotImplemented, then it checks rmul on the right operand. I don't know if ruby has similar functionality.

Sign In or Register to comment.