-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
285 lines (219 loc) · 8.7 KB
/
main.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
import sys
import os
import bpy
import bpy.props
import re
import json
# Add the 'libs' folder to the Python path
libs_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "lib")
if libs_path not in sys.path:
sys.path.append(libs_path)
import openai
from utilities import *
bl_info = {
"name": "Blender Copilot",
"blender": (2, 82, 0),
"category": "Object",
"author": "Pramish Paudel",
"version": (1, 0, 1),
"location": "3D View > UI > Copilot",
"description": "Automate Blender using OpenAI's GPT to perform various tasks.",
"warning": "",
"wiki_url": "",
"tracker_url": "",
}
system_prompt = """You are an assistant made for the purposes of helping the user with Blender, the 3D software.
- Respond with your answers in markdown (```) as shown in example.
- Preferably import entire modules instead of bits.
- Do not perform destructive operations on the meshes.
- Do not use cap_ends. Do not do more than what is asked (setting up render settings, adding cameras, etc)
- Do not respond with anything that is not Python code.
- Use alpha channel for color ex: (1,0,0,1).
- Check if the material exits before applying color. If not material, create new.
- If asked to animate, use keyframe animation for animation.
Example:
user: create 10 cubes in random locations from -10 to 10
assistant:
```
import bpy
import random
# Create a new material with a random color
def create_random_material():
mat = bpy.data.materials.new(name="RandomColor")
mat.diffuse_color = (random.uniform(0,1), random.uniform(0,1), random.uniform(0,1), 1) # alpha channel is required
return mat
bpy.ops.mesh.primitive_cube_add()
#how many cubes you want to add
count = 10
for c in range(0,count):
x = random.randint(-10,10)
y = random.randint(-10,10)
z = random.randint(-10,10)
bpy.ops.mesh.primitive_cube_add(location=(x,y,z))
cube = bpy.context.active_object
# Assign a random material to the cube
cube.data.materials.append(create_random_material())
```"""
class GPT4_OT_DeleteMessage(bpy.types.Operator):
bl_idname = "gpt4.delete_message"
bl_label = "Delete Message"
bl_options = {'REGISTER', 'UNDO'}
message_index: bpy.props.IntProperty()
def execute(self, context):
context.scene.gpt4_chat_history.remove(self.message_index)
return {'FINISHED'}
class GPT4_OT_ShowCode(bpy.types.Operator):
bl_idname = "gpt4.show_code"
bl_label = "Show Code"
bl_options = {'REGISTER', 'UNDO'}
code: bpy.props.StringProperty(
name="Code",
description="The generated code",
default="",
)
def execute(self, context):
text_name = "GPT4_Generated_Code.py"
text = bpy.data.texts.get(text_name)
if text is None:
text = bpy.data.texts.new(text_name)
text.clear()
text.write(self.code)
text_editor_area = None
for area in context.screen.areas:
if area.type == 'TEXT_EDITOR':
text_editor_area = area
break
if text_editor_area is None:
text_editor_area = split_area_to_text_editor(context)
text_editor_area.spaces.active.text = text
return {'FINISHED'}
class GPT4_PT_Panel(bpy.types.Panel):
bl_label = "Copilot"
bl_idname = "blender_copilot_PT_Panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'GPT Copilot'
def draw(self, context):
layout = self.layout
column = layout.column(align=True)
column.label(text="Chat history:")
box = column.box()
for index, message in enumerate(context.scene.gpt4_chat_history):
if message.type == 'assistant':
row = box.row()
row.label(text="Assistant: ")
show_code_op = row.operator("gpt4.show_code", text="Show Code")
show_code_op.code = message.content
delete_message_op = row.operator("gpt4.delete_message", text="", icon="TRASH", emboss=False)
delete_message_op.message_index = index
else:
row = box.row()
row.label(text=f"User: {message.content}")
delete_message_op = row.operator("gpt4.delete_message", text="", icon="TRASH", emboss=False)
delete_message_op.message_index = index
column.separator()
column.label(text="GPT Model:")
column.prop(context.scene, "gpt4_model", text="")
column.label(text="Enter your message:")
column.prop(context.scene, "gpt4_chat_input", text="")
button_label = "Please wait...(this might take some time)" if context.scene.gpt4_button_pressed else "Execute"
row = column.row(align=True)
row.operator("gpt4.send_message", text=button_label)
row.operator("gpt4.clear_chat", text="Clear Chat")
column.separator()
class GPT4_OT_ClearChat(bpy.types.Operator):
bl_idname = "gpt4.clear_chat"
bl_label = "Clear Chat"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
context.scene.gpt4_chat_history.clear()
return {'FINISHED'}
class GPT4_OT_Execute(bpy.types.Operator):
bl_idname = "gpt4.send_message"
bl_label = "Send Message"
bl_options = {'REGISTER', 'UNDO'}
natural_language_input: bpy.props.StringProperty(
name="Command",
description="Enter the natural language command",
default="",
)
def execute(self, context):
global system_prompt
openai.api_key = get_api_key(context, __name__)
# if null then set to env key
if not openai.api_key:
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
self.report({'ERROR'}, "No API key detected. Please set the API key in the addon preferences.")
return {'CANCELLED'}
context.scene.gpt4_button_pressed = True
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
## add context to system prompt
# Get the minimal scene data
scene_data = {
"objects": []
}
for obj in bpy.context.scene.objects:
scene_data["objects"].append({
"name": obj.name,
"type": obj.type,
# "location": list(obj.location),
# "rotation_euler": list(obj.rotation_euler),
# "scale": list(obj.scale),
})
if len(scene_data["objects"]) == 0: scene_data = None
# if scene_data:
# system_prompt = system_prompt + """Below is the minimal scene context.\n""" + json.dumps(scene_data)
blender_code = generate_blender_code(context.scene.gpt4_chat_input, context.scene.gpt4_chat_history, context,
system_prompt)
message = context.scene.gpt4_chat_history.add()
message.type = 'user'
message.content = context.scene.gpt4_chat_input
# Clear the chat input field
context.scene.gpt4_chat_input = ""
if blender_code:
message = context.scene.gpt4_chat_history.add()
message.type = 'assistant'
message.content = blender_code
global_namespace = globals().copy()
try:
exec(blender_code, global_namespace)
except Exception as e:
self.report({'ERROR'}, f"Error executing generated code: {e}")
context.scene.gpt4_button_pressed = False
return {'CANCELLED'}
context.scene.gpt4_button_pressed = False
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(GPT4_OT_Execute.bl_idname)
class GPT4AddonPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
api_key: bpy.props.StringProperty(
name="API Key",
description="Enter your OpenAI API Key",
default="",
subtype="PASSWORD",
)
def draw(self, context):
layout = self.layout
layout.prop(self, "api_key")
def register():
bpy.utils.register_class(GPT4AddonPreferences)
bpy.utils.register_class(GPT4_OT_Execute)
bpy.utils.register_class(GPT4_PT_Panel)
bpy.utils.register_class(GPT4_OT_ClearChat)
bpy.utils.register_class(GPT4_OT_ShowCode)
bpy.utils.register_class(GPT4_OT_DeleteMessage)
bpy.types.VIEW3D_MT_mesh_add.append(menu_func)
init_props()
def unregister():
bpy.utils.unregister_class(GPT4AddonPreferences)
bpy.utils.unregister_class(GPT4_OT_Execute)
bpy.utils.unregister_class(GPT4_PT_Panel)
bpy.utils.unregister_class(GPT4_OT_ClearChat)
bpy.utils.register_class(GPT4_OT_ShowCode)
bpy.utils.register_class(GPT4_OT_DeleteMessage)
bpy.types.VIEW3D_MT_mesh_add.remove(menu_func)
clear_props()
if __name__ == "__main__":
register()