Skip to content

ValueError: 9 is not a valid SpritePackingRotation #81

Closed

Description

@JulianWinter1124

Code

import os
import UnityPy
def unpack_all_assets(source_folder : str, destination_folder : str):
    # iterate over all files in source folder
    for root, dirs, files in os.walk(source_folder):
        for file_name in files:
            # generate file_path
            file_path = os.path.join(root, file_name)
            # load that file via UnityPy.load
            env = UnityPy.load(file_path)
            # iterate over internal objects
            for obj in env.objects:
                # process specific object types
                if obj.type.name in ["Texture2D", "Sprite"]:
                    # parse the object data
                    data = obj.read()
                    # create destination path
                    dest = os.path.join(destination_folder, data.name)
                    # make sure that the extension is correct
                    # you probably only want to do so with images/textures
                    dest, ext = os.path.splitext(dest)
                    dest = dest + ".png"
                    img = data.image
                    img.save(dest)
            # alternative way which keeps the original path
            for path,obj in env.container.items():
                if obj.type.name in ["Texture2D", "Sprite"]:
                    data = obj.read()
                    # create dest based on original path
                    dest = os.path.join(destination_folder, *path.split("/"))
                    # make sure that the dir of that path exists
                    os.makedirs(os.path.dirname(dest), exist_ok = True)
                    # correct extension
                    dest, ext = os.path.splitext(dest)
                    dest = dest + ".png"
                    data.image.save(dest)

Error
ValueError: 9 is not a valid SpritePackingRotation

Bug
This error is thrown from Pythons enum library because no SpritePackingRotation with value 9 is defined.

To Reproduce

  • sadly cannot include because i do not own the copyright
  • following data:
    • Python 3.7
    • UnityPy==1.7.28

