Exemple #1
0
def test_export_images_from_view_file():
    """Test export images method using the from_view_file classmethod."""
    file_path = r'tests/assets/gridbased.hbjson'
    results_folder = r'tests/assets/df_results'
    target_folder = r'tests/assets/temp'
    view_file_path = r'tests/assets/view.vf'

    model = Model.from_hbjson(file_path, load_grids=SensorGridOptions.Mesh)
    model.update_display_mode(DisplayMode.Wireframe)

    daylight_factor = []
    for grid in model.sensor_grids.data:
        res_file = pathlib.Path(results_folder, f'{grid.identifier}.res')
        grid_res = [float(v) for v in res_file.read_text().splitlines()]
        daylight_factor.append(grid_res)

    model.sensor_grids.add_data_fields(daylight_factor,
                                       name='Daylight-factor',
                                       per_face=True,
                                       data_range=(0, 20))
    model.sensor_grids.color_by = 'Daylight-factor'
    model.sensor_grids.display_mode = DisplayMode.SurfaceWithEdges

    # actors
    actors = Actor.from_model(model=model)

    # Initialize a scene
    scene = Scene(background_color=(255, 255, 255))
    scene.add_actors(actors)

    # A camera setup using the classmethod
    camera = Camera.from_view_file(file_path=view_file_path)

    # Add all the cameras to the scene
    scene.add_cameras(camera)

    # if target folder exists, delete it and create a fresh new folder
    if os.path.isdir(target_folder):
        shutil.rmtree(target_folder)
    os.mkdir(target_folder)

    # Export images for all the cameras
    images_path = scene.export_images(folder=target_folder,
                                      image_type=ImageTypes.png,
                                      name='camera')

    for path in images_path:
        assert os.path.isfile(path)

    shutil.rmtree(target_folder)
Exemple #2
0
def test_from_view_file():
    """Test creation of a camera object from a Radiance view file."""
    vf = r'tests/assets/view.vf'
    camera = Camera.from_view_file(vf)
    assert isinstance(camera, Camera)
Exemple #3
0
def export(hbjson_file, name, folder, image_type, image_width, image_height,
           background_color, display_mode_model, grid_options,
           display_mode_grid, view, config):
    """Export images from radiance views in a HBJSON file.

    \b
    Args:
        hbjson-file: Path to an HBJSON file.

    """
    folder = pathlib.Path(folder)
    folder.mkdir(exist_ok=True)

    # Set image types
    if image_type == 'png':
        image_type = ImageTypes.png
    elif image_type == 'jpg':
        image_type = ImageTypes.jpg
    elif image_type == 'ps':
        image_type == ImageTypes.ps
    elif image_type == 'tiff':
        image_type == ImageTypes.tiff
    elif image_type == 'ps':
        image_type == ImageTypes.ps
    elif image_type == 'pnm':
        image_type == ImageTypes.pnm

    # Set Sensor grids
    if grid_options == 'ignore':
        grid_options = SensorGridOptions.Ignore
    elif grid_options == 'points':
        grid_options = SensorGridOptions.Sensors
    elif grid_options == 'meshes':
        grid_options = SensorGridOptions.Mesh

    try:
        model = Model.from_hbjson(hbjson=hbjson_file, load_grids=grid_options)

        # Set model's display mode
        if display_mode_model == 'shaded':
            model.update_display_mode(DisplayMode.Shaded)
        elif display_mode_model == 'surface':
            model.update_display_mode(DisplayMode.Surface)
        elif display_mode_model == 'surfacewithedges':
            model.update_display_mode(DisplayMode.SurfaceWithEdges)
        elif display_mode_model == 'wireframe':
            model.update_display_mode(DisplayMode.Wireframe)
        elif display_mode_model == 'points':
            model.update_display_mode(DisplayMode.Points)

        # Set model's grid's display mode
        if display_mode_grid == 'shaded':
            model.sensor_grids.display_mode = DisplayMode.Shaded
        elif display_mode_model == 'surface':
            model.sensor_grids.display_mode = DisplayMode.Surface
        elif display_mode_model == 'surfacewithedges':
            model.sensor_grids.display_mode = DisplayMode.SurfaceWithEdges
        elif display_mode_model == 'wireframe':
            model.sensor_grids.display_mode = DisplayMode.Wireframe
        elif display_mode_model == 'points':
            model.sensor_grids.display_mode = DisplayMode.Points

        actors = Actor.from_model(model)

        scene = Scene(background_color=background_color)
        scene.add_actors(actors)

        # Set a default camera if there are not cameras in the model
        if not model.cameras and not view:
            # Use the centroid of the model for the camera position
            actors = Actor.from_model(model=model)
            position = Actor.get_centroid(actors)
            camera = Camera(position=position, type='l')
            scene.add_cameras(camera)

        else:
            # Collection cameras from model, if the model has it
            if len(model.cameras) != 0:
                cameras = model.cameras
                scene.add_cameras(cameras)

            # if view files are provided collect them
            if view:
                for vf in view:
                    camera = Camera.from_view_file(file_path=vf)
                    scene.add_cameras(camera)

        # load config if provided
        if config:
            model.load_config(config)

        output = scene.export_images(folder=folder,
                                     name=name,
                                     image_type=image_type,
                                     image_width=image_width,
                                     image_height=image_height)

    except Exception:
        traceback.print_exc()
        sys.exit(1)
    else:
        print(f'Success: {output}', file=sys.stderr)
        return sys.exit(0)