How to access to self.face_idx_list. extracting a numpy.int64 type failed.
The difference between this article and the last one is numpy.array contains numpy.int64 instead of numpy.float64. We can access numpy.float64 value as the following:
vec[i] = boost::python::extract< float >( float32_3_obj.attr("__getitem__")(i));
I.e., we can convert numpy.float64 value to float value by boost::python::extract< float >(f). However, when we try to convert numpy.int64 to int as the following code, it fails.
vec[i] = boost::python::extract< int >(
int32_3_obj.attr("__getitem__")(i));
We can not convert numpy.int64 by boost::python::extract< int >(i). We will get the following error:
TypeError: No registered converter was able to produce a C++ rvalue of type int from this Python object of type numpy.int64.That's strange. float is OK, but int isn't. I looked up the web, there are some articles regarding with this issue. http://boost.2283326.n4.nabble.com/extracting-a-numpy-int32-type-td2701573.html I understand as that: numpy registered a conversion function from numpy.float64 to float, but, there is no conversion function registered numpy.int64 in 64bit machine. There was a suggestion to use python function int() to convert the numpy.int64 values. This works in the python world, but not in the C++ world.
I suggest the following code:
int vec[] = {0, 0, 0};This code first gets a numpy.int64 object as a python object, then uses numpy.int64's attribute method __int__ that converts numpy.int64 to int.
for(int i = 0; i < 3; ++i){
object int64_obj = int32_3_obj.attr("__getitem__")(i);
vec[i] = boost::python::extract< int >(
int64_obj.attr("__int__")());
}
This gives you information about the conversion numpy.int64 to int. But some of readers might wonder how to find this information. How to find this information seems more useful, actually it is easy, ask python.
The former article mentioned that all the python members are attributes, and the attributes can be seen themselves a sequence of the attributes using dir() function. Also we can use type() function to see the type. In this case, from the python interpreter, we can use type() to get the type information.
>>> import numpy>>> a = numpy.array([1,2,3])>>> type(a[0])Now we see the numpy.array element is 'numpy.int64' instead of 'int'. This type object is again python object, we can use dir() to get the attribute information. Then you can find an attribute called __int__. Let's use that attribute and see the type of it:
>>> type(a[0].__int__())Now we know this is the conversion function we are looking for Therefore, we can use this conversion attribute method to get the int object. When we have int object, then we can use extract< int > to get a C++ int.
This is the basic of how to access to a user defined python object from C++ code.
I hope this article is useful to you.
Comments