Added XYZ animation frames #1

Open
John-Ferrier wants to merge 1 commits from John-Ferrier/io_mesh_atomic:main into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
3 changed files with 147 additions and 115 deletions

View File

@ -8,12 +8,12 @@
# Start of project : 2011-08-31 by CB # Start of project : 2011-08-31 by CB
# First publication in Blender : 2011-11-11 by CB # First publication in Blender : 2011-11-11 by CB
# Fusion of the PDB, XYZ and Panel : 2019-03-22 by CB # Fusion of the PDB, XYZ and Panel : 2019-03-22 by CB
# Last modified : 2024-05-28 by CB # Last modified : 2024-07-27 by JF
# #
# Contributing authors # Contributing authors
# ==================== # ====================
# John Ferrier (ferrier.j@northeastern.edu)
# #
# So far ... none ... .
# #
# #
# Acknowledgements # Acknowledgements

View File

@ -27,7 +27,6 @@ from .xyz_export import export_xyz
class IMPORT_OT_xyz(Operator, ImportHelper): class IMPORT_OT_xyz(Operator, ImportHelper):
bl_idname = "import_mesh.xyz" bl_idname = "import_mesh.xyz"
bl_label = "Import XYZ (*.xyz)" bl_label = "Import XYZ (*.xyz)"
bl_description = "Import a XYZ atomic structure"
bl_options = {'PRESET', 'UNDO'} bl_options = {'PRESET', 'UNDO'}
filename_ext = ".xyz" filename_ext = ".xyz"
@ -162,7 +161,7 @@ class IMPORT_OT_xyz(Operator, ImportHelper):
self.use_center_all, self.use_center_all,
self.use_camera, self.use_camera,
self.use_lamp, self.use_lamp,
filepath_xyz) filepath_xyz, self.use_frames) # Added self.use_frames for animation purposes in xyz_import.py [ read_xyz_file() ]
# Load frames # Load frames
if len(ALL_FRAMES) > 1 and self.use_frames: if len(ALL_FRAMES) > 1 and self.use_frames:

View File

@ -4,6 +4,7 @@
import os import os
import bpy import bpy
import re
from math import pi, sqrt from math import pi, sqrt
from mathutils import Vector, Matrix from mathutils import Vector, Matrix
@ -192,17 +193,49 @@ def read_elements():
ELEMENTS.append(li) ELEMENTS.append(li)
def extract_number(file_name):
match = re.findall(r'\d+', file_name)
return int(''.join(match)) if match else 0
# filepath_pdb: path to pdb file # filepath_pdb: path to pdb file
# radiustype : '0' default # radiustype : '0' default
# '1' atomic radii # '1' atomic radii
# '2' van der Waals # '2' van der Waals
def read_xyz_file(filepath_xyz,radiustype): def read_xyz_file(filepath_xyz,radiustype, use_frames):
number_frames = 0 number_frames = 0
total_number_atoms = 0 total_number_atoms = 0
#### Added .xyz sequence for animations! Original author has animation functions built in but
# never allowed for ALL_FRAMES to be built out. Just added a functionality for ALL_FRAMES.
# Files need to be in the format of my_cool_file_1.xyz, my_cool_file_2.xyz, ..., my_cool_file_x.xyz
# No other numbers, as extract_number() puts all numbers together for sorting. Guess it could work if
# the pre-emptive numbers are all the same... This was mostly just a quick fix for my use-case though.
# - JF
# Get the file directory
file_dir = os.path.dirname( filepath_xyz )
files = []
# If the use_frames option is selected on import, create a full list of the .xyz files in the dir
if use_frames:
for file in os.listdir( file_dir ):
if file.endswith('.xyz'):
files.append( file )
# Sort the list of .xyz files
files = sorted( files, key = extract_number )
else:
# If not, just append the single file
files.append( filepath_xyz )
# Cycle through each sorted file
for xyzf in files:
# Open the file ... # Open the file ...
filepath_xyz_p = open(filepath_xyz, "r") filepath_xyz_p = open( os.path.join( file_dir, xyzf ), "r")
#Go through the whole file. #Go through the whole file.
FLAG = False FLAG = False
@ -441,7 +474,7 @@ def import_xyz(Ball_type,
put_to_center_all, put_to_center_all,
use_camera, use_camera,
use_light, use_light,
filepath_xyz): filepath_xyz, use_frames):
# List of materials # List of materials
atom_material_list = [] atom_material_list = []
@ -454,7 +487,7 @@ def import_xyz(Ball_type,
# ------------------------------------------------------------------------ # ------------------------------------------------------------------------
# READING DATA OF ATOMS # READING DATA OF ATOMS
Number_of_total_atoms = read_xyz_file(filepath_xyz, radiustype) Number_of_total_atoms = read_xyz_file(filepath_xyz, radiustype, use_frames)
# We show the atoms of the first frame. # We show the atoms of the first frame.
first_frame = ALL_FRAMES[0] first_frame = ALL_FRAMES[0]
@ -540,7 +573,7 @@ def import_xyz(Ball_type,
sum_vec = Vector((0.0,0.0,0.0)) sum_vec = Vector((0.0,0.0,0.0))
# Sum of all atom coordinates # Sum of all atom coordinates
for (i, atoms_of_one_type) in enumerate(frame): for i, atoms_of_one_type in enumerate(frame):
# This is a guarantee that only the total number of atoms of the # This is a guarantee that only the total number of atoms of the
# first frame is used. Condition is, so far, that the number of # first frame is used. Condition is, so far, that the number of