-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_centroid.py
52 lines (37 loc) · 1.41 KB
/
obj_centroid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import argparse
import numpy as np
def obj_centroid(obj_file):
"""
Calculates the centroid of a mesh from an OBJ file and returns the X,Y,Z coordinates.
Parameters:
obj_file (str): Path to the OBJ file.
Returns:
centroid (tuple): A tuple containing the X,Y,Z coordinates of the centroid.
"""
# Open the OBJ file and read the vertex data
with open(obj_file, 'r') as f:
vertices = []
for line in f:
if line.startswith('v '):
vertex = list(map(float, line.split()[1:]))
vertices.append(vertex)
# Convert the vertices list to a numpy array for easier calculations
vertices = np.array(vertices)
# Calculate the centroid using numpy
centroid = np.mean(vertices, axis=0)
# Return the centroid as a tuple of floats
return tuple(map(float, centroid))
def main():
"""
Entry point for the script. Parses command line arguments and calls obj_centroid.
"""
# Set up argument parser
parser = argparse.ArgumentParser(description='Calculate the centroid of a mesh from an OBJ file.')
parser.add_argument('obj_file', help='Path to the OBJ file.')
# Parse arguments
args = parser.parse_args()
# Calculate the centroid and print the result
centroid = obj_centroid(args.obj_file)
print('Centroid: ({:.4f}, {:.4f}, {:.4f})'.format(*centroid))
if __name__ == '__main__':
main()