Example #1
0
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)
Example #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)
        ret = godot_string_to_pyobj(ffi.addressof(raw))
        lib.godot_string_destroy(ffi.addressof(raw))
        return ret

    elif gdtype == lib.GODOT_VARIANT_TYPE_VECTOR2:
        raw = lib.godot_variant_as_vector2(p_gdvar)
        return godot_bindings_module.Vector2.build_from_gdobj(raw)

    elif gdtype == lib.GODOT_VARIANT_TYPE_RECT2:
        raw = lib.godot_variant_as_rect2(p_gdvar)
        return godot_bindings_module.Rect2.build_from_gdobj(raw)

    elif gdtype == lib.GODOT_VARIANT_TYPE_VECTOR3:
        raw = lib.godot_variant_as_vector3(p_gdvar)
        return godot_bindings_module.Vector3.build_from_gdobj(raw)

    elif gdtype == lib.GODOT_VARIANT_TYPE_TRANSFORM2D:
        raw = lib.godot_variant_as_transform2d(p_gdvar)
        return godot_bindings_module.Transform2D.build_from_gdobj(raw)

    elif gdtype == lib.GODOT_VARIANT_TYPE_PLANE:
        raw = lib.godot_variant_as_plane(p_gdvar)
        return godot_bindings_module.Plane.build_from_gdobj(raw)

    elif gdtype == lib.GODOT_VARIANT_TYPE_QUAT:
        raw = lib.godot_variant_as_quat(p_gdvar)
        return godot_bindings_module.Quat.build_from_gdobj(raw)

    elif gdtype == lib.GODOT_VARIANT_TYPE_AABB:
        raw = lib.godot_variant_as_aabb(p_gdvar)
        return godot_bindings_module.AABB.build_from_gdobj(raw)

    elif gdtype == lib.GODOT_VARIANT_TYPE_BASIS:
        raw = lib.godot_variant_as_basis(p_gdvar)
        return godot_bindings_module.Basis.build_from_gdobj(raw)

    elif gdtype == lib.GODOT_VARIANT_TYPE_TRANSFORM:
        raw = lib.godot_variant_as_transform(p_gdvar)
        return godot_bindings_module.Transform.build_from_gdobj(raw)

    elif gdtype == lib.GODOT_VARIANT_TYPE_COLOR:
        raw = lib.godot_variant_as_color(p_gdvar)
        return godot_bindings_module.Color.build_from_gdobj(raw)

    elif gdtype == lib.GODOT_VARIANT_TYPE_NODE_PATH:
        p_raw = godot_node_path_alloc(initialized=False)
        p_raw[0] = lib.godot_variant_as_node_path(p_gdvar)
        return godot_bindings_module.NodePath.build_from_gdobj(p_raw, steal=True)

    elif gdtype == lib.GODOT_VARIANT_TYPE_RID:
        raw = lib.godot_variant_as_rid(p_gdvar)
        return godot_bindings_module.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:
        p_raw = godot_dictionary_alloc(initialized=False)
        p_raw[0] = lib.godot_variant_as_dictionary(p_gdvar)
        return godot_bindings_module.Dictionary.build_from_gdobj(p_raw, steal=True)

    elif gdtype == lib.GODOT_VARIANT_TYPE_ARRAY:
        p_raw = godot_array_alloc(initialized=False)
        p_raw[0] = lib.godot_variant_as_array(p_gdvar)
        return godot_bindings_module.Array.build_from_gdobj(p_raw, steal=True)

    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_BYTE_ARRAY:
        p_raw = godot_pool_byte_array_alloc(initialized=False)
        p_raw[0] = lib.godot_variant_as_pool_byte_array(p_gdvar)
        return godot_bindings_module.PoolByteArray.build_from_gdobj(p_raw, steal=True)

    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_INT_ARRAY:
        p_raw = godot_pool_int_array_alloc(initialized=False)
        p_raw[0] = lib.godot_variant_as_pool_int_array(p_gdvar)
        return godot_bindings_module.PoolIntArray.build_from_gdobj(p_raw, steal=True)

    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_REAL_ARRAY:
        p_raw = godot_pool_real_array_alloc(initialized=False)
        p_raw[0] = lib.godot_variant_as_pool_real_array(p_gdvar)
        return godot_bindings_module.PoolRealArray.build_from_gdobj(p_raw, steal=True)

    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_STRING_ARRAY:
        p_raw = godot_pool_string_array_alloc(initialized=False)
        p_raw[0] = lib.godot_variant_as_pool_string_array(p_gdvar)
        return godot_bindings_module.PoolStringArray.build_from_gdobj(p_raw, steal=True)

    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_VECTOR2_ARRAY:
        p_raw = godot_pool_vector2_array_alloc(initialized=False)
        p_raw[0] = lib.godot_variant_as_pool_vector2_array(p_gdvar)
        return godot_bindings_module.PoolVector2Array.build_from_gdobj(
            p_raw, steal=True
        )

    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_VECTOR3_ARRAY:
        p_raw = godot_pool_vector3_array_alloc(initialized=False)
        p_raw[0] = lib.godot_variant_as_pool_vector3_array(p_gdvar)
        return godot_bindings_module.PoolVector3Array.build_from_gdobj(
            p_raw, steal=True
        )

    elif gdtype == lib.GODOT_VARIANT_TYPE_POOL_COLOR_ARRAY:
        p_raw = godot_pool_color_array_alloc(initialized=False)
        p_raw[0] = lib.godot_variant_as_pool_color_array(p_gdvar)
        return godot_bindings_module.PoolColorArray.build_from_gdobj(p_raw, steal=True)

    else:
        raise TypeError(
            "Unknown Variant type `%s` (this should never happen !)" % gdtype
        )