Beispiel #1
0
def save_render_image(img_name, quality, do_render_image, add_colorbar=None, cb_ticks_num=None, cb_ticks_font_size=None):
    if add_colorbar is None:
        add_colorbar = bpy.context.scene.save_views_with_cb
    if cb_ticks_num is None:
        cb_ticks_num = bpy.context.scene.cb_ticks_num
    if cb_ticks_font_size is None:
        cb_ticks_font_size = bpy.context.scene.cb_ticks_font_size
    if do_render_image:
        camera_mode()
        image_fname = render_image(img_name, quality=quality, overwrite=True)
        camera_mode()
    else:
        image_fname = save_image(img_name, add_index_to_name=False, add_colorbar=add_colorbar,
                                 cb_ticks_num=cb_ticks_num, cb_ticks_font_size=cb_ticks_font_size)
    mu.write_to_stderr('Saving image to {}'.format(image_fname))
    return image_fname
Beispiel #2
0
def render_movie(play_type,
                 play_from,
                 play_to,
                 camera_fname='',
                 play_dt=1,
                 set_to_camera_mode=False):
    set_play_to(play_to)
    bpy.context.scene.play_type = play_type
    bpy.context.scene.render_movie = True
    print('In play movie!')
    for limits in range(play_from, play_to + 1, play_dt):
        print('limits: {}'.format(limits))
        mu.write_to_stderr('rendering frame {}'.format(limits))
        bpy.context.scene.frame_current = limits
        try:
            plot_something(None,
                           bpy.context,
                           limits,
                           camera_fname=camera_fname,
                           set_to_camera_mode=set_to_camera_mode)
        except:
            print(traceback.format_exc())
            print('Error in plotting at {}!'.format(limits))
            mu.write_to_stderr(traceback.format_exc())
        else:
            mu.write_to_stderr('Done!')
Beispiel #3
0
def render_movie(play_type,
                 play_from,
                 play_to,
                 camera_fname='',
                 play_dt=1,
                 set_to_camera_mode=True,
                 rotate_brain=False):
    set_play_to(play_to)
    bpy.context.scene.play_type = play_type
    bpy.context.scene.render_movie = True
    bpy.context.scene.rotate_brain_while_playing = rotate_brain
    print('In play movie!')
    play_range = list(range(play_from, play_to + 1, play_dt))
    runs_num = len(play_range)
    for run, limits in enumerate(play_range):
        print('limits: {}'.format(limits))
        mu.write_to_stderr('Plotting {} frame {} ({}-{}, dt {})'.format(
            play_type, limits, play_from, play_to, play_dt))
        bpy.context.scene.frame_current = limits
        rotate_while_playing()
        try:
            now = time.time()
            plot_something(None,
                           bpy.context,
                           limits,
                           camera_fname=camera_fname,
                           set_to_camera_mode=set_to_camera_mode)
        except:
            print(traceback.format_exc())
            print('Error in plotting at {}!'.format(limits))
            mu.write_to_stderr(traceback.format_exc())
        else:
            time_took = time.time() - now
            more_time = time_took / (run + 1) * (runs_num - (run + 1))
            mu.write_to_stderr(('{}/{}, {:.2f}s, {:.2f}s to go!'.format(
                run, runs_num, time_took, more_time)))
Beispiel #4
0
def _save_all_views(views=None,
                    inflated_ratio_in_file_name=False,
                    rot_lh_axial=True,
                    render_images=False,
                    quality=0,
                    img_name_prefix='',
                    add_colorbar=False,
                    cb_ticks_num=None,
                    cb_ticks_font_size=None,
                    overwrite=True):
    def get_image_name(view_name):
        return '{}{}{}_{}'.format(
            '{}_'.format(hemi) if hemi != '' else '',
            '{}_'.format(img_name_prefix) if img_name_prefix != '' else '',
            surf_name, view_name)

    def should_save_image(img_name):
        return overwrite or not op.isfile(get_full_output_fname(img_name))

    def save_medial_views():
        if _addon().ROT_MEDIAL_LEFT in views:
            image_name = '{}_left_medial'.format(surf_name)
            if should_save_image(image_name):
                _addon().hide_hemi('rh')
                _addon().show_hemi('lh')
                _addon().rotate_view(_addon().ROT_SAGITTAL_RIGHT)
                image_fname = save_render_image(image_name, quality,
                                                render_images, add_colorbar,
                                                cb_ticks_num,
                                                cb_ticks_font_size)
                images_names.append(image_fname)
        if _addon().ROT_MEDIAL_RIGHT in views:
            image_name = '{}_right_medial'.format(surf_name)
            if should_save_image(image_name):
                _addon().show_hemi('rh')
                _addon().hide_hemi('lh')
                _addon().rotate_view(_addon().ROT_SAGITTAL_LEFT)
                image_fname = save_render_image(
                    '{}_right_medial'.format(surf_name), quality,
                    render_images, add_colorbar, cb_ticks_num,
                    cb_ticks_font_size)
                images_names.append(image_fname)
        _addon().show_hemi('rh')
        _addon().show_hemi('lh')

    if views is None:
        views = list(_addon().ANGLES_DICT.keys(
        ))  # + [_addon().ROT_MEDIAL_LEFT, _addon().ROT_MEDIAL_RIGHT]
    else:
        views = list(map(int, views))
    inf_r = bpy.context.scene.inflating
    if inflated_ratio_in_file_name:
        surf_name_dict = {-1: 'pial', 0: 'inflated', 1: 'flat'}
        surf_name = surf_name_dict.get(inf_r, '')
        if surf_name == '':
            if -1 < inf_r < 0:
                surf_name = '{:.1f}_inflated'.format(1 - inf_r)
            else:
                surf_name = '{:.1f}_flat'.format(inf_r)
    else:
        surf_name = 'pial' if inf_r == -1 else 'inflated' if -1 < inf_r <= 0 else 'flat'
    if mu.get_hemi_obj('rh').hide and not mu.get_hemi_obj('lh').hide:
        hemi = 'lh'
    elif not mu.get_hemi_obj('rh').hide and mu.get_hemi_obj('lh').hide:
        hemi = 'rh'
    elif not mu.get_hemi_obj('rh').hide and not mu.get_hemi_obj('lh').hide:
        hemi = ''
    else:
        mu.write_to_stderr('You need to show at least one hemi')
    org_view_ang = tuple(mu.get_view3d_region().view_rotation)
    images_names = []
    for view in views:
        view_name = _addon().view_name(view)
        img_name = get_image_name(view_name)
        if not should_save_image(img_name):
            continue
        _addon().rotate_view(view)
        if hemi == 'lh' and rot_lh_axial and view in (
                _addon().ROT_AXIAL_SUPERIOR, _addon().ROT_AXIAL_INFERIOR):
            _addon().rotate_brain(dz=180)
        mu.center_view()
        image_fname = save_render_image(img_name, quality, render_images,
                                        add_colorbar, cb_ticks_num,
                                        cb_ticks_font_size)
        print(image_fname, view, hemi)
        images_names.append(image_fname)
    # if not mu.get_hemi_obj('rh').hide and not mu.get_hemi_obj('lh').hide:
    if views is None:
        save_medial_views()
    # todo: doesn't work
    mu.rotate_view3d(org_view_ang)
    mu.center_view()
    return images_names