-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
618 lines (481 loc) · 19.1 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
bl_info = {
"name": "Material Crafter",
"author": "Renato Sortino, Giuseppe Vecchio",
"version": (1, 0, 0),
"blender": (4, 1, 0),
"location": "View3D > Sidebar > Material Crafter",
"description": "Add on for generating texture maps using diffusion models.",
"support": "COMMUNITY",
"warning": "Requires installation of dependencies and works best with a GPU",
"category": "Development",
}
MC_version = bl_info["version"]
LAST_UPDATED = "Jun 13rd 2024"
global installing
installing = False
#TODO Refactor
def progress_bar(self, context):
row = self.layout.row()
row.progress(
factor=context.window_manager.progress,
type="BAR",
text=context.window_manager.progress_text #if context.window_manager.progress < 1 else "Installation Finished !"
)
row.scale_x = 2
def set_dependencies_installed(are_installed):
global dependencies_installed
dependencies_installed = are_installed
# Blender modules:
import bpy
# Python modules:
from pathlib import Path
import sys
import importlib
import subprocess
# Local modules:
sys.path.append(Path(__file__).parent)
from .src import helpers
from .src.textures import load_texture_maps
# Refresh Locals for development:
if "bpy" in locals():
modules = {
"helpers": helpers,
}
for i in modules:
if i in locals():
importlib.reload(modules[i])
pm = helpers.PathManager()
# ======== Pre Dependency =================== #
class MC_PGT_Input_Properties_Pre(bpy.types.PropertyGroup):
# Install Dependencies panel:
mc_path: bpy.props.StringProperty(
name="Material Crafter Path",
description="The save path for the virtual environments. If you have already "
"installed Material Crafter, or you are using a different version of Blender, you can use your"
" old Environment Path. Regardless of the method, always initiate your Environment.",
default=f"{pm.named_paths['material_crafter'].parent}",
maxlen=1024,
subtype="DIR_PATH",
)
agree_to_license: bpy.props.BoolProperty(
name="I agree",
default=False,
description="I agree to the Material Crafter License and the Hugging Face Stable Diffusion License.",
)
# # ======== Pre-Dependency Operators ======== #
class MCPRE_OT_install_dependencies(bpy.types.Operator):
bl_idname = "mc.install_dependencies"
bl_label = "Install dependencies"
bl_description = (
"Downloads and installs the required python packages for this add-on. "
"Internet connection is required. Blender may have to be started with "
"elevated permissions in order to install the package."
)
bl_options = {"REGISTER", "INTERNAL"}
def invoke(self, context, event):
msg = "This will install python packages. It will take several minutes, depending on your connection speed."
if dependencies_installed:
msg = "Dependencies are already installed. Are you sure you want to reinstall all packages?"
return context.window_manager.invoke_confirm(self, event, message=msg)
@classmethod
def poll(cls, context):
if not bpy.context.scene.input_tool_pre.agree_to_license:
cls.poll_message_set("Please accept the license before installing")
return bpy.context.scene.input_tool_pre.agree_to_license
def execute(self, context):
global installing
installing = True
mc_path = (
Path(bpy.context.scene.input_tool_pre.mc_path) / "Material-Crafter-Add-on"
)
venv_path = mc_path / "venv"
# Install pip:
helpers.install_pip()
self.report({"INFO"}, "PIP installed.")
# Install Venv:
if not venv_path.exists():
subprocess.run([sys.executable, "-m", "venv", venv_path], check=True)
# Importing dependencies
try:
pm.update_named_paths(mc_path, "material_crafter")
pm.update_named_paths(venv_path, "venv")
helpers.install_modules(venv_path=venv_path, context=context)
self.report({"INFO"}, "Python modules installed successfully.")
pm.save_named_paths()
except (subprocess.CalledProcessError, ImportError) as err:
self.report({"ERROR"}, str(err))
return {"CANCELLED"}
set_dependencies_installed(True)
for mc_cls in classes:
if not mc_cls.is_registered:
bpy.utils.register_class(mc_cls)
bpy.types.Scene.input_tool = bpy.props.PointerProperty(
type=MC_PGT_Input_Properties
)
installing = False
return {"FINISHED"}
# ======== Pre-Dependency UI Panels ======== #
class MCPRE_PT_warning_panel(bpy.types.Panel):
bl_label = "Material Crafter Warning"
bl_category = "Material Crafter"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
@classmethod
def poll(cls, context):
mc_path = (
Path(bpy.context.scene.input_tool_pre.mc_path) / "Material-Crafter-Add-on"
)
return not mc_path.exists()
def draw(self, context):
layout = self.layout
lines = [
f"Please install the missing dependencies for the \"{bl_info.get('name')}\" add-on.",
f"1. Open Edit > Preferences > Add-ons.",
f"2. Search for the \"{bl_info.get('name')}\" add-on.",
f'3. Under "Preferences" click on the "{MCPRE_OT_install_dependencies.bl_label}"',
f" button. This will download and install the missing Python packages,",
f" if Blender has the required permissions. If you are experiencing issues,",
f" re-open Blender with Administrator privileges.",
]
for line in lines:
layout.label(text=line)
class MCPRE_preferences(bpy.types.AddonPreferences):
bl_idname = __name__
def draw(self, context):
layout = self.layout
scene = context.scene
input_tool_pre = scene.input_tool_pre
row = layout.row()
row.prop(input_tool_pre, "mc_path")
# Hugging Face and Material Crafter License agreement:
# This line represents the character space readable in Blender's UI system:
# |=======================================================================|
lines = [
f"Please read the two following License Agreements. You must accept the terms ",
f"of the License Agreement before continuing with the installation.",
]
for line in lines:
layout.alignment = "CENTER"
layout.label(text=line)
row = layout.row()
row.operator(
"wm.url_open", text="Stable Diffusion License", icon="HELP"
).url = "https://huggingface.co/spaces/CompVis/stable-diffusion-license"
row = layout.row()
row.operator(
"wm.url_open", text="Material Crafter License (OpenRAIL)", icon="HELP"
).url = "https://huggingface.co/blog/open_rail"
row_agree_to_license = layout.row()
row_agree_to_license.alignment = "CENTER"
row_agree_to_license.prop(input_tool_pre, "agree_to_license")
row_install_dependencies_button = layout.row()
row_install_dependencies_button.operator(
MCPRE_OT_install_dependencies.bl_idname, icon="CONSOLE"
)
if installing:
progress_bar(self, context)
if dependencies_installed and bpy.context.scene.input_tool_pre.agree_to_license:
row_agree_to_license.enabled = False
row_dependencies_installed = layout.row()
row_dependencies_installed.alignment = "CENTER"
row_dependencies_installed.label(text="Dependencies already installed", icon="ERROR")
pre_dependency_classes = (
# Property Group Classes:
MC_PGT_Input_Properties_Pre,
# Operator Classes:
MCPRE_OT_install_dependencies,
# Panel Classes
MCPRE_preferences,
MCPRE_PT_warning_panel,
)
# ======== User input Property Group ======== #
class MC_PGT_Input_Properties(bpy.types.PropertyGroup):
dir_name: bpy.props.StringProperty(
name="Name", description="Name of the generated texture"
)
prompt: bpy.props.StringProperty(
name="Prompt", description="Text prompt to generate the texture"
)
image_prompt: bpy.props.StringProperty(
name="Image Prompt", description="Image prompt to generate the texture",
subtype="FILE_PATH",
)
prompt_type: bpy.props.EnumProperty(
name="Prompt Type", description="Either prompt text or image for generation",
default="text",
items=[
("text", "Text", "Text input"),
("image", "Image", "Image input"),
]
)
save_path: bpy.props.StringProperty(
name="Save Path",
description="Save path",
default=pm.named_paths['texture_output'].as_posix(),
maxlen=1024,
subtype="DIR_PATH",
)
model_id: bpy.props.EnumProperty(
name="Model ID",
description="Select main model for generation",
items=[
("gvecchio/MatForger", "gvecchio/MatForger", "MatForger"),
],
)
device: bpy.props.EnumProperty(
name="Device Type",
description="Select render device for Stable Diffusion",
items=[
("cuda", "Cuda (GPU)", "Render with Cuda (GPU)"),
("cpu", "CPU", "Render with CPU"),
],
)
precision: bpy.props.EnumProperty(
name="Precision",
description="Floating Point Precision. This affects memory usage",
default="fp16",
items=[
("fp32", "FP32 (Full)", "Full Precision"),
("fp16", "FP16 (Half)", "Half Precision"),
],
)
scheduler: bpy.props.EnumProperty(
name="Scheduler",
default="ddim",
description="Diffusion Scheduler.",
items=[
("ddim", "DDIM", "DDIM Scheduler"),
("euler", "Euler Discrete", "Euler Discrete"),
],
)
guidance_scale: bpy.props.FloatProperty(
name="Guidance Scale",
default=6.0,
step=0.1,
max=20.0,
min=0.0,
description="Classifier-free Guidance scale factor",
)
tileable: bpy.props.BoolProperty(
name="Tileable",
default=True,
description="Generate a tileable material",
)
patched: bpy.props.BoolProperty(
name="Patched",
default=True,
description="Enables patched diffusion. Reduces memory consumption when working with high resolutions but can affect quality",
)
free_u: bpy.props.BoolProperty(
name="Free U",
default=False,
description="Enables FreeU in diffusers. This may impair the image quality and the map consistency",
)
height: bpy.props.IntProperty(
name="Height",
default=512,
step=32,
min=128,
max=4096,
description="Height of the generated textures (higher sizes consume more memory)",
)
width: bpy.props.IntProperty(
name="Width",
default=512,
step=32,
min=128,
max=4096,
description="Width of the generated textures (higher sizes consume more memory)",
)
num_steps: bpy.props.IntProperty(
name="Steps",
default=50,
step=5,
min=0,
max=1000,
description="Number of diffusion sampling steps",
)
# ======== Operators ======== #
class CreateTextures(bpy.types.Operator):
bl_idname = "mc.create_textures"
bl_label = "Create Textures"
bl_description = "Creates textures with Stable Diffusion by using the Texture Description as text input."
bl_options = {"REGISTER", "UNDO"}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event, message="This operation may require several minutes. Make sure to open the Window console before running to keep track of the progress.")
@classmethod
def poll(cls, context):
r'''
Allows material creation only if an element accepting materials is selected
'''
has_materials = hasattr(bpy.context.active_object.data, "materials")
if not has_materials:
cls.poll_message_set("Please select an object that supports materials")
return has_materials
def execute(self, context):
venv_path = pm.named_paths['venv']
if bpy.context.scene.input_tool.prompt_type == "text":
prompt = bpy.context.scene.input_tool.prompt
elif bpy.context.scene.input_tool.prompt_type == "image":
prompt = bpy.context.scene.input_tool.image_prompt
assert Path(prompt).exists(), f"Image prompt path not found at {prompt}"
user_input = {
"name": bpy.context.scene.input_tool.dir_name,
"prompt": prompt,
"prompt_type": bpy.context.scene.input_tool.prompt_type,
"save_path": Path(bpy.path.abspath(bpy.context.scene.input_tool.save_path)),
"model_path": bpy.context.scene.input_tool.model_id,
"precision": bpy.context.scene.input_tool.precision,
"device": bpy.context.scene.input_tool.device,
}
sd_kwargs = {
"guidance_scale": bpy.context.scene.input_tool.guidance_scale,
"height": bpy.context.scene.input_tool.height,
"width": bpy.context.scene.input_tool.width,
"num_inference_steps": bpy.context.scene.input_tool.num_steps,
"scheduler": bpy.context.scene.input_tool.scheduler,
"tileable": bpy.context.scene.input_tool.tileable,
"patched": bpy.context.scene.input_tool.patched,
"free_u": bpy.context.scene.input_tool.free_u,
}
try:
helpers.execution_handler(venv_path, "generate", {**user_input, **sd_kwargs})
load_texture_maps(Path(user_input['save_path']), user_input['name'])
pm.update_named_paths(user_input["save_path"], "texture_output")
self.report({"INFO"}, f"New Material Created!")
except subprocess.CalledProcessError as e:
print(e)
self.report({"ERROR"}, "Running text2img raised an exception:\n {e}")
return {"CANCELLED"}
return {"FINISHED"}
# ======== UI Panels ======== #
class MC_PT_Main(bpy.types.Panel):
bl_label = "Material Crafter"
bl_idname = "MC_PT_Main"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Material Crafter"
def draw(self, context):
layout = self.layout
scene = context.scene
input_tool = scene.input_tool
"""
The Main panel for Material Crafter.
"""
row = layout.row()
row.prop(input_tool, "prompt_type")
if input_tool.prompt_type == "text":
row = layout.row()
row.prop(input_tool, "prompt")
elif input_tool.prompt_type == "image":
row = layout.row()
row.prop(input_tool, "image_prompt")
row = layout.row()
row = layout.row()
row.prop(input_tool, "model_id")
row = layout.row()
row.prop(input_tool, "precision")
row = layout.row()
row.prop(input_tool, "device")
layout.separator()
row = layout.row()
row.prop(input_tool, "save_path")
row = layout.row()
row.prop(input_tool, "dir_name")
row = layout.row()
row.prop(input_tool, "tileable")
layout.separator()
layout.operator(
"mc.create_textures", icon="DISCLOSURE_TRI_RIGHT", text="Create Textures"
)
header, body = layout.panel("Diffusion Parameters", default_closed=False)
row = header.row()
row.label(text="Diffusion Parameters")
row = body.row()
row.prop(input_tool, "scheduler")
row = body.row()
row.prop(input_tool, "guidance_scale")
row = body.row()
row.prop(input_tool, "height")
row = body.row()
row.prop(input_tool, "width")
row = body.row()
row.prop(input_tool, "num_steps")
row = body.row()
row.prop(input_tool, "patched")
row = body.row()
row.prop(input_tool, "free_u")
class MC_PT_Model_Warning(bpy.types.Panel):
bl_label = "Material Crafter Warning"
bl_category = "Material Crafter"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
@classmethod
def poll(cls, context):
model_dir_name = f"models--{bpy.context.scene.input_tool.model_id.replace('/', '--')}"
model_dir = pm.named_paths['model'] / "hub" / model_dir_name
return not model_dir.exists()
def draw(self, context):
layout = self.layout
model_dir_name = pm.named_models['model'] / "hub" / f"models--{bpy.context.scene.input_tool.model_id.replace('/', '--')}"
lines = [
f"Weights for {bpy.context.scene.input_tool.model_id} model not found in cache at {model_dir_name}",
f"The weights will be donwloaded from Huggingface hub, which may take several minutes, depending on your connection speed."
]
for line in lines:
layout.label(text=line)
class MC_PT_Help(bpy.types.Panel):
bl_label = "Help"
bl_idname = "MC_PT_Help"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_category = "Material Crafter"
def draw(self, context):
layout = self.layout
scene = context.scene
input_tool = scene.input_tool
row = layout.row()
row.label(text=f"Looking for help?")
row = layout.row()
row = layout.row()
layout.label(text=f"{MC_version}, {LAST_UPDATED}")
# ======== Blender add-on register/unregister handling ======== #
classes = (
# Property Group Classes:
MC_PGT_Input_Properties,
# Operator Classes:
CreateTextures,
# Panel Classes:
MC_PT_Model_Warning,
MC_PT_Main,
MC_PT_Help,
)
def register():
global dependencies_installed
dependencies_installed = False
# Setup progress variables
bpy.types.WindowManager.progress = bpy.props.FloatProperty()
bpy.types.WindowManager.progress_text = bpy.props.StringProperty()
for cls in pre_dependency_classes:
bpy.utils.register_class(cls)
bpy.types.Scene.input_tool_pre = bpy.props.PointerProperty(
type=MC_PGT_Input_Properties_Pre
)
if pm.paths_file_exists():
pm.load_paths_file()
if pm.named_paths['venv'].exists():
set_dependencies_installed(True)
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.input_tool = bpy.props.PointerProperty(
type=MC_PGT_Input_Properties
)
return
def unregister():
for cls in pre_dependency_classes:
bpy.utils.unregister_class(cls)
if dependencies_installed:
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
del bpy.types.Scene.input_tool
del bpy.types.Scene.input_tool_pre