Jump to content

Help converting C.O.F.F.E.E. to Python


ninjad
Go to solution Solved by jed,

Recommended Posts

I'm trying to salvage a C.O.F.F.E.E. script from an old project. The script translates position data from a controller object to percent values which are fed into pose morph tags. Here's the code:

main()
{
var Range = 20 / sqrt(2);

var Dist_NE = abs(-(In_X) + -(In_Y) + 20) / sqrt(2);
var Dist_NW = abs(In_X + -(In_Y) + 20) / sqrt(2);
var Dist_SE = abs(-(In_X) + In_Y + 20) / sqrt(2);
var Dist_SW = abs(In_X + In_Y + 20) / sqrt(2);

var Perc_NE = 1 - (Dist_NE / Range);
var Perc_NW = 1 - (Dist_NW / Range);
var Perc_SE = 1 - (Dist_SE / Range);
var Perc_SW = 1 - (Dist_SW / Range);

if (Perc_NE > 0) {Out_NE = Perc_NE;} else {Out_NE = 0;}
if (Perc_NW > 0) {Out_NW = Perc_NW;} else {Out_NW = 0;}
if (Perc_SE > 0) {Out_SE = Perc_SE;} else {Out_SE = 0;}
if (Perc_SW > 0) {Out_SW = Perc_SW;} else {Out_SW = 0;}
}

 

Attached is the project file. The code can be found in the Xpresso tag in a node titled "Poses.Calc". You can select the "Lips.Ctrl" or "Bite.Ctrl" objects and play around with it, but you'll need a copy of C4D R19 or older.

180524-facelkjaf~5-coffee_script.c4d

Link to comment
  • Solution

I don't understand your scene, but I've translated the code. Things move and there's no error messages 😀

 

import c4d
import math


def main():
    global Out_NE, Out_NW, Out_SE, Out_SW

    Range = 20 / math.sqrt(2)

    Dist_NE = abs(-In_X -In_Y + 20) / math.sqrt(2)
    Dist_NW = abs(In_X -In_Y + 20) / math.sqrt(2)
    Dist_SE = abs(-In_X + In_Y + 20) / math.sqrt(2)
    Dist_SW = abs(In_X + In_Y + 20) / math.sqrt(2)

    Perc_NE = 1 - (Dist_NE / Range)
    Perc_NW = 1 - (Dist_NW / Range)
    Perc_SE = 1 - (Dist_SE / Range)
    Perc_SW = 1 - (Dist_SW / Range)

    if Perc_NE > 0:
        Out_NE = Perc_NE
    else:
        Out_NE = 0

    if Perc_NW > 0:
        Out_NW = Perc_NW
    else:
        Out_NW = 0

    if Perc_SE > 0:
        Out_SE = Perc_SE
    else:
        Out_SE = 0

    if Perc_SW > 0:
        Out_SW = Perc_SW
    else:
        Out_SW = 0

 

 

180524-facelkjaf_5-coffee_script_0001.c4d

 

 

Link to comment

Wow, thanks for the quick reply, this works great!

 

I extracted the scene from a larger sketch, so it's just the lips and teeth for a cartoon character. The lips are animated with a pose morph tag, and the strengths are influenced by the controller in the script. I know C4D has a 2D Vector Field, User Data interface already, but using this controller allows you to record the movements with Cappucino, and also zoom in close to create smoother movements when recording.

 

If you know a better way to create multiple point controls I'm all ears.

 

Another quick question, is there a shortcut for adding variables in the script to the input/outputs of the node and vice versus, adding the input/outputs of the node as variables in the script.

 

I like how Xpresso allows you to drag & drop parameters of an object into the input/outputs of its node, I wish the Python node would have the same functionality.

Link to comment

re extra ports -

 

you can add input ports by clicking the LHS blue square and selecting the correct data type. Ditto output on the RHS. You can rename the ports to something meaningful and these are the variable names used in the script (don't use Python reserved words). Output ports must be declared global.

 

Python does have drag and drop, but slightly different to XPresso. If I had my code in a Python tag and wanted to control a cube's Y size, but didn't know the correct syntax, I can drag the relevant field into the Python window and it gives

 

Cube[c4d.PRIM_CUBE_LEN,c4d.VECTOR_Y]

 

then I'd edit the cube name to what my cube was called.

Link to comment
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.

×
×
  • Create New...

Copyright Core 4D © 2023 Powered by Invision Community