Our Modifications:

Relevant to Blender v2.31

The first step is to come up with a game plan. What is this plugin going to do, how are the users going to interact with it. For this example we will create a simple texture that creates a simple brick/block pattern.

Now we'll copy our generic plugin to cube.c and will fill in the gaps.

Its always a good idea to add some comments. First off tell users what the plugin does, where they can get a copy, who they should contact for bugs/improvements, and any licensing restrictions on the code. When using comments make sure you use /* */ style comments. The plugins are in C and some cCcompilers do not accept // style comments.

   Description: This plugin is a sample texture plugin that creates a simple
   brick/block pattern with it.

   It takes two values a brick size, and a mortar size.
   The brick size is the size of each brick.
   The mortar size is the mortar size in between bricks.

   Author: Kent Mein (mein@cs.umn.edu)
   Website: http://www.cs.umn.edu/~mein/blender/plugins
   Licensing: Public Domain
   Last Modified: Tue Oct 21 05:57:13 CDT 2003

Next we need to fill in the Name, you should really keep this the same as your .c file, preferably descriptive, less than 23 chars, no spaces, and all lowercase.

char name[24]= "cube.c";

We are going to keep this plugin simple, and only have one type that deals with intensity. So we need the following:

#define NR_TYPES        1
char stnames[NR_TYPES][16]= {"Default"};

For our user interface we are going to allow people to change; The size of the brick and mortar, as well as the intensity values returned for the brick and mortar. For that we need to edit the varstr and Cast. The Cast should have a variable for each entry in varstr.

/* Structure for buttons,
 *  butcode      name           default  min  max  Tool tip
VarStruct varstr[]= {
   {NUM|FLO,   "Brick",      .8,    0.1,   1.0, "Size of Cell"},
   {NUM|FLO,   "Mortar",     .1,    0.0,   0.4, "Size of boarder in cell"},
   {NUM|FLO,   "Brick Int",   1,    0.0,   1.0, "Color of Brick"},
   {NUM|FLO,   "Mortar Int",  0,    0.0,   1.0, "Color of Mortar"},

typedef struct Cast {
        float brick,mortar, bricki, mortari;
} Cast;

Now we need to fill in plugin_tex_doit, we basically want to break down our texture into "cells" which will consist of a brick and the mortar along the bottom edges of that brick. Then determine if we are in the brick or the mortar. The following code should do that.

int plugin_tex_doit(int stype, Cast *cast, float *texvec, float *dxt,
   float *dyt) {
   int c[3];
   float pos[3], cube;

   /* setup the size of our cell */
   cube = cast->brick + cast->mortar;

   /* we need to do is determine where we are inside of the current brick. */
   c[0] = (int)(texvec[0] / cube);
   c[1] = (int)(texvec[1] / cube);
   c[2] = (int)(texvec[2] / cube);

   pos[0] = ABS(texvec[0] - (c[0] * cube));
   pos[1] = ABS(texvec[1] - (c[1] * cube));
   pos[2] = ABS(texvec[2] - (c[2] * cube));

   /* Figure out if we are in a mortar position within the brick or not. */
   if ((pos[0] <= cast->mortar) || (pos[1] <= cast->mortar) ||
       (pos[2] <= cast->mortar)) {
      result[0] = cast->mortari;
   } else {
      result[0] = cast->bricki;
   return 0;

One thing to note, the ABS function is defined in a header in plugins/include. There are some other common functions there as well be sure to take a look at what's there.