def get_objects_to_color(self, names, objects_indices): curves_num = 0 for ind in range(min(self.topK, len(objects_indices))): orig_name = names[objects_indices[ind]] if 'unknown' not in orig_name: obj = bpy.data.objects.get(orig_name) if not obj is None and not obj.animation_data is None: curves_num += len(obj.animation_data.action.fcurves) colors = cu.get_distinct_colors(curves_num) objects_names, objects_colors, objects_data = defaultdict( list), defaultdict(list), defaultdict(list) for ind in range(min(self.topK, len(objects_indices)) - 1, -1, -1): if bpy.data.objects.get(names[objects_indices[ind]]): orig_name = names[objects_indices[ind]] obj_type = mu.check_obj_type(orig_name) objects_names[obj_type].append(orig_name) objects_colors[obj_type].append(cu.name_to_rgb('green')) objects_data[obj_type].append(1.0) if 'unknown' not in orig_name: filter_roi_func(orig_name) for fcurve in bpy.data.objects[ orig_name].animation_data.action.fcurves: fcurve.color_mode = 'CUSTOM' fcurve.color = tuple(next(colors)) else: print("Can't find {}!".format(names[objects_indices[ind]])) return objects_names, objects_colors, objects_data
def modal(self, context, event): # def show_fcurves(obj): # mu.change_fcurves_colors(obj) # mu.view_all_in_graph_editor() if self.right_clicked: if len(bpy.context.selected_objects): selected_obj_name = bpy.context.selected_objects[0].name selected_obj_type = mu.check_obj_type(selected_obj_name) if selected_obj_type in [ mu.OBJ_TYPE_CORTEX_LH, mu.OBJ_TYPE_CORTEX_RH, mu.OBJ_TYPE_ELECTRODE, mu.OBJ_TYPE_EEG ]: obj = bpy.data.objects.get(selected_obj_name) if obj: mu.change_fcurves_colors(obj) if selected_obj_type in [ mu.OBJ_TYPE_CORTEX_INFLATED_LH, mu.OBJ_TYPE_CORTEX_INFLATED_RH ]: pial_obj_name = selected_obj_name[len('inflated_'):] pial_obj = bpy.data.objects.get(pial_obj_name) if not pial_obj is None: pial_obj.select = True mu.change_fcurves_colors(pial_obj) self.right_clicked = False if time.time() - self.press_time > 1 and event.type == 'RIGHTMOUSE': self.press_time = time.time() self.right_clicked = True return {'PASS_THROUGH'}
def invoke(self, context, event=None): if bpy.context.scene.electrodes_color_only_selected: for elc_obj in bpy.context.selected_objects: if not elc_obj.hide and mu.check_obj_type(elc_obj) == mu.OBJ_TYPE_ELECTRODE: _addon().object_coloring(elc_obj, bpy.context.scene.electrodes_color) else: for elc_obj in ElecsPanel.parent.children: if not elc_obj.hide: _addon().object_coloring(elc_obj, bpy.context.scene.electrodes_color) return {'FINISHED'}
def color_groups_manually(): ColoringMakerPanel.what_is_colored.add(WIC_GROUPS) init_activity_map_coloring('FMRI') labels = ColoringMakerPanel.labels_groups[bpy.context.scene.labels_groups] objects_names, colors, data = defaultdict(list), defaultdict(list), defaultdict(list) for label in labels: obj_type = mu.check_obj_type(label['name']) if obj_type is not None: objects_names[obj_type].append(label['name']) colors[obj_type].append(np.array(label['color']) / 255.0) data[obj_type].append(1.) clear_subcortical_fmri_activity() color_objects(objects_names, colors, data)
def filter_rois(self, current_file_to_upload): print('filter_ROIs') _addon().show_rois() source_files = [ op.join(self.current_activity_path, current_file_to_upload.format(hemi=hemi)) for hemi in mu.HEMIS ] objects_indices, names, self.filter_values = self.get_object_to_filter( source_files) Filtering.objects_indices, Filtering.filter_objects = objects_indices, names deselect_all_objects() if bpy.context.scene.selection_type == 'diff': filter_obj_names = [names[ind] for ind in objects_indices] brain_obj = bpy.data.objects['Brain'] for fcurve in brain_obj.animation_data.action.fcurves: con_name = mu.get_fcurve_name(fcurve) fcurve.hide = con_name not in filter_obj_names fcurve.select = not fcurve.hide brain_obj.select = True curves_num = 0 for ind in range(min(self.topK, len(objects_indices))): orig_name = names[objects_indices[ind]] if 'unknown' not in orig_name: obj = bpy.data.objects.get(orig_name) if not obj is None and not obj.animation_data is None: curves_num += len(obj.animation_data.action.fcurves) colors = cu.get_distinct_colors(curves_num) objects_names, objects_colors, objects_data = defaultdict( list), defaultdict(list), defaultdict(list) for ind in range(min(self.topK, len(objects_indices)) - 1, -1, -1): if bpy.data.objects.get(names[objects_indices[ind]]): orig_name = names[objects_indices[ind]] obj_type = mu.check_obj_type(orig_name) objects_names[obj_type].append(orig_name) objects_colors[obj_type].append(cu.name_to_rgb('green')) objects_data[obj_type].append(1.0) if 'unknown' not in orig_name: filter_roi_func(orig_name) for fcurve in bpy.data.objects[ orig_name].animation_data.action.fcurves: fcurve.color_mode = 'CUSTOM' fcurve.color = tuple(next(colors)) else: print("Can't find {}!".format(names[objects_indices[ind]])) _addon().color_objects(objects_names, objects_colors, objects_data)
def _electrodes_update(): if _addon() is None or not ElecsPanel.init: return # mu.print_traceback() prev_elect = ElecsPanel.current_electrode ElecsPanel.current_electrode = current_electrode = bpy.context.scene.electrodes bpy.context.scene.current_lead = ElecsPanel.groups[current_electrode] update_cursor() color_electrodes(current_electrode, prev_elect) # bpy.context.scene.objects.active = bpy.data.objects[current_electrode] selected_electrodes = [ obj.name for obj in bpy.context.selected_objects if mu.check_obj_type(obj.name) == mu.OBJ_TYPE_ELECTRODE ] # Check if it's a new selection: # print(len(selected_electrodes), current_electrode, ElecsPanel.prev_electrodes) if len(selected_electrodes ) == 1 and current_electrode not in ElecsPanel.prev_electrodes: # Clear and init prev_electrodes unselect_prev_electrode(ElecsPanel.prev_electrodes) ElecsPanel.prev_electrodes = set([current_electrode]) # Check if this is a new electrodes where the shift is pressed elif len(selected_electrodes ) > 1 and current_electrode not in ElecsPanel.prev_electrodes: # Add current electrode to prev_electrodes ElecsPanel.prev_electrodes.add(current_electrode) # Check if the user unselect one of the selected electrodes elif len(selected_electrodes ) > 1 and current_electrode in ElecsPanel.prev_electrodes: bpy.data.objects[current_electrode].select = False unselect_prev_electrode([current_electrode]) ElecsPanel.prev_electrodes.remove(current_electrode) else: clear_electrodes_selection() if not ElecsPanel.lookup is None: loc = ElecsPanel.lookup.get(current_electrode, None) if loc is None: print("Can't find {} in ElecsPanel.lookup!".format( current_electrode)) else: print_electrode_loc(loc) if bpy.context.scene.color_lables: plot_labels_probs(loc) else: print('lookup table is None!') # mu.change_fcurves_colors(bpy.data.objects[current_electrode]) mu.change_selected_fcurves_colors(mu.OBJ_TYPE_ELECTRODE)
def main_func(self): bpy.data.objects['Brain'].select = False closest_area = find_closest_obj() bpy.types.Scene.where_am_i_str = closest_area WhereAmI.where_am_I_selected_obj = bpy.data.objects[closest_area] WhereAmI.where_am_I_selected_obj_org_hide = bpy.data.objects[ closest_area].hide bpy.context.scene.objects.active = bpy.data.objects[closest_area] closest_area_type = mu.check_obj_type(closest_area) if closest_area_type in [ mu.OBJ_TYPE_CORTEX_LH, mu.OBJ_TYPE_CORTEX_RH, mu.OBJ_TYPE_ELECTRODE, mu.OBJ_TYPE_EEG ]: _addon().select_roi(closest_area) else: bpy.data.objects[closest_area].select = True bpy.data.objects[closest_area].hide = False
def modal(self, context, event): # def show_fcurves(obj): # mu.change_fcurves_colors(obj) # mu.view_all_in_graph_editor() if self.right_clicked: if len(bpy.context.selected_objects): selected_obj_name = bpy.context.selected_objects[0].name selected_obj_type = mu.check_obj_type(selected_obj_name) if selected_obj_type in [mu.OBJ_TYPE_CORTEX_LH, mu.OBJ_TYPE_CORTEX_RH, mu.OBJ_TYPE_ELECTRODE, mu.OBJ_TYPE_EEG]: obj = bpy.data.objects.get(selected_obj_name) if obj: mu.change_fcurves_colors(obj) if selected_obj_type in [mu.OBJ_TYPE_CORTEX_INFLATED_LH, mu.OBJ_TYPE_CORTEX_INFLATED_RH]: pial_obj_name = selected_obj_name[len('inflated_'):] pial_obj = bpy.data.objects.get(pial_obj_name) if not pial_obj is None: pial_obj.select = True mu.change_fcurves_colors(pial_obj) self.right_clicked = False if time.time() - self.press_time > 1: if event.type == 'RIGHTMOUSE': self.press_time = time.time() self.right_clicked = True if event.type == 'LEFTMOUSE': if _addon().fMRI_clusters_files_exist() and bpy.context.scene.plot_fmri_cluster_per_click: _addon().find_closest_cluster(only_within=True) if not _addon().render_in_queue() is None: rendering_data = mu.queue_get(_addon().render_in_queue()) if not rendering_data is None: try: rendering_data = rendering_data.decode(sys.getfilesystemencoding(), 'ignore') if '*** finish rendering! ***' in rendering_data.lower(): print('Finish rendering!') _addon().finish_rendering() except: print("Can't read the stdout from the rendering") return {'PASS_THROUGH'}
def color_manually(): ColoringMakerPanel.what_is_colored.add(WIC_MANUALLY) init_activity_map_coloring('FMRI') subject_fol = mu.get_user_fol() objects_names, colors, data = defaultdict(list), defaultdict(list), defaultdict(list) for line in mu.csv_file_reader(op.join(subject_fol, 'coloring', '{}.csv'.format(bpy.context.scene.coloring_files))): obj_name, color_name = line[0], line[1:] if obj_name[0] == '#': continue if isinstance(color_name, list) and len(color_name) == 1: color_name = color_name[0] obj_type = mu.check_obj_type(obj_name) if isinstance(color_name, str) and color_name.startswith('mark'): import filter_panel filter_panel.filter_roi_func(obj_name, mark=color_name) else: if isinstance(color_name, str): color_rgb = cu.name_to_rgb(color_name) # Check if the color is already in RBG elif len(color_name) == 3: color_rgb = color_name else: print('Unrecognize color! ({})'.format(color_name)) continue color_rgb = list(map(float, color_rgb)) if obj_type is not None: objects_names[obj_type].append(obj_name) colors[obj_type].append(color_rgb) data[obj_type].append(1.) color_objects(objects_names, colors, data) if op.isfile(op.join(subject_fol, 'coloring', '{}_legend.jpg'.format(bpy.context.scene.coloring_files))): cmd = '{} -m src.preproc.electrodes_preproc -s {} -a {} -f show_labeling_coloring'.format( bpy.context.scene.python_cmd, mu.get_user(), bpy.context.scene.atlas) print('Running {}'.format(cmd)) mu.run_command_in_new_thread(cmd, False)
def modal(self, context, event): if self.left_clicked: self.left_clicked = False active_image, pos = click_inside_images_view(event) if active_image is not None: xyz = _addon().slices_were_clicked(active_image, pos) bpy.context.scene.cursor_location = tuple(xyz) set_cursor_pos() if bpy.context.scene.cursor_is_snapped: # and is_view_3d(): _addon().set_tkreg_ras( bpy.context.scene.cursor_location * 10, False) snap_cursor(True) if bpy.context.scene.slices_rotate_view_on_click: mu.rotate_view_to_vertice() # if bpy.context.scene.slices_zoom > 1: # ohad(pos/bpy.context.scene.slices_zoom) return {'PASS_THROUGH'} if not click_inside_3d_view(event): return {'PASS_THROUGH'} if _addon().meg.change_cursor_on_selection(): cluster = _addon().select_meg_cluster( event, context, bpy.context.scene.cursor_location) if cluster is not None: return {'PASS_THROUGH'} cursor_moved = np.linalg.norm( SelectionListener.cursor_pos - bpy.context.scene.cursor_location) > 1e-3 if cursor_moved and bpy.data.objects.get('inner_skull', None) is not None: _addon().find_point_thickness() return {'PASS_THROUGH'} if bpy.context.scene.cursor_is_snapped: # and is_view_3d(): snap_cursor(True) if _addon().fMRI_clusters_files_exist( ) and bpy.context.scene.plot_fmri_cluster_per_click: _addon().find_closest_cluster(only_within=True) tkreg_ras = _addon().calc_tkreg_ras_from_cursor() if tkreg_ras is not None: _addon().set_tkreg_ras(tkreg_ras, move_cursor=False) # if _addon().is_pial(): # tkreg_ras = bpy.context.scene.cursor_location * 10 # _addon().set_tkreg_ras(tkreg_ras) # elif bpy.context.scene.cursor_is_snapped: # tkreg_ras = _addon().calc_tkreg_ras_from_snapped_cursor() # _addon().set_tkreg_ras(tkreg_ras) if cursor_moved: set_cursor_pos() # print('cursor position was changed by the user!') _addon().create_slices(pos=tkreg_ras) _addon().freeview.save_cursor_position() clear_slice() if bpy.context.scene.find_closest_label_on_click: # coloring_panel.WIC_CONTOURS in _addon().what_is_colored(): _addon().find_closest_label( plot_contour=bpy.context.scene.plot_closest_label_contour) if self.right_clicked: self.right_clicked = False if not click_inside_3d_view(event): return {'PASS_THROUGH'} # print(bpy.context.selected_objects) # cluster = _addon().select_meg_cluster(event, context) # if cluster is not None: # return {'PASS_THROUGH'} if len(bpy.context.selected_objects): mu.unfilter_graph_editor() if bpy.context.scene.fit_graph_on_selection: mu.view_all_in_graph_editor() selected_obj = bpy.context.active_object # bpy.context.selected_objects[-1] selected_obj_name = selected_obj.name selected_obj_type = mu.check_obj_type(selected_obj_name) if selected_obj_type in [ mu.OBJ_TYPE_CORTEX_LH, mu.OBJ_TYPE_CORTEX_RH ]: _addon().select_roi(selected_obj_name) elif selected_obj_type in [ mu.OBJ_TYPE_CORTEX_INFLATED_LH, mu.OBJ_TYPE_CORTEX_INFLATED_RH ]: pial_obj_name = selected_obj_name[len('inflated_'):] pial_obj = bpy.data.objects.get(pial_obj_name) if not pial_obj is None: # pial_obj.select = True _addon().select_roi(pial_obj_name) # mu.change_selected_fcurves_colors(pial_obj) # mu.change_selected_fcurves_colors() elif selected_obj_type == mu.OBJ_TYPE_CON: _addon().select_connection(selected_obj_name) elif selected_obj_type == mu.OBJ_TYPE_CON_VERTICE: _addon().vertices_selected(selected_obj_name) elif selected_obj_type == mu.OBJ_TYPE_ELECTRODE: bpy.context.scene.cursor_is_snapped = False _addon().electode_was_manually_selected(selected_obj_name) try: _addon().dell.dell_ct_electrode_was_selected( selected_obj_name) except: pass if bpy.context.scene.find_curves_sep_auto: _addon().calc_best_curves_sep() elif bpy.context.scene.curves_sep > 0: _addon().curves_sep_update() else: _addon().clear_electrodes_selection() #todo: should call to _addon().clear_rois_selection() # if is_activity(): # bpy.context.scene.cursor_location = mouse_coo_to_3d_loc(event, context) # snap_cursor(True) # _addon().find_closest_label() if time.time() - self.press_time > 1 and event.value == 'PRESS': if event.type == 'RIGHTMOUSE': self.press_time = time.time() self.right_clicked = True if event.type == 'LEFTMOUSE': self.press_time = time.time() self.left_clicked = True if time.time() - self.press_time > 0.1: if event.type == 'TIMER': if bpy.context.scene.rotate_brain: if _addon().render.is_camera_view(): _addon().render.camera_mode('ORTHO') _addon().show_hide.rotate_brain() _addon().render.camera_mode('CAMERA') else: _addon().show_hide.rotate_brain() if _addon() and _addon().render_in_queue(): rendering_data = mu.queue_get(_addon().render_in_queue()) if not rendering_data is None: try: rendering_data = rendering_data.decode( sys.getfilesystemencoding(), 'ignore') if '*** finish rendering! ***' in rendering_data.lower(): print('Finish rendering!') _addon().finish_rendering() except: print("Can't read the stdout from the rendering") return {'PASS_THROUGH'}
def get_selected_electrodes(): return [obj.name for obj in bpy.context.selected_objects if mu.check_obj_type(obj.name) == mu.OBJ_TYPE_ELECTRODE]
def get_selected_labels(): return [obj.name for obj in bpy.context.selected_objects if mu.check_obj_type(obj.name) in [mu.OBJ_TYPE_CORTEX_RH, mu.OBJ_TYPE_CORTEX_LH]]
def modal(self, context, event): # def show_fcurves(obj): # mu.change_fcurves_colors(obj) # mu.view_all_in_graph_editor() if self.left_clicked: self.left_clicked = False if bpy.context.scene.cursor_is_snapped: snap_cursor(True) if _addon().fMRI_clusters_files_exist( ) and bpy.context.scene.plot_fmri_cluster_per_click: _addon().find_closest_cluster(only_within=True) if _addon().is_pial(): _addon().set_tkreg_ras_coo(bpy.context.scene.cursor_location * 10) if self.cursor_pos != tuple(bpy.context.scene.cursor_location): self.cursor_pos = tuple(bpy.context.scene.cursor_location) _addon().create_slices() _addon().save_cursor_position() if bpy.context.scene.is_sliced_ind > -1: tmp_new = bpy.context.scene.cursor_location[ bpy.context.scene.is_sliced_ind] tmp_old = bpy.context.scene.last_cursor_location[ bpy.context.scene.is_sliced_ind] if abs(tmp_new - tmp_old) > 0.005: _addon().clear_slice() if bpy.data.objects.get('inner_skull', None) is not None: _addon().find_point_thickness() _addon().slices_were_clicked() if self.right_clicked: self.right_clicked = False # print(bpy.context.selected_objects) if len(bpy.context.selected_objects): mu.unfilter_graph_editor() if bpy.context.scene.fit_graph_on_selection: mu.view_all_in_graph_editor() selected_obj = bpy.context.active_object # bpy.context.selected_objects[-1] selected_obj_name = selected_obj.name selected_obj_type = mu.check_obj_type(selected_obj_name) if selected_obj_type in [ mu.OBJ_TYPE_CORTEX_LH, mu.OBJ_TYPE_CORTEX_RH ]: _addon().select_roi(selected_obj_name) elif selected_obj_type in [ mu.OBJ_TYPE_CORTEX_INFLATED_LH, mu.OBJ_TYPE_CORTEX_INFLATED_RH ]: pial_obj_name = selected_obj_name[len('inflated_'):] pial_obj = bpy.data.objects.get(pial_obj_name) if not pial_obj is None: # pial_obj.select = True _addon().select_roi(pial_obj_name) # mu.change_selected_fcurves_colors(pial_obj) # mu.change_selected_fcurves_colors() elif selected_obj_type == mu.OBJ_TYPE_CON: _addon().select_connection(selected_obj_name) elif selected_obj_type == mu.OBJ_TYPE_CON_VERTICE: _addon().vertices_selected(selected_obj_name) elif selected_obj_type == mu.OBJ_TYPE_ELECTRODE: _addon().electode_was_manually_selected(selected_obj_name) if bpy.context.scene.find_curves_sep_auto: _addon().calc_best_curves_sep() elif bpy.context.scene.curves_sep > 0: _addon().curves_sep_update() else: _addon().clear_electrodes_selection() if time.time() - self.press_time > 1: if event.type == 'RIGHTMOUSE': self.press_time = time.time() self.right_clicked = True if event.type == 'LEFTMOUSE': self.press_time = time.time() self.left_clicked = True if time.time() - self.press_time > 0.1: if event.type == 'TIMER': if bpy.context.scene.rotate_brain: _addon().rotate_brain() if _addon() and _addon().render_in_queue(): rendering_data = mu.queue_get(_addon().render_in_queue()) if not rendering_data is None: try: rendering_data = rendering_data.decode( sys.getfilesystemencoding(), 'ignore') if '*** finish rendering! ***' in rendering_data.lower(): print('Finish rendering!') _addon().finish_rendering() except: print("Can't read the stdout from the rendering") return {'PASS_THROUGH'}