[SESI logo]

Prisms Development Toolkit Documentation

Side Effects Software Inc. 1997

Custom SOPs

Accepting Numeric Input

There are several other forms of input your SOP can take from a user. These are:
1. Toggles
A switch value which corresponds to a checkbox in the SOP. The returned value is 1 if the box is checked, 0 if not.
2. Strings
Character strings (such as are used by the Font SOP)
3. File Names
This gives the user a file chooser, with which to select a file name for input or output. There is one of these in the DXF SOP.
4. Object Names
This presents a chooser with a list of all objects in the currently running action, and allows the user to choose one. The Magnet SOP uses this.
5. Function Execution Button
The user is presented with a button, which when pushed executes a function. Usually this is used to initialize or reinitialize the state of the SOP, eg Skeleton.

Source Listing


#include <custom.h>			/* you must have this! */

    .
    .	ommitted SOP code
    .

/*
 * Define the amount of space used by the sop data and name the fields
 */
typedef struct sop_data 
{
    SOP_EXPR	Single;
    SOP_EXPR	Double[2];
    SOP_EXPR	Triple[3];
    SOP_EXPR	Menu;
    SOP_EXPR	UserMenu;
    
    SOP_EXPR	Toggle;
    SOP_EXPR    String;
    SOP_EXPR	File;
    SOP_EXPR	Function;
    SOP_EXPR	Object;
    

    /*
     * You can stick any private data you want after the declaration
     * of the SOP_EXPR fields.  Here, we keep track of the number of
     * times the SOP has been cooked (for the $COOK local variable)
     */
    int		cookCount;
} SOP_DATA;

/*
 * Creata a structure that the sop library can use to figure out 
 * what is in your SOP_DATA struct. NOTE: This structure MUST
 * correspond to the one above.
 */
static SOP_PARMS	sopParms[] =
{
    { SOPPARM_EXPR,	"Single Label",	NULL,  {NULL} },
    { SOPPARM_DOUBLE,	"Double Label",	NULL , {NULL} },
    { SOPPARM_DOUBLE,	"Ignored",	NULL , {NULL} },
    { SOPPARM_TRIPLE,	"Triple Label",	NULL , {NULL} },
    { SOPPARM_TRIPLE,	"Ignored",	NULL , {NULL} },
    { SOPPARM_TRIPLE,	"Ignored",	NULL , {NULL} },
    { SOPPARM_MENU,	"Menu Label",	NULL , {NULL} },
    { SOPPARM_USERMENU,	"UserMenu Label",	NULL , {NULL} },
    
    { SOPPARM_TOGGLE,	"Toggle Label",	NULL , {NULL} },
    { SOPPARM_STR,	"String Label",	NULL,  {NULL} },
    { SOPPARM_FILE,	"File Label",	NULL , {NULL} },
    { SOPPARM_FUNC,	"Func Label",	NULL , {NULL} },
    { SOPPARM_OBJECT,	"Object Label",	NULL , {NULL} },
    
    { NULL },
};

/*
 * This function is used when creating a new sop. It sets up the
 * initial values of the sop.
 */
static void
sopDefaults(SOP *sop)		/* Put in default values */
{
    SOP_DATA	*sb;

    COOKVAR.fval = 0;
    if (sop->special) free(sop->special);
    sb = (SOP_DATA *)Calloc(sizeof(SOP_DATA), "Sample sopDefaults");
    sop->special = (char *)sb;

    sop->nsources = NSOURCES;
    sop->sopin[0].type = SOURCE_PREV;

    InitSopExpr(&sb->Single, "$COOK");
    InitSopExpr(&sb->Double[0], "2");
    InitSopExpr(&sb->Double[1], "3");
    InitSopExpr(&sb->Triple[0], "4");
    InitSopExpr(&sb->Triple[1], "5");
    InitSopExpr(&sb->Triple[2], "6");
    InitSopMenu(&sb->Menu, menuItems, ITEM1, "Menu Title");
    
    InitSopToggle(&sb->Toggle, "Toggel label2");
    InitSopString(&sb->String, "Default String");
    InitSopFile(&sb->File, "Default File");
    InitSopObject(&sb->Object, "");
    
    /* 
     * Make a UserMenu -- A field with a pop-up menu at the end.
     * The creation and behaviour of the menu are in doUserMenu
     */
    InitSopUserMenu(&sb->UserMenu, "NOTHING", doUserMenu);
    
    /*
     * I am making a button that will call a function when pressed
     * in this case it calls sopDefaults (ie resets the sop)
     * You could put some other function in there
     */
    InitSopFunc(&sb->Function, "Set to Defaults", sopDefaults);
    

    /*
     * Now we initialize any "private" data
     */
    sb->cookCount = 0;
}

