Exemplo n.º 1
0
def register_shape(app, path):
    with suppress_stdout():
        scene = brender.Scene(app, shape=(1000, 1000))
        mesh = brender.Mesh.from_3ds(scene, path)
        mesh.remove_doubles()
        mesh.make_normals_consistent()
        mesh.enable_smooth_shading()
        mesh.unwrap_uv()

    with database.session_scope() as sess:
        shape = Shape(source=args.source_name,
                      source_id=path.name,
                      category=args.category)
        sess.add(shape)
        sess.flush()
        shape_dir = Path(config.BLOB_ROOT, 'shapes', str(shape.id))

        bpy.ops.export_scene.obj(filepath=str(TMP_OBJ_PATH))

        try:
            shape.models_dir.mkdir(parents=True)
            shutil.copy(str(TMP_OBJ_PATH), str(shape.obj_path))
            shutil.copy(str(TMP_MTL_PATH), str(shape.mtl_path))
        except:
            shape.obj_path.unlink()
            shape.mtl_path.unlink()
            shape_dir.rmdir()
            raise

        sess.commit()
Exemplo n.º 2
0
def main():
    with session_scope() as sess:
        materials = (sess.query(models.Material).order_by(
            models.Material.id.asc()).all())

    # Initialize Brender and Scene.
    app = brender.Brender()
    app.init()
    scene = brender.Scene(app, shape=_REND_SHAPE, aa_samples=196)
    scene.set_envmap(_ENVMAP_PATH, scale=5.0)

    # Initialize Camera.
    rk_camera = cameras.spherical_coord_to_cam(
        60.0,
        azimuth=math.pi / 2 - math.pi / 12,
        elevation=math.pi / 2 - math.pi / 6,
        cam_dist=2.5,
        max_len=_REND_SHAPE[0] / 2)
    camera = brender.CalibratedCamera(scene, rk_camera.cam_to_world(),
                                      rk_camera.fov)
    scene.set_active_camera(camera)

    with scene.select():
        mesh = brender.mesh.Monkey(position=(0, 0, 0))
        mesh.enable_smooth_shading()

    pbar = tqdm(materials)
    for material in pbar:
        pbar.set_description(material.name)
        uv_ref_scale = 2**(material.default_scale - 4)
        bmat = material_to_brender(material, uv_ref_scale=uv_ref_scale)
        brender.mesh.set_material(mesh.bobj, bmat)
        if bmat.has_uvs:
            mesh.compute_uv_density()

        with suppress_stdout():
            rend = scene.render_to_array(format='exr')

        material.save_data('previews/monkey.studio021.exr', rend)
        material.save_data('previews/monkey.studio021.png',
                           to_8bit(toolbox.images.linear_to_srgb(rend)))
Exemplo n.º 3
0
def register_shape(app, shape, path):
    shape_dir = Path(config.BLOB_ROOT, 'shapes', str(shape.id))

    with suppress_stdout():
        scene = brender.Scene(app, shape=(1000, 1000))
        mesh = brender.Mesh.from_3ds(scene, path)
        mesh.remove_doubles()
        mesh.make_normals_consistent()
        mesh.enable_smooth_shading()
        mesh.unwrap_uv()

    bpy.ops.export_scene.obj(filepath=str(TMP_OBJ_PATH))

    try:
        if not shape.models_dir.exists():
            shape.models_dir.mkdir(parents=True)
        shutil.copy(str(TMP_OBJ_PATH), str(shape.obj_path))
        shutil.copy(str(TMP_MTL_PATH), str(shape.mtl_path))
    except:
        shape.obj_path.unlink()
        shape.mtl_path.unlink()
        shape_dir.rmdir()
        raise
