3333import sys
3434import addon_utils
3535
36- # install addon
3736
38- print ("start installing mastodon blender view addon..." )
39- argv = sys .argv
40- addon_zip = argv [argv .index ('--' ) + 1 ]
41- bpy .ops .preferences .addon_install (filepath = addon_zip )
42- print ("mastodon blender view addon installed" )
37+ def main ():
38+ print ("start installing mastodon blender view addon..." )
39+ install_mastodon_blender_view_addon ()
40+ print ("mastodon blender view addon installed" )
41+ print ("install pip if needed..." )
42+ install_pip ()
43+ print ("pip is installed" )
44+ addon_dir = get_mastodon_blender_view_addon_dir ()
45+ print (f"Installation directory of the Mastodon Blender View addon: { addon_dir } " )
46+ install_dependencies (packages = ['grpcio' , 'bidict' , 'grpcio-tools' , 'pandas' ], target = os .path .join (addon_dir , "libs" ))
47+ import_dependencies ()
48+ print ("dependencies installed" )
49+ compile_grpc_protocol (addon_dir )
50+ print ("google RPC code compiled" )
4351
44- # install pip
4552
46- os .environ .pop ("PIP_REQ_TRACKER" , None )
47- ensurepip .bootstrap ()
48- os .environ .pop ("PIP_REQ_TRACKER" , None )
53+ def install_mastodon_blender_view_addon ():
54+ argv = sys .argv
55+ addon_zip = argv [argv .index ('--' ) + 1 ]
56+ bpy .ops .preferences .addon_install (filepath = addon_zip )
4957
50- # get python path
5158
52- def get_python_path ():
53- try :
54- # 2.92 and older
55- path = bpy .app .binary_path_python
56- except AttributeError :
57- # 2.93 and later
58- path = sys .executable
59- return os .path .abspath (path )
59+ def install_pip ():
60+ os .environ .pop ("PIP_REQ_TRACKER" , None )
61+ ensurepip .bootstrap ()
62+ os .environ .pop ("PIP_REQ_TRACKER" , None )
6063
61- python_path = get_python_path ()
6264
63- print (f"Blender python binary: { python_path } " )
65+ def get_mastodon_blender_view_addon_dir () -> str :
66+ filename_init_py = [m .__file__ for m in addon_utils .modules () if m .__name__ == "mastodon_blender_view" ][0 ]
67+ addon_dir = os .path .dirname (filename_init_py )
68+ return addon_dir
6469
65- # get installation path of "mastodon_blender_view" folder
6670
67- filename_init_py = [m .__file__ for m in addon_utils .modules () if m .__name__ == "mastodon_blender_view" ][0 ]
68- addon_dir = os .path .dirname (filename_init_py )
69- print (f"Installation directory of the Blender Mastodon plugin: { addon_dir } " )
71+ def install_dependencies (packages : list [str ], target : str ):
72+ print (f"Install dependencies into folder: { target } " )
7073
71- # install dependencies
74+ subprocess . check_call ([ get_python_path (), "-m" , "pip" , " install" , "--target" , target , * packages ])
7275
73- addon_libs_dir = os .path . join ( addon_dir , " libs" )
74- print ( f"Install dependencies into folder: { addon_libs_dir } " )
76+ if target not in sys .path : # add addon libs dir to system path
77+ sys . path . insert ( 0 , target )
7578
76- packages = ['grpcio' , 'bidict' , 'grpcio-tools' , 'pandas' ]
77- subprocess .check_call ([python_path , "-m" , "pip" , "install" , "--target" , addon_libs_dir , * packages ])
7879
79- if addon_libs_dir not in sys .path : # add addon libs dir to system path
80- sys .path .insert (0 , addon_libs_dir )
80+ def import_dependencies ():
81+ import bidict
82+ import grpc
83+ import google .protobuf
84+ import pandas
8185
82- import bidict
83- import grpc
84- import google .protobuf
85- import pandas
8686
87- print ("dependencies installed" )
87+ def compile_grpc_protocol (addon_dir ):
88+ cmd = [get_python_path (), '-m' , 'grpc_tools.protoc' , '-I.' , '--python_out=.' , '--grpc_python_out=.' ,
89+ 'mastodon_blender_view/mastodon-blender-view.proto' ]
90+ cwd = os .path .dirname (addon_dir )
91+ env = os .environ .copy ()
92+ addon_libs_dir = os .path .join (addon_dir , "libs" )
93+ env ["PYTHONPATH" ] = addon_libs_dir + os .pathsep + env .get ("PYTHONPATH" ) if "PYTHONPATH" in env else addon_libs_dir
94+ subprocess .check_output (cmd , cwd = cwd , env = env )
95+
96+ try :
97+ from mastodon_blender_view import mastodon_blender_view_pb2
98+ from mastodon_blender_view import mastodon_blender_view_pb2_grpc
99+ except ModuleNotFoundError :
100+ raise Exception ("installing google RPC generated code failed" )
88101
89- # install google rpc protocol
90- cmd = [python_path , '-m' , 'grpc_tools.protoc' , '-I.' , '--python_out=.' , '--grpc_python_out=.' , 'mastodon_blender_view/mastodon-blender-view.proto' ]
91- cwd = path .dirname (addon_dir )
92- env = os .environ .copy ()
93- env ["PYTHONPATH" ] = addon_libs_dir + os .pathsep + env .get ("PYTHONPATH" ) if "PYTHONPATH" in env else addon_libs_dir
94- subprocess .check_output (cmd , cwd = os .cwd , env = env )
95102
96- try :
97- from mastodon_blender_view import mastodon_blender_view_pb2
98- from mastodon_blender_view import mastodon_blender_view_pb2_grpc
99- except ModuleNotFoundError :
100- raise Exception ("installing google RPC generated code failed" )
103+ def get_python_path ():
104+ try :
105+ # 2.92 and older
106+ path = bpy .app .binary_path_python
107+ except AttributeError :
108+ # 2.93 and later
109+ path = sys .executable
110+ return os .path .abspath (path )
111+
101112
102- print ( "google RPC code compiled" )
113+ main ( )
0 commit comments