-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andrej Orsula <orsula.andrej@gmail.com>
- Loading branch information
1 parent
6aa13f1
commit fb96c44
Showing
4 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,25 @@ | ||
from importlib.util import find_spec | ||
from typing import Tuple | ||
|
||
from simforge.utils import is_semver_compatible, logging | ||
|
||
BPY_SEMVER_MIN: Tuple[int, int, int] = (4, 3, 0) | ||
|
||
|
||
def verify_bpy_version(): | ||
def verify_bpy_version() -> bool: | ||
if not find_spec("bpy"): | ||
logging.critical("Unable to find 'bpy' module") | ||
return False | ||
|
||
import bpy | ||
|
||
if not is_semver_compatible( | ||
required=BPY_SEMVER_MIN, | ||
current=bpy.app.version, | ||
): | ||
logging.critical( | ||
f"Current version of Blender 'bpy={bpy.app.version_string}' is not semantically compatible with requirement 'bpy^{'.'.join(map(str, BPY_SEMVER_MIN))}'" | ||
f"Current version of Blender 'bpy={bpy.app.version_string}' is not semantically compatible with requirement 'bpy^{'.'.join(map(str, BPY_SEMVER_MIN))}' (assets might need to be generated inside a subprocess)" | ||
) | ||
return False | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import tempfile | ||
from pathlib import Path | ||
from typing import Sequence, Type | ||
|
||
import pytest | ||
|
||
from simforge import ( | ||
Asset, | ||
AssetRegistry, | ||
AssetType, | ||
FileFormatConfig, | ||
Model, | ||
ModelFileFormat, | ||
) | ||
from simforge.generators.blender.version import verify_bpy_version | ||
|
||
ASSETS: Sequence[Type[Asset]] = tuple( | ||
asset | ||
for asset_type, assets in AssetRegistry.items() | ||
for asset in assets | ||
if asset_type is not AssetType.MATERIAL | ||
) | ||
|
||
|
||
@pytest.mark.skipif(not ASSETS, reason="No registered assets") | ||
@pytest.mark.parametrize("asset_type", ASSETS) | ||
def test_gen_asset(asset_type: Type[Asset]): | ||
_generate_asset(asset_type=asset_type) | ||
|
||
|
||
@pytest.mark.skipif(not ASSETS, reason="No registered assets") | ||
@pytest.mark.parametrize("num_assets", (2, 4)) | ||
def test_gen_num_assets(num_assets: int): | ||
_generate_asset(asset_type=ASSETS[0], num_assets=num_assets) | ||
|
||
|
||
@pytest.mark.skipif(not ASSETS, reason="No registered assets") | ||
@pytest.mark.parametrize("seed", (42, 1337)) | ||
def test_gen_seed(seed: int): | ||
_generate_asset(asset_type=ASSETS[0], seed=seed) | ||
|
||
|
||
@pytest.mark.skipif(not ASSETS, reason="No registered assets") | ||
@pytest.mark.parametrize("ext", map(str, ModelFileFormat)) | ||
def test_gen_ext(ext: str): | ||
file_format = ModelFileFormat.from_ext(ext) | ||
if file_format in (ModelFileFormat.GLB, ModelFileFormat.GLTF, ModelFileFormat.SDF): | ||
pytest.xfail("GLTF-based export returns error despite success") | ||
_generate_asset(asset_type=ASSETS[0], file_format=file_format) | ||
|
||
|
||
@pytest.mark.skipif(not ASSETS, reason="No registered assets") | ||
def test_gen_cache(): | ||
_generate_asset(asset_type=ASSETS[0], check_use_cache=True) | ||
|
||
|
||
def _generate_asset( | ||
asset_type: Type[Asset], | ||
seed: int = 0, | ||
num_assets: int = 1, | ||
file_format: FileFormatConfig = None, | ||
check_use_cache: bool = False, | ||
subprocess: bool = not verify_bpy_version(), | ||
): | ||
with tempfile.TemporaryDirectory(prefix="simforge_") as tmpdir: | ||
asset_kwargs = {} | ||
if issubclass(asset_type, Model): | ||
asset_kwargs["texture_resolution"] = 16 | ||
asset = asset_type(**asset_kwargs) | ||
generator = asset.generator_type( | ||
outdir=Path(tmpdir), | ||
seed=seed, | ||
num_assets=num_assets, | ||
file_format=file_format, | ||
use_cache=check_use_cache, | ||
) | ||
output = ( | ||
generator.generate_subprocess(asset) | ||
if subprocess | ||
else generator.generate(asset) | ||
) | ||
assert len(output) == num_assets | ||
|
||
if check_use_cache: | ||
output = ( | ||
generator.generate_subprocess(asset) | ||
if subprocess | ||
else generator.generate(asset) | ||
) | ||
assert len(output) == num_assets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
import pytest | ||
|
||
|
||
@pytest.mark.order(1) | ||
def test_import(): | ||
import simforge # noqa: F401 |