-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext_to_cad.py
345 lines (267 loc) · 12.8 KB
/
text_to_cad.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import base64
import json
import os
import tempfile
import time
from enum import Enum
from pathlib import Path
from urllib.request import Request, urlopen
import bpy
bl_info = {
"name": "Text To CAD",
"description": "Generate an solid object from text, using Zoo.dev",
"author": "Zoo.dev",
"version": (1, 0),
"blender": (3, 0, 0),
"location": "3D Viewport > Object Mode > Add > Text To CAD",
"warning": "", # used for warning icon and text in addons panel
"doc_url": "https://github.com/KittyCAD/text-to-cad-blender-plugin",
"tracker_url": "https://github.com/KittyCAD/text-to-cad-blender-plugin/issues",
"support": "COMMUNITY",
"category": "Add Mesh",
}
def import_fbx(path: Path) -> None:
bpy.ops.import_scene.fbx(filepath=str(path))
def import_glb(path: Path) -> None:
bpy.ops.import_scene.gltf(filepath=str(path))
def import_obj(path: Path) -> None:
bpy.ops.wm.obj_import(filepath=str(path))
def import_ply(path: Path) -> None:
bpy.ops.wm.ply_import(filepath=str(path))
def import_stl(path: Path) -> None:
bpy.ops.import_mesh.stl(filepath=str(path))
class OutputFormat(Enum):
# define an enum class to hold some mappings
# the following format is required for items in an Enum property
# (identifier, name, description, icon, number)
fbx = {"func": import_fbx, "format": ("fbx", "fbx", "", "", 0)}
glb = {"func": import_glb, "format": ("glb", "glb", "", "", 1)}
gltf = {"func": import_glb, "format": ("gltf", "gltf", "", "", 2)}
obj = {"func": import_obj, "format": ("obj", "obj", "", "", 3)}
ply = {"func": import_ply, "format": ("ply", "ply", "", "", 4)}
stl = {"func": import_stl, "format": ("stl", "stl", "", "", 5)}
def import_file(path: Path, output_format: str) -> None:
# generic import function, get the correct function from output mapping
func = OutputFormat[output_format].value["func"]
func(path)
def call_zoo_api(prompt: str, output_format: str, output_dir: Path) -> Path | str:
# define the url for the POST request
post_url = f"https://api.zoo.dev/ai/text-to-cad/{output_format}"
# define the authorization string
auth = f"Bearer {os.environ['KITTYCAD_API_TOKEN']}"
# create the json data string which contains our text prompt
data = json.dumps({"prompt": prompt}).encode("utf-8")
# define headers
# the User-Agent header is necessary to prevent an HTTP 403 error
headers = {
"Authorization": auth,
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0",
}
# create the response
req = Request(post_url, data=data, headers=headers)
with urlopen(req) as response:
# decode the response to a dict
result = json.loads(response.read().decode("utf-8"))
# get the id of the request
op_id = result["id"]
# the requests are made asynchronously so keep checking the operations via the id
# https://zoo.dev/docs/api/api-calls/get-an-async-operation
while result["status"] not in ["completed", "failed"]:
async_url = f"https://api.zoo.dev/async/operations/{op_id}"
headers = {"Authorization": auth, "User-Agent": "Mozilla/5.0"}
async_req = Request(async_url, headers=headers)
with urlopen(async_req) as response:
result = json.loads(response.read().decode("utf-8"))
# using a sleep so that we don't keep pinging the site and get rate limited
time.sleep(10)
if result["status"] == "completed":
# get the base64 encoded string of the output
outputs = result["outputs"][f"source.{output_format}"]
# this seems strange I have to do this. See the official kittycad implementation
# https://github.com/KittyCAD/kittycad.py/blob/main/kittycad/models/base64data.py#L39
decoded = base64.urlsafe_b64decode(outputs.strip("=") + "===")
# save contents to a file on disk at the users location
fp = output_dir / f"{op_id}.{output_format}"
with open(fp, "wb") as out:
out.write(decoded)
return fp
if result["status"] == "failed":
# we've not generated an object for some reason
# return the error string
return result["error"]
def check_for_token() -> bool:
return "KITTYCAD_API_TOKEN" in os.environ
class TextToCAD(bpy.types.Operator):
"""Text To CAD using Zoo.dev"""
bl_idname = "text_to_cad.send"
bl_label = "Text To CAD"
bl_description = (
"Generate a solid object from text. This may take several minutes to run"
)
bl_options = {"REGISTER", "UNDO"}
# tracking number of instances and invocation
instances = 0
invoked = False
# get the home path of the user
home_path = str(Path.home())
def setter_getter(name: str):
return {
"get": lambda self: getattr(bpy.context.scene.selected_dir, name),
"set": lambda self, value: setattr(
bpy.context.scene.selected_dir, name, value
),
}
# create a text property for prompt entry
text: bpy.props.StringProperty(
name="Text Prompt",
default="Create a 2m by 2m plate with 4 holes and rounded corners.",
description="Describe an object that can be represented in geometric shapes.",
**setter_getter("text"),
)
# create a dropdown menu for output selection
output_format: bpy.props.EnumProperty(
items=[i.value["format"] for i in OutputFormat],
name="Output Format",
default=5,
description="Select from the possible output formats to save the file to disk",
)
def get_output_dir(self):
return bpy.context.scene.selected_dir.output_dir
def set_output_dir(self, value):
props = bpy.context.scene.selected_dir
props.output_dir = value
if TextToCAD.instances != 0 or not TextToCAD.invoked:
return
TextToCAD.instances += 1
bpy.ops.text_to_cad.send(
"INVOKE_DEFAULT",
text=props.text,
output_format=props.output_format,
output_dir=props.output_dir,
)
TextToCAD.instances -= 1
TextToCAD.invoked = False
# create a folder selection menu option
output_dir: bpy.props.StringProperty(
name="Output Directory",
default=home_path,
description="Select a directory to save the generated CAD file",
subtype="DIR_PATH",
maxlen=1024,
get=get_output_dir,
set=set_output_dir,
)
def execute(self, _context) -> set:
# check if the user has an API token
if not check_for_token():
msg = """Could not find the environment variable 'KITTYCAD_API_TOKEN',
please proceed to https://zoo.dev/account to setup your account.
If you already have an account, create an API token at https://zoo.dev/account/api-tokens"""
self.report({"ERROR"}, msg)
# exit with a cancelled status
return {"CANCELLED"}
self.report({"INFO"}, "Calling the Text To CAD API, this may take a while...")
# set the output dir. Blender will set relative paths to current .blend file
self.output_dir = os.path.realpath(bpy.path.abspath(self.output_dir))
# call the Zoo API
text_to_cad_output = call_zoo_api(
self.text, self.output_format, Path(self.output_dir)
)
if isinstance(text_to_cad_output, Path):
# if we get a path object back, a file was created. Notify the user and import
self.report({"INFO"}, f"Output file saved to {str(text_to_cad_output)}")
import_file(text_to_cad_output, self.output_format)
else:
# failed generation, report error to user
self.report({"ERROR"}, text_to_cad_output)
TextToCAD.invoked = False
return {"FINISHED"}
def invoke(self, context, _event):
TextToCAD.invoked = True
res = context.window_manager.invoke_props_dialog(self, width=600)
return res
class TextToCADProps(bpy.types.PropertyGroup):
# the implementation for the folder browser is using methods described here:
# https://blender.stackexchange.com/a/295298
home_path = str(Path.home())
text: bpy.props.StringProperty(
name="Text Prompt",
default="Create a 2m by 2m plate with 4 holes and rounded corners.",
description="Describe an object that can be represented in geometric shapes.",
)
# create a dropdown menu for output selection
output_format: bpy.props.EnumProperty(
items=[i.value["format"] for i in OutputFormat],
name="Output Format",
default=5,
)
# create a folder selection menu
output_dir: bpy.props.StringProperty(
name="Output Directory",
default=home_path,
description="Select a directory to save the generated CAD file",
subtype="DIR_PATH",
maxlen=1024,
)
def create_icon() -> str:
im_str = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x06\x00\x00\x00\x1f\xf3\xffa\x00\x00\x01\xa5IDATx\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8c"\x82i\x9a\xbb\x18\xf5K\xf12\xb4\xaf\xfdF`\x07\xfa\xe3#\xfd\xad\xfe=\xe0\'\xb2\x8a\xb2\xa5\'B\xe7\xea\x00\x00\x00\x00IEND\xaeB`\x82'
with tempfile.NamedTemporaryFile(
mode="w+b", prefix="zoo-icon-", suffix=".png", delete=False
) as fp:
fp.write(im_str)
return fp.name
def menu_func(self, _context):
# add a separator to the menu before adding our addon
pcoll = preview_collections["main"]
my_icon = pcoll["my_icon"]
self.layout.separator()
self.layout.operator(TextToCAD.bl_idname, icon_value=my_icon.icon_id)
# store keymaps here to access after registration
addon_keymaps = []
# We can store multiple preview collections here,
# however in this example we only store "main"
preview_collections = {}
def register():
# Note that preview collections returned by bpy.utils.previews
# are regular py objects - you can use them to store custom data.
import bpy.utils.previews
pcoll = bpy.utils.previews.new()
icon = create_icon()
# load a preview thumbnail of a file and store in the previews collection
pcoll.load("my_icon", icon, "IMAGE")
preview_collections["main"] = pcoll
# register classes and add to menu
bpy.utils.register_class(TextToCAD)
bpy.utils.register_class(TextToCADProps)
bpy.types.VIEW3D_MT_add.append(menu_func)
bpy.types.Scene.selected_dir = bpy.props.PointerProperty(type=TextToCADProps)
# handle the keymap
wm = bpy.context.window_manager
# Note that in background mode (no GUI available), keyconfigs are not available either,
# so we have to check this to avoid nasty errors in background case.
kc = wm.keyconfigs.addon
if kc:
km = wm.keyconfigs.addon.keymaps.new(name="Object Mode", space_type="EMPTY")
kmi = km.keymap_items.new(
TextToCAD.bl_idname, "T", "PRESS", ctrl=True, shift=True
)
addon_keymaps.append((km, kmi))
def unregister():
for pcoll in preview_collections.values():
bpy.utils.previews.remove(pcoll)
preview_collections.clear()
# Note: when unregistering, it's usually good practice to do it in reverse order you registered.
# Can avoid strange issues like keymap still referring to operators already unregistered...
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
# unregister and remove from menus
bpy.utils.unregister_class(TextToCAD)
bpy.utils.unregister_class(TextToCADProps)
bpy.types.VIEW3D_MT_add.remove(menu_func)
del bpy.types.Scene.selected_dir
# This allows you to run the script directly from Blender's Text editor
# to test the add-on without having to install it.
if __name__ == "__main__":
register()