Esempio n. 1
0
 def constructor(self):
     gd_classname = godot_string_from_pyobj(classname)
     # TODO: alloc this on the stack (using _malloca ?)
     args = ffi.new("void*[]", [gd_classname])
     ret = godot_variant_alloc()
     lib.godot_method_bind_ptrcall(cls._meth_instance, cls._instance, args, ret)
     objret = lib.godot_variant_as_object(ret)
     # Quick'n dirty fix to prevent Ressource objects from beeing automatically
     # freed when the variant is destroyed given it holds the only ref on it
     self._gd_var = ret
     return objret
Esempio n. 2
0
def variant_to_pyobj(p_gdvar):
    """
    Convert Godot variant to regular Python object
    :param p_gdvar: Godot variant as ``<cdata 'struct godot_variant *'>`` (note the pointer)
    """
    gdtype = lib.godot_variant_get_type(p_gdvar)
    if gdtype == lib.GODOT_VARIANT_TYPE_NIL:
        return None
    elif gdtype == lib.GODOT_VARIANT_TYPE_BOOL:
        return bool(lib.godot_variant_as_bool(p_gdvar))
    elif gdtype == lib.GODOT_VARIANT_TYPE_INT:
        return int(lib.godot_variant_as_int(p_gdvar))
    elif gdtype == lib.GODOT_VARIANT_TYPE_REAL:
        return float(lib.godot_variant_as_real(p_gdvar))
    elif gdtype == lib.GODOT_VARIANT_TYPE_STRING:
        raw = lib.godot_variant_as_string(p_gdvar)
        return godot_string_to_pyobj(ffi.addressof(raw))
    elif gdtype == lib.GODOT_VARIANT_TYPE_VECTOR2:
        raw = lib.godot_variant_as_vector2(p_gdvar)
        return Vector2.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_RECT2:
        raw = lib.godot_variant_as_rec2(p_gdvar)
        return Rect2.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_VECTOR3:
        raw = lib.godot_variant_as_vector3(p_gdvar)
        return Vector3.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_TRANSFORM2D:
        raw = lib.godot_variant_as_transform2d(p_gdvar)
        return Transform2D.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_PLANE:
        raw = lib.godot_variant_as_plane(p_gdvar)
        return Plane.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_QUAT:
        raw = lib.godot_variant_as_quat(p_gdvar)
        return Quat.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_AABB:
        raw = lib.godot_variant_as_aabb(p_gdvar)
        return AABB.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_BASIS:
        raw = lib.godot_variant_as_basis(p_gdvar)
        return Basis.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_TRANSFORM:
        raw = lib.godot_variant_as_transform(p_gdvar)
        return Transform.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_COLOR:
        raw = lib.godot_variant_as_color(p_gdvar)
        return Color.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_NODE_PATH:
        raw = lib.godot_variant_as_node_path(p_gdvar)
        return NodePath.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_RID:
        raw = lib.godot_variant_as_rid(p_gdvar)
        return RID.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_OBJECT:
        p_raw = lib.godot_variant_as_object(p_gdvar)
        # TODO: optimize this
        tmpobj = godot_bindings_module.Object(p_raw)
        return getattr(godot_bindings_module, tmpobj.get_class())(p_raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_DICTIONARY:
        raw = lib.godot_variant_as_dictionary(p_gdvar)
        return Dictionary.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_ARRAY:
        raw = lib.godot_variant_as_array(p_gdvar)
        return Array.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_BYTE_ARRAY:
        raw = lib.godot_variant_as_pool_byte_array(p_gdvar)
        return PoolByteArray.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_INT_ARRAY:
        raw = lib.godot_variant_as_pool_int_array(p_gdvar)
        return PoolIntArray.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_REAL_ARRAY:
        raw = lib.godot_variant_as_pool_real_array(p_gdvar)
        return PoolRealArray.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_STRING_ARRAY:
        raw = lib.godot_variant_as_pool_string_array(p_gdvar)
        return PoolStringArray.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_VECTOR2_ARRAY:
        raw = lib.godot_variant_as_pool_vector2_array(p_gdvar)
        return PoolVector2Array.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_VECTOR3_ARRAY:
        raw = lib.godot_variant_as_pool_vector3_array(p_gdvar)
        return PoolVector3Array.build_from_gdobj(raw)
    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_COLOR_ARRAY:
        raw = lib.godot_variant_as_pool_color_array(p_gdvar)
        return PoolColorArray.build_from_gdobj(raw)
    else:
        raise TypeError(
            "Unknown Variant type `%s` (this should never happen !)" % gdtype)