def save_to_file(self): from zeobuilder.zml import dump_to_file dirname = os.path.dirname(self.filename) if not os.path.isdir(dirname): os.mkdir(dirname) f = file(self.filename, "w") dump_to_file(f, self.settings) f.close()
def __call__(self, f, universe, folder, nodes=None): if nodes is None: dump_to_file(f, [universe, folder]) else: Universe = context.application.plugins.get_node(name="Universe") new_universe = Universe() new_universe.children = [node for node in nodes if node.is_indirect_child_of(universe)] Folder = context.application.plugins.get_node("Folder") new_folder = Folder(name="Root folder") new_folder.children = [node for node in nodes if node.is_indirect_child_of(folder)] dump_to_file(f, [new_universe, new_folder])
def do(self): cache = context.application.cache originals = cache.nodes parent = cache.parent highest_index = cache.highest_index serialized = StringIO.StringIO() dump_to_file(serialized, originals) serialized.seek(0) duplicates = load_from_file(serialized) for duplicate in duplicates: highest_index += 1 primitive.Add(duplicate, parent, index=highest_index)
def __call__(self, f, universe, folder, nodes=None): if nodes is None: dump_to_file(f, [universe, folder]) else: Universe = context.application.plugins.get_node(name="Universe") new_universe = Universe() new_universe.children = [ node for node in nodes if node.is_indirect_child_of(universe) ] Folder = context.application.plugins.get_node("Folder") new_folder = Folder(name="Root folder") new_folder.children = [ node for node in nodes if node.is_indirect_child_of(folder) ] dump_to_file(f, [new_universe, new_folder])
def copy_to_clipboard(nodes): serialized = StringIO.StringIO() if dump_to_file(serialized, nodes) > 0: def get_func(clipboard, selection_data, info, user_data): selection_data.set("ZML", 8, serialized.getvalue()) def clear_func(clipboard, user_data): serialized.close() clipboard = gtk.clipboard_get() clipboard.set_with_data([("ZML", 0, 0)], get_func, clear_func)
def do(self): # create the repetitions vector repetitions = [] if hasattr(self.parameters, "repetitions_a"): repetitions.append(self.parameters.repetitions_a) else: repetitions.append(1) if hasattr(self.parameters, "repetitions_b"): repetitions.append(self.parameters.repetitions_b) else: repetitions.append(1) if hasattr(self.parameters, "repetitions_c"): repetitions.append(self.parameters.repetitions_c) else: repetitions.append(1) repetitions = numpy.array(repetitions, int) # serialize the positioned children universe = context.application.model.universe positioned = [ node for node in universe.children if ( isinstance(node, GLTransformationMixin) and isinstance(node.transformation, Translation) ) ] if len(positioned) == 0: return serialized = StringIO.StringIO() dump_to_file(serialized, positioned) # create the replica's # replicate the positioned objects new_children = {} for cell_index in iter_all_positions(repetitions): cell_index = numpy.array(cell_index) cell_hash = tuple(cell_index) serialized.seek(0) nodes = load_from_file(serialized) new_children[cell_hash] = nodes for node in nodes: t = node.transformation.t + numpy.dot(universe.cell.matrix, cell_index) new_transformation = node.transformation.copy_with(t=t) node.set_transformation(new_transformation) # forget about serialized stuff serialized.close() del serialized new_connectors = [] # replicate the objects that connect these positioned objects for cell_index in iter_all_positions(repetitions): cell_index = numpy.array(cell_index) cell_hash = tuple(cell_index) for connector in universe.children: # Only applicable to ReferentMixin with only SpatialReference # children if not isinstance(connector, ReferentMixin): continue skip = False for reference in connector.children: if not isinstance(reference, SpatialReference): skip = True break if skip: continue # first locate the new first target for this cell_index first_target_orig = connector.children[0].target first_target_index = positioned.index(first_target_orig) first_target = new_children[cell_hash][first_target_index] assert first_target is not None new_targets = [first_target] for reference in connector.children[1:]: # then find the other new targets, taking into account # periodicity other_target_orig = reference.target shortest_vector = universe.shortest_vector(( other_target_orig.transformation.t -first_target_orig.transformation.t )) translation = first_target.transformation.t + shortest_vector other_target_pos = translation other_cell_index = numpy.floor(universe.cell.to_fractional(other_target_pos)).astype(int) other_cell_index %= repetitions other_cell_hash = tuple(other_cell_index) other_target_index = positioned.index(other_target_orig) other_cell_children = new_children.get(other_cell_hash) assert other_cell_children is not None other_target = other_cell_children[other_target_index] assert other_target is not None new_targets.append(other_target) state = connector.__getstate__() state["targets"] = new_targets new_connectors.append(connector.__class__(**state)) # remove the existing nodes while len(universe.children) > 0: primitive.Delete(universe.children[0]) del positioned # multiply the cell matrix and reset the number of repetitions new_matrix = universe.cell * repetitions primitive.SetProperty(universe, "cell", new_matrix) primitive.SetProperty(universe, "repetitions", numpy.array([1, 1, 1], int)) # add the new nodes for nodes in new_children.itervalues(): for node in nodes: primitive.Add(node, universe) for connector in new_connectors: primitive.Add(connector, universe)
def do(self): # create the repetitions vector repetitions = [] if hasattr(self.parameters, "repetitions_a"): repetitions.append(self.parameters.repetitions_a) else: repetitions.append(1) if hasattr(self.parameters, "repetitions_b"): repetitions.append(self.parameters.repetitions_b) else: repetitions.append(1) if hasattr(self.parameters, "repetitions_c"): repetitions.append(self.parameters.repetitions_c) else: repetitions.append(1) repetitions = numpy.array(repetitions, int) # serialize the positioned children universe = context.application.model.universe positioned = [ node for node in universe.children if (isinstance(node, GLTransformationMixin) and isinstance(node.transformation, Translation)) ] if len(positioned) == 0: return serialized = StringIO.StringIO() dump_to_file(serialized, positioned) # create the replica's # replicate the positioned objects new_children = {} for cell_index in iter_all_positions(repetitions): cell_index = numpy.array(cell_index) cell_hash = tuple(cell_index) serialized.seek(0) nodes = load_from_file(serialized) new_children[cell_hash] = nodes for node in nodes: t = node.transformation.t + numpy.dot(universe.cell.matrix, cell_index) new_transformation = node.transformation.copy_with(t=t) node.set_transformation(new_transformation) # forget about serialized stuff serialized.close() del serialized new_connectors = [] # replicate the objects that connect these positioned objects for cell_index in iter_all_positions(repetitions): cell_index = numpy.array(cell_index) cell_hash = tuple(cell_index) for connector in universe.children: # Only applicable to ReferentMixin with only SpatialReference # children if not isinstance(connector, ReferentMixin): continue skip = False for reference in connector.children: if not isinstance(reference, SpatialReference): skip = True break if skip: continue # first locate the new first target for this cell_index first_target_orig = connector.children[0].target first_target_index = positioned.index(first_target_orig) first_target = new_children[cell_hash][first_target_index] assert first_target is not None new_targets = [first_target] for reference in connector.children[1:]: # then find the other new targets, taking into account # periodicity other_target_orig = reference.target shortest_vector = universe.shortest_vector( (other_target_orig.transformation.t - first_target_orig.transformation.t)) translation = first_target.transformation.t + shortest_vector other_target_pos = translation other_cell_index = numpy.floor( universe.cell.to_fractional(other_target_pos)).astype( int) other_cell_index %= repetitions other_cell_hash = tuple(other_cell_index) other_target_index = positioned.index(other_target_orig) other_cell_children = new_children.get(other_cell_hash) assert other_cell_children is not None other_target = other_cell_children[other_target_index] assert other_target is not None new_targets.append(other_target) state = connector.__getstate__() state["targets"] = new_targets new_connectors.append(connector.__class__(**state)) # remove the existing nodes while len(universe.children) > 0: primitive.Delete(universe.children[0]) del positioned # multiply the cell matrix and reset the number of repetitions new_matrix = universe.cell * repetitions primitive.SetProperty(universe, "cell", new_matrix) primitive.SetProperty(universe, "repetitions", numpy.array([1, 1, 1], int)) # add the new nodes for nodes in new_children.itervalues(): for node in nodes: primitive.Add(node, universe) for connector in new_connectors: primitive.Add(connector, universe)
def save_to_file(self): from zeobuilder.zml import dump_to_file f = file(self.filename, "w") dump_to_file(f, self.settings) f.close()