Skip to content

Commit 29fbca8

Browse files
committed
fix: loading issue
1 parent 15e9161 commit 29fbca8

File tree

6 files changed

+99
-69
lines changed

6 files changed

+99
-69
lines changed

src/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"name": "Script To Button",
1616
"author": "RivinHD",
1717
"blender": (4, 2, 0),
18-
"version": (2, 3, 3),
18+
"version": (2, 3, 4),
1919
"location": "View3D",
2020
"category": "System",
2121
"doc_url": "https://github.com/RivinHD/ScriptToButton/wiki",

src/blender_manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ schema_version = "1.0.0"
33
# Example of manifest file for a Blender extension
44
# Change the values according to your extension
55
id = "script_to_button"
6-
version = "2.3.3"
6+
version = "2.3.4"
77
name = "Script To Button"
88
tagline = "Converts scripts to buttons"
99
maintainer = "RivinHD"

src/dynamic_panels.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ def poll(self, context: Context) -> bool:
4545
area = context.area.ui_type
4646
panel = panel_names[self.bl_order]
4747
return any(
48-
(button.panel == panel and area in button.areas) for button in stb
48+
(
49+
button.panel == panel
50+
and (len(button.areas) == 0 or area in button.areas)
51+
)
52+
for button in stb
4953
)
5054

5155
def draw_header(self, context: Context):
@@ -57,7 +61,10 @@ def draw(self, context):
5761
area = context.area.ui_type
5862
panel = panel_names[self.bl_order]
5963
buttons = filter(
60-
lambda x: area in x.areas and x.panel == panel, context.scene.stb
64+
lambda x: (
65+
(len(x.areas) == 0 or area in x.areas) and x.panel == panel
66+
),
67+
context.scene.stb,
6168
)
6269
for button in sorted(buttons, key=lambda x: x.name):
6370
row = layout.row(align=True)
@@ -96,7 +103,11 @@ def poll(self, context: Context) -> bool:
96103
area = context.area.ui_type
97104
panel = panel_names[self.bl_order]
98105
return any(
99-
(button.panel == panel and area in button.areas) for button in stb
106+
(
107+
button.panel == panel
108+
and (len(button.areas) == 0 or area in button.areas)
109+
)
110+
for button in stb
100111
)
101112

102113
def draw(self, context: Context):

src/functions.py

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,6 @@
2222

2323
classes = []
2424
NotOneStart = [False]
25-
ALL_AREAS = [
26-
"3D_Viewport",
27-
"UV_Editor",
28-
"Compositor",
29-
"Video_Sequencer",
30-
"Movie_Clip_Editor",
31-
"Dope_Sheet",
32-
"Graph_Editor",
33-
"Nonlinear_Animation",
34-
"Text_Editor",
35-
]
3625

3726

3827
def get_preferences(context: Context) -> STB_preferences:
@@ -140,7 +129,7 @@ def get_areas(text: str) -> list:
140129
area_types.append(comment.split("-")[2])
141130
if len(area_types):
142131
return area_types
143-
return ALL_AREAS
132+
return [] # Empty = All Areas
144133

145134

