-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
351 lines (289 loc) · 9.37 KB
/
__init__.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
346
347
348
349
350
351
import os
import bpy
from bpy.props import (StringProperty,
BoolProperty,
IntProperty,
FloatProperty,
PointerProperty,
)
from bpy.types import (Panel,
Menu,
Operator,
PropertyGroup,
)
from .operators import (WM_OT_RunSD,
WM_OT_GenerateTxt,
WM_OT_PreviewCameraPath
)
bl_info = {
"name": "Stable Diffusion textures generator",
"description": "",
"author": "Pawel Kowalski",
"version": (0, 0, 1),
"blender": (2, 80, 0),
"location": "3D View > Tools",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Material"
}
class SDProperties(PropertyGroup):
prompt: StringProperty(
name="Prompt",
description="Description of desired image",
default="An oil painting of a pirate treasure chest, gold, coins, highly detailed, trending on artstation, "
"concept art, Professional, gold coins on ground, wooden box with wooden cover",
maxlen=256,
subtype='NONE'
)
negative_prompt: StringProperty(
name="Negative prompt",
description="Description of how image should not look like",
default="blue topping, dark, shadows, bright spots, glossy, only gold",
maxlen=256,
subtype='NONE'
)
out_dir: StringProperty(
name="Output directory",
description="Directory for output and temporary files.",
default="cwd",
maxlen=256,
subtype='DIR_PATH'
)
out_txt: StringProperty(
name="Output directory",
description="Directory for output and temporary files.",
default="cwd" + os.sep + "txt.png",
maxlen=256,
subtype='DIR_PATH'
)
host: StringProperty(
name="SD server host address",
description="Stable Diffusion server address",
default="127.0.0.1",
maxlen=256,
subtype='NONE'
)
num_inference_steps: IntProperty(
name="Number of interference steps",
description="A number of denoising steps",
default=30,
min=1,
max=100
)
guidance_scale: FloatProperty(
name="Guidance scale",
description="Scale for classifier-free guidance",
default=7.5,
min=1.0,
max=20.0
)
seed: IntProperty(
name="Seed",
description="Random seed",
default=1234567,
min=0
)
views_num: IntProperty(
name="Number of views",
description="Number of views that will be rendered around the object for texture generation",
default=4,
min=0,
max=8
)
camera_r: FloatProperty(
name="Camera radius",
description="Radius of camera path",
default=6.0,
min=1.0,
max=20.0
)
camera_z: FloatProperty(
name="Camera Z position",
description="Z position of camera path",
default=2,
min=1.0,
max=20.0
)
port: IntProperty(
name="Server port",
description="Port of stable diffusion server",
default=5000,
min=0
)
target: PointerProperty(
name="Target object",
description="Object that will be used for the texture generation",
type=bpy.types.Object
)
depth_based_blending: BoolProperty(
name="Depth-based blending",
description="Enable/disable depth-based renders blending for texture",
default=True
)
clear_txt: BoolProperty(
name="Start with empty texture",
description="If enabled, the output texture will be cleared before generation.\n"
"If disabled, the output texture will be used as a starting point for generation\n"
"This might improve control over the result and improve quality of the result.",
default=True
)
resolution_x: IntProperty(
name="Resolution X",
description="Resolution X",
default=768,
min=0
)
resolution_y: IntProperty(
name="Resolution Y",
description="Resolution X",
default=512,
min=0
)
# my_enum: EnumProperty(
# name="Dropdown:",
# description="Apply Data to attribute.",
# items=[('OP1', "Option 1", ""),
# ('OP2', "Option 2", ""),
# ('OP3', "Option 3", ""),
# ]
# )
# ------------------------------------------------------------------------
# Menus
# ------------------------------------------------------------------------
class OBJECT_MT_CustomMenu(Menu):
bl_label = "Select"
bl_idname = "OBJECT_MT_custom_menu"
def draw(self, context):
layout = self.layout
# Built-in operators
layout.operator("object.select_all", text="Select/Deselect All").action = 'TOGGLE'
layout.operator("object.select_all", text="Inverse").action = 'INVERT'
layout.operator("object.select_random", text="Random")
# ------------------------------------------------------------------------
# Panel in Object Mode
# ------------------------------------------------------------------------
class OBJECT_PT_MainSDPanel(Panel):
bl_label = "Stable Diffusion Texture Generator"
bl_idname = "OBJECT_PT_main_sd_panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Tools"
bl_context = "objectmode"
@classmethod
def poll(self, context):
return context.object is not None
def draw(self, context):
layout = self.layout
scene = context.scene
sd_tool = scene.sd_txt_tool
layout.label(text="SD generation settings")
layout.prop(sd_tool, "prompt")
layout.prop(sd_tool, "negative_prompt")
layout.prop(sd_tool, "target")
layout.prop(sd_tool, "out_dir")
layout.prop(sd_tool, "out_txt")
layout.prop(sd_tool, "clear_txt")
class OBJECT_PT_SDPanel(Panel):
bl_label = "Stable Diffusion settings"
bl_idname = "OBJECT_PT_sd_panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Tools"
bl_context = "objectmode"
bl_parent_id = "OBJECT_PT_main_sd_panel"
bl_options = {"DEFAULT_CLOSED"}
@classmethod
def poll(self, context):
return context.object is not None
def draw(self, context):
layout = self.layout
scene = context.scene
sd_tool = scene.sd_txt_tool
layout.prop(sd_tool, "depth_based_blending")
layout.prop(sd_tool, "num_inference_steps")
layout.prop(sd_tool, "guidance_scale")
layout.prop(sd_tool, "seed")
layout.prop(sd_tool, "resolution_x")
layout.prop(sd_tool, "resolution_y")
class OBJECT_PT_ScenePanel(Panel):
bl_label = "Scene settings"
bl_idname = "OBJECT_PT_scene_panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Tools"
bl_context = "objectmode"
bl_parent_id = "OBJECT_PT_main_sd_panel"
bl_options = {"DEFAULT_CLOSED"}
@classmethod
def poll(self, context):
return context.object is not None
def draw(self, context):
layout = self.layout
scene = context.scene
sd_tool = scene.sd_txt_tool
layout.prop(sd_tool, "views_num")
layout.prop(sd_tool, "camera_r")
layout.prop(sd_tool, "camera_z")
# layout.operator("wm.preview_camera")
class OBJECT_PT_ServerPanel(Panel):
bl_label = "SD server settings"
bl_idname = "OBJECT_PT_server_panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Tools"
bl_context = "objectmode"
bl_parent_id = "OBJECT_PT_main_sd_panel"
bl_options = {"DEFAULT_CLOSED"}
@classmethod
def poll(self, context):
return context.object is not None
def draw(self, context):
layout = self.layout
scene = context.scene
sd_tool = scene.sd_txt_tool
layout.prop(sd_tool, "host")
layout.prop(sd_tool, "port")
layout.operator("wm.start_sd_server")
class OBJECT_PT_ActionsPanel(Panel):
bl_label = "Execute"
bl_idname = "OBJECT_PT_actions_panel"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Tools"
bl_context = "objectmode"
bl_parent_id = "OBJECT_PT_main_sd_panel"
@classmethod
def poll(self, context):
return context.object is not None
def draw(self, context):
layout = self.layout
scene = context.scene
sd_tool = scene.sd_txt_tool
layout.operator("wm.generate_txt")
# ------------------------------------------------------------------------
# Registration
# ------------------------------------------------------------------------
classes = (
SDProperties,
WM_OT_RunSD,
WM_OT_GenerateTxt,
WM_OT_PreviewCameraPath,
OBJECT_PT_MainSDPanel,
OBJECT_PT_SDPanel,
OBJECT_PT_ScenePanel,
OBJECT_PT_ServerPanel,
OBJECT_PT_ActionsPanel
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.Scene.sd_txt_tool = PointerProperty(type=SDProperties)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
del bpy.types.Scene.sd_txt_tool
if __name__ == "__main__":
register()