Skip to content

Commit d183f99

Browse files
committed
add JSON and PARQUET
1 parent 3c9e756 commit d183f99

4 files changed

Lines changed: 48 additions & 13 deletions

File tree

csv_importer/addon.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import bpy
22
from . import ops, props, ui
33
from .props import CSVImporterObjectProperties, CSVExporterSceneProperties
4-
from bpy.props import PointerProperty
4+
from bpy.props import PointerProperty, EnumProperty
55
from .ops import ImportCsvPolarsOperator
66
from .utils import add_current_module_to_path
77

@@ -20,6 +20,18 @@ def register():
2020
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
2121
bpy.types.Object.csv = PointerProperty(type=CSVImporterObjectProperties) # type: ignore
2222
bpy.types.Scene.csv_export = PointerProperty(type=CSVExporterSceneProperties) # type: ignore
23+
24+
# Add export type property to window manager for UI
25+
bpy.types.WindowManager.csv_export_type = EnumProperty( # type: ignore
26+
name="Export Type",
27+
description="Type of file to export",
28+
items=[
29+
('CSV', "CSV", "Export as CSV"),
30+
('JSON', "JSON", "Export as JSON"),
31+
('PARQUET', "Parquet", "Export as Parquet")
32+
],
33+
default='CSV'
34+
)
2335

2436

2537
def unregister():
@@ -28,3 +40,4 @@ def unregister():
2840
bpy.utils.unregister_class(cls)
2941
del bpy.types.Object.csv # type: ignore
3042
del bpy.types.Scene.csv_export # type: ignore
43+
del bpy.types.WindowManager.csv_export_type # type: ignore

csv_importer/exporters.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,7 @@ def from_polars_df_to_csv(df: pl.DataFrame, export_path: str) -> None:
7171
expanded_df = expanded_df.select(column_order)
7272

7373
# Write processed DataFrame to CSV file
74-
expanded_df.write_csv(export_path)
74+
expanded_df.write_csv(export_path)
75+
76+
77+

csv_importer/ops.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,22 @@ def execute(self, context):
134134
self.report({"INFO"}, "Hot reload started")
135135
return {"FINISHED"}
136136

137-
138137
class CSV_OT_ExportData(bpy.types.Operator):
139138
bl_idname = "csv.export_data"
140-
bl_label = "Export CSV"
139+
bl_label = "Export Data"
141140
bl_options = {"REGISTER", "UNDO"}
142-
bl_description = "Export mesh attribute data to CSV file"
141+
bl_description = "Export mesh attribute data to file"
142+
143+
export_type: bpy.props.EnumProperty( # type: ignore
144+
name="Export Type",
145+
description="Type of file to export",
146+
items=[
147+
('CSV', "CSV", "Export as CSV"),
148+
('JSON', "JSON", "Export as JSON"),
149+
('PARQUET', "Parquet", "Export as Parquet")
150+
],
151+
default='CSV'
152+
)
143153

144154
def execute(self, context):
145155
scene = context.scene
@@ -167,14 +177,19 @@ def execute(self, context):
167177
# Convert Blender object to Polars DataFrame
168178
df = from_blender_to_polars_df(export_object)
169179

170-
# Export DataFrame to CSV
171-
from_polars_df_to_csv(df, export_path)
180+
# Export DataFrame based on selected type
181+
if self.export_type == 'CSV':
182+
from_polars_df_to_csv(df, export_path)
183+
elif self.export_type == 'JSON':
184+
df.write_json(export_path)
185+
else: # PARQUET
186+
df.write_parquet(export_path)
172187

173-
self.report({"INFO"}, f"CSV file exported to: {export_path} for object: {export_object.name} ({len(df)} rows, {len(df.columns)} columns)")
188+
self.report({"INFO"}, f"{self.export_type} file exported to: {export_path} for object: {export_object.name} ({len(df)} rows, {len(df.columns)} columns)")
174189
return {"FINISHED"}
175190

176191
except Exception as e:
177-
self.report({"ERROR"}, f"Failed to export CSV file: {e}")
192+
self.report({"ERROR"}, f"Failed to export {self.export_type} file: {e}")
178193
return {"CANCELLED"}
179194

180195

csv_importer/ui.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def draw(self, context):
4040
"csv.toggle_hot_reload", text=message, icon=icon, depress=obj.csv.hot_reload
4141
)
4242

43-
4443
class CSV_PT_ExportPanel(bpy.types.Panel):
4544
bl_label = "Export"
4645
bl_idname = "CSV_PT_ExportPanel"
@@ -62,9 +61,14 @@ def draw(self, context):
6261
row = layout.row()
6362
row.prop(scene.csv_export, "export_path", text="Export Path")
6463

65-
# Export button
66-
row = layout.row()
67-
row.operator("csv.export_data", text="Export CSV", icon="EXPORT")
64+
# Export format buttons
65+
row = layout.row(align=True)
66+
op = row.operator("csv.export_data", text="CSV", icon="FILE")
67+
op.export_type = 'CSV'
68+
op = row.operator("csv.export_data", text="JSON", icon="FILE")
69+
op.export_type = 'JSON'
70+
op = row.operator("csv.export_data", text="Parquet", icon="FILE")
71+
op.export_type = 'PARQUET'
6872

6973

7074
CLASSES = (CSV_PT_ObjectPanel, CSV_PT_ExportPanel)

0 commit comments

Comments
 (0)