2022-11-12 23:35:01 +00:00
# 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/>.
2022-12-02 13:32:29 +00:00
# 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__ ]
2022-11-12 23:35:01 +00:00
import bpy
import os
2022-11-13 23:49:09 +00:00
import webbrowser
2022-11-13 19:38:31 +00:00
2022-11-13 23:49:09 +00:00
from . api . tree import *
from . preferences import GeometryScriptPreferences
2022-11-14 23:31:26 +00:00
from . absolute_path import absolute_path
2022-11-12 23:35:01 +00:00
bl_info = {
" name " : " Geometry Script " ,
" author " : " Carson Katri " ,
" description " : " Python scripting for geometry nodes " ,
" blender " : ( 3 , 0 , 0 ) ,
2022-12-03 00:23:48 +00:00
" version " : ( 0 , 1 , 2 ) ,
2022-11-12 23:35:01 +00:00
" 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 " )
)
2022-11-13 23:49:09 +00:00
class OpenDocumentation ( bpy . types . Operator ) :
bl_idname = " geometry_script.open_documentation "
bl_label = " Open Documentation "
def execute ( self , context ) :
2022-11-14 23:31:26 +00:00
webbrowser . open ( ' file:// ' + absolute_path ( ' docs/documentation.html ' ) )
2022-11-13 23:49:09 +00:00
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 )
2022-11-12 23:35:01 +00:00
def templates_menu_draw ( self , context ) :
self . layout . menu ( TEXT_MT_templates_geometryscript . __name__ )
2022-11-13 23:49:09 +00:00
def editor_header_draw ( self , context ) :
self . layout . menu ( GeometryScriptMenu . bl_idname )
def auto_resolve ( ) :
if bpy . context . scene . geometry_script_settings . auto_resolve :
2022-11-14 23:31:26 +00:00
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
2022-11-13 23:49:09 +00:00
return 1
2022-11-12 23:35:01 +00:00
def register ( ) :
bpy . utils . register_class ( TEXT_MT_templates_geometryscript )
bpy . types . TEXT_MT_templates . append ( templates_menu_draw )
2022-11-13 23:49:09 +00:00
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 )
2022-11-15 03:32:32 +00:00
bpy . app . timers . register ( auto_resolve , persistent = True )
2022-11-12 23:35:01 +00:00
def unregister ( ) :
bpy . utils . unregister_class ( TEXT_MT_templates_geometryscript )
2022-11-13 23:49:09 +00:00
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 :
2022-12-02 13:59:04 +00:00
pass