def create_conncection_for_both_conditions(d, layers_rods, indices, mask, windows_num, norm_fac, T, radius): N = len(indices) parent_obj = bpy.data.objects[PARENT_OBJ] print('Create connections for both conditions') now = time.time() for run, (ind, conn_name, (i, j)) in enumerate(zip(indices, d.con_names[mask], d.con_indices[mask])): mu.time_to_go(now, run, N, runs_num_to_print=10) if d.con_colors.ndim == 3: con_color = np.hstack((d.con_colors[ind, 0, :], [0.])) else: con_color = np.hstack((d.con_colors[ind, :], [0.])) p1, p2 = d.locations[i, :] * 0.1, d.locations[j, :] * 0.1 mu.cylinder_between(p1, p2, radius, layers_rods) # mu.create_material('{}_mat'.format(conn_name), (0, 0, 1, 1), 1) mu.create_material('{}_mat'.format(conn_name), con_color, 1) cur_obj = bpy.context.active_object cur_obj.name = conn_name cur_obj.parent = parent_obj # cur_obj.animation_data_clear() if windows_num == 1: continue for cond_id, cond in enumerate(d.conditions): # insert_frame_keyframes(cur_obj, '{}-{}'.format(conn_name, cond), d.con_values[ind, -1, cond_id], T) for t in range(windows_num): extra_time_points = 0 if norm_fac ==1 else 2 timepoint = t * norm_fac + extra_time_points mu.insert_keyframe_to_custom_prop(cur_obj, '{}-{}'.format(conn_name, cond), d.con_values[ind, t, cond_id], timepoint) finalize_fcurves(cur_obj)
def create_conncection_per_condition(d, layers_rods, indices, mask, windows_num, norm_fac, T, radius): N = len(indices) parent_obj = bpy.data.objects[get_connections_parent_name()] print('Create connections for both conditions') con_color = (1, 1, 1, 1) now = time.time() for run, (ind, conn_name, (i, j)) in enumerate(zip(indices, d.con_names[mask], d.con_indices[mask])): mu.time_to_go(now, run, N, runs_num_to_print=10) p1, p2 = d.locations[i, :] * 0.1, d.locations[j, :] * 0.1 mu.cylinder_between(p1, p2, radius, layers_rods) mu.create_material('{}_mat'.format(conn_name), con_color, 1) cur_obj = bpy.context.active_object cur_obj.name = conn_name cur_obj.parent = parent_obj # cur_obj.animation_data_clear() if windows_num == 1: continue for cond_id, cond in enumerate(d.conditions): # insert_frame_keyframes(cur_obj, '{}-{}'.format(conn_name, cond), d.con_values[ind, -1, cond_id], T) for t in range(windows_num): extra_time_points = 0 if norm_fac == 1 else 2 timepoint = t * norm_fac + extra_time_points mu.insert_keyframe_to_custom_prop(cur_obj, '{}-{}'.format(conn_name, cond), d.con_values[ind, t, cond_id], timepoint) fcurve = cur_obj.animation_data.action.fcurves[cond_id] fcurve.keyframe_points[0].co[1] = 0 fcurve.keyframe_points[-1].co[1] = 0 finalize_fcurves(cur_obj) mu.change_fcurves_colors(parent_obj.children)
def create_keyframes_for_parent_obj(d, indices, mask, windows_num, norm_fac, T, stat=STAT_DIFF): # Create keyframes for the parent obj (conditions diff) if windows_num == 1: return if stat not in [STAT_DIFF, STAT_AVG]: print("Wrong type of stat!") return parent_obj = bpy.data.objects[get_connections_parent_name()] stat_data = calc_stat_data(d.con_values, stat) N = len(indices) now = time.time() for run, (ind, conn_name) in enumerate(zip(indices, d.con_names[mask])): mu.time_to_go(now, run, N, runs_num_to_print=100) # insert_frame_keyframes(parent_obj, conn_name, stat_data[ind, -1], T) for t in range(windows_num): extra_time_points = 0 if norm_fac == 1 else 2 timepoint = t * norm_fac + extra_time_points mu.insert_keyframe_to_custom_prop(parent_obj, conn_name, stat_data[ind, t], timepoint) fcurve = parent_obj.animation_data.action.fcurves[run] fcurve.keyframe_points[0].co[1] = 0 fcurve.keyframe_points[-1].co[1] = 0 finalize_fcurves(parent_obj) finalize_objects_creations()
def create_conncection_for_both_conditions(d, layers_rods, indices, mask, windows_num, norm_fac, T, radius): N = len(indices) parent_obj = bpy.data.objects[PARENT_OBJ] print('Create connections for both conditions') now = time.time() for run, (ind, conn_name, (i, j)) in enumerate( zip(indices, d.con_names[mask], d.con_indices[mask])): mu.time_to_go(now, run, N, runs_num_to_print=10) if d.con_colors.ndim == 3: con_color = np.hstack((d.con_colors[ind, 0, :], [0.])) else: con_color = np.hstack((d.con_colors[ind, :], [0.])) p1, p2 = d.locations[i, :] * 0.1, d.locations[j, :] * 0.1 mu.cylinder_between(p1, p2, radius, layers_rods) # mu.create_material('{}_mat'.format(conn_name), (0, 0, 1, 1), 1) mu.create_material('{}_mat'.format(conn_name), con_color, 1) cur_obj = bpy.context.active_object cur_obj.name = conn_name cur_obj.parent = parent_obj # cur_obj.animation_data_clear() if windows_num == 1: continue for cond_id, cond in enumerate(d.conditions): # insert_frame_keyframes(cur_obj, '{}-{}'.format(conn_name, cond), d.con_values[ind, -1, cond_id], T) for t in range(windows_num): extra_time_points = 0 if norm_fac == 1 else 2 timepoint = t * norm_fac + extra_time_points mu.insert_keyframe_to_custom_prop( cur_obj, '{}-{}'.format(conn_name, cond), d.con_values[ind, t, cond_id], timepoint) finalize_fcurves(cur_obj)
def keyframe_empty_test(self, empty_name, closest_mesh_name, vertex_ind, data_path): obj = bpy.data.objects[empty_name] lookup = np.load(op.join(data_path, 'activity_map_' + closest_mesh_name + '_verts_lookup.npy')) file_num_str = str(int(lookup[vertex_ind, 0])) line_num = int(lookup[vertex_ind, 1]) data_file = np.load( op.join(data_path, 'activity_map_' + closest_mesh_name + '_verts', file_num_str + '.npy')) data = data_file[line_num, :].squeeze() number_of_time_points = len(data) mu.insert_keyframe_to_custom_prop(obj, 'data', 0, 0) mu.insert_keyframe_to_custom_prop(obj, 'data', 0, number_of_time_points + 1) for ii in range(number_of_time_points): print(ii) frame_str = str(ii) self.insert_keyframe_to_custom_prop(self, obj, 'data', float(data[ii]), ii + 1) # insert_keyframe_to_custom_prop(obj,'data',0,ii+1) fcurves = bpy.data.objects[empty_name].animation_data.action.fcurves[0] mod = fcurves.modifiers.new(type='LIMITS')
def create_keyframes_for_parent_obj(d, indices, mask, windows_num, norm_fac, T, stat=STAT_DIFF): # Create keyframes for the parent obj (conditions diff) if windows_num == 1: return if stat not in [STAT_DIFF, STAT_AVG]: print("Wrong type of stat!") return parent_obj = bpy.data.objects[PARENT_OBJ] stat_data = calc_stat_data(d.con_values, stat) N = len(indices) now = time.time() for run, (ind, conn_name) in enumerate(zip(indices, d.con_names[mask])): mu.time_to_go(now, run, N, runs_num_to_print=100) # insert_frame_keyframes(parent_obj, conn_name, stat_data[ind, -1], T) for t in range(windows_num): extra_time_points = 0 if norm_fac ==1 else 2 timepoint = t * norm_fac + extra_time_points mu.insert_keyframe_to_custom_prop(parent_obj, conn_name, stat_data[ind, t], timepoint) finalize_fcurves(parent_obj) finalize_objects_creations()
def add_data_to_electrodes_parent_obj(parent_obj, all_data, meta, stat=STAT_DIFF, window_len=None): # todo: merge with add_data_to_brain_parent_obj, same code parent_obj.animation_data_clear() sources = {} # for obj_name, data in zip(f['names'], f['data']): all_data_stat = meta['stat'] if 'stat' in meta else [None] * len(meta['names']) T = all_data.shape[1] if window_len is None or 'dt' not in meta else int(window_len / meta['dt']) for obj_name, data, data_stat in zip(meta['names'], all_data, all_data_stat): obj_name = obj_name.astype(str) if data_stat is None: if stat == STAT_AVG or data.shape[1] == 1: data_stat = np.squeeze(np.mean(data, axis=1)) elif stat == STAT_DIFF: data_stat = np.squeeze(np.diff(data, axis=1)) sources[obj_name] = data_stat sources_names = sorted(list(sources.keys())) N = len(sources_names) # T = DataMakerPanel.addon.get_max_time_steps() # len(sources[sources_names[0]]) + 2 now = time.time() for obj_counter, source_name in enumerate(sources_names): mu.time_to_go(now, obj_counter, N, runs_num_to_print=10) data = sources[source_name] mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0, 1) mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0, T + 2) for ind in range(T): mu.insert_keyframe_to_custom_prop(parent_obj, source_name, data[ind], ind + 2) fcurves = parent_obj.animation_data.action.fcurves[obj_counter] mod = fcurves.modifiers.new(type='LIMITS') print('Finished keyframing {}!!'.format(parent_obj.name))
def add_data_to_electrodes(source_files): print('Adding data to Electrodes') conditions = [] for input_file in source_files: # todo: we don't need to load this twice (also in add_data_to_electrodes_obj f = np.load(input_file) print('{} loaded'.format(input_file)) now = time.time() N = len(f['names']) for obj_counter, (obj_name, data) in enumerate(zip(f['names'], f['data'])): mu.time_to_go(now, obj_counter, N, runs_num_to_print=10) obj_name = obj_name.astype(str) # print(obj_name) if bpy.data.objects.get(obj_name, None) is None: print("{} doesn't exist!".format(obj_name)) continue cur_obj = bpy.data.objects[obj_name] for cond_ind, cond_str in enumerate(f['conditions']): cond_str = cond_str.astype(str) # Set the values to zeros in the first and last frame for current object(current label) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, 1) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, len(f['data'][0]) + 2) print('keyframing ' + obj_name + ' object in condition ' + cond_str) # For every time point insert keyframe to current object for ind, timepoint in enumerate(data[:, cond_ind]): mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + str(cond_str), timepoint, ind + 2) # remove the orange keyframe sign in the fcurves window fcurves = bpy.data.objects[obj_name].animation_data.action.fcurves[cond_ind] mod = fcurves.modifiers.new(type='LIMITS') conditions.extend(f['conditions']) print('Finished keyframing!!') return conditions
def keyframe_empty_test(self, empty_name, closest_mesh_name, vertex_ind, data_path): obj = bpy.data.objects[empty_name] lookup = np.load( op.join(data_path, 'activity_map_' + closest_mesh_name + '_verts_lookup.npy')) file_num_str = str(int(lookup[vertex_ind, 0])) line_num = int(lookup[vertex_ind, 1]) data_file = np.load( op.join(data_path, 'activity_map_' + closest_mesh_name + '_verts', file_num_str + '.npy')) data = data_file[line_num, :].squeeze() number_of_time_points = len(data) mu.insert_keyframe_to_custom_prop(obj, 'data', 0, 0) mu.insert_keyframe_to_custom_prop(obj, 'data', 0, number_of_time_points + 1) for ii in range(number_of_time_points): # print(ii) frame_str = str(ii) mu.insert_keyframe_to_custom_prop(obj, 'data', float(data[ii]), ii + 1) # insert_keyframe_to_custom_prop(obj,'data',0,ii+1) fcurves = bpy.data.objects[empty_name].animation_data.action.fcurves[0] mod = fcurves.modifiers.new(type='LIMITS')
def set_cluster_time_series(cluster): cluster_uid_name = get_cluster_fcurve_name(cluster) _addon().create_empty_if_doesnt_exists(PARENT_OBJ_NAME, _addon().EMPTY_LAYER, bpy.context.scene.layers, 'Functional maps') parent_obj = bpy.data.objects[PARENT_OBJ_NAME] T = len(cluster.label_data) cluster.label_data = np.array(cluster.label_data, dtype=np.float64) fcurves_names = mu.get_fcurves_names(parent_obj) if not cluster_uid_name in fcurves_names: # Set the values to zeros in the first and last frame for current object(current label) mu.insert_keyframe_to_custom_prop(parent_obj, cluster_uid_name, 0, 1) mu.insert_keyframe_to_custom_prop(parent_obj, cluster_uid_name, 0, T + 2) # For every time point insert keyframe to current object for ind, t_data in enumerate(cluster.label_data): mu.insert_keyframe_to_custom_prop(parent_obj, cluster_uid_name, t_data, ind + 2) # remove the orange keyframe sign in the fcurves window fcurve_ind = len(fcurves_names) fcurves = parent_obj.animation_data.action.fcurves[fcurve_ind] mod = fcurves.modifiers.new(type='LIMITS') else: fcurve_ind = fcurves_names.index(cluster_uid_name) fcurve = parent_obj.animation_data.action.fcurves[fcurve_ind] fcurve.keyframe_points[0].co[1] = 0 fcurve.keyframe_points[-1].co[1] = 0 for t in range(T): if fcurve.keyframe_points[t + 1].co[1] != cluster.label_data[t]: fcurve.keyframe_points[t + 1].co[1] = cluster.label_data[t]
def add_data_to_electrodes(all_data, meta_data, window_len=None): print('Adding data to Electrodes') now = time.time() N = len(meta_data['names']) T = all_data.shape[1] if window_len is None or not 'dt' in meta_data else int(window_len / meta_data['dt']) for obj_counter, (obj_name, data) in enumerate(zip(meta_data['names'], all_data)): mu.time_to_go(now, obj_counter, N, runs_num_to_print=10) obj_name = obj_name.astype(str) # print(obj_name) if bpy.data.objects.get(obj_name, None) is None: print("{} doesn't exist!".format(obj_name)) continue cur_obj = bpy.data.objects[obj_name] for cond_ind, cond_str in enumerate(meta_data['conditions']): cond_str = cond_str.astype(str) # Set the values to zeros in the first and last frame for current object(current label) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, 1) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, T + 2) print('keyframing ' + obj_name + ' object in condition ' + cond_str) # For every time point insert keyframe to current object for ind, timepoint in enumerate(data[:T, cond_ind]): mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + str(cond_str), timepoint, ind + 2) # remove the orange keyframe sign in the fcurves window fcurves = bpy.data.objects[obj_name].animation_data.action.fcurves[cond_ind] mod = fcurves.modifiers.new(type='LIMITS') conditions = meta_data['conditions'] print('Finished keyframing!!') return conditions
def add_data_to_parent_obj(parent_obj, source_files, stat, self=None): sources = {} parent_obj.animation_data_clear() for input_file in source_files: if not op.isfile(input_file): print(self, "Can't load file {}!".format(input_file)) continue print('loading {}'.format(input_file)) f = np.load(input_file) for obj_name, data in zip(f['names'], f['data']): obj_name = obj_name.astype(str) # Check if there is only one condition if data.shape[1] == 1: stat = STAT_AVG if bpy.data.objects.get(obj_name) is None: if obj_name.startswith('rh') or obj_name.startswith('lh'): obj_name = obj_name[3:] if bpy.data.objects.get(obj_name) is None: continue if not bpy.context.scene.import_unknown and 'unkown' in obj_name: continue if stat == STAT_AVG: data_stat = np.squeeze(np.mean(data, axis=1)) elif stat == STAT_DIFF: data_stat = np.squeeze(np.diff(data, axis=1)) else: data_stat = data sources[obj_name] = data_stat if len(sources) == 0: print('No sources in {}'.format(source_files)) return sources_names = sorted(list(sources.keys())) N = len(sources_names) T = len(sources[sources_names[0]]) + 2 now = time.time() for obj_counter, source_name in enumerate(sources_names): mu.time_to_go(now, obj_counter, N, runs_num_to_print=10) data = sources[source_name] # Set the values to zeros in the first and last frame for Brain object mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0, 1) mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0, T) # For every time point insert keyframe to the main Brain object # If you want to delete prints make sure no sleep is needed # print('keyframing Brain object {}'.format(obj_name)) for ind in range(data.shape[0]): # if len(data[ind]) == 2: # print('keyframing Brain object') mu.insert_keyframe_to_custom_prop(parent_obj, source_name, data[ind], ind + 2) # print('keyframed') # remove the orange keyframe sign in the fcurves window fcurves = parent_obj.animation_data.action.fcurves[obj_counter] mod = fcurves.modifiers.new(type='LIMITS') if bpy.data.objects.get(' '): bpy.context.scene.objects.active = bpy.data.objects[' '] print('Finished keyframing the brain parent obj!!')
def add_data_to_brain(base_path='', files_prefix='', objs_prefix=''): if base_path == '': base_path = mu.get_user_fol() source_files = [op.join(base_path, '{}labels_data_lh.npz'.format(files_prefix)), op.join(base_path, '{}labels_data_rh.npz'.format(files_prefix)), op.join(base_path, '{}sub_cortical_activity.npz'.format(files_prefix))] print('Adding data to Brain') number_of_maximal_time_steps = -1 obj_counter = 0 conditions = [] for input_file in source_files: if not op.isfile(input_file): mu.message(None, '{} does not exist!'.format(input_file)) continue f = np.load(input_file) print('{} loaded'.format(input_file)) number_of_maximal_time_steps = max(number_of_maximal_time_steps, len(f['data'][0])) for obj_name, data in zip(f['names'], f['data']): # print('in label loop') obj_name = obj_name.astype(str) if not bpy.context.scene.import_unknown and 'unknown' in obj_name: continue obj_name = '{}{}'.format(objs_prefix, obj_name) print(obj_name) cur_obj = bpy.data.objects[obj_name] # print('cur_obj name = '+cur_obj.name) for cond_ind, cond_str in enumerate(f['conditions']): # cond_str = str(cond_str) # if cond_str[1] == "'": # cond_str = cond_str[2:-1] cond_str = cond_str.astype(str) # Set the values to zeros in the first and last frame for current object(current label) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, 1) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, len(f['data'][0]) + 2) print('keyframing ' + obj_name + ' object') # For every time point insert keyframe to current object for ind, timepoint in enumerate(data[:, cond_ind]): # print('keyframing '+obj_name+' object') mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, timepoint, ind + 2) # remove the orange keyframe sign in the fcurves window fcurves = bpy.data.objects[obj_name].animation_data.action.fcurves[cond_ind] mod = fcurves.modifiers.new(type='LIMITS') conditions.extend(f['conditions']) try: bpy.ops.graph.previewrange_set() except: pass bpy.types.Scene.maximal_time_steps = number_of_maximal_time_steps for obj in bpy.data.objects: obj.select = False if bpy.data.objects.get(' '): bpy.context.scene.objects.active = bpy.data.objects[' '] selection_panel.set_conditions_enum(conditions) print('Finished keyframing!!')
def keyframe_empty(self, empty_name, closest_mesh_name, vertex_ind, data_path): obj = bpy.data.objects[empty_name] number_of_time_points = len(glob.glob(op.join(data_path, 'activity_map_' + closest_mesh_name + '2', '', ) + '*.npy')) mu.insert_keyframe_to_custom_prop(obj, 'data', 0, 0) mu.insert_keyframe_to_custom_prop(obj, 'data', 0, number_of_time_points + 1) for ii in range(number_of_time_points): # print(ii) frame_str = str(ii) f = np.load(op.join(data_path, 'activity_map_' + closest_mesh_name + '2', 't' + frame_str + '.npy')) mu.insert_keyframe_to_custom_prop(obj, 'data', float(f[vertex_ind, 0]), ii + 1) fcurves = bpy.data.objects[empty_name].animation_data.action.fcurves[0] mod = fcurves.modifiers.new(type='LIMITS')
def init_electrodes_animation(window_length=2500): parent_obj = bpy.data.objects['Deep_electrodes'] # T = _addon().get_max_time_steps() N = len(parent_obj.children) now = time.time() for obj_counter, source_obj in enumerate(parent_obj.children): mu.time_to_go(now, obj_counter, N, runs_num_to_print=10) source_name = source_obj.name mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0, 1) mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0, window_length + 2) for ind in range(window_length): mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0.1, ind + 2) fcurves = parent_obj.animation_data.action.fcurves[obj_counter] mod = fcurves.modifiers.new(type='LIMITS')
def add_data_to_electrodes_parent_obj(parent_obj, input_file, meta_file=None, stat=STAT_DIFF): # todo: merge with add_data_to_brain_parent_obj, same code parent_obj.animation_data_clear() sources = {} if not op.isfile(input_file): raise Exception("Can't load file {}!".format(input_file)) print('loading {}'.format(input_file)) if meta_file is None: f = np.load(input_file) data = f['data'] else: f = np.load(meta_file) data = np.load(input_file) # for obj_name, data in zip(f['names'], f['data']): all_data_stat = f['stat'] if 'stat' in f else [None] * len(f['names']) for obj_name, data, data_stat in zip(f['names'], data, all_data_stat): obj_name = obj_name.astype(str) if data_stat is None: if stat == STAT_AVG: data_stat = np.squeeze(np.mean(data, axis=1)) elif stat == STAT_DIFF: data_stat = np.squeeze(np.diff(data, axis=1)) sources[obj_name] = data_stat sources_names = sorted(list(sources.keys())) N = len(sources_names) T = DataMakerPanel.addon.get_max_time_steps( ) # len(sources[sources_names[0]]) + 2 now = time.time() for obj_counter, source_name in enumerate(sources_names): mu.time_to_go(now, obj_counter, N, runs_num_to_print=10) data = sources[source_name] mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0, 1) mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0, T + 2) for ind in range(data.shape[0]): mu.insert_keyframe_to_custom_prop(parent_obj, source_name, data[ind], ind + 2) fcurves = parent_obj.animation_data.action.fcurves[obj_counter] mod = fcurves.modifiers.new(type='LIMITS') print('Finished keyframing {}!!'.format(parent_obj.name))
def add_data_to_electrodes(input_file, meta_file=None): print('Adding data to Electrodes') conditions = [] # todo: we don't need to load this twice (also in add_data_to_electrodes_obj if meta_file is None: f = np.load(input_file) data = f['data'] else: data = np.load(input_file) f = np.load(meta_file) print('{} loaded'.format(input_file)) now = time.time() N = len(f['names']) for obj_counter, (obj_name, data) in enumerate(zip(f['names'], data)): mu.time_to_go(now, obj_counter, N, runs_num_to_print=10) obj_name = obj_name.astype(str) # print(obj_name) if bpy.data.objects.get(obj_name, None) is None: print("{} doesn't exist!".format(obj_name)) continue cur_obj = bpy.data.objects[obj_name] for cond_ind, cond_str in enumerate(f['conditions']): cond_str = cond_str.astype(str) # Set the values to zeros in the first and last frame for current object(current label) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, 1) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, len(data[0]) + 2) print('keyframing ' + obj_name + ' object in condition ' + cond_str) # For every time point insert keyframe to current object for ind, timepoint in enumerate(data[:, cond_ind]): mu.insert_keyframe_to_custom_prop( cur_obj, obj_name + '_' + str(cond_str), timepoint, ind + 2) # remove the orange keyframe sign in the fcurves window fcurves = bpy.data.objects[obj_name].animation_data.action.fcurves[ cond_ind] mod = fcurves.modifiers.new(type='LIMITS') conditions = f['conditions'] print('Finished keyframing!!') return conditions
def add_data_to_electrodes_parent_obj(self, parent_obj, source_files, stat): # todo: merge with add_data_to_brain_parent_obj, same code parent_obj.animation_data_clear() sources = {} for input_file in source_files: if not op.isfile(input_file): self.report({'ERROR'}, "Can't load file {}!".format(input_file)) continue print('loading {}'.format(input_file)) f = np.load(input_file) # for obj_name, data in zip(f['names'], f['data']): all_data_stat = f['stat'] if 'stat' in f else [None] * len(f['names']) for obj_name, data, data_stat in zip(f['names'], f['data'], all_data_stat): obj_name = obj_name.astype(str) if data_stat is None: if stat == STAT_AVG: data_stat = np.squeeze(np.mean(data, axis=1)) elif stat == STAT_DIFF: data_stat = np.squeeze(np.diff(data, axis=1)) sources[obj_name] = data_stat sources_names = sorted(list(sources.keys())) N = len(sources_names) T = DataMakerPanel.addon.get_max_time_steps() # len(sources[sources_names[0]]) + 2 now = time.time() for obj_counter, source_name in enumerate(sources_names): mu.time_to_go(now, obj_counter, N, runs_num_to_print=10) data = sources[source_name] mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0, 1) mu.insert_keyframe_to_custom_prop(parent_obj, source_name, 0, T + 2) for ind in range(data.shape[0]): mu.insert_keyframe_to_custom_prop(parent_obj, source_name, data[ind], ind + 2) fcurves = parent_obj.animation_data.action.fcurves[obj_counter] mod = fcurves.modifiers.new(type='LIMITS') print('Finished keyframing {}!!'.format(parent_obj.name))
def insert_frame_keyframes(parent_obj, conn_name, last_data, T): mu.insert_keyframe_to_custom_prop(parent_obj, conn_name, 0, 1) mu.insert_keyframe_to_custom_prop(parent_obj, conn_name, 0, T + 1) # last_data, T + 1) mu.insert_keyframe_to_custom_prop(parent_obj, conn_name, 0, T + 2)
def add_data_to_brain(base_path='', files_prefix='', objs_prefix=''): if base_path == '': base_path = mu.get_user_fol() source_files = [ op.join(base_path, '{}labels_data_lh.npz'.format(files_prefix)), op.join(base_path, '{}labels_data_rh.npz'.format(files_prefix)), op.join(base_path, '{}sub_cortical_activity.npz'.format(files_prefix)) ] print('Adding data to Brain') number_of_maximal_time_steps = -1 obj_counter = 0 conditions = [] for input_file in source_files: if not op.isfile(input_file): print('{} does not exist!'.format(input_file)) continue f = np.load(input_file) print('{} loaded'.format(input_file)) number_of_maximal_time_steps = max(number_of_maximal_time_steps, len(f['data'][0])) for obj_name, data in zip(f['names'], f['data']): # print('in label loop') obj_name = obj_name.astype(str) if not bpy.context.scene.import_unknown and 'unknown' in obj_name: continue # obj_name = '{}{}'.format(objs_prefix, obj_name) # print(obj_name) cur_obj = bpy.data.objects[obj_name] # print('cur_obj name = '+cur_obj.name) print('keyframing {}'.format(obj_name)) for cond_ind, cond_str in enumerate(f['conditions']): # cond_str = str(cond_str) # if cond_str[1] == "'": # cond_str = cond_str[2:-1] cond_str = cond_str.astype(str) # Set the values to zeros in the first and last frame for current object(current label) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, 1) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, len(f['data'][0]) + 2) # For every time point insert keyframe to current object for ind, timepoint in enumerate(data[:, cond_ind]): # print('keyframing '+obj_name+' object') mu.insert_keyframe_to_custom_prop( cur_obj, obj_name + '_' + cond_str, timepoint, ind + 2) # remove the orange keyframe sign in the fcurves window fcurves = bpy.data.objects[ obj_name].animation_data.action.fcurves[cond_ind] mod = fcurves.modifiers.new(type='LIMITS') conditions.extend(f['conditions']) try: bpy.ops.graph.previewrange_set() except: pass bpy.types.Scene.maximal_time_steps = number_of_maximal_time_steps for obj in bpy.data.objects: obj.select = False if bpy.data.objects.get(' '): bpy.context.scene.objects.active = bpy.data.objects[' '] selection_panel.set_conditions_enum(conditions) print('Finished keyframing!!')