-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvicho_operators.py
273 lines (212 loc) · 9.42 KB
/
vicho_operators.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
import subprocess
import sys
import bpy
import webbrowser
import os
from .misc.funcs import export_milo_ymap_xml
from bpy.props import StringProperty
from bpy_extras.io_utils import ExportHelper
from .vicho_dependencies import dependencies_manager, is_dotnet_installed, dotnet_link
from importlib import util, machinery
class ContextSelectionRestrictedHelper:
@classmethod
def poll(cls, context):
return context.active_object is not None
class VICHO_OT_export_mlo_file(bpy.types.Operator, ContextSelectionRestrictedHelper):
bl_idname = "vicho.exportmlostransformstofile"
bl_label = "Export MLO transforms to YMAP"
def execute(self, context):
objs = context.selected_objects
for obj in objs:
if obj.sollum_type == "sollumz_bound_composite" or obj.type == "MESH":
export_milo_ymap_xml(
"map1", obj, context.scene.ymap_instance_name_field
)
self.report(
{"INFO"}, f"{obj.name} location and rotation exported to file"
)
else:
self.report(
{"WARNING"}, f"{obj.name} is not a Bound Composite or a Mesh"
)
return {"FINISHED"}
class VICHO_OT_paste_obj_trans_from_pick_obj(bpy.types.Operator):
bl_idname = "vicho.pasteobjtransfrompickedobject"
bl_label = "Set transforms"
@classmethod
def poll(cls, context):
return context.scene.CopyDataFromObject is not None and (
context.scene.locationOb_checkbox
or context.scene.rotationOb_checkbox
or context.scene.scaleOb_checkbox
)
def execute(self, context):
from_obj = context.scene.CopyDataFromObject
to_obj = context.scene.PasteDataToObject
if context.scene.locationOb_checkbox:
to_obj.location = from_obj.location
if context.scene.rotationOb_checkbox:
to_obj.rotation_euler = from_obj.rotation_euler
if context.scene.scaleOb_checkbox:
to_obj.scale = from_obj.scale
return {"FINISHED"}
class VICHO_OT_mlo_ymap_file_browser(bpy.types.Operator, ExportHelper):
"""Export MLO instance to YMAP"""
bl_idname = "vicho.mloyampfilebrowser"
bl_label = "Export MLO transforms to YMAP"
bl_action = "Export a YMAP MLO"
bl_showtime = True
filename_ext = ".ymap"
filter_glob: StringProperty(default="*.ymap", options={"HIDDEN"})
def execute(self, context):
try:
export_milo_ymap_xml(
self.filepath,
context.active_object,
context.scene.ymap_instance_name_field,
)
self.report({"INFO"}, f"{self.filepath} successfully exported")
return {"FINISHED"}
except Exception:
self.report({"ERROR"}, f"Error exporting {self.filepath} ")
return {"FINISHED"}
class VICHO_OT_del_all_cols_attrs(bpy.types.Operator, ContextSelectionRestrictedHelper):
"""Delete all color attributes from selected objects"""
bl_idname = "vicho.deleteallcolorattributes"
bl_label = "Color Attributes"
def execute(self, context):
objects = context.selected_objects
removed_count = 0
for obj in objects:
if obj.type == "MESH":
if obj.data.color_attributes:
removed_count = removed_count + 1
attrs = obj.data.color_attributes
for r in range(len(obj.data.color_attributes) - 1, -1, -1):
attrs.remove(attrs[r])
return {"FINISHED"}
class VICHO_OT_del_all_vgs(bpy.types.Operator, ContextSelectionRestrictedHelper):
"""Delete all vertex groups from selected objects"""
bl_idname = "vicho.deleteallvertexgroups"
bl_label = "Vertex Groups"
def execute(self, context):
objects = context.selected_objects
for obj in objects:
if obj.type == "MESH":
for i in range(len(obj.vertex_groups)):
obj.vertex_groups.remove(obj.vertex_groups[0])
else:
continue
return {"FINISHED"}
class VICHO_OT_detect_meshes_no_textures(bpy.types.Operator, ContextSelectionRestrictedHelper):
"""Detect meshes with no textures in selected objects and then it print them in the console"""
bl_idname = "vicho.detectmesheswithnotextures"
bl_label = "Detect meshes with no textures"
def execute(self, context):
objects = context.selected_objects
for obj in objects:
if obj.type == "MESH":
if len(obj.material_slots) < 1:
print(f"{obj.name} has no material slots")
else:
for slot in obj.material_slots:
if slot.material.use_nodes:
if (
not slot.material.node_tree.nodes["Principled BSDF"]
.inputs["Base Color"]
.is_linked
):
print(f"{obj.name} has no texture")
return {"FINISHED"}
class VICHO_OT_rename_all_uvs(bpy.types.Operator, ContextSelectionRestrictedHelper):
"""Rename all UV maps from selected objects to Sollumz' standard"""
bl_idname = "vicho.renamealluvmaps"
bl_label = "UV Maps"
def execute(self, context):
objects = context.selected_objects
for obj in objects:
if obj.type == "MESH":
for indx, uvmap in enumerate(obj.data.uv_layers):
uvmap.name = f"UVMap {indx}"
return {"FINISHED"}
class VICHO_OT_rename_all_cas(bpy.types.Operator, ContextSelectionRestrictedHelper):
"""Rename all color attributes from selected objects to Sollumz' standard"""
bl_idname = "vicho.renameallcolorattributes"
bl_label = "Color Attributes"
def execute(self, context):
objects = context.selected_objects
for obj in objects:
if obj.type == "MESH":
for indx, color_attr in enumerate(obj.data.color_attributes):
color_attr.name = f"Color {indx + 1}"
return {"FINISHED"}
class VICHO_OT_install_depens(bpy.types.Operator):
bl_idname = "vicho.installdependencies"
bl_label = "Install dependencies (Python.NET)"
bl_description = "Install dependencies (Python.NET)"
def execute(self, context):
try:
if not is_dotnet_installed():
self.report(
{"ERROR"},
".NET 8.0 or later is not installed. Please install it first.",
)
return {"CANCELLED"}
find_pythonnet: machinery.ModuleSpec = util.find_spec("pythonnet")
if find_pythonnet is not None:
self.report({"INFO"}, "Python.NET is already installed")
else:
self.report({"INFO"}, "Installing Python.NET...")
python_exe = os.path.join(sys.prefix, "bin", "python.exe")
target = os.path.join(sys.prefix, "lib", "site-packages")
subprocess.call([python_exe, "-m", "ensurepip"])
subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'pip'])
subprocess.call([python_exe, '-m', 'pip', 'install', '--upgrade', 'pythonnet', '-t', target])
self.report({"INFO"}, "Python.NET installed successfully")
if dependencies_manager.load_dependencies():
self.report({"INFO"}, "Dependencies loaded successfully")
print(f"dependencies.available: {dependencies_manager.available}")
print(f"clr: {dependencies_manager.clr}")
print(f"List: {dependencies_manager.List}")
print(f"GameFiles: {dependencies_manager.GameFiles}")
print(f"Utils: {dependencies_manager.Utils}")
else:
self.report({"ERROR"}, "Failed to load dependencies")
return {"CANCELLED"}
if dependencies_manager.available:
self.report(
{"INFO"}, "All dependencies are now available and ready to use"
)
else:
self.report(
{"ERROR"}, "Dependencies are still not available after loading"
)
return {"CANCELLED"}
except subprocess.CalledProcessError as e:
self.report({"ERROR"}, f"Error installing Python.NET: {str(e)}")
return {"CANCELLED"}
except Exception as e:
self.report({"ERROR"}, f"Unexpected error: {str(e)}")
return {"CANCELLED"}
return {"FINISHED"}
class VICHO_OT_install_dotnet(bpy.types.Operator):
bl_idname = "vicho.installdotnetruntime"
bl_label = "Install .NET 8 runtime"
bl_description = "Install .NET 8 runtime"
def execute(self, context):
# download .NET 8 runtime from Microsoft
try:
webbrowser.open(dotnet_link)
self.report({"INFO"}, "Download .NET 8 runtime from Microsoft's website")
except Exception:
self.report(
{"ERROR"},
"Error opening web browser to download .NET 8 runtime from Microsoft's website",
)
return {"FINISHED"}
class VICHO_OT_fake_op(bpy.types.Operator):
"""Fake operator"""
bl_idname = "vicho.fake_op"
bl_label = ""
def execute(self, context):
return {"FINISHED"}