-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
116 lines (88 loc) · 3.11 KB
/
__init__.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
bl_info = {
"name": "Add Airfoil",
"author": "Ian Huish",
"version": (2, 0, 0),
"blender": (4, 2, 0),
"location": "File > Import > Add Airfoil",
"description": "Read Dat file and create airfoil mesh",
"warning": "",
"wiki_url": "https://github.com/nerk987/add_airfoil",
"tracker_url": "http://github.com/nerk987/add_airfoil/issues",
"category": "Add Mesh",
}
# Revised to name the mesh with the text from the first line
import bpy
import bmesh
from bpy_extras.object_utils import AddObjectHelper
from bpy_extras.io_utils import ImportHelper
from bpy.props import (
FloatProperty,
)
def add_airfoil(filename):
"""
This function takes inputs and returns vertex and face arrays.
no actual mesh data creation is done here.
"""
verts = []
AirfoilName = ""
datFile = open(filename, 'r')
FirstLine = True
for line in datFile:
if FirstLine:
AirfoilName = line[:-1]
FirstLine = False
line = line.replace(","," ")
line = line.replace(";"," ")
print(line)
try:
verts.append([0.0, float(line.split()[0]), float(line.split()[1])])
except Exception:
pass
# print("Verts: ", verts)
datFile.close()
return verts, AirfoilName
class AddAirfoil(bpy.types.Operator, AddObjectHelper, ImportHelper):
"""Add an airfoil"""
bl_idname = "mesh.airfoil_add"
bl_label = "Add Airfoil"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
# ImportHelper mixin class uses this
filename_ext = ".txt"
filter_glob: StringProperty(
default="*.txt",
options={'HIDDEN'},
maxlen=255, # Max internal buffer length, longer would be clamped.
)
verts_loc, AirfoilName = add_airfoil(self.filepath)
if AirfoilName == "" or not AirfoilName.isalpha:
AirfoilName = "Airfoil"
mesh = bpy.data.meshes.new(AirfoilName)
bm = bmesh.new()
for v_co in verts_loc:
bm.verts.new(v_co)
bm.verts.ensure_lookup_table()
for i in range(len(verts_loc)-1):
bm.edges.new([bm.verts[i], bm.verts[i+1]])
bm.edges.new([bm.verts[0], bm.verts[-1]])
bm.to_mesh(mesh)
mesh.update()
# add the mesh as an object into the scene with this utility module
from bpy_extras import object_utils
object_utils.object_data_add(context, mesh, operator=self)
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(AddAirfoil.bl_idname, icon='MESH_CUBE')
# Register and add to the "add mesh" menu (required to use F3 search "Add Box" for quick access)
def register():
bpy.utils.register_class(AddAirfoil)
bpy.types.VIEW3D_MT_mesh_add.append(menu_func)
bpy.types.TOPBAR_MT_file_import.append(menu_func)
def unregister():
bpy.utils.unregister_class(AddAirfoil)
bpy.types.VIEW3D_MT_mesh_add.remove(menu_func)
bpy.types.TOPBAR_MT_file_import.remove(menu_func)
if __name__ == "__main__":
register()
# test call
bpy.ops.mesh.airfoil_add()