Exemplo n.º 4
0
def main():
    app = brender.Brender()

    if not args.list_path.exists():
        print(f'{args.list_path!s} does not exist.')
        return

    with args.list_path.open('r') as f:
        file_list = f.read().strip().split('\n')
        inference_paths = [
            Path(args.inference_dir, f'{p}.json') for p in file_list
        ]

    with session_scope() as sess:
        envmap = sess.query(models.Envmap).get(13)
        materials = sess.query(models.Material).all()
        mat_by_id = {m.id: m for m in materials}

        app.init()

        engine = Engine.CYCLES
        scene = brender.Scene(app,
                              shape=_REND_SHAPE,
                              engine=engine,
                              tile_size=(40, 40),
                              aa_samples=128,
                              diffuse_samples=3,
                              specular_samples=3,
                              background_mode=BackgroundMode.ENVMAP,
                              background_color=(1, 1, 1, 1))
        envmap_rotation = (0, 0, (envmap.azimuth + 3 * math.pi / 2))
        scene.set_envmap(envmap.get_data_path('hdr.exr'),
                         scale=1.0,
                         rotation=envmap_rotation)

        floor = Plane((0, 0, 0), radius=1000)

        camera = brender.BasicCamera(
            scene,
            position=(0.217, -22.76, 7.49),
            rotation=(0.99, 0, 0),
        )
        scene.set_active_camera(camera)

        grid_width = 13
        grid_height = int(math.ceil(len(inference_paths) / grid_width)) + 10
        grid_coords = []
        for r, row in enumerate(
                np.linspace(-grid_height / 2, grid_height / 2, grid_height)):
            offset = 0
            if r % 2 == 0:
                offset = 0.5
            for c, col in enumerate(
                    np.linspace(-grid_width / 2, grid_width / 2, grid_width)):
                if r == 0 and c in {0, 1, grid_width - 1, grid_width - 2}:
                    continue
                if r in {1, 2, 3} and c in {0, grid_width - 1}:
                    continue

                coord = (col + offset, row)
                grid_coords.append(coord)

        random.seed(1000)
        random.shuffle(inference_paths)

        pbar = tqdm(inference_paths)
        for i, inference_path in enumerate(pbar):
            with inference_path.open('r') as f:
                inference_dict = json.load(f)

            pair = sess.query(ExemplarShapePair).get(inference_dict['pair_id'])
            pbar.set_description(f"Adding pair {pair.id}")

            location = (1.5 * grid_coords[i][0], 1.5 * grid_coords[i][1], 0)

            mesh = add_pair_mesh(scene, pair, inference_dict, mat_by_id)
            scene.meshes.append(mesh)
            mesh.bobj.location = location
            mesh.bobj.location[2] -= mesh.compute_min_pos()

        tqdm.write(f'Saving blend file to {args.out_path!s}')
        # bpy.ops.file.make_paths_absolute()
        if args.pack_assets:
            bpy.ops.file.pack_all()
        bpy.ops.wm.save_as_mainfile(filepath=str(args.out_path))
Exemplo n.º 5
0
async def process_pair(app: brender.Brender,
                       pair: ExemplarShapePair,
                       cam_angles: List[Tuple[float, float]],
                       mats_by_subst: Dict[str, List[NodesMaterial]],
                       num_rends,
                       *,
                       envmaps_by_split,
                       is_dry_run,
                       epoch,
                       collector_ctx,
                       required_substances=None):
    rk_mesh, _ = pair.shape.load()
    rk_mesh.resize(1)
    with open(_TMP_MESH_PATH, 'w') as f:
        wavefront.save_obj_file(f, rk_mesh)

    seg_substances = utils.compute_segment_substances(pair)

    if required_substances:
        for subst in required_substances:
            if subst not in seg_substances.values():
                return

    print(seg_substances.values())

    scene = brender.Scene(app,
                          shape=_REND_SHAPE,
                          tile_size=(40, 40),
                          aa_samples=128,
                          diffuse_samples=3,
                          specular_samples=3,
                          background_mode=BackgroundMode.COLOR,
                          background_color=(1, 1, 1, 1))

    with suppress_stdout():
        mesh = Mesh.from_obj(scene, _TMP_MESH_PATH)
        # mesh.remove_doubles()
        mesh.enable_smooth_shading()

    for i in range(num_rends):
        bmats = []

        try:
            r = do_render(scene, pair, mesh, envmaps_by_split, cam_angles,
                          rk_mesh, seg_substances, mats_by_subst, bmats)
        finally:
            while len(bmats) > 0:
                bmat = bmats.pop()
                bmat.bobj.name = bmat.bobj.name
                del bmat

        if not is_dry_run:
            await collector.send_data(
                **collector_ctx,
                split_set=pair.shape.split_set,
                pair_id=pair.id,
                epoch=epoch,
                iteration=i,
                params=r['params'],
                ldr_image=r['ldr'],
                hdr_image=r['hdr'],
                seg_map=r['seg_map'],
                seg_vis=r['seg_vis'],
                normal_image=r['normal_image'],
            )
Exemplo n.º 6
0
def construct_inference_scene(app: brender.Brender,
                              pair: models.ExemplarShapePair,
                              pair_inference_dict,
                              mat_by_id,
                              envmap: models.Envmap,
                              scene_type='inferred',
                              num_samples=256,
                              rend_shape=(1280, 1280),
                              tile_size=(512, 512),
                              frontal_camera=False,
                              diagonal_camera=False,
                              add_floor=True):
    if scene_type not in {'inferred', 'mtl'}:
        raise ValueError('Invalid scene type.')

    inference_dict = pair_inference_dict['segments']
    rk_mesh, _ = pair.shape.load(size=1)
    rk_mesh.resize(1)

    scene = brender.Scene(app, shape=rend_shape,
                          num_samples=num_samples,
                          tile_size=tile_size,
                          background_mode=BackgroundMode.COLOR,
                          background_color=(1.0, 1.0, 1.0, 0))
    envmap_rotation = (0, 0, (envmap.azimuth + math.pi/2 + pair.azimuth))
    scene.set_envmap(envmap.get_data_path('hdr.exr'),
                     scale=0.8, rotation=envmap_rotation)

    if frontal_camera:
        distance = 1.5
        fov = 50
        azimuth, elevation = pair.shape.get_frontal_angles()
    elif diagonal_camera:
        distance = 1.5
        fov = 50
        azimuth, elevation = pair.shape.get_demo_angles()
    else:
        distance = 4.0
        fov = pair.fov
        azimuth, elevation = pair.azimuth, pair.elevation

    # Get exemplar camera parameters.
    rk_camera = cameras.spherical_coord_to_cam(
        fov, azimuth, elevation, cam_dist=distance,
        max_len=rend_shape[0]/2)

    camera = brender.CalibratedCamera(scene, rk_camera.cam_to_world(), fov)

    scene.set_active_camera(camera)

    with suppress_stdout():
        mesh = Mesh.from_obj(scene, pair.shape.resized_obj_path)
        mesh.make_normals_consistent()
        mesh.enable_smooth_shading()
    mesh.recenter()

    if add_floor:
        min_pos = mesh.compute_min_pos()
        floor_mat = DiffuseMaterial(diffuse_color=(1.0, 1.0, 1.0))
        floor_mesh = Plane(position=(0, 0, min_pos))
        floor_mesh.set_material(floor_mat)

    if scene_type == 'inferred':
        for seg_id, seg_name in enumerate(rk_mesh.materials):
            if str(seg_id) not in inference_dict:
                continue
            mat_id = int(inference_dict[str(seg_id)]['material'][0]['id'])
            material = mat_by_id[mat_id]
            uv_ref_scale = 2 ** (material.default_scale - 3)
            print(f'[Pair {pair.id}] Settings segment {seg_id} ({seg_name}) '
                  f'to material {material.name}')
            # Activate only current material.
            for bobj in bpy.data.materials:
                if bobj.name == seg_name:
                    bmat = loader.material_to_brender(
                        material, bobj=bobj, uv_ref_scale=uv_ref_scale)
                    scene.add_bmat(bmat)

        # This needs to come after the materials are initialized.
        print('Computing UV density...')
        mesh.compute_uv_density()

    return scene
Exemplo n.º 7
0
def render_pair(app: brender.Brender, pair: ExemplarShapePair,
                materials_by_substance, base_out_dir):
    # Load shapenet mesh and resize to 1.0 to match Blender size.
    rk_mesh, _ = pair.shape.load()
    rk_mesh.resize(1)
    with open(_TMP_MESH_PATH, 'w') as f:
        wavefront.save_obj_file(f, rk_mesh)

    scene = brender.Scene(app, shape=_REND_SHAPE, aa_samples=32)
    envmap_rotation = (0, 0, (math.pi + math.pi/2 + pair.azimuth))
    scene.set_envmap(_ENVMAP_PATH, scale=5, rotation=envmap_rotation)

    with suppress_stdout():
        mesh = Mesh.from_obj(scene, _TMP_MESH_PATH)

    mat_substances = utils.compute_segment_substances(pair)

    # Get exemplar camera parameters.
    rk_camera = cameras.spherical_coord_to_cam(
        pair.fov, pair.azimuth, pair.elevation, cam_dist=2.0,
        max_len=_REND_SHAPE[0]/2)

    segment_im = render_segments(rk_mesh, rk_camera)

    camera = brender.CalibratedCamera(
        scene, rk_camera.cam_to_world(), pair.fov)
    scene.set_active_camera(camera)

    bmats = []
    segment_pbar = tqdm(rk_mesh.materials)
    for segment_name in segment_pbar:
        segment_pbar.set_description(f'Segment {segment_name}')

        try:
            mat_subst = mat_substances[segment_name]
            materials = materials_by_substance[mat_subst]
        except KeyError:
            continue

        out_dir = Path(base_out_dir, str(pair.id), str(segment_name))
        out_dir.mkdir(parents=True, exist_ok=True)

        material_pbar = tqdm(materials)
        for material in material_pbar:
            material_pbar.set_description(f'Material {material.id}')
            out_path = Path(out_dir, f'{material.id}.png')
            if out_path.exists():
                material_pbar.set_description(
                    f'Material {material.id} already rendered')
                continue

            # Activate only current material.
            bobj = None
            for bobj in bpy.data.materials:
                if bobj.name == segment_name:
                    break

            bobj_matches = [o for o in bpy.data.materials
                            if o.name == segment_name]
            if len(bobj_matches) == 0:
                bmat = InvisibleMaterial(bobj=bobj)
            else:
                bmat = loader.material_to_brender(material, bobj=bobj)

            bmats.append(bmat)

            with suppress_stdout():
                rend_im = scene.render_to_array()

            vis.image(rend_im.transpose((2, 0, 1)), win='rend-im')

            rend_im[segment_im != rk_mesh.materials.index(segment_name)] = 0
            fg_bbox = mask_bbox(segment_im > -1)
            rend_im = crop_tight_fg(rend_im, _FINAL_SHAPE, bbox=fg_bbox,
                                    fill=0, use_pil=True)

            with warnings.catch_warnings():
                warnings.simplefilter('ignore', UserWarning)
                skio.imsave(str(out_path), rend_im)

    while len(bmats) > 0:
        bmat = bmats.pop()
        bmat.bobj.name = bmat.bobj.name
        del bmat
