forked from kattkieru/nodesmith
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·118 lines (83 loc) · 3.15 KB
/
main.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
## ----------------------------------------------------------------------
"""
NODESMITH
NODESMITH.PY
Command-line launcher for the nodesmith api. Can be used to generate
an entire plugin structure from an input JSON file.
Created: 12 July 2016
Author: kiki
"""
## ----------------------------------------------------------------------
import argparse
import json
import os
import re
import string
import sys
try:
from implib import reload
except:
from imp import reload
basepath = os.path.dirname( os.path.abspath(os.path.dirname(sys.argv[0])) )
#basepath = os.sep.join( [basepath,'..'] )
if not basepath in sys.path:
# print("Adding %s to path" % basepath)
sys.path.insert( 0, basepath )
## ----------------------------------------------------------------------
from nodesmith import mpxnode
from nodesmith import plugin
for mod in mpxnode, plugin:
reload(mod)
from nodesmith.plugin import Plugin
## ----------------------------------------------------------------------
print( "\n\nnodesmith.py" )
parser = argparse.ArgumentParser(
description='Generate a Maya plugin from an input JSON description.'
)
parser.add_argument( 'filename', type=argparse.FileType('r') )
parser.add_argument( '-folder', metavar='folder', type=str,
help='Output location.', default='.' )
parser.add_argument( '-force', metavar='force', type=bool,
help='Overwrites existing files.', default=False )
parser.add_argument( '-debug', metavar='debug', type=bool,
help='Enable debugging information.', default=False )
parser.add_argument( '-cmake', metavar='cmake', type=bool,
help='Create a CMakeListst.txt file with the build.', default=True )
args = parser.parse_args()
if args.filename is None:
parser.print_help()
sys.exit(0)
with args.filename as fp:
plugin_data = json.load( fp )
plugin = Plugin()
plugin.from_json( plugin_data )
print("Plugin: %s\n" % plugin.name )
print("+ Writing output to '%s'." % args.folder )
all_cpp_files = [ 'plugin_main.cpp' ]
with open(os.sep.join([args.folder, 'common.h']), 'w') as fp:
print( "\t+ Writing common.h ..." )
fp.write( plugin.generate_common_header() )
with open(os.sep.join([args.folder, 'plugin_main.cpp']), 'w') as fp:
print( "\t+ Writing plugin_main.cpp ..." )
fp.write( plugin.generate_plugin_cpp() )
for _, node in plugin.nodes.items():
print( "\t+ Writing Node: %s" % node.class_name )
header_name = '%s.h' % node.class_name
class_name = '%s.cpp' % node.class_name
main_name = '%s_main.cpp' % node.class_name
with open(os.sep.join([args.folder, header_name]), 'w') as fp:
print( "\t\t+ %s ..." % header_name )
fp.write( node.generate_include() )
with open(os.sep.join([args.folder, class_name]), 'w') as fp:
print( "\t\t+ %s ..." % class_name )
fp.write( node.generate_class() )
all_cpp_files.append( class_name )
with open(os.sep.join([args.folder, main_name]), 'w') as fp:
print( "\t\t+ %s ..." % main_name )
fp.write( node.generate_node_main() )
all_cpp_files.append( main_name )
if args.cmake:
with open(os.sep.join([args.folder, 'CMakeLists.txt']), 'w') as fp:
print( "\t+ Writing CMake project ..." )
fp.write( plugin.generate_plugin_cmake() )
print( "++ Project generation complete." )