Activity

  1. K0lb3 commented on Jan 24, 2022

    @K0lb3
    Owner

    Sounds like the issue seems to be with the Sprite object parsing as a packing rotation of 9 is indeed invalid.
    See Unity-Technologies/UnityCsReference/Sprites.bindings.cs
    Can you check if it's only a specific Sprite that causes the issue or if all Sprites of the game cause that issue?
    If all of them cause the issue, can you share the Unity version of the asset as well as the typetree of the Sprite object?

       print(obj.dump_typetree_structure())
  2. JulianWinter1124 commented on Jan 24, 2022

    @JulianWinter1124
    Author

    There are multiple sprites to fail (most of them actually). It also worked for an older version of the game.
    The stated Unity Version in the .ab file is 2020.3.13f1.

    level   type    name    meta_flag
    ------- ----------------------  -------------------     -----------
    0       Sprite  Base    32768
    1       string  m_Name  557057
    2       Array   Array   540673
    3       int     size    524289
    3       char    data    524289
    1       Rectf   m_Rect  0
    2       float   x       0
    2       float   y       0
    2       float   width   0
    2       float   height  0
    1       Vector2f        m_Offset        2097152
    2       float   x       2097152
    2       float   y       2097152
    1       Vector4f        m_Border        2097152
    2       float   x       2097152
    2       float   y       2097152
    2       float   z       2097152
    2       float   w       2097152
    1       float   m_PixelsToUnits 0
    1       Vector2f        m_Pivot 2097152
    2       float   x       2097152
    2       float   y       2097152
    1       uint    m_Extrude       0
    1       bool    m_IsPolygon     16384
    1       pair    m_RenderDataKey 0
    2       GUID    first   0
    3       uint    data[0] 0
    3       uint    data[1] 0
    3       uint    data[2] 0
    3       uint    data[3] 0
    2       SInt64  second  0
    1       vector  m_AtlasTags     32768
    2       Array   Array   49152
    3       int     size    0
    3       string  data    32768
    4       Array   Array   16385
    5       int     size    1
    5       char    data    1
    1       PPtr    m_SpriteAtlas   16384
    2       int     m_FileID        8388609
    2       SInt64  m_PathID        8388609
    1       SpriteRenderData        m_RD    49152
    2       PPtr    texture 0
    3       int     m_FileID        8388609
    3       SInt64  m_PathID        8388609
    2       PPtr    alphaTexture    0
    3       int     m_FileID        8388609
    3       SInt64  m_PathID        8388609
    2       vector  secondaryTextures       32768
    3       Array   Array   49152
    4       int     size    0
    4       SecondarySpriteTexture  data    32768
    5       PPtr    texture 0
    6       int     m_FileID        8388609
    6       SInt64  m_PathID        8388609
    5       string  name    49152
    6       Array   Array   16385
    7       int     size    1
    7       char    data    1
    2       vector  m_SubMeshes     32769
    3       Array   Array   16385
    4       int     size    1
    4       SubMesh data    1
    5       uint    firstByte       1
    5       uint    indexCount      1
    5       int     topology        1
    5       uint    baseVertex      1
    5       uint    firstVertex     1
    5       uint    vertexCount     1
    5       AABB    localAABB       1
    6       Vector3f        m_Center        2097153
    7       float   x       2097153
    7       float   y       2097153
    7       float   z       2097153
    6       Vector3f        m_Extent        2097153
    7       float   x       2097153
    7       float   y       2097153
    7       float   z       2097153
    2       vector  m_IndexBuffer   32769
    3       Array   Array   16385
    4       int     size    1
    4       UInt8   data    1
    2       VertexData      m_VertexData    32769
    3       uint    m_VertexCount   1
    3       vector  m_Channels      32769
    4       Array   Array   16385
    5       int     size    1
    5       ChannelInfo     data    1
    6       UInt8   stream  1
    6       UInt8   offset  1
    6       UInt8   format  1
    6       UInt8   dimension       1
    3       TypelessData    m_DataSize      16385
    4       int     size    1
    4       UInt8   data    1
    2       vector  m_Bindpose      49153
    3       Array   Array   16385
    4       int     size    1
    4       Matrix4x4f      data    1
    5       float   e00     1
    5       float   e01     1
    5       float   e02     1
    5       float   e03     1
    5       float   e10     1
    5       float   e11     1
    5       float   e12     1
    5       float   e13     1
    5       float   e20     1
    5       float   e21     1
    5       float   e22     1
    5       float   e23     1
    5       float   e30     1
    5       float   e31     1
    5       float   e32     1
    5       float   e33     1
    2       Rectf   textureRect     0
    3       float   x       0
    3       float   y       0
    3       float   width   0
    3       float   height  0
    2       Vector2f        textureRectOffset       2097152
    3       float   x       2097152
    3       float   y       2097152
    2       Vector2f        atlasRectOffset 2097152
    3       float   x       2097152
    3       float   y       2097152
    2       uint    settingsRaw     0
    2       Vector4f        uvTransform     2097152
    3       float   x       2097152
    3       float   y       2097152
    3       float   z       2097152
    3       float   w       2097152
    2       float   downscaleMultiplier     0
    1       vector  m_PhysicsShape  32768
    2       Array   Array   49152
    3       int     size    0
    3       vector  data    32768
    4       Array   Array   16384
    5       int     size    0
    5       Vector2f        data    2097152
    6       float   x       2097152
    6       float   y       2097152
    1       vector  m_Bones 32768
    2       Array   Array   49152
    3       int     size    0
    3       SpriteBone      data    32768
    4       string  name    49152
    5       Array   Array   16385
    6       int     size    1
    6       char    data    1
    4       Vector3f        position        2097152
    5       float   x       2097152
    5       float   y       2097152
    5       float   z       2097152
    4       Quaternionf     rotation        2097152
    5       float   x       2097152
    5       float   y       2097152
    5       float   z       2097152
    5       float   w       2097152
    4       float   length  0
    4       int     parentId        0```
    
  3. K0lb3 commented on Feb 5, 2022

    @K0lb3
    Owner

    Looks like 2020,3 (or maybe even earlier) uses more detailed bones than previously.
    So far the bones used to be a simple vector of 2 float values, but now they have a class on their own with more details.
    UnityPy 1.7.33 should fix the issue, can you please check it?

  4. JulianWinter1124 commented on Feb 7, 2022

    @JulianWinter1124
    Author

    Hey again, I just tested and the same error appears for all files. Also made double sure I am on version 1.7.33.

    Also I just noticed there is also an error with 10:
    9 is not a valid SpritePackingRotation

    10 is not a valid SpritePackingRotation

    Traceback (most recent call last):
      File "unpack_all_assets.py", line 47, in <module>
        unpack_all_assets(p, os.path.splitext(os.path.basename(p))[0])
      File "unpack_all_assets.py", line 42, in unpack_all_assets
        data.image.save(dest)
      File "/home/julian/anaconda3/envs/web/lib/python3.7/site-packages/UnityPy/classes/Sprite.py", line 14, in image
        return SpriteHelper.get_image_from_sprite(self)
      File "/home/julian/anaconda3/envs/web/lib/python3.7/site-packages/UnityPy/export/SpriteHelper.py", line 45, in get_image_from_sprite
        atlas = m_Sprite.m_SpriteAtlas.read()
      File "/home/julian/anaconda3/envs/web/lib/python3.7/site-packages/UnityPy/files/ObjectReader.py", line 156, in read
        raise e
      File "/home/julian/anaconda3/envs/web/lib/python3.7/site-packages/UnityPy/files/ObjectReader.py", line 154, in read
        obj = getattr(classes, self.type.name, classes.Object)(self)
      File "/home/julian/anaconda3/envs/web/lib/python3.7/site-packages/UnityPy/classes/SpriteAtlas.py", line 19, in __init__
        value = SpriteAtlasData(reader)
      File "/home/julian/anaconda3/envs/web/lib/python3.7/site-packages/UnityPy/classes/SpriteAtlas.py", line 34, in __init__
        self.settingsRaw = SpriteSettings(reader)
      File "/home/julian/anaconda3/envs/web/lib/python3.7/site-packages/UnityPy/classes/Sprite.py", line 134, in __init__
        self.packingRotation = SpritePackingRotation((self.settingsRaw >> 2) & 0xF)  # 4
      File "/home/julian/anaconda3/envs/web/lib/python3.7/enum.py", line 310, in __call__
        return cls.__new__(cls, value)
      File "/home/julian/anaconda3/envs/web/lib/python3.7/enum.py", line 564, in __new__
        raise exc
      File "/home/julian/anaconda3/envs/web/lib/python3.7/enum.py", line 548, in __new__
        result = cls._missing_(value)
      File "/home/julian/anaconda3/envs/web/lib/python3.7/enum.py", line 577, in _missing_
        raise ValueError("%r is not a valid %s" % (value, cls.__name__))
    ValueError: 9 is not a valid SpritePackingRotation

    The traceback

    Also this page should show the rotations for the specific Unity version: https://github.com/Unity-Technologies/UnityCsReference/blob/2020.3.13f1/Runtime/2D/Common/ScriptBindings/Sprites.bindings.cs (It still does not show 9 nor 10 though)

  5. K0lb3 commented on Feb 7, 2022

    @K0lb3
    Owner

    Cough, well, I already referred to the page you mentioned :P

    I was a bit sleepy when I checked it two days ago and only fixed another possible problem with Sprites that had nothing to do with this one.

    I can't really figure out how to force the Unity Editor to produce a sprite with a specific packing rotation,
    so it's kinda hard to figure this out without a sample.
    A single sprite with its texture would help a ton.

    sprite = obj.read()
    # sprite that caused the error
    with open("raw_texture","wb") as f:
        f.write(sprite.m_RD.texture.get_raw_data())
    
    with open("raw_sprite","wb") as f:
        f.write(sprite.get_raw_data())

    You could also send it to me by mail to prevent it from appearing in the public.
    rudolf.kolbe@ovgu.de

  6. JulianWinter1124 commented on Feb 8, 2022

    @JulianWinter1124
    Author

    I sent you a mail with more details. sprite.m_RD.texture.get_raw_data() threw an error

  7. K0lb3 commented on Feb 8, 2022

    @K0lb3
    Owner

    Thanks for the mail.
    Looks like the problem was related to the SpriteAtlasData that is used by the Sprites.
    Starting with Unity 2020.2 secondaryTextures were added, which weren't parsed yet in UnityPy, which in turn let the reader come off the track and read the data wrong.
    1.7.34 fixes the problem.

  8. JulianWinter1124 commented on Feb 8, 2022

    @JulianWinter1124
    Author

    it works 🥳

  9. added a commit that references this issue on Sep 14, 2025
    17d67e5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions