-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdateDB.py
More file actions
218 lines (179 loc) · 7.72 KB
/
updateDB.py
File metadata and controls
218 lines (179 loc) · 7.72 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
bl_info = {
"name": "DB Map Updater",
"author": "AJ Frio",
"version": (1, 0),
"blender": (4, 1, 0),
"location": "World",
"description": "Update the DB Map for the selected object",
"category": "Object",
}
import bpy
import sqlite3
import os
simpleMats = {"plastic": 19, "silverHardware": 6, "blackHardware": 13, "chrome": 15, "permBlack": 13, "permMat": 17, "pulleys": 6, "rubber": 17, "cloth": 10, "UC": "UC"}
complexMats = {"plastic": 11, "silverHardware": 6, "blackHardware": 9, "chrome": 3, "permBlack": 9, "permMat": 10, "pulleys": 5, "rubber": 11, "cloth": 10, "UC": "UC"}
# Add this enum items list for the dropdown
color_items = [
('UC', "Color", ""),
('silverHardware', "Silver Hardware", ""),
('chrome', "Chrome", ""),
('blackHardware', "Black Hardware", ""),
('permMat', "Matte Black", ""),
('permBlack', "Black Powdercoat", ""),
('rubber', "Rubber", ""),
('plastic', "Plastic", ""),
('cloth', "Cloth", ""),
]
def get_selected_object_name():
# Get the active (selected) object
selected_obj = bpy.context.active_object
# Check if there is an active object
if selected_obj is not None:
return str(selected_obj.name).split(' ')[0]
else:
return None
def find_path():
end = 'OneDrive - Rep Fitness/Documents - Product and Engineering/Internal Docs/ID Team/paint.db'
start = os.path.dirname(os.path.realpath(__file__))
start = start.split('\\')
first = '\\'.join(start[:3])
path = first + '\\' + end
print(path)
return path
class ColorSelectorPanel(bpy.types.Panel):
bl_label = "DB Map Color Selector"
bl_idname = "WORLD_PT_db_map_updater"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "world"
def draw(self, context):
layout = self.layout
scene = context.scene
# Create dropdown for color selection
layout.prop(scene, "color_dropdown")
# Create Add button
layout.operator("object.add_to_db", text="Add")
class SelectColorOperator(bpy.types.Operator):
bl_idname = "object.select_color"
bl_label = "Select Color"
def execute(self, context):
context.scene.selected_color = "UC"
return {'FINISHED'}
class SelectSilverHardwareOperator(bpy.types.Operator):
bl_idname = "object.select_silver_hardware"
bl_label = "Select Silver Hardware"
def execute(self, context):
context.scene.selected_color = "silverHardware"
return {'FINISHED'}
class SelectChromeOperator(bpy.types.Operator):
bl_idname = "object.select_chrome"
bl_label = "Select Chrome"
def execute(self, context):
context.scene.selected_color = "chrome"
return {'FINISHED'}
class SelectBlackHardwareOperator(bpy.types.Operator):
bl_idname = "object.select_black_hardware"
bl_label = "Select Black Hardware"
def execute(self, context):
context.scene.selected_color = "blackHardware"
return {'FINISHED'}
class SelectMatteBlackOperator(bpy.types.Operator):
bl_idname = "object.select_matte_black"
bl_label = "Select Matte Black"
def execute(self, context):
context.scene.selected_color = "permMat"
return {'FINISHED'}
class SelectBlackPowdercoatOperator(bpy.types.Operator):
bl_idname = "object.select_black_powdercoat"
bl_label = "Select Black Powdercoat"
def execute(self, context):
context.scene.selected_color = "permBlack"
return {'FINISHED'}
class SelectRubberOperator(bpy.types.Operator):
bl_idname = "object.select_rubber"
bl_label = "Select Rubber"
def execute(self, context):
context.scene.selected_color = "rubber"
return {'FINISHED'}
class SelectPlasticOperator(bpy.types.Operator):
bl_idname = "object.select_plastic"
bl_label = "Select Plastic"
def execute(self, context):
context.scene.selected_color = "plastic"
return {'FINISHED'}
class SelectClothOperator(bpy.types.Operator):
bl_idname = "object.select_cloth"
bl_label = "Select Cloth"
def execute(self, context):
context.scene.selected_color = "cloth"
return {'FINISHED'}
class AddToDBOperator(bpy.types.Operator):
bl_idname = "object.add_to_db"
bl_label = "Add to DB"
def execute(self, context):
obj_name = get_selected_object_name()
if obj_name:
# Use the dropdown value instead of selected_color
color = context.scene.color_dropdown
print(f"Selected object: {obj_name}, Color: {color}")
db_path = find_path()
try:
# Add timeout parameter and use with statement for proper connection handling
conn = sqlite3.connect(db_path, timeout=10)
with conn: # This ensures proper closing even if an exception occurs
cur = conn.cursor()
# Check if the object already exists in the database
cur.execute("SELECT * FROM obj_color_map WHERE name = ?", (obj_name,))
existing_record = cur.fetchone()
if existing_record:
# Update existing record
cur.execute("UPDATE obj_color_map SET mapped_index_lowq = ?, mapped_index_highq = ? WHERE name = ?",
(simpleMats[color], complexMats[color], obj_name))
self.report({'INFO'}, f"Updated {obj_name} in DB")
else:
# Insert new record
cur.execute("INSERT INTO obj_color_map (name, mapped_index_lowq, mapped_index_highq) VALUES (?, ?, ?)",
(obj_name, simpleMats[color], complexMats[color]))
self.report({'INFO'}, "Object added to DB")
# Connection is automatically closed when exiting the with block
except sqlite3.Error as e:
self.report({'ERROR'}, f"Database error: {str(e)}")
return {'CANCELLED'}
else:
self.report({'ERROR'}, "No object selected!")
return {'FINISHED'}
def register():
# Replace the string property with an enum property
bpy.types.Scene.color_dropdown = bpy.props.EnumProperty(
name="Select Color",
description="Choose a color or material type",
items=color_items,
default='UC'
)
bpy.utils.register_class(ColorSelectorPanel)
bpy.utils.register_class(SelectColorOperator)
bpy.utils.register_class(SelectSilverHardwareOperator)
bpy.utils.register_class(SelectChromeOperator)
bpy.utils.register_class(SelectBlackHardwareOperator)
bpy.utils.register_class(SelectMatteBlackOperator)
bpy.utils.register_class(SelectBlackPowdercoatOperator)
bpy.utils.register_class(SelectRubberOperator)
bpy.utils.register_class(SelectPlasticOperator)
bpy.utils.register_class(SelectClothOperator)
bpy.utils.register_class(AddToDBOperator)
def unregister():
# Update this line to remove the enum property
del bpy.types.Scene.color_dropdown
bpy.utils.unregister_class(ColorSelectorPanel)
bpy.utils.unregister_class(SelectColorOperator)
bpy.utils.unregister_class(SelectSilverHardwareOperator)
bpy.utils.unregister_class(SelectChromeOperator)
bpy.utils.unregister_class(SelectBlackHardwareOperator)
bpy.utils.unregister_class(SelectMatteBlackOperator)
bpy.utils.unregister_class(SelectBlackPowdercoatOperator)
bpy.utils.unregister_class(SelectRubberOperator)
bpy.utils.unregister_class(SelectPlasticOperator)
bpy.utils.unregister_class(SelectClothOperator)
bpy.utils.unregister_class(AddToDBOperator)
if __name__ == "__main__":
register()