Пример #1
0
def import_rois(base_path):
    anatomy_inputs = {'Cortex-rh': op.join(base_path, '{}.pial.rh'.format(bpy.context.scene.atlas)),
                      'Cortex-lh': op.join(base_path, '{}.pial.lh'.format(bpy.context.scene.atlas)),
                      'Cortex-inflated-rh': op.join(base_path, '{}.inflated.rh'.format(bpy.context.scene.atlas)),
                      'Cortex-inflated-lh': op.join(base_path, '{}.inflated.lh'.format(bpy.context.scene.atlas)),
                      'Subcortical_structures': op.join(base_path, 'subcortical'),
                      'Cerebellum': op.join(base_path, 'cerebellum')}
    brain_layer = _addon().BRAIN_EMPTY_LAYER

    bpy.context.scene.layers = [ind == brain_layer for ind in range(len(bpy.context.scene.layers))]
    layers_array = bpy.context.scene.layers
    emptys_names = ['Brain'] + list(anatomy_inputs.keys()) # ["Brain", "Subcortical_structures", "Cortex-lh", "Cortex-rh", 'Cortex-inflated-rh', 'Cortex-inflated-rh']
    for name in emptys_names:
        create_empty_if_doesnt_exists(name, brain_layer, layers_array)
    bpy.context.scene.layers = [ind == DataMakerPanel.addon.ROIS_LAYER for ind in range(len(bpy.context.scene.layers))]

    # todo: check each hemi
    inflated_imported = False
    for anatomy_name, anatomy_input_base_path in anatomy_inputs.items():
        if not op.isdir(anatomy_input_base_path):
            print('The anatomy folder {} does not exist'.format(anatomy_input_base_path))
            continue
        current_mat = bpy.data.materials['unselected_label_Mat_cortex']
        if anatomy_name in ['Subcortical_structures', 'Cerebellum']:
            current_mat = bpy.data.materials['unselected_label_Mat_subcortical']
        print('importing from {}'.format(anatomy_input_base_path))
        for ply_fname in glob.glob(op.join(anatomy_input_base_path, '*.ply')):
            try:
                new_obj_name = mu.namebase(ply_fname)
                fol_name = anatomy_input_base_path.split(op.sep)[-1]
                surf_name = 'pial' if fol_name == 'subcortical' or len(fol_name.split('.')) == 1 else fol_name.split('.')[-2]
                if surf_name == 'inflated':
                    new_obj_name = '{}_{}'.format(surf_name, new_obj_name)
                    mu.change_layer(_addon().INFLATED_ROIS_LAYER)
                elif surf_name == 'pial':
                    mu.change_layer(_addon().ROIS_LAYER)
                if not bpy.data.objects.get(new_obj_name) is None:
                    # print('{} was already imported'.format(new_obj_name))
                    continue
                if 'inflated' in new_obj_name:
                    inflated_imported = True
                bpy.ops.object.select_all(action='DESELECT')
                # print(ply_fname)
                bpy.ops.import_mesh.ply(filepath=op.join(anatomy_input_base_path, ply_fname))
                cur_obj = bpy.context.selected_objects[0]
                cur_obj.select = True
                bpy.ops.object.shade_smooth()
                cur_obj.parent = bpy.data.objects[anatomy_name]
                cur_obj.scale = [0.1] * 3
                cur_obj.active_material = current_mat
                cur_obj.hide = False
                cur_obj.name = new_obj_name
            except:
                print('Error in importing {}'.format(ply_fname))
            # cur_obj.location[0] += 5.5 if 'rh' in anatomy_name else -5.5
            # time.sleep(0.3)
    if inflated_imported:
        bpy.data.objects['Cortex-inflated-rh'].location[0] += 5.5
        bpy.data.objects['Cortex-inflated-lh'].location[0] -= 5.5
    bpy.ops.object.select_all(action='DESELECT')
Пример #2
0
def import_plane():
    # plane.dimensions[0] = 5.59
    # plane.dimensions[1] = 4.19
    skull_plane = bpy.data.objects.get('skull_plane', None)
    if skull_plane is not None:
        pass
        # return skull_plane
        # skull_plane.select = True
        # bpy.ops.object.delete()
    else:
        mu.change_layer(_addon().SKULL_LAYER)
        emptys_name = 'Skull'
        bpy.ops.object.select_all(action='DESELECT')
        bpy.ops.import_mesh.ply(
            filepath=op.join(mu.get_mmvt_dir(), 'skull_plane.ply'))
        if len(bpy.context.selected_objects) == 0:
            return None
        skull_plane = bpy.context.selected_objects[0]
        skull_plane.select = True
        bpy.ops.object.shade_smooth()
        bpy.ops.mesh.uv_texture_add()
        skull_plane.data.uv_textures['UVMap'].data[0].image = bpy.data.images[
            'neuropace.jpg']
        skull_plane.active_material = bpy.data.materials['Activity_map_mat']
        skull_plane.name = 'skull_plane'
        skull_plane.parent = bpy.data.objects[emptys_name]
    skull_plane.hide_select = True
    skull_plane.location[0] = skull_plane.location[1] = 0
    skull_plane.location[2] = 10
    skull_plane.rotation_mode = 'XYZ'
    skull_plane.rotation_euler[0] = skull_plane.rotation_euler[1] = 0
    skull_plane.rotation_euler[2] = -math.pi / 2
Пример #3
0
def create_eeg_mesh():
    mu.change_layer(_addon().BRAIN_EMPTY_LAYER)
    create_empty_if_doesnt_exists('Helmets', _addon().BRAIN_EMPTY_LAYER, bpy.context.scene.layers, 'Functional maps')
    mu.change_layer(_addon().EEG_LAYER)
    current_mat = bpy.data.materials['unselected_label_Mat_cortex']
    bpy.ops.import_mesh.ply(filepath=op.join(mu.get_user_fol(), 'eeg', 'eeg_helmet.ply'))
    mesh_obj = bpy.context.selected_objects[0]
    mesh_obj.name = 'eeg_helmet'
    mesh_obj.select = True
    bpy.ops.object.shade_smooth()
    mesh_obj.parent = bpy.data.objects['Helmets']
    mesh_obj.scale = [0.1] * 3
    mesh_obj.active_material = current_mat
    mesh_obj.hide = False
    return mesh_obj
Пример #4
0
def import_hemis_for_functional_maps(base_path):
    mu.change_layer(_addon().BRAIN_EMPTY_LAYER)
    layers_array = bpy.context.scene.layers
    emptys_names = [
        'Functional maps', 'Subcortical_meg_activity_map',
        'Subcortical_fmri_activity_map'
    ]
    for name in emptys_names:
        create_empty_if_doesnt_exists(name,
                                      _addon().BRAIN_EMPTY_LAYER, layers_array,
                                      'Functional maps')

    # for ii in range(len(bpy.context.scene.layers)):
    #     bpy.context.scene.layers[ii] = (ii == brain_layer)

    print("importing Hemispheres")
    # # for cur_val in bpy.context.scene.layers:
    # #     print(cur_val)
    # print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    now = time.time()
    ply_files = glob.glob(op.join(base_path, 'surf', '*.ply'))
    N = len(ply_files)
    for ind, ply_fname in enumerate(ply_files):
        mu.time_to_go(now, ind, N, 10)
        bpy.ops.object.select_all(action='DESELECT')
        obj_name = mu.namebase(ply_fname).split(sep='.')[0]
        surf_name = mu.namebase(ply_fname).split(sep='.')[1]
        if surf_name == 'inflated':
            obj_name = '{}_{}'.format(surf_name, obj_name)
            mu.change_layer(_addon().INFLATED_ACTIVITY_LAYER)
        elif surf_name == 'pial':
            mu.change_layer(_addon().ACTIVITY_LAYER)
        else:
            raise Exception(
                'The surface type {} is not supported!'.format(surf_name))
        if bpy.data.objects.get(obj_name) is None:
            bpy.ops.import_mesh.ply(
                filepath=op.join(base_path, 'surf', ply_fname))
            cur_obj = bpy.context.selected_objects[0]
            cur_obj.select = True
            bpy.ops.object.shade_smooth()
            cur_obj.scale = [0.1] * 3
            cur_obj.hide = False
            cur_obj.name = obj_name
            if surf_name == 'inflated':
                cur_obj.active_material = bpy.data.materials[
                    'Inflated_Activity_map_mat']
                cur_obj.location[
                    0] += 5.5 if obj_name == 'inflated_rh' else -5.5
            else:
                cur_obj.active_material = bpy.data.materials[
                    'Activity_map_mat']
            cur_obj.parent = bpy.data.objects["Functional maps"]
            cur_obj.hide_select = True
            cur_obj.data.vertex_colors.new()
        else:
            print('{} already exists'.format(ply_fname))

    _addon().create_inflated_curv_coloring()
    bpy.ops.object.select_all(action='DESELECT')
