-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetaTools3.py
185 lines (150 loc) · 7.06 KB
/
MetaTools3.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# ============================================================
# ┌┬┐┌─┐┌┬┐┌─┐┬─┐┬ ┬
# ┌┬┐┌─┐┌┬┐┌─┐┬─┐┬ ┬
# │││├┤ ││││ │├┬┘└┬┘
# ┴ ┴└─┘┴ ┴└─┘┴└─ ┴
# ┴ ┴└─┘┴ ┴└─┘┴└─ ┴
# ============================================================
# May 11, 2023
#
# This code was generated by GPT-4, an AI model by OpenAI.
# But memory made it happen. If you add to this script please add your name.
#
# metatools -
# a blender addon for adding cross-platform metadata for metaverse building
import bpy
import os
import csv
from bpy.types import Panel, Operator, PropertyGroup
from bpy.props import StringProperty, PointerProperty
# Function to export the collections
def export_collections(folder_name, export_type, merge_collections):
blend_file_path = bpy.data.filepath
if not blend_file_path:
raise Exception("Save the .blend file before running the script.")
folder_path = os.path.dirname(blend_file_path)
export_folder = os.path.join(folder_path, folder_name)
if not os.path.exists(export_folder):
os.makedirs(export_folder)
scene = bpy.context.scene
for collection in scene.collection.children:
if collection.hide_viewport == False:
bpy.ops.object.select_all(action='DESELECT')
for obj in collection.objects:
if obj.visible_get() and obj.hide_viewport == False:
obj.select_set(True)
if merge_collections:
bpy.ops.object.join()
export_file_path = os.path.join(export_folder, collection.name)
if export_type == 'GLB':
bpy.ops.export_scene.gltf(filepath=export_file_path + '.glb', use_selection=True)
elif export_type == 'FBX':
bpy.ops.export_scene.fbx(filepath=export_file_path + '.fbx', use_selection=True, use_custom_props=True)
for obj in collection.objects:
if obj.select_get():
obj.select_set(False)
print(f"Exported visible collections as {export_type} files.")
# Define the settings for exporting collections
class ExportSettings(bpy.types.PropertyGroup):
folder_name: bpy.props.StringProperty(name="Folder Name", default="Exported_Collections")
export_type: bpy.props.EnumProperty(name="Export Type", items=[("GLB", "GLB", "Export as GLB"), ("FBX", "FBX", "Export as FBX")])
merge_collections: bpy.props.BoolProperty(name="Merge Collections", default=False)
# Define the settings for importing CSV
class CSVImporterProperties(PropertyGroup):
csv_path: StringProperty(name="CSV Path", subtype='FILE_PATH')
template_object: PointerProperty(name="Template Object", type=bpy.types.Object)
# Define the panel for exporting collections
class EXPORT_PT_panel(bpy.types.Panel):
bl_label = "Export Collections"
bl_idname = "EXPORT_PT_panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'metatools'
def draw(self, context):
layout = self.layout
settings = context.scene.export_settings
layout.prop(settings, "folder_name", text="Folder Name")
layout.prop(settings, "export_type", text="Export Type")
layout.prop(settings, "merge_collections", text="Merge Collections")
layout.operator("export.collections", text="Export Collections")
# Define the operator for exporting collections
class EXPORT_OT_collections(bpy.types.Operator):
bl_idname = "export.collections"
bl_label = "Export Collections"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
settings = context.scene.export_settings
export_collections(settings.folder_name, settings.export_type, settings.merge_collections)
return {'FINISHED'}
# Define the panel for importing CSV
class CSVImporterPanel(Panel):
bl_idname = "OBJECT_PT_csv_importer"
bl_label = "CSV Importer"
bl_category = "metatools"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
def draw(self, context):
layout = self.layout
csv_importer = context.scene.csv_importer
layout.prop(csv_importer, "csv_path")
layout.prop(csv_importer, "template_object")
layout.operator("object.import_csv")
layout.operator("object.add_metadata")
# Define the operator for importing CSV
class OBJECT_OT_ImportCSV(Operator):
bl_idname = "object.import_csv"
bl_label = "Import CSV"
bl_description = "Import a CSV file and create objects and custom properties"
def execute(self, context):
csv_path = bpy.path.abspath(context.scene.csv_importer.csv_path)
template_object = context.scene.csv_importer.template_object
with open(csv_path, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
# assume the first row contains column names
column_names = next(reader)
# read each row and create an object for each row
for row in reader:
obj = template_object.copy() # create a copy of the template object
obj.data = template_object.data.copy() # copy the mesh data
obj.animation_data_clear() # clear animation data
obj.location = (0, 0, 0) # set the location to (0, 0, 0)
obj.name = row[0] # set the object name to the first column
bpy.context.scene.collection.objects.link(obj) # add the object to the scene collection
# add custom properties to the object for each column
for i, value in enumerate(row):
column_name = column_names[i]
obj[column_name] = value # set the value as a string
return {'FINISHED'}
class OBJECT_OT_AddMetadata(Operator):
bl_idname = "object.add_metadata"
bl_label = "Add Metadata"
bl_description = "Add Metadata custom components to the selected object(s)"
def execute(self, context):
selected_objects = context.selected_objects
for obj in selected_objects:
obj["Author"] = ""
obj["Lore"] = ""
obj["URL"] = ""
obj["Year"] = ""
return {'FINISHED'}
classes = [
CSVImporterProperties,
CSVImporterPanel,
OBJECT_OT_ImportCSV,
OBJECT_OT_AddMetadata,
ExportSettings,
EXPORT_PT_panel,
EXPORT_OT_collections,
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.csv_importer = PointerProperty(type=CSVImporterProperties)
bpy.types.Scene.export_settings = bpy.props.PointerProperty(type=ExportSettings)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
del bpy.types.Scene.csv_importer
del bpy.types.Scene.export_settings
if __name__ == "__main__":
register()