146135
AREA_PARSE_DICT = {
@@ -371,8 +360,21 @@ def add_prop(button: STB_button_properties, property) -> bool:
371360

372361
def add_button(context: Context, name: str, textname: str, description: str):
373362
STB_pref = get_preferences(context)
363+
index = context.scene.stb.find(name)
374364
texts = bpy.data.texts
375365
text = texts[textname].as_string() # Get selected Text
366+
if index != -1:
367+
context.scene.stb.remove(index)
368+
new = context.scene.stb.add() # Create new Instance
369+
new.name = check_for_duplicates(get_all_button_names(context), name)
370+
if description != "":
371+
new.description = description
372+
fails = add_areas_and_props(new, text)
373+
if new.panel not in panels.panel_names:
374+
panels.register_button_panel(new.panel)
375+
376+
update_text_header(texts[textname], new)
377+
text = texts[textname].as_string() # Get selected Text
376378
if STB_pref.autosave:
377379
save_text(texts[textname], name)
378380
if STB_pref.autoload:
@@ -383,19 +385,42 @@ def add_button(context: Context, name: str, textname: str, description: str):
383385
else:
384386
texts[textname].clear()
385387
texts[textname].write(text) # Write to Text
386-
index = context.scene.stb.find(name)
387-
if index != -1:
388-
context.scene.stb.remove(index)
389-
new = context.scene.stb.add() # Create new Instance
390-
new.name = check_for_duplicates(get_all_button_names(context), name)
391-
if description != "":
392-
new.description = description
393-
fails = add_areas_and_props(new, text)
394-
if new.panel not in panels.panel_names:
395-
panels.register_button_panel(new.panel)
388+
396389
return fails
397390

398391

392+
def update_text_header(text: bpy.types.Text, button: STB_button_properties):
393+
lines = [line.body for line in text.lines]
394+
if len(button.areas):
395+
if lines[0].strip().startswith("#STB"):
396+
line = ""
397+
else:
398+
line = ""
399+
lines.insert(0, line)
400+
line += " /// ".join(
401+
map(
402+
lambda x: "#STB-Area-%s" % AREA_PARSE_DICT_INVERSE[x.name], button.areas
403+
)
404+
)
405+
lines[0] = line
406+
407+
if button.description != "":
408+
escaped_description = button.description.replace('"', '\\"')
409+
if lines[0].strip().startswith("#STB-Area"):
410+
line = lines[0]
411+
line += " /// "
412+
elif lines[0].strip().startswith("#STB-Tooltip"):
413+
line = ""
414+
else:
415+
line = ""
416+
lines.insert(0, line)
417+
line += f'#STB-Tooltip-"{escaped_description}"'
418+
lines[0] = line
419+
420+
text.clear()
421+
text.write("\n".join(lines))
422+
423+
399424
def remove_button(context: Context, delete_file: bool, delete_text: bool):
400425
STB_pref = get_preferences(context)
401426
name = STB_pref.selected_button
@@ -413,6 +438,9 @@ def remove_button(context: Context, delete_file: bool, delete_text: bool):
413438
stb.remove(index)
414439
if index - 1 >= 0:
415440
stb[index - 1].selected = True
441+
else:
442+
scene = bpy.context.scene
443+
scene["stb_button.selected_name"] = ""
416444
panel_names = set(button.panel for button in stb)
417445
for panel in set(panels.panel_names).difference(panel_names):
418446
panels.unregister_button_panel(panel)
@@ -528,12 +556,6 @@ def add_areas_and_props(button: STB_button_properties, text: str) -> tuple[list,
528556
new = button.areas.add()
529557
new.name = pars
530558
new.area = pars
531-
if len(areas) == len(failed_areas): # failed to add areas
532-
for ele in ALL_AREAS:
533-
pars = area_parser(ele)
534-
new = button.areas.add()
535-
new.name = pars
536-
new.area = pars
537559

538560
prop_list_dict = get_props(text) # Get Props
539561
failed_props = []

src/operators.py

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ def get_description(self):
4545
else:
4646
STB_pref = get_preferences(bpy.context)
4747
text_name = STB_pref.texts_list
48-
text = bpy.data.texts[text_name].as_string() # Get selected Text
49-
return functions.get_description(text)
48+
if bpy.data.texts.find(text_name) != -1:
49+
text = bpy.data.texts[text_name].as_string() # Get selected Text
50+
return functions.get_description(text)
51+
else:
52+
return ""
5053
return value
5154

5255
description: StringProperty(
@@ -110,6 +113,7 @@ def execute(self, context: Context) -> set[str]:
110113
elif STB_pref.texts_list == "":
111114
self.report({"ERROR"}, "You need to select a Text")
112115
return {"FINISHED"}
116+
113117
fails = functions.add_button(context, self.name, txt, self.description)
114118
if len(fails[0]) or len(fails[1]):
115119
self.report(
@@ -684,7 +688,6 @@ def execute(self, context):
684688

685689
STB_pref = get_preferences(context)
686690
button: STB_button_properties = context.scene.stb[STB_pref.selected_button]
687-
property_changed = False
688691

689692
text_index = bpy.data.texts.find(STB_pref.selected_button)
690693
if text_index == -1:
@@ -693,35 +696,28 @@ def execute(self, context):
693696
else:
694697
text = bpy.data.texts[text_index]
695698
lines = [line.body for line in text.lines]
699+
first_line = ""
696700

697701
if len(self.stb_areas):
698-
property_changed = True
699-
if lines[0].strip().startswith("#STB"):
700-
line = ""
701-
else:
702-
line = ""
703-
lines.insert(0, line)
704-
line += " /// ".join(map(lambda x: "#STB-Area-%s" % x.name, self.stb_areas))
705-
lines[0] = line
706-
707-
if button.description != self.description:
708-
property_changed = True
709-
button.description = self.description
710-
escaped_description = self.description.replace('"', '\\"')
711-
if lines[0].strip().startswith("#STB-Area"):
712-
line = lines[0]
713-
line += " /// "
714-
elif lines[0].strip().startswith("#STB-Tooltip"):
715-
line = ""
716-
else:
717-
line = ""
718-
lines.insert(0, line)
719-
line += f'#STB-Tooltip-"{escaped_description}"'
720-
lines[0] = line
702+
if not (lines[0].strip() == "" or lines[0].strip().startswith("#STB")):
703+
lines.insert(0, first_line)
704+
first_line += " /// ".join(
705+
map(lambda x: "#STB-Area-%s" % x.name, self.stb_areas)
706+
)
707+
708+
button.description = self.description
709+
escaped_description = self.description.replace('"', '\\"')
710+
if first_line != "":
711+
first_line += " /// "
712+
elif not (lines[0].strip() == "" or lines[0].strip().startswith("#STB")):
713+
lines.insert(0, first_line)
714+
first_line += f'#STB-Tooltip-"{escaped_description}"'
715+
716+
if first_line != "":
717+
lines[0] = first_line
721718

722719
edited_lines = []
723720
for prop in filter(lambda x: x.use_delete, self.stb_properties):
724-
property_changed = True
725721
line: str = lines[prop.line - 1]
726722
line_start = line.find("#STB")
727723
if line_start == -1:
@@ -759,10 +755,9 @@ def execute(self, context):
759755
if line.strip() == "":
760756
lines.pop(i)
761757

762-
if property_changed:
763-
text.clear()
764-
text.write("\n".join(lines))
765-
bpy.ops.stb.reload()
758+
text.clear()
759+
text.write("\n".join(lines))
760+
bpy.ops.stb.reload()
766761
context.area.tag_redraw()
767762
return {"FINISHED"}
768763

@@ -821,10 +816,12 @@ def text_properties_items(self, context):
821816
@classmethod
822817
def poll(cls, context: Context):
823818
STB_pref = get_preferences(context)
824-
area = context.area.ui_type
825-
button = context.scene.stb[STB_pref.selected_button]
826-
is_visible = area in button.areas
827-
return STB_pref.selected_button != "" and is_visible
819+
if STB_pref.selected_button != "":
820+
button = context.scene.stb[STB_pref.selected_button]
821+
area = context.area.ui_type
822+
is_visible = len(button.areas) == 0 or area in button.areas
823+
return is_visible
824+
return False
828825

829826
def invoke(self, context: Context, event: Event):
830827
STB_pref = get_preferences(context)

src/panels.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def draw(self, context):
8181
return
8282
area = context.area.ui_type
8383
button = stb[STB_pref.selected_button]
84-
is_visible = area in button.areas
84+
is_visible = len(button.areas) == 0 or area in button.areas
8585
if not is_visible:
8686
layout.label(text="Selected button is hidden in this Area.")
8787
return

0 commit comments

Comments
 (0)