-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path__init__.py
156 lines (122 loc) · 4.83 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
import bpy
import os
import sys
from .properties import register_properties, unregister_properties
from .operators import OBJECT_OT_GenerateTexture, OBJECT_OT_SelectPipette
from .panel import (
OBJECT_PT_MainPanel,
OBJECT_OT_OpenNewInputImage,
OBJECT_PT_IPAdapterPanel,
OBJECT_OT_OpenNewIPAdapterImage,
OBJECT_PT_LoRAPanel,
OBJECT_PT_AdvancedPanel,
)
class MockUpScene:
"""
A mockup class to simulate the scene properties for pipeline testing.
"""
def __init__(self):
self.num_loras = 0
self.use_ipadapter = True
self.ipadapter_strength = 0.5
self.mesh_complexity = "HIGH"
self.depth_controlnet_strength = 1.0
self.canny_controlnet_strength = 1.0
self.normal_controlnet_strength = 1.0
self.sd_version = "sd15"
self.checkpoint_path = "runwayml/stable-diffusion-v1-5"
self.canny_controlnet_path = "lllyasviel/sd-controlnet-canny"
self.normal_controlnet_path = "lllyasviel/sd-controlnet-normal"
self.depth_controlnet_path = "lllyasviel/sd-controlnet-depth"
class InstallModelsOperator(bpy.types.Operator):
"""Operator to install necessary models"""
bl_idname = "diffused_texture_addon.install_models"
bl_label = "Install Models"
bl_description = "Install the necessary models for DiffusedTexture"
bl_options = {"REGISTER", "INTERNAL"}
def execute(self, context):
# Retrieve the HuggingFace cache path from preferences
prefs = bpy.context.preferences.addons[__package__].preferences
hf_cache_path = prefs.hf_cache_path
# Set environment variable if path is provided
if hf_cache_path:
os.environ["HF_HOME"] = hf_cache_path
# If blender is set to offline this will not work, but lets not sneak always on onto the user
if not bpy.app.online_access:
os.environ["HF_HUB_OFFLINE"] = "1"
# Logic for model installation
try:
# Import after setting HF_HOME
import diffusers
from .diffusedtexture.diffusers_utils import create_pipeline
# Safely create the cache directory if it does not exist
if hf_cache_path:
os.makedirs(hf_cache_path, exist_ok=True)
# Create the pipeline
mockup_scene = MockUpScene()
pipe = create_pipeline(mockup_scene)
del pipe # Clean up to avoid memory issues
self.report({"INFO"}, f"Models installed successfully in {hf_cache_path}.")
except Exception as e:
self.report({"ERROR"}, f"Failed to install models: {str(e)}")
return {"CANCELLED"}
return {"FINISHED"}
class DiffuseTexPreferences(bpy.types.AddonPreferences):
# bl_idname = __package__
bl_idname = __package__
# Path setting for HuggingFace cache directory
hf_cache_path: bpy.props.StringProperty(
name="HuggingFace Cache Path",
description="Path to a custom HuggingFace cache directory",
subtype="DIR_PATH",
default="",
)
def draw(self, context):
layout = self.layout
# Add a text block to explain that the user needs to explicitly allow online access
box = layout.box()
row = box.row()
row.label(text="Please ensure that Blender is allowed to access the internet")
row = box.row()
row.label(text="in order to install models. Do so in:")
row = box.row()
row.label(text="Preferences > System > Network > Allow Online Access.")
# HuggingFace Cache Path setting
layout.prop(self, "hf_cache_path", text="HuggingFace Cache Path")
# make the Install Models button unavailable if the "online access" is disabled
if not bpy.app.online_access:
layout.label(
text="Online access is disabled. Enable it in Preferences > System > Network > Allow Online Access."
)
row = layout.row()
row.enabled = False
row.operator(
InstallModelsOperator.bl_idname, text="Install Models", icon="IMPORT"
)
else:
# Button to execute the model installation function
layout.operator(
InstallModelsOperator.bl_idname, text="Install Models", icon="IMPORT"
)
classes = [
DiffuseTexPreferences,
InstallModelsOperator,
OBJECT_OT_GenerateTexture,
OBJECT_OT_SelectPipette,
OBJECT_PT_MainPanel,
OBJECT_OT_OpenNewInputImage,
OBJECT_PT_AdvancedPanel,
OBJECT_PT_IPAdapterPanel,
OBJECT_OT_OpenNewIPAdapterImage,
OBJECT_PT_LoRAPanel,
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
register_properties()
def unregister():
unregister_properties()
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()