Converting COFFEE to Python



  • I'm trying to convert some C.O.F.F.E.E. script over to Python. What am I doing wrong here?

    import c4d
    #Welcome to the world of Python
    
    
    def main():
        global XA, YA, YB, ZB, alpha, K
        
        X_A = Input1
        Z_A = Input2
        Z_B = Input3
        Y_B = Input4
        alpha = Input5
       
        
        Z_A2 = Z_A * Z_A
        Z_B2 = Z_B * Z_B
        Y_B2 = Y_B * Y_B
        YA = X_A*sin(alpha) 
        YA2 = YA * YA
        XA = X_A * cos(alpha)
       
        R2 = Z_B2 + Y_B2
        K = Z_A*Z_B
        K2 = K * K
        YB = (K * YA + Z_A * sqrt(YA2 * R2 + Z_A2 * R2-K2))/(YA2 + Z_A2)
        ZB = (K - YA * YB) / Z_A
    

    Here's what I'm trying to replicate:

    main()
    {
        var Z_B,Y_B,X_A,Z_A,Z_B2,Y_B2,Z_A2,YA2,alpha,K,K2,R2;
    
        X_A=Input1;
        Z_A=Input2;
        Z_B=Input3;
        Y_B=Input4;
        alpha=Input5;
        
    // non riesco a trovare una tabella con tutte le funzioni
    // compatibili in COFFEE. Trovato nell'SDK.
    // es. 2^3 ---> pow(2,3) 
    // quando si eleva a potenza di 2 o 3 è più veloce la doppia
    // o tripla moltiplicazione
       Z_A2=Z_A*Z_A;Z_B2=Z_B*Z_B;
       Y_B2=Y_B*Y_B;
       YA=X_A*sin(alpha); YA2=YA*YA;
       XA=X_A*cos(alpha);
       
       R2=Z_B2+Y_B2;
       K=Z_A*Z_B;K2=K*K;
       YB=(K*YA+Z_A*sqrt(YA2*R2+Z_A2*R2-K2))/(YA2+Z_A2);
       ZB=(K-YA*YB)/Z_A;
    
    }
    


  • Hi @bnash, first of all, welcome in the plugincafe community.

    Regarding your issue, I would suggest you read Python Console Manual since the python console is the place to go when something unexpected happens in python, as it will print you all the python errors message.
    Looking at your code it seems that you are in an Xpresso tag (please include the context for your next post) if it's true, then I suggest you read Python Xpresso Node Manual.

    So regarding your issue, while with the previous information you should be able now to know what's wrong.
    All mathematical function that you use are not know from python, for that you have to import the math module.
    If you are new to python, modules are libraries of classes, functions in order to organize your code.

    So it will give us something like

    import c4d
    # Import the math module, so we can use the function defined inside the math module
    import math
    
    
    def main():
        global XA, YA, YB, ZB, alpha, K
        
        X_A = Input1
        Z_A = Input2
        Z_B = Input3
        Y_B = Input4
        alpha = Input5
       
        
        Z_A2 = Z_A * Z_A
        Z_B2 = Z_B * Z_B
        Y_B2 = Y_B * Y_B
        YA = X_A* math.sin(alpha) 
        YA2 = YA * YA
        XA = X_A * math.cos(alpha)
       
        R2 = Z_B2 + Y_B2
        K = Z_A*Z_B
        K2 = K * K
        YB = (K * YA + Z_A * math.sqrt(YA2 * R2 + Z_A2 * R2-K2))/(YA2 + Z_A2)
        ZB = (K - YA * YB) / Z_A
    

    Beside of that, you may be interested in the Tips to switch from C.O.F.F.E.E to Python page from the python documentation, to converts your code.

    Finally, I've set up your topic correctly using the Q&A New Functionality, don't worry since it's your first topic, but please do it for the next one.

    If you have any questions, please let me know.
    Cheers,
    Maxime.



  • Any news?
    I turned your topic as solved, but feel free to ask questions, or bump it again if you need it.

    Cheers,
    Maxime.


Log in to reply