def pyobj_to_gdobj(pyobj, steal_gdobj=True): if pyobj is None: return ffi.NULL elif isinstance(pyobj, bool): return godot_bool_alloc(1 if pyobj else 0) elif isinstance(pyobj, int): return godot_int_alloc(pyobj) elif isinstance(pyobj, float): return godot_real_alloc(pyobj) elif isinstance(pyobj, str): gdobj = godot_string_alloc(initialized=False) lib.godot_string_new_with_wide_string(gdobj, pyobj, -1) return gdobj elif isinstance(pyobj, BaseBuiltinWithGDObjOwnership): if steal_gdobj: return pyobj._gd_ptr else: return pyobj._copy_gdobj(pyobj._gd_ptr) elif isinstance(pyobj, BaseBuiltin): return pyobj._gd_ptr elif isinstance(pyobj, BaseObject): # TODO: copy ptr box ? return pyobj._gd_ptr else: raise TypeError("Cannot convert Python object `%s` into Godot object." % pyobj)
def new_uninitialized_gdobj(gdtype): # TODO: use dict to optimize this ? # It seems Godot encode Variant as type nil... if gdtype == lib.GODOT_VARIANT_TYPE_NIL: return godot_variant_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_BOOL: return godot_bool_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_INT: return godot_int_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_REAL: return godot_real_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_STRING: return godot_string_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_VECTOR2: return godot_vector2_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_RECT2: return godot_rect2_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_VECTOR3: return godot_vector3_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_TRANSFORM2D: return godot_transform2d_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_PLANE: return godot_plane_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_QUAT: return godot_quat_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_AABB: return godot_aabb_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_BASIS: return godot_basis_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_TRANSFORM: return godot_transform_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_COLOR: return godot_color_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_NODE_PATH: return godot_node_path_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_RID: return godot_rid_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_OBJECT: return godot_object_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_DICTIONARY: return godot_dictionary_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_ARRAY: return godot_array_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_BYTE_ARRAY: return godot_pool_byte_array_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_INT_ARRAY: return godot_pool_int_array_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_REAL_ARRAY: return godot_pool_real_array_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_STRING_ARRAY: return godot_pool_string_array_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_VECTOR2_ARRAY: return godot_pool_vector2_array_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_VECTOR3_ARRAY: return godot_pool_vector3_array_alloc() elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_COLOR_ARRAY: return godot_pool_color_array_alloc() else: raise TypeError( "Unknown Variant type `%s` (this should never happen !)" % gdtype)
def get_class_consts(cls, classname): consts = [] ret = godot_pool_string_array_alloc() lib.godot_pool_string_array_new(ret) gd_classname = godot_string_from_pyobj(classname) gd_true = godot_bool_alloc(True) args = ffi.new("void*[2]", [gd_classname, gd_true]) # 2nd arg should be false, which what we get by not initializing it lib.godot_method_bind_ptrcall(cls._meth_get_integer_constant_list, cls._instance, args, ret) for i in range(lib.godot_pool_string_array_size(ret)): godot_str = lib.godot_pool_string_array_get(ret, i) raw_str = lib.godot_string_wide_str(ffi.addressof(godot_str)) consts.append(ffi.string(raw_str)) return consts
def get_class_properties(cls, classname): properties = [] ret = godot_array_alloc() lib.godot_array_new(ret) gd_classname = godot_string_from_pyobj(classname) gd_true = godot_bool_alloc(True) args = ffi.new("void*[2]", [gd_classname, gd_true]) # 2nd arg should be false, which what we get by not initializing it lib.godot_method_bind_ptrcall(cls._meth_get_property_list, cls._instance, args, ret) for i in range(lib.godot_array_size(ret)): var = lib.godot_array_get(ret, i) gddict = lib.godot_variant_as_dictionary(ffi.addressof(var)) propdict = Dictionary.build_from_gdobj(gddict) properties.append(propdict) return properties