forked from carson-katri/geometry-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
118 lines (98 loc) · 4.32 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
# 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
# MERCHANTIBILITY 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, see <http://www.gnu.org/licenses/>.
# Set the `geometry_script` module to the current module in case the folder is named differently.
import sys
sys.modules['geometry_script'] = sys.modules[__name__]
import bpy
import os
import webbrowser
from .api.tree import *
from .preferences import GeometryScriptPreferences
from .absolute_path import absolute_path
bl_info = {
"name" : "Geometry Script",
"author" : "Carson Katri",
"description" : "Python scripting for geometry nodes",
"blender" : (3, 0, 0),
"version" : (0, 1, 2),
"location" : "",
"warning" : "",
"category" : "Node"
}
class TEXT_MT_templates_geometryscript(bpy.types.Menu):
bl_label = "Geometry Script"
def draw(self, _context):
self.path_menu(
[os.path.join(os.path.dirname(os.path.realpath(__file__)), "examples")],
"text.open",
props_default={"internal": True},
filter_ext=lambda ext: (ext.lower() == ".py")
)
class OpenDocumentation(bpy.types.Operator):
bl_idname = "geometry_script.open_documentation"
bl_label = "Open Documentation"
def execute(self, context):
webbrowser.open('file://' + absolute_path('docs/documentation.html'))
return {'FINISHED'}
class GeometryScriptSettings(bpy.types.PropertyGroup):
auto_resolve: bpy.props.BoolProperty(name="Auto Resolve", default=False, description="If the file is edited externally, automatically accept the changes")
class GeometryScriptMenu(bpy.types.Menu):
bl_idname = "TEXT_MT_geometryscript"
bl_label = "Geometry Script"
def draw(self, context):
layout = self.layout
text = context.space_data.text
if text and len(text.filepath) > 0:
layout.prop(context.scene.geometry_script_settings, 'auto_resolve')
layout.operator(OpenDocumentation.bl_idname)
def templates_menu_draw(self, context):
self.layout.menu(TEXT_MT_templates_geometryscript.__name__)
def editor_header_draw(self, context):
self.layout.menu(GeometryScriptMenu.bl_idname)
def auto_resolve():
if bpy.context.scene.geometry_script_settings.auto_resolve:
try:
for area in bpy.context.screen.areas:
for space in area.spaces:
if space.type == 'TEXT_EDITOR':
with bpy.context.temp_override(area=area, space=space):
text = bpy.context.space_data.text
if text and text.is_modified:
bpy.ops.text.resolve_conflict(resolution='RELOAD')
if bpy.context.space_data.use_live_edit:
bpy.ops.text.run_script()
except:
pass
return 1
def register():
bpy.utils.register_class(TEXT_MT_templates_geometryscript)
bpy.types.TEXT_MT_templates.append(templates_menu_draw)
bpy.utils.register_class(GeometryScriptSettings)
bpy.utils.register_class(GeometryScriptPreferences)
bpy.utils.register_class(OpenDocumentation)
bpy.utils.register_class(GeometryScriptMenu)
bpy.types.TEXT_HT_header.append(editor_header_draw)
bpy.types.Scene.geometry_script_settings = bpy.props.PointerProperty(type=GeometryScriptSettings)
bpy.app.timers.register(auto_resolve, persistent=True)
def unregister():
bpy.utils.unregister_class(TEXT_MT_templates_geometryscript)
bpy.types.TEXT_MT_templates.remove(templates_menu_draw)
bpy.utils.unregister_class(GeometryScriptSettings)
bpy.utils.unregister_class(GeometryScriptPreferences)
bpy.utils.unregister_class(OpenDocumentation)
bpy.utils.unregister_class(GeometryScriptMenu)
bpy.types.TEXT_HT_header.remove(editor_header_draw)
try:
bpy.app.timers.unregister(auto_resolve)
except:
pass