示例#1
0
def pyobj_to_variant(pyobj, p_gdvar=None):
    # `initialized=False` means we MUST manually init this by hand no matter what
    p_gdvar = p_gdvar if p_gdvar else godot_variant_alloc(initialized=False)
    try:
        if pyobj is None:
            lib.godot_variant_new_nil(p_gdvar)
        elif (isinstance(pyobj, bool)):
            lib.godot_variant_new_bool(p_gdvar, pyobj)
        elif (isinstance(pyobj, int)):
            lib.godot_variant_new_int(p_gdvar, pyobj)
        elif (isinstance(pyobj, float)):
            lib.godot_variant_new_real(p_gdvar, pyobj)
        elif (isinstance(pyobj, str)):
            gdstr = ffi.new("godot_string*")
            pyobj_as_bytes = pyobj.encode()
            lib.godot_string_new_data(gdstr, pyobj_as_bytes,
                                      len(pyobj_as_bytes))
            lib.godot_variant_new_string(p_gdvar, gdstr)
        elif (isinstance(pyobj, bytes)):
            gdstr = ffi.new("godot_string*")
            lib.godot_string_new_data(gdstr, pyobj, len(pyobj))
            lib.godot_variant_new_string(p_gdvar, gdstr)
        elif isinstance(pyobj, BaseBuiltin):
            if pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_VECTOR2:
                lib.godot_variant_new_vector2(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_RECT2:
                lib.godot_variant_new_rect2(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_VECTOR3:
                lib.godot_variant_new_vector3(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_TRANSFORM2D:
                lib.godot_variant_new_transform2d(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_PLANE:
                lib.godot_variant_new_plane(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_QUAT:
                lib.godot_variant_new_quat(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_AABB:
                lib.godot_variant_new_aabb(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_BASIS:
                lib.godot_variant_new_basis(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_TRANSFORM:
                lib.godot_variant_new_transform(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_COLOR:
                lib.godot_variant_new_color(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_NODE_PATH:
                lib.godot_variant_new_nodepath(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_RID:
                lib.godot_variant_new_rid(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_OBJECT:
                lib.godot_variant_new_object(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_DICTIONARY:
                lib.godot_variant_new_dictionary(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_ARRAY:
                lib.godot_variant_new_array(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOLBYTEARRAY:
                lib.godot_variant_new_poolbytearray(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOLINTARRAY:
                lib.godot_variant_new_poolintarray(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOLREALARRAY:
                lib.godot_variant_new_poolrealarray(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOLSTRINGARRAY:
                lib.godot_variant_new_poolstringarray(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOLVECTOR2ARRAY:
                lib.godot_variant_new_poolvector2array(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOLVECTOR3ARRAY:
                lib.godot_variant_new_poolvector3array(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOLCOLORARRAY:
                lib.godot_variant_new_poolcolorarray(p_gdvar, pyobj._gd_ptr)
        elif isinstance(pyobj, BaseObject):
            lib.godot_variant_new_object(p_gdvar, pyobj._gd_ptr)
        else:
            raise TypeError("Cannot convert `%s` to Godot's Variant" % pyobj)
    except:
        # Must init the variant anyway to avoid segfault in it destructor
        lib.godot_variant_new_nil(p_gdvar)
        raise
    return p_gdvar
示例#2
0
def pyobj_to_variant(pyobj, p_gdvar=None, for_ffi_return=False):
    """
    `initialized=False` means we MUST manually init this by hand no matter what

    `for_ffi_return=True` means the returned variant won't have it destructor called
    once it is garbage collected by python.
    This is typically what we want when we pass the variant object by copy as a
    return value to Godot (which is then in charge of calling the destructor itself).
    """
    if not p_gdvar:
        if for_ffi_return:
            p_gdvar = ffi.new("godot_variant*")
        else:
            p_gdvar = godot_variant_alloc(initialized=False)
    try:
        if pyobj is None:
            lib.godot_variant_new_nil(p_gdvar)
        elif (isinstance(pyobj, bool)):
            lib.godot_variant_new_bool(p_gdvar, pyobj)
        elif (isinstance(pyobj, int)):
            lib.godot_variant_new_int(p_gdvar, pyobj)
        elif (isinstance(pyobj, float)):
            lib.godot_variant_new_real(p_gdvar, pyobj)
        elif (isinstance(pyobj, str)):
            gdstr = godot_string_alloc(initialized=False)
            lib.godot_string_new_with_wide_string(gdstr, pyobj, len(pyobj))
            lib.godot_variant_new_string(p_gdvar, gdstr)
        elif isinstance(pyobj, BaseBuiltin):
            if pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_VECTOR2:
                lib.godot_variant_new_vector2(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_RECT2:
                lib.godot_variant_new_rect2(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_VECTOR3:
                lib.godot_variant_new_vector3(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_TRANSFORM2D:
                lib.godot_variant_new_transform2d(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_PLANE:
                lib.godot_variant_new_plane(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_QUAT:
                lib.godot_variant_new_quat(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_AABB:
                lib.godot_variant_new_aabb(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_BASIS:
                lib.godot_variant_new_basis(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_TRANSFORM:
                lib.godot_variant_new_transform(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_COLOR:
                lib.godot_variant_new_color(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_NODE_PATH:
                lib.godot_variant_new_node_path(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_RID:
                lib.godot_variant_new_rid(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_OBJECT:
                lib.godot_variant_new_object(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_DICTIONARY:
                lib.godot_variant_new_dictionary(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_ARRAY:
                lib.godot_variant_new_array(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOL_BYTE_ARRAY:
                lib.godot_variant_new_pool_byte_array(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOL_INT_ARRAY:
                lib.godot_variant_new_pool_int_array(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOL_REAL_ARRAY:
                lib.godot_variant_new_pool_real_array(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOL_STRING_ARRAY:
                lib.godot_variant_new_pool_string_array(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOL_VECTOR2_ARRAY:
                lib.godot_variant_new_pool_vector2_array(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOL_VECTOR3_ARRAY:
                lib.godot_variant_new_pool_vector3_array(p_gdvar, pyobj._gd_ptr)
            elif pyobj.GD_TYPE == lib.GODOT_VARIANT_TYPE_POOL_COLOR_ARRAY:
                lib.godot_variant_new_pool_color_array(p_gdvar, pyobj._gd_ptr)
        elif isinstance(pyobj, BaseObject):
            lib.godot_variant_new_object(p_gdvar, pyobj._gd_ptr)
        else:
            raise TypeError("Cannot convert `%s` to Godot's Variant" % pyobj)

    except BaseException:
        # Must init the variant anyway to avoid segfault in it destructor
        lib.godot_variant_new_nil(p_gdvar)
        raise

    return p_gdvar