def test_should_read_scene_data():
    blend = BlenderFile('fixtures/test1.blend')

    worlds = blend.list('World')
    assert worlds.file is blend, 'World file is not blend'
    assert len(worlds) == 1, 'Test blend should have one world'

    world = worlds.find_by_name('TestWorld')
    assert world.file is blend
    assert isinstance(world, worlds.object)
    assert world.VERSION == blend.header.version
    assert len(world.mtex) == 18
    assert world.aodist > 12.8999 and world.aodist < 12.90001
    assert world.id.name[0:11] == b'WOTestWorld'

    scenes = blend.list('Scene')
    assert len(scenes) == 1, 'Test blend should have one scene'

    rctfs = blend.list('rctf')
    pytest.raises(BlenderFileReadException, rctfs.find_by_name,
                  'blah')  # rctf object do not have a name
    pytest.raises(BlenderFileReadException, blend.list,
                  'foos')  # foos is not a valid structure
    pytest.raises(
        KeyError, worlds.find_by_name,
        'BOO')  # There are no worlds by the name of BOO in the blend file

    blend.close()
Beispiel #2
0
def compress_models_fast(model_name=None, write_to_disk=False):
    print('find models')
    from tinyblend import BlenderFile
    if not application.compressed_models_folder.exists():
        application.compressed_models_folder.mkdir()

    files = os.listdir(application.models_folder)
    compressed_files = os.listdir(application.compressed_models_folder)

    for f in files:
        if f.endswith('.blend'):
            # print('f:', application.compressed_models_folder + '/' + f)
            print('compress______', f)
            blend = BlenderFile(application.models_folder + '/' + f)
            number_of_objects = len(blend.list('Object'))

            for o in blend.list('Object'):
                if not o.data.mvert:
                    continue
                # print(o.id.name.decode("utf-8", "strict"))
                object_name = o.id.name.decode("utf-8").replace(".", "_")[2:]
                object_name = object_name.split('\0', 1)[0]
                print('name:', object_name)

                vertices = o.data.mvert
                verts = [v.co for v in o.data.mvert]
                verts = tuple(verts)

                file_content = 'Mesh(' + str(verts)

                file_name = ''.join([f.split('.')[0], '.ursinamesh'])
                if number_of_objects > 1:
                    file_name = ''.join(
                        [f.split('.')[0], '_', object_name, '.ursinamesh'])
                file_path = os.path.join(application.compressed_models_folder,
                                         file_name)
                print(file_path)

                tris = tuple([triindex.v for triindex in o.data.mloop])
                flippedtris = list()
                for i in range(0, len(tris) - 3, 3):
                    flippedtris.append(tris[i + 2])
                    flippedtris.append(tris[i + 1])
                    flippedtris.append(tris[i + 0])

                file_content += ', triangles=' + str(flippedtris)

                if o.data.mloopuv:
                    uvs = tuple([v.uv for v in o.data.mloopuv])
                    file_content += ', uvs=' + str(uvs)

                file_content += ''', mode='triangle')'''

                if write_to_disk:
                    with open(file_path, 'w') as file:
                        file.write(file_content)

                return file_content
def test_should_read_scene_data_28():
    blend = BlenderFile('fixtures/test_blender28.blend')

    worlds = blend.list('World')
    assert worlds.file is blend, 'World file is not blend'
    assert len(worlds) == 1, 'Test blend should have one world'

    scenes = blend.list('Scene')
    assert len(scenes) == 1, 'Test blend should have one scene'

    blend.close()
def test_should_lookup_pointer_array():
    blend = BlenderFile('fixtures/test1.blend')

    obj = blend.list('Object').find_by_name('Suzanne')
    data = obj.data

    assert data.totvert == len(data.mvert)
def test_equality():
    blend = BlenderFile('fixtures/test1.blend')

    worlds = blend.list('World')

    world1 = worlds.find_by_name('TestWorld')
    world2 = worlds.find_by_name('TestWorld')

    assert id(world1) is not id(world2)
    assert world1 == world2
def test_weakref():
    blend = BlenderFile('fixtures/test1.blend')
    worlds = blend.list('World')

    del blend

    pytest.raises(RuntimeError, getattr, worlds, 'file')
    pytest.raises(RuntimeError, len, worlds)
    pytest.raises(RuntimeError, repr, worlds)
    pytest.raises(RuntimeError, str, worlds)
    pytest.raises(RuntimeError, worlds.find_by_name, '...')
def test_cache_lookup():
    blend = BlenderFile('fixtures/test1.blend')
    v = blend.header.version

    worlds = blend.list('World')

    assert BlenderObjectFactory.CACHE[v]['World']() is not None
    assert BlenderObject.CACHE[v]['World']() is not None

    del worlds
    gc.collect()

    assert BlenderObjectFactory.CACHE[v]['World']() is None
    assert BlenderObject.CACHE[v]['World']() is None

    worlds = blend.list('World')
    assert isinstance(worlds, BlenderObjectFactory)
    assert BlenderObjectFactory.CACHE[v]['World']() is not None
    assert BlenderObject.CACHE[v]['World']() is not None

    blend.close()
def test_should_lookup_pointer():
    BlenderObject.CACHE = {}
    BlenderObjectFactory.CACHE = {}

    blend = BlenderFile('fixtures/test1.blend')

    worlds = blend.list('World')
    scenes = blend.list('Scene')

    world = worlds.find_by_name('TestWorld')
    scene = scenes.find_by_name('MyTestScene')

    pytest.raises(BlenderFileReadException, blend._from_address, 0)
    pytest.raises(AttributeError, setattr, scene, 'world', 0)
    pytest.raises(AttributeError, delattr, scene, 'world')

    scene_world = scene.world

    assert type(scene_world) is worlds.object
    assert scene_world is not world
    assert scene.world == world
    assert scene.world is scene_world
    assert scene.id.next is None  # Null pointer lookup returns None