94{
95 std::string thisStr = "__this__";
96
97
98 if (!PyObject_HasAttrString(obj, thisStr.c_str()))
99 {
100 return nullptr;
101 }
102
103 PyObject *thisAttr = PyObject_GetAttrString(obj, thisStr.c_str());
104 if (thisAttr == nullptr)
105 {
106 return nullptr;
107 }
108
109#if PY_MAJOR_VERSION == 2
110 std::string str = PyString_AsString(thisAttr);
111#else
112 std::string str = PyUnicode_AsUTF8(thisAttr);
113#endif
114
115 if (str.size() < 21)
116 {
117 return nullptr;
118 }
119
120 char hex_address[32], *pEnd;
121 auto iter = str.find("_p_vtk");
122
123 if (iter == std::string::npos)
124 {
125 return nullptr;
126 }
127
128 auto className = str.substr(iter).find("vtk");
129 if (className == std::string::npos)
130 {
131 return nullptr;
132 }
133
134 long address = stol(str.substr(1, 17));
135 vtkObjectBase *vtkObject = (vtkObjectBase *)((void *)address);
136
137 if (vtkObject->IsA(str.substr(className).c_str()))
138 {
139 return vtkObject;
140 }
141
142 return nullptr;
143}