Exemplo n.º 8
0
def main():
    with session_scope() as sess:
        materials = (
            sess.query(models.Material)
            # .filter(sa.and_(models.Material.default_scale.isnot(None),
            #                 models.Material.substance == 'fabric'))
            # .filter(models.Material.type == MaterialType.MDL)
            .order_by(models.Material.id.asc()).all())
        shape = sess.query(models.Shape).get(4682)
        # shape = sess.query(models.Shape).get(2333)

    # Initialize Brender and Scene.
    app = brender.Brender()
    app.init()
    scene = brender.Scene(app, shape=_REND_SHAPE, aa_samples=196)
    envmap_rotation = (0, 0,
                       (math.pi + math.pi / 2 - math.pi / 2 - math.pi / 12))
    scene.set_envmap(_ENVMAP_PATH, scale=2.0, rotation=envmap_rotation)

    # Initialize Camera.
    rk_camera = cameras.spherical_coord_to_cam(
        60.0,
        azimuth=-math.pi / 2 - math.pi / 12,
        elevation=math.pi / 2 - math.pi / 6,
        cam_dist=1.2,
        max_len=_REND_SHAPE[0] / 2)
    camera = brender.CalibratedCamera(scene, rk_camera.cam_to_world(),
                                      rk_camera.fov)
    scene.set_active_camera(camera)

    # Load shapenet mesh and resize to 1.0 to match Blender size.
    rk_mesh, _ = shape.load()
    rk_mesh.resize(1)
    with open(_TMP_MESH_PATH, 'w') as f:
        wavefront.save_obj_file(f, rk_mesh)
    mesh = brender.mesh.Mesh.from_obj(scene, _TMP_MESH_PATH)

    # Align the mesh to a camera looking straight at the diffuser. The diffuser
    # for Studio 021 is at azimuth=pi/2, elevation=pi/2.
    # brender.mesh.align_mesh_to_direction(mesh, math.pi / 2, math.pi / 2)

    # with scene.select():
    #     mesh = brender.mesh.Monkey(position=(0, 0, 0))

    pbar = tqdm(materials)
    for material in pbar:
        uv_ref_scale = 2**(material.default_scale - 4)
        pbar.set_description(material.name)

        data_name = f'previews/chair_{material.default_scale}.png'
        if material.data_exists(data_name):
            continue

        # _, _, uv_density = measure_uv_density(mesh.bobj)
        bmat = material_to_brender(material, uv_ref_scale=uv_ref_scale)
        brender.mesh.set_material(mesh.bobj, bmat)
        if bmat.has_uvs:
            mesh.compute_uv_density()

        with suppress_stdout():
            rend = scene.render_to_array(format='exr')

        # material.save_data(
        #     f'previews/chair.exr',
        #     rend)
        bpy.ops.wm.save_as_mainfile(filepath='/local1/data/test.blend')
        material.save_data(data_name,
                           to_8bit(toolbox.images.linear_to_srgb(rend)))