forked from atticus-lv/object_asset_wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
support_ops.py
151 lines (115 loc) · 4.69 KB
/
support_ops.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
# Copyright (C) 2019 h0bB1T
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
#
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import bpy, os, re
from bpy.types import Operator
from . properties import Properties, StringProperty
from . preview_helper import PreviewHelper
from . preferences import PreferencesPanel
from . utils import export_file, CategoriesCache, ASSET_TYPE_OBJECT, ASSET_TYPE_MATERIAL, PREVIEW_EXT
class RefreshObjectPreviews(Operator):
bl_idname = "asset_wizard.refresh_object_previews_op"
bl_label = "Refresh"
bl_description = "Refresh previews for this category (if e.g. externally modified)"
def execute(self, context):
PreviewHelper.setData(
ASSET_TYPE_OBJECT,
(ASSET_TYPE_OBJECT, Properties.get().iobj_categories),
True
)
CategoriesCache.update_cache(ASSET_TYPE_OBJECT)
return {'FINISHED'}
class ReRenderObjectPreview(Operator):
bl_idname = "asset_wizard.rerender_object_preview_op"
bl_label = "ReRender"
bl_description = "ReRender preview for current selection"
def execute(self, context):
Properties.get_render_previews().add_job(
ASSET_TYPE_OBJECT,
Properties.get().iobj_previews
)
return {'FINISHED'}
class RefreshMaterialPreviews(Operator):
bl_idname = "asset_wizard.refresh_material_previews_op"
bl_label = "Refresh"
bl_description = "Refresh previews for this category (if e.g. externally modified)"
def execute(self, context):
PreviewHelper.setData(
ASSET_TYPE_MATERIAL,
(ASSET_TYPE_MATERIAL, Properties.get().imat_categories),
True
)
CategoriesCache.update_cache(ASSET_TYPE_MATERIAL)
return {'FINISHED'}
class ReRenderMaterialPreview(Operator):
bl_idname = "asset_wizard.rerender_material_preview_op"
bl_label = "ReRender"
bl_description = "ReRender preview for current selection"
def execute(self, context):
Properties.get_render_previews().add_job(
ASSET_TYPE_MATERIAL,
Properties.get().imat_previews
)
return {'FINISHED'}
class RemoveAsset(Operator):
bl_idname = "asset_wizard.remove_asset_op"
bl_label = "Remove Asset?"
bl_description = "Remove asset from library"
asset_type: StringProperty()
asset: StringProperty()
def execute(self, context):
asset = os.path.join(PreferencesPanel.get().root, self.asset)
preview = os.path.splitext(asset)[0] + PREVIEW_EXT
failed = False
try:
if os.path.exists(asset):
os.remove(asset)
except Exception as ex:
failed = True
failed = False
try:
if os.path.exists(preview):
os.remove(preview)
except Exception as ex:
failed = True
if self.asset_type == ASSET_TYPE_OBJECT:
bpy.ops.asset_wizard.refresh_object_previews_op()
elif self.asset_type == ASSET_TYPE_MATERIAL:
bpy.ops.asset_wizard.refresh_material_previews_op()
if failed:
self.report({'ERROR'}, "Removing the asset failed")
else:
self.report({'INFO'}, "Asset removed")
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)
class AutoNumberExportName(Operator):
bl_idname = "asset_wizard.auto_number_export_name_op"
bl_label = "Next number"
bl_description = "Increase postfix number of export asset name"
asset_type: StringProperty()
def execute(self, context):
if self.asset_type == ASSET_TYPE_OBJECT:
props = Properties.get()
name = props.eobj_asset_name
m = re.search(r'\d+$', name)
if m is not None:
num = m.group()
base = name[:-len(num)]
name = base + str(int(num) + 1)
else:
name = name + "1"
props.eobj_asset_name = name
return {'FINISHED'}