[Bf-python] genutils

models at paposo.com models at paposo.com
Fri Nov 21 03:13:57 CET 2003


Maybe I off on this but: (Im totally off some say)
Doesn't this mean that you will pass a string into the function as a
parameter instead of using a constant defined in the module's dictionary?
ie. Texture.setThingy("clouds")
It looks like you are getting the integer value of a defined constant and
adding it to the bitmask for the texture based on a string passed to a
function. You could simply pass a constant defined in the module's
dictionary instead and you wouldn't have to check whether or not the string
was valid.
ie. Texture.setThingy(CLOUDS)
(Then again you could probably just pass an integer :|)  Just an idea.
However, that being said I think map utils might come in handy. Just make
them user-friendly :)
 (p.s. I'm assuming EXPP_TEX_FLAG_COLORBAND, etc. is a redefinition of a
constant in DNA_texture_types ? doh )

Example idea :)

PyObject *Texture_Init (void)
{
..........
 #undef EXPP_ADDCONST
 #define EXPP_ADDCONST(x) PyDict_SetItemString(dict, #x,
PyInt_FromLong(TEX_##x))
 EXPP_ADDCONST(COLORBAND);
 EXPP_ADDCONST(FLIPBLEND);
 EXPP_ADDCONST(NEGALPHA);
..............
}

static PyObject *Texture_setThingy(BPy_Texture *self, PyObject *args)
{
 short flagValue;

 if (!PyArg_ParseTuple(args, "h", &flagValue))
  return (EXPP_ReturnPyObjError (PyExc_TypeError,
      "expected enumerated constant "));
......................
if (type == TEX_COLORBAND)
  self->Texture->flag |= TEX_COLORBAND;
else if (type == TEX_FLIPBLEND)
  self->Texture->flag |= TEX_COLORBAND;
else if (type == TEX_NEGALPHA)
  self->Texture->flag |= TEX_NEGALPHA;
else
    return EXPP_ReturnPyObjError (PyExc_TypeError,
       "bad flag value");
................
}

Here you would need to do this:
import Blender
import Texture
from Texture import *

Texture.setThingy(COLORBAND)

If you type the wrong name it will throw an "I don't understand this string"
error. Otherwise it will XOR the defined constant into the flags value
inside of the texture data. You could also parse a list containing module
constants and do the same XORing thingy.

Sorry you wanted to know I think :) Keep up the great work!


----- Original Message -----
From: "alex mole" <nal at blueyonder.co.uk>
To: <bf-python at blender.org>
Sent: Thursday, November 20, 2003 7:22 PM
Subject: [Bf-python] genutils


> [sorry for the length of this email!]
>
> Hi
>
>
> Is it ok if i add some things to genutils.[ch]?
>
> I've written a few functions and made a structure that saves a *lot* of
> "if...else if..." code by mapping strings to integers, and this sort of
> code is needed in many places in the python module code. The code looks
> like this:
>
>
> ------------------ 8< ----------------------
>
> typedef struct {
>      const char *sval;
>      int ival;
> } EXPP_map_pair;
>
> /* maps must end with a pair that has NULL as sval */
>
> int EXPP_map_getIntVal (const EXPP_map_pair *map,
>                                      const char *sval, int *ival)
> {
>      while (map->sval)
>      {
>          if (STREQ(sval, map->sval))
>          {
>              *ival = map->ival;
>              return 1;
>          }
>          ++map;
>      }
>      return 0;
> }
>
> int EXPP_map_getShortVal (const EXPP_map_pair *map,
>                                          const char *sval, short *ival)
> {
>      while (map->sval)
>      {
>          if (STREQ(sval, map->sval))
>          {
>              *ival = map->ival;
>              return 1;
>          }
>          ++map;
>      }
>      return 0;
> }
>
> int EXPP_map_getStrVal (const EXPP_map_pair *map,
>                                          int ival, const char **sval)
> {
>      while (map->sval)
>      {
>          if (ival == map->ival)
>          {
>              *sval = map->sval;
>              return 1;
>          }
>          ++map;
>      }
>      return 0;
> }
>
>
> -------------------- 8< -----------------------
>
>
> and this can be used like:
>
>
> static const EXPP_map_pair tex_flag_map[] = {
>      { "ColorBand",  EXPP_TEX_FLAG_COLORBAND },
>      { "FlipBlend",  EXPP_TEX_FLAG_FLIPBLEND },
>      { "NegAlpha",   EXPP_TEX_FLAG_NEGALPHA },
>      { NULL, 0 }
> };
>
>
>
>      //flag names in sf[]
>      for (i=0; i<3; ++i)
>      {
>          if (!sf[i]) break;
>          if (!expp_tex_getShortVal(tex_flags_map, sf[i], &thisflag))
>              return EXPP_ReturnPyObjError (PyExc_ValueError,
>                                          "invalid texture flag name");
>          flags |= thisflag;
>      }
>
>
>
> If you compare this code to eg. Material_setMode(), I think you'll agree
> that this is much simpler and [probably] less error prone. Plus it has
> the advantage of automatically being able to work "backwards" and get
> strings from integers.
>
>
> I currently have this defined just in my Texture.c, but I would like to
> use it in MTex.c as well, and I'm sure that other places could benefit
> from this....
>
>
> let me know what you think :)
>
> alex
>
>
>
>
>
> ________________________________________________________________________
> This email has been scanned for all viruses by the MessageLabs Email
> Security System. For more information on a proactive email security
> service working around the clock, around the globe, visit
> http://www.messagelabs.com
> ________________________________________________________________________
> _______________________________________________
> Bf-python mailing list
> Bf-python at blender.org
> http://www.blender.org/mailman/listinfo/bf-python
>
>




More information about the Bf-python mailing list