Пример #5
0
def import_skull():
    mu.change_layer(_addon().BRAIN_EMPTY_LAYER)
    base_path = op.join(mu.get_user_fol(), 'skull')
    emptys_name = 'Skull'
    layers_array = bpy.context.scene.layers
    _addon().create_empty_if_doesnt_exists(emptys_name,
                                           _addon().BRAIN_EMPTY_LAYER,
                                           layers_array)

    for skull_type in ['inner_skull', 'outer_skull']:
        bpy.ops.object.select_all(action='DESELECT')
        bpy.ops.import_mesh.ply(
            filepath=op.join(base_path, '{}.ply'.format(skull_type)))
        cur_obj = bpy.context.selected_objects[0]
        cur_obj.select = True
        bpy.ops.object.shade_smooth()
        cur_obj.scale = [0.1] * 3
        cur_obj.hide = False
        cur_obj.name = skull_type
        cur_obj.active_material = bpy.data.materials['Activity_map_mat']
        cur_obj.parent = bpy.data.objects[emptys_name]
        cur_obj.hide_select = True
        cur_obj.data.vertex_colors.new()
Пример #6
0
def import_hemis_for_functional_maps(base_path):
    mu.change_layer(_addon().BRAIN_EMPTY_LAYER)
    layers_array = bpy.context.scene.layers
    emptys_names = ['Functional maps', 'Subcortical_meg_activity_map', 'Subcortical_fmri_activity_map']
    for name in emptys_names:
        create_empty_if_doesnt_exists(name, _addon().BRAIN_EMPTY_LAYER, layers_array, 'Functional maps')

    # for ii in range(len(bpy.context.scene.layers)):
    #     bpy.context.scene.layers[ii] = (ii == brain_layer)

    print("importing Hemispheres")
    # # for cur_val in bpy.context.scene.layers:
    # #     print(cur_val)
    # print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    now = time.time()
    ply_files = glob.glob(op.join(base_path, 'surf', '*.ply'))
    N = len(ply_files)
    for ind, ply_fname in enumerate(ply_files):
        try:
            mu.time_to_go(now, ind, N, 10)
            bpy.ops.object.select_all(action='DESELECT')
            obj_name = mu.namebase(ply_fname).split(sep='.')[0]
            surf_name = mu.namebase(ply_fname).split(sep='.')[1]
            if surf_name == 'inflated':
                obj_name = '{}_{}'.format(surf_name, obj_name)
                mu.change_layer(_addon().INFLATED_ACTIVITY_LAYER)
            elif surf_name == 'pial':
                mu.change_layer(_addon().ACTIVITY_LAYER)
            else:
                raise Exception('The surface type {} is not supported!'.format(surf_name))
            if bpy.data.objects.get(obj_name) is None:
                bpy.ops.import_mesh.ply(filepath=op.join(base_path, 'surf', ply_fname))
                cur_obj = bpy.context.selected_objects[0]
                cur_obj.select = True
                bpy.ops.object.shade_smooth()
                cur_obj.scale = [0.1] * 3
                cur_obj.hide = False
                cur_obj.name = obj_name
                if surf_name == 'inflated':
                    cur_obj.active_material = bpy.data.materials['Inflated_Activity_map_mat']
                    cur_obj.location[0] += 5.5 if obj_name == 'inflated_rh' else -5.5
                else:
                    cur_obj.active_material = bpy.data.materials['Activity_map_mat']
                cur_obj.parent = bpy.data.objects["Functional maps"]
                cur_obj.hide_select = True
                cur_obj.data.vertex_colors.new()
            # else:
            #     print('{} already exists'.format(ply_fname))
        except:
            print('Error in importing {}'.format(ply_fname))

    _addon().create_inflated_curv_coloring()
    bpy.ops.object.select_all(action='DESELECT')
