Vex typeid seems to be a mystery

   621   4   2
User Avatar
Member
6 posts
Joined: 7月 2012
Offline
So I was in a situation where I had the verify the data type of some values using a printf statement. So, for the first time, I come across the typeid() function. Which returns integer numbers instead of words like float, integer, vector 2, etc. So I searched google to find out if there is correspondence list somewhere which can tell me what number corresponds to what datatype. Scarcely do I find anything. I was almost troubled, like: no one is ever checking the return type of vex values; Is there another way"
Sidefx help docs also seldom mention it. So question is: am I missing something?
Anyway I went on to create my little vex snippet to tell what what IDs are what. Basically

vector myvar;
printf(%s,typeid(myvar)); which returns 3
------------------------------------------------
vector myvar;
printf(%s,typeid(myvar)); which returns 13

Or

int myvar;
printf(%s,typeid(myvar)); which returns 0
------------------------------------------------
int myvar;
printf(%s,typeid(myvar)); which returns 10

or

float myvar;
printf(%s,typeid(myvar)); which returns 1
------------------------------------------------
int myvar;
printf(%s,typeid(myvar)); which returns 11

and so on...
As you can see, there is a pattern where if the Id of a type is n, the array of that that same type will be 1n
Edited by Leozard - 2024年3月12日 14:28:12
User Avatar
Member
143 posts
Joined: 5月 2017
Offline
Oh glad to see this post, recently struggling with attritype [www.sidefx.com] to get a diffirence between a vector4 and a matrix2. But finally there is a solution H20

Here is a kind of wrapper function to get the name of the data type instead of the id:
string gettype(int id)
{
    string type[] = {"int", "float", 
                     "vector2", "vector3", "vector4", 
                     "matrix2", "matrix3", "matrix4",
                     "string", "dict",
                     "int_array", "float_array",
                     "vector2_array", "vector3_array", "vector4_array", 
                     "matrix2_array", "matrix3_array", "matrix4_array",
                     "string_array", "dict_array"};                          
   return type[id];
}
printf("%s \n", gettype(typeid(d@test)));

Here's a generic [www.sidefx.com] attempt, works only with none array types. Can't figure out how to impliment them?
#define GENERIC_MACRO(TYPE) \
string gettype(TYPE data) { \
    string type[] = {"int", "float", "vector2", "vector3", "vector4", "matrix2", "matrix3", "matrix4", "string", "dict"}; \
    return type[typeid(data)]; \
} \

GENERIC_MACRO(int)
GENERIC_MACRO(float)
GENERIC_MACRO(vector2)
GENERIC_MACRO(vector)
GENERIC_MACRO(vector4)
GENERIC_MACRO(matrix2)
GENERIC_MACRO(matrix3)
GENERIC_MACRO(matrix)
GENERIC_MACRO(string)
GENERIC_MACRO(dict)
#undef GENERIC_MACR
printf("%s \n", gettype(d@test));

Here is a list of the type names mapped to the id:
 0	- int
 1	- float
 2	- vector2
 3	- vector
 4	- vector4 
 5	- matrix2
 6	- matrix3
 7	- matrix
 8	- string
 9	- dict

10	- int array
11	- float array
12	- vector2 array
13	- vector array
14	- vector4 array
15	- matrix2 array
16	- matrix3 array
17	- matrix array
18	- string array
19	- dict array
Edited by viklc - 2024年3月12日 16:45:13
User Avatar
Member
6 posts
Joined: 7月 2012
Offline
Wow. Thanks Viklc. Would be nice to have that first function into some library(Like Python modules) that could be called in any wrangle node.
And your list of mapped ids seems to be exhaustive.
User Avatar
Member
143 posts
Joined: 5月 2017
Offline
You can use external vex libraries similar to python modules.

Under $HOUDINI_PATH (Win: C:\Users\*user\Documents\houdini20.0\) you can add a "vex" folder if it is not already there. Within the vex folder, create another folder with the name "include". Here you can save your external vex files with the "vfl" suffix.

For example:
$HOUDINI_PATH\vex\include\my_vexlib.vfl
int foobar(int a, b)
{
    return a + b;
}

Now this function can be called within a wrangle by importing the file with the "#include" preprocessor:
#include "my_vexlib.vfl"
int n = foobar(5, 10);
printf("%d \n", n);

Your can even define structs [www.sidefx.com] this way, which allows a kind of semi-object-oriented programming.

It is also possible to embed vfl files in others, which leads to more complex library structures.
Edited by viklc - 2024年3月15日 18:03:32
User Avatar
Member
6 posts
Joined: 7月 2012
Offline
Thanks Viklc. Trying that now.
  • Quick Links