/*
 * This function is used to read data into the sop.
 * You should make sure that its format matches that which is 
 * output by sopPrint
 */
static void
sopScan(SOP *sop, int argc, char *argv[])
{
    SOP_DATA	*sb;	

    sb = (SOP_DATA *) sop->special;	

    
    /* 
     * Arguments to SopScan... are:
     *		character to be used for option (see Note 1)
     *		Pointer to storage area	in SOP
     *		The SOP pointer
     * Note :   The following option characters are reserved 
     *		and shouldn't be used:
     *		a,s,H,I,J,K,L,M,N,Y,Z,o,f  
     */
    SopScanToggle  ('T', &sb->Toggle,   sop);
    SopScanString  ('S', &sb->String,   sop);
    SopScanFile    ('F', &sb->File,     sop);
    SopScanObject  ('o', &sb->Object,	sop);
    
    SopScanExpr    ('e', &sb->Single,   sop);
    SopScanDouble  ('d',  sb->Double,   sop);
    SopScanTriple  ('t',  sb->Triple,   sop);
    SopScanMenu    ('m', &sb->Menu,     menuTexts, 6);
    SopScanUserMenu('u', &sb->UserMenu, sop);
}

/*
 * This function is used to output the sops data to a file.
 * The data will later be read back in by sopScan.
 */
static void
sopPrint(FILE *fp, SOP *sop)
{
    SOP_DATA	*sb;
    sb = (SOP_DATA *)sop->special;

    
    /*
     * Arguments to SopPrint... are:
     *		File Pointer
     *		Data Pointer
     *		Default values (NOTE: Double has 2, 
     *			and Triple has 3, Toggle 0)
     *		The string that precedes the data
     * 	Data is only printed if different from the default.
     */
    SopPrintToggle  (fp, &sb->Toggle,   "-T");
    SopPrintString  (fp, &sb->String,   "", "-S");
    SopPrintFile    (fp, &sb->File,     "", "-F");
    SopPrintObject  (fp, &sb->Object,	"", "-o");
    
    SopPrintExpr    (fp, &sb->Single,   "", "-e");
    SopPrintDouble  (fp,  sb->Double,   "", "", "-d");
    SopPrintTriple  (fp,  sb->Triple,   "", "", "", "-t");
    SopPrintMenu    (fp, &sb->Menu,     menuTexts, "", "-m");
    SopPrintUserMenu(fp, &sb->UserMenu, "", "-u");
}

static void
sopCook(SOP *sop)	/* This is where you build a PDP */
{
    SOP_DATA	*sb;
    OBJECT	*obp;
    char	*expanded;
    int		 idx;
    PolyDetail	*sourcePdp;
    PolyPointPointer	tmppt;
    int		npts;


    sb = (SOP_DATA *)sop->special;
    sb->cookCount++;
    COOKVAR.fval = sb->cookCount;  

    if (sop->pdp) FreePolyDetail(sop->pdp);
    sop->pdp = 0;

    /* GETSOURCE will check that you have a 
	valid source and gets its pdp */
    GETSOURCE(sourcePdp, sop, 0, "Invalid source SOP");

    /* Always copy the input and modify the copy, never the source */
    sop->pdp = PolyCopyDetail(sourcePdp);

    printf("\n\nPrinting the State of Sample's gadgets:\n");

    
    /* 
     * calling SopExpandString will expand any variables in the string
     * and flag the SOP as frame dependent if needed.  Do not free
     * the string returned.
     */
    expanded = SopExpandString(sop, sb->String.value.sval);
    printf("String : %s\n", expanded);

    expanded = SopExpandString(sop, sb->File.value.sval);
    printf("File : %s\n", expanded);

    /*
     * calling EVALUATE scans the expression, evaluates variables, etc
     * and puts the resulting value in the Expression's .value.fval
     * field.
     * EVALUATELV should be called if the sop has local variables.
     * Since we have one defined, we use EVALUATELV in this example.
     */

    /*
     * As an illustration, cause a SOP error if the toggle is on
     */
    if (sb->Toggle.value.ival != 0)
	SOPERROR(sop, "The toggle is on. Causing this error.");

    if (isstring(sb->Object.expr->source))
    {
	EVALUATELV(sop, sb->Object)
	obp = GetObjectKey((int)sb->Object.value.fval);
	if (obp) printf("Object is %s\n", obp->name);
	else	 printf("No object selected\n");
    }
    

    EVALUATELV(sop, sb->Single);
    printf("Single : %g\n", sb->Single.value.fval);

    EVALUATELV(sop, sb->Double[0]);
    EVALUATELV(sop, sb->Double[1]);
    printf("Double : (%g, %g)\n", sb->Double[0].value.fval,
				  sb->Double[1].value.fval);

    EVALUATELV(sop, sb->Triple[0]);
    EVALUATELV(sop, sb->Triple[1]);
    EVALUATELV(sop, sb->Triple[2]);
    printf("Triple : (%g, %g, %g)\n", sb->Triple[0].value.fval,
				      sb->Triple[1].value.fval,
				      sb->Triple[2].value.fval);

    idx = sb->Menu.value.ival;
    printf("Menu : %d = %s\n", idx, menuItems[idx].menu);
    printf("UserMenu's field: %s\n", sb->UserMenu.value.sval);

    /*
     *	There are macros used to traverse lists. Here, we multiply each
     *	point's (x,y,z) values by 0.5. You will probably do something
     * 	more useful.
     */
    FOR_ALL_POINTS(sop->pdp->ppt, tmppt, npts)
    {
	tmppt->pos.x *= 0.5;
	tmppt->pos.y *= 0.5;
	tmppt->pos.z *= 0.5;
    }

    CLEARSOPNML(sop)
	printf("after sop cook\n");
}

#define SOPUSAGE	\
"severything -S String -F File -e Single -d double1 double2 \
-t triple1 triple2 \
triple 3 -T -m menuItem -o Object -u UserMenu\n\
\t-s    Just a string\
\tetc\n"

SOP_FUNC	EverythingSopRef = {
	"everything",
	"S:F:e:d::t:::Tm:u:",
	sopScan, sopPrint, sopCook, sopDefaults, 
	sopClean, sopGPMake, sopGetParms,
	SOPUSAGE,
};

Notes:

1. The syntax of the second line in the SOP_FUNC structure, which in this example is "S:F:e:d::t:::TM:u:" has the following syntax:
First, the choice of letter to describe each argument can be any single letter, except for the action reserved ones, which are
a f o s H I J K L M N Y Z
Second, each letter is followed by one or more colons ( : ), which indicate how many arguments that parameter expects. So -T, which is a toggle, takes no arguments, and is followed by zero colons. -d, which takes two arguments, is followed by two colons, etc.


For More Detailed Information:

The sopParms table and SOP_DATA structures
The sopDefault routine
The sopScan routine
The sopPrint routine
The SOP_FUNC reference structure

Tookit Overview
Expression Functions | User Functions | Custom SOPs
Prisms Internal Format | Programmer's Reference
Compiling SOPs | Using sopbuild | SOP Routine Reference
Dynamic SOPs | Prisms Types | Macros
Math Routines | Data Routines | Polygon Routines | Winged-edge Routines

Copyright © 1997 Side Effects Software Inc.
477 Richmond Street West, Toronto, Ontario, Canada M5V 3E7