Пример #7
0
def import_rois(base_path):
    anatomy_inputs = {
        'Cortex-rh':
        op.join(base_path, '{}.pial.rh'.format(bpy.context.scene.atlas)),
        'Cortex-lh':
        op.join(base_path, '{}.pial.lh'.format(bpy.context.scene.atlas)),
        'Cortex-inflated-rh':
        op.join(base_path, '{}.inflated.rh'.format(bpy.context.scene.atlas)),
        'Cortex-inflated-lh':
        op.join(base_path, '{}.inflated.lh'.format(bpy.context.scene.atlas)),
        'Subcortical_structures':
        op.join(base_path, 'subcortical')
    }
    brain_layer = DataMakerPanel.addon.BRAIN_EMPTY_LAYER

    bpy.context.scene.layers = [
        ind == brain_layer for ind in range(len(bpy.context.scene.layers))
    ]
    layers_array = bpy.context.scene.layers
    emptys_names = ['Brain'] + list(
        anatomy_inputs.keys()
    )  # ["Brain", "Subcortical_structures", "Cortex-lh", "Cortex-rh", 'Cortex-inflated-rh', 'Cortex-inflated-rh']
    for name in emptys_names:
        create_empty_if_doesnt_exists(name, brain_layer, layers_array)
    bpy.context.scene.layers = [
        ind == DataMakerPanel.addon.ROIS_LAYER
        for ind in range(len(bpy.context.scene.layers))
    ]

    for anatomy_name, anatomy_input_base_path in anatomy_inputs.items():
        if not op.isdir(anatomy_input_base_path):
            print('The anatomy folder {} does not exist'.format(
                anatomy_input_base_path))
            continue
        current_mat = bpy.data.materials['unselected_label_Mat_cortex']
        if anatomy_name == 'Subcortical_structures':
            current_mat = bpy.data.materials[
                'unselected_label_Mat_subcortical']
        print('importing from {}'.format(anatomy_input_base_path))
        for ply_fname in glob.glob(op.join(anatomy_input_base_path, '*.ply')):
            new_obj_name = mu.namebase(ply_fname)
            fol_name = anatomy_input_base_path.split(op.sep)[-1]
            surf_name = 'pial' if fol_name == 'subcortical' else fol_name.split(
                '.')[1]
            if surf_name == 'inflated':
                new_obj_name = '{}_{}'.format(surf_name, new_obj_name)
                mu.change_layer(_addon().INFLATED_ROIS_LAYER)
            elif surf_name == 'pial':
                mu.change_layer(_addon().ROIS_LAYER)
            if not bpy.data.objects.get(new_obj_name) is None:
                continue
            bpy.ops.object.select_all(action='DESELECT')
            # print(ply_fname)
            bpy.ops.import_mesh.ply(
                filepath=op.join(anatomy_input_base_path, ply_fname))
            cur_obj = bpy.context.selected_objects[0]
            cur_obj.select = True
            bpy.ops.object.shade_smooth()
            cur_obj.parent = bpy.data.objects[anatomy_name]
            cur_obj.scale = [0.1] * 3
            cur_obj.active_material = current_mat
            cur_obj.hide = False
            cur_obj.name = new_obj_name
            # cur_obj.location[0] += 5.5 if 'rh' in anatomy_name else -5.5
            # time.sleep(0.3)
    bpy.data.objects['Cortex-inflated-rh'].location[0] += 5.5
    bpy.data.objects['Cortex-inflated-lh'].location[0] -= 5.5
    bpy.ops.object.select_all(action='DESELECT')