You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""Helper functions to aid in reading/writing chemical data."""importase, ase.data, ase.ioimportbpyclassChemical(ase.Atoms):
def__init__(self, atoms: ase.Atoms, context: bpy.context):
# TODO: Add support for multi-image structuresself.atoms=atomsself.__context=context# Create a new working directory for the moleculeself.collection_name="New Chemical Structure"self.collection=bpy.data.collections.new(self.collection_name)
context.scene.collection.children.link(self.collection)
# ======# Public# ======@classmethoddeffrom_file(cls, filepath: str, context: bpy.context):
""" Constructor for when we've got a filepath specified. Reads from disk. """atoms=ase.io.read(filepath)
# Center the atoms# TODO: Make centering optionalifany(atoms.pbc):
# System is periodic; don't need to centerpasselse:
# System is nonperiodic; we should center itatoms.center(about=0)
returncls(atoms, context)
defadd_structure_to_scene(self) ->None:
""" Adds the stored atoms object into the scene. """# TODO: This can be refactored into a dectorator that steps into a collection and leaves# Save a reference to the previous collection, and change to the new one# This way, any new objects we spawn wind up in the new collection. Keeps stuff neat and tidy.prev_collection=self.__context.view_layer.active_layer_collectionmolecule_layer_collection=self.__context.view_layer.layer_collection.children[-1]
self.__context.view_layer.active_layer_collection=molecule_layer_collectionself.__create_molecule_object()
# And then, finally, return to the collection we started out inself.__context.view_layer.active_layer_collection=prev_collection# =======# Private# =======@propertydef__active_collection(self):
returnself.__context.view_layer.active_layer_collection.collectiondef__create_molecule_object(self) ->None:
""" This will create a molecule object from the atoms object stored in this class. """unique_symbols=set(self.atoms.get_chemical_symbols())
forsymbolinunique_symbols:
# Create the meshselected_atoms=self.atoms[self.atoms.symbols==symbol]
homonuclear_mesh=self.__mesh_from_atoms(selected_atoms)
# Add the mesh to the collectionhomonuclear_positions_name=f"PointCloud_{symbol}_{self.collection_name}"homonuclear_object=bpy.data.objects.new(homonuclear_positions_name, homonuclear_mesh)
homonuclear_object.instance_type="VERTS"self.__active_collection.objects.link(homonuclear_object)
# Create and bind instances for the atomic typenurbs=self.__spawn_nurbs_from_atomic_symbol(symbol)
nurbs.parent=homonuclear_objectdef__mesh_from_atoms(self, atoms: ase.Atoms, mesh_name: str=None) ->bpy.types.Mesh:
""" Creates a point cloud based on the atomic positions passed in. """ifmesh_nameisNone:
mesh_name=f"Mesh_{self.collection_name}"verts=atoms.get_positions() +self.__context.scene.cursor.locationedges= [] # TODO: Derive edges from atomic neighborlistfaces= []
mesh=bpy.data.meshes.new(mesh_name)
mesh.from_pydata(verts, edges, faces)
mesh.validate()
mesh.update()
returnmeshdef__spawn_nurbs_from_atomic_symbol(self, atom_type: str) ->bpy.types.Object:
""" Spawns NURBs spheres to be instanced later on in the atomic coordinates. """# Look up the covalent radiusatomic_number=ase.data.atomic_numbers[atom_type]
covalent_radius=ase.data.covalent_radii[atomic_number]
# Spawn the atom, set its name, and hide it from rendersbpy.ops.surface.primitive_nurbs_surface_sphere_add(radius=covalent_radius,
location=self.__context.scene.cursor.location)
bpy.context.active_object.name=f"instance_{atom_type}"bpy.context.active_object.hide_render=Truebpy.context.active_object.hide_set(True)
# Store a reference to the object we createdcurrent_object=bpy.context.active_objectreturncurrent_object
cbef910fa82f5fc91f781675a249d90fe68dfebd
The text was updated successfully, but these errors were encountered:
Make centering optional
hydridic_blender/utils/ingest.py
Line 35 in f31a5a1
cbef910fa82f5fc91f781675a249d90fe68dfebd
The text was updated successfully, but these errors were encountered: