Jump to content

Rhino

COFFEE Script node to Python

Recommended Posts

Hi there,

 

I am trying to convert an old Xpresso setup for use in R20, but unfortunately it contains some 2 coffee script nodes. I would be very grateful if someone could please help me convert the following 2 coffee scripts into python

 

SCRIPT NUMBER 1

main()
{
    Output1 = MinimizeAngle(Input1,Input2);
}

 

SCRIPT NUMBER 2

main()
{
    var InMatrix=new(Matrix);
    InMatrix->SetRotHPB(Rotation);
    InMatrix->SetV0(Position);
    NewMatrix=InMatrix->GetClone();
}

 

Any help would be greatly appreciated!

 

Many thanks,

 

 

Share this post


Link to post
Share on other sites

Here is the first

 

# Get the "shortest" angle between two vectors, provided via input ports Input1 and Input2,
# The result is sent to output port Output1
import c4d

def main():
    global Output1
    Output1 = c4d.utils.GetOptimalAngle(Input1, Input2, c4d.ROTATIONORDER_DEFAULT)

 

And the second one

As far as I can see this script is meant to create a matrix from a rotation and position, provided via input ports called Rotation and Position.

The output port is then NewMatrix. I am not 100% sure this is what it is supposed to be, but without the actual scene file where the Xpresso and nodes can be investigated from, it is hard to tell. So, I'll go with my first assumption.

The code for this is then

 

# construct a matrix from Position and Rotation,
# input ports are Position and Rotation, output prt is NewMatrix
import c4d

def main():
    NewMatrix = c4d.utils.HPBToMatrix(Rotation, order=ROTATIONORDER_DEFAULT)
    NewMatrix.off = Position

 

Share this post


Link to post
Share on other sites
  • Topic Author
  • Hi C4DS, thanks for the fast reply!

     

    I am trying to get the old CStools Mocam rig up and working on R20 (see attached). As far as I can see these are the only two coffee nodes that need replacing, (they do occur a few times however )

     

    The first script you provided seems to link up fine in the xpresso editor, however the second scripts output doesn't allow itself to be connected. Maybe it's the wrong type of output?

     

    Thanks again for your help! 

    MOCAM.c4d

    Share this post


    Link to post
    Share on other sites
    2 hours ago, Rhino said:

    The first script you provided seems to link up fine in the xpresso editor, however the second scripts output doesn't allow itself to be connected. Maybe it's the wrong type of output

    I can't seem to find the second COFFEE node you are referring to. All I see are 7 COFFEE nodes with the first script.

     

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • The second COFFEE node has been renamed to 'Generate Matrix' 

    Share this post


    Link to post
    Share on other sites

    @C4DS a related question. What does -> mean ? I don't think I've seen it in Python, but have seen it elsewhere.

    Share this post


    Link to post
    Share on other sites
    1 hour ago, jed said:

    @C4DS a related question. What does -> mean ? I don't think I've seen it in Python, but have seen it elsewhere.

     

    You could google this and find a better answer, but I'll try my best at explaining it.

     

    The "->" you see in COFFEE and other programming languages (C++), is a way to access an object's member or function.

    In the second script there is this "InMatrix" variable which refers to a matrix object, an instance of a matrix class. Be careful with "object". In this context it isn't a Cinema 4D mesh object or null object, nor deformer, nor generator, ... The object here is a "container" which holds some data and functions (members and methods in C++).

    The matrix class has a GetClone function which allows to create a copy of itself and pass it along.

    InMatrix->GetClone() thus means that from our InMatrix object, we call its internal function called GetClone, and the resulting copy is assigned to NewMatrix, a new object instantiated from the matrix class.

     

    EDIT:

    Well, for completeness. In C++ (other languages probably as well, but I am most familiar with C++) to access members and methods of an instantiated class or structure one can use "->" or sometimes just a dot.

    myObject->member

    myObject->method()

    - or -

    myOtherObject.member

    myOtherObject.method()

    It all depends how the object was instantiated, as a pointer or not. But I guess this all is getting beyond the scope of this forum.

     

    Share this post


    Link to post
    Share on other sites
    1 hour ago, Rhino said:

    The second COFFEE node has been renamed to 'Generate Matrix' 

    OK, got it.

    As I thought, the input ports are "Position" and "Rotation", both of type vector, while the output port is "NewMatrix" of type matrix.

    So, when replacing the COFFEE node with a Python Node you should create the appropriate input and output ports.

     

    With the first scripts the input and output ports are floating point values. By default, when you create a Python node in Xpresso you get 2 input ports ... of type Real (or floating point), and an output port, again of type Real.

    What you need to do for the second script is create a Python node, remove the input and output ports, and create new ones of the appropriate types.

    I don't think you can change the port type (at least you could not in the past), therefore you need to remove the existing ports and create new ones.

    Name them accordingly and you'll be set!

     

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • Thanks C4DS! All the nodes seem to link up fine now, and I have added the python scripts that you mention.

     

    Unfortunately they still seem to not be working... I have replaced all of the coffee nodes with the python versions. Is there something I am missing? (scene attached)

     

    Many thanks,

    MOCAM_R20v03.c4d

    Share this post


    Link to post
    Share on other sites

    I see a lot of yellow :cry:

     

    Some Python troubleshooting tips -

     

    There's a log of python errors in menu > script > console. If you have several Python nodes, you should rename them Python 1, Python 2 etc to see which node the error message refers to.

     

    While XPresso is very forgiving when you mix data types (eg connect integer to string) Python is not. You should go through all the Python nodes and check port data types. If you hover the mouse over a port it shows the data type. This must match what the code is expecting.

     

    Here the code expects a vector but the port is real

     

    errors.png.fc9e85e7c2cc2aa2f47a1cdd39feb927.png

     

    in this case you should double click the port to remove it and make a new one of type vector

     

    You can get info on unfamiliar functions using search at C4D Python SDK

     

    good luck !

    Share this post


    Link to post
    Share on other sites
  • Topic Author
  • Thanks for your help so far! I have managed to change the ports but there still seems to be a problem with this script

     

    # construct a matrix from Position and Rotation,
    # input ports are Position and Rotation, output prt is NewMatrix
    import c4d

    def main():
        NewMatrix = c4d.utils.HPBToMatrix(Rotation, order=ROTATIONORDER_DEFAULT)
        NewMatrix.off = Position

     

    The error coming up is as follows

     

    Traceback (most recent call last):

    File "Python_02, line 6, in main

    NameError: global name 'Rotation' is not defined

     

    Any ideas on how to fix this please?

    MOCAM_R20v04.c4d

    Share this post


    Link to post
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.
    Note: Your post will require moderator approval before it will be visible.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.


    • Recently Browsing   0 members

      No registered users viewing this page.

    YOUTUBE CHANNEL:

    ABOUT US:

    C4D Cafe is the largest CINEMA 4D community. We provide facilities for discussion, showcasing and learning our favorite software :)
    ×
    ×
    • Create New...