Matrix.__rmul__ Strange Error Message



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

    On 09/04/2011 at 14:06, xxxxxxxx wrote:

    Hi,

    When reading the documentation about c4d.Matrix.__rmul__ and c4d.Matrix.__mul__ (these are the LHS and RHS binary multplication operators in Python) it dawned upon me, that __rmul__ might be the equivalent for the GetMulV function in COFFEE, that is, if you muliply a matrix with a vector you get GetMulP equivalence but if you multiply a vector with a matrix (reverse order) you get the GetMulV equivalence.

    A hint came from the doc text saying that one order includes position information while the other doesn't.
    This seems like in COFFEE where GetMulV is for direction vectors and GetMulP is for points (position vectors).

    So then I set up a very simple test scene to test the two variants and compare them to the equivalent COFFEE code in order to check if my interpretation is correct. The test object is not in the world origin and is rotated. I used a COFFEE and Python expression tag since they validate immediatly.

    The result was that the COFFEE code returns two different vectors for GetMulV and GetMulP (as one might expect if the object is rotated and moved) but for Python the vectors returned are the same, while theoretically for

    matrix * vector
    

    and

    vector * matrix
    

    there should be different outcomes just like in COFFEE (if I understand the docs right). I mucked about and tried using inverse matrices and whatnot but couldn't figure out the equivalent code for a GetMulV operation in Python.

    So then I tried to call matrix.__mul__(v) and matrix.__rmul__(v) directly and I while __mul__ is working as expected for __rmul__ I got this error message:

    SystemError: /perforce_buildsystem_osx/.../c4d/PyVector/PyVector.cpp:628: bad argument to internal function
    

    The Python and COFFEE code is really simple, just some integer component vector that gets multiplied GetMulP and GetMulV style with the objects global matrix.



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

    On 10/04/2011 at 07:50, xxxxxxxx wrote:

    Can you please post an example that reproduces the error message?

    Please note, C4D uses the left handed coordinate system. __mul__ and __rmul__ have nothing to do with this, it depends on the order of the parameters how they are passed to the type. Actually you don't need to call them manually.

    c4d.Matrix.__mul__(v1, v2) same as => v1*v2
    c4d.Matrix.__rmul__(v1, v2) same as => v2*v1

    The following page contains information of operators in Python and how they are handled:
    http://docs.python.org/library/operator.html

    Coffee GetMulP => Python Vector*Matrix
    Coffee GetMulV => Python Vector^Matrix
    Coffee GetMulM => Python Matrix*Matrix

    Cheers, Sebastian



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

    On 10/04/2011 at 11:21, xxxxxxxx wrote:

    Originally posted by xxxxxxxx

    Can you please post an example that reproduces the error message?

    Sure thing, I will post it after my reply.

    Since I was vagely familiar with how __rmul__ and __mul__ are used within Python
    I assumed the following picture from how the documentation is written:

    Coffee GetMulP => Python Matrix*Vector
    Coffee GetMulV => Python Vector*Matrix
    Coffee GetMulM => Python Matrix*Matrix

    I was missing that the ^ operator is used in the second case and not
    a reverse order mult for GetMulV.

    So, thank you very much for clearing it up.

    Both code snippets now behave exactly the same, which will make it easier to translate
    between COFFEE and Python :)

    Cheers,

    Andre

    My initial Python code:

    import c4d   
    from c4d.utils import *   
      
    #Welcome to the world of Python   
      
    def formatVector(v) :   
        return "[%.6f,%.6f,%.6f]" % (v.x, v.y, v.z)   
      
    def main() :   
        tag = op   
        obj = tag.GetObject()   
           
        mg = obj.GetMg()   
      
        v = c4d.Vector(10, 45, -22)   
        KP = mg.__rmul__(v)   
        KV = mg.__mul__(v)   
      
        print "KP (Python) = %s" % formatVector(KP)   
        print "KV (Python) = %s" % formatVector(KV)   
    

    My corrected Python code (just the changed parts) :

      
        KP = v * mg   
        KV = v ^ mg   
    

    For comparison the COFFEE code that I checked against:

    main(doc,op) {   
            
         var mg = op->GetMg();   
            
         var v = vector(10, 45, -22);   
         var KP = mg->GetMulP(v);   
         var KV = mg->GetMulV(v);   
      
         println("KP (COFFEE) = " + tostring(KP));   
         println("KV (COFFEE) = " + tostring(KV));   
    }
    

Log in to reply