コード例 #1
0
def rpict_command(octree, view, rad_params, rad_params_locked, metric,
                  resolution, scale_factor, output, dry_run):
    """Run rpict command for an input octree and a view file.

    Note that, if an ambient cache file (.amb) is found next to the view file,
    and it is determined to be valid (with a non-zero size) it will be
    automatically used within the rpict command.

    \b
    Args:
        octree: Path to octree file.
        view: Path to view file.

    """
    try:
        options = RpictOptions()
        # parse input radiance parameters
        if rad_params:
            options.update_from_string(rad_params.strip())
        # overwrite input values with protected ones
        if rad_params_locked:
            options.update_from_string(rad_params_locked.strip())
        # overwrite the -i attribute depending on the metric to be calculated
        if metric in ('illuminance', 'irradiance'):
            options.i = True
        elif metric in ('luminance', 'radiance'):
            options.i = False
        else:
            raise ValueError('Metric "{}" is not recognized.'.format(metric))
        # overwrite the -x and -y attribute depending on the input resolution
        if resolution:
            options.x = int(resolution * scale_factor)
            options.y = int(resolution * scale_factor)
        # sense wether there is an ambient cache file next to the view
        for base_file in os.listdir(os.path.dirname(view)):
            if base_file.endswith('.amb'):
                full_amb_path = os.path.join(os.path.dirname(view), base_file)
                if os.stat(full_amb_path).st_size != 0:
                    options.af = os.path.join(os.path.dirname(view), base_file)
                    break

        # write the metric type into the view name such that it's in the HDR header
        metric_view = os.path.basename(view).replace('.vf',
                                                     '_{}.vf'.format(metric))
        full_metric_view = os.path.join(os.path.dirname(view), metric_view)
        shutil.copyfile(view, full_metric_view)

        # create command.
        rpict = Rpict(options=options,
                      output=output,
                      octree=octree,
                      view=full_metric_view)

        if dry_run:
            click.echo(rpict)
        else:
            env = None
            if folders.env != {}:
                env = folders.env
            env = dict(os.environ, **env) if env else None
            rpict.run(env=env)
        os.remove(full_metric_view)
    except Exception:
        _logger.exception('Failed to run rpict command.')
        sys.exit(1)
    else:
        sys.exit(0)
コード例 #2
0
def split_view(view, count, skip_overture, octree, rad_params, folder,
               log_file):
    """Split a radiance view file into smaller views based on count.

    \b
    Args:
        view: Full path to input sensor view file.
        count: Maximum number of sensors in new files. The number will be rounded to
            closest round number for each file. For example if the input file has 21
            sensors and input count is set to 5 this command will generate 4 files where
            the first three files will have 5 sensors and the last file will have 6.
    """
    try:
        # split the view into smaller views
        view_obj = View.from_file(view)
        views = view_obj.grid(y_div_count=count)
        views_info = []
        for c, v in enumerate(views):
            name = '%s_%04d' % (view_obj.identifier, c)
            path = '%s.vf' % name
            full_path = os.path.join(folder, path)
            v.to_file(folder, path, mkdir=True)
            views_info.append({
                'name': name,
                'path': path,
                'full_path': full_path
            })

        # create the ambient cache file if specified
        amb_file = os.path.basename(view).replace('.vf', '.amb')
        if not skip_overture:
            options = RpictOptions()
            if rad_params:
                options.update_from_string(rad_params.strip())
            # overwrite default image size to be small for the ambient cache (64 x 64)
            options.x = 64
            options.y = 64
            options.af = amb_file

            # create command and run it to get the .amb file
            assert octree is not None, \
                'Octree  must be specified for an overture calculation.'
            out_file = os.path.basename(view).replace('.vf', '.unf')
            rpict = Rpict(options=options,
                          output=out_file,
                          octree=octree,
                          view=view)
            env = None
            if folders.env != {}:
                env = folders.env
            env = dict(os.environ, **env) if env else None
            rpict.run(env=env, cwd=folder)
            os.remove(os.path.join(folder, out_file))
        else:  # write a dummy ambient file so that queenbee does not crash
            write_to_file_by_name(folder, amb_file, '')

        # record all of the view files that were generated
        log_file.write(json.dumps(views_info))
    except Exception:
        _logger.exception('Failed to split view file.')
        sys.exit(1)
    else:
        sys.exit(0)