-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnewProject.py
177 lines (132 loc) · 4.94 KB
/
newProject.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import sys
import importlib
import bpy
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty
import pygit2 as git
# Local imports implemented to support Blender refreshes
modulesNames = ("gitHelpers", "reports")
for module in modulesNames:
if module in sys.modules:
importlib.reload(sys.modules[module])
else:
parent = ".".join(__name__.split(".")[:-1])
globals()[module] = importlib.import_module(f"{parent}.{module}")
NEW_PROJECT_ICON = 'NEWFOLDER'
class BlenditNewProject(bpy.types.Operator, ExportHelper):
"""Create a Blendit project."""
bl_label = "Create New Project"
bl_idname = "blendit.new_project"
# ExportHelper mixin class uses this
filename_ext = ""
filter_glob: StringProperty(
options={'HIDDEN'},
# Max internal buffer length, longer would be clamped.
maxlen=255,
)
filepath: StringProperty(
default="/",
options={'HIDDEN'},
subtype='DIR_PATH'
)
filename: StringProperty(
name="Name",
description="Name of the project",
options={'TEXTEDIT_UPDATE'},
subtype='FILE_NAME'
)
location: StringProperty(
name="Location",
description="Location of the project"
)
# Get global/default git config if .gitconfig or .git/config exists
try:
defaultConfig = git.Config.get_global_config()
except OSError:
defaultConfig = {}
defaultUser = (defaultConfig["user.name"]
if "user.name" in defaultConfig else "Artist")
defaultEmail = (defaultConfig["user.email"]
if "user.email" in defaultConfig else "artist@example.com")
username: StringProperty(
name="User",
default=defaultUser,
description="Username of the artist",
options={'TEXTEDIT_UPDATE'},
)
email: StringProperty(
name="Email",
default=defaultEmail,
description="Email of the artist",
options={'TEXTEDIT_UPDATE'},
)
def draw(self, context):
layout = self.layout.box()
layout.label(text="Create New Project", icon=NEW_PROJECT_ICON)
if not self.filename.strip():
layout.label(text="Name cannot be empty.", icon="ERROR")
row = layout.row()
row.enabled = False
row.prop(self, "filename")
if not self.filepath.strip():
layout.label(text="Location cannot be empty.", icon="ERROR")
self.location = self.filepath
row = layout.row()
row.enabled = False
row.prop(self, "location")
if not self.username.strip():
layout.label(text="Username cannot be empty.", icon="ERROR")
layout.prop(self, "username")
if not self.email.strip():
layout.label(text="Email cannot be empty.", icon="ERROR")
layout.prop(self, "email")
def execute(self, context):
filename = self.filename.strip()
filepath = self.filepath.strip()
username = self.username.strip()
email = self.email.strip()
if not filename:
self.report({'ERROR_INVALID_INPUT'}, "Name cannot be empty.")
return {'CANCELLED'}
if not filepath:
self.report({'ERROR_INVALID_INPUT'}, "Path cannot be empty.")
return {'CANCELLED'}
if not username:
self.report({'ERROR_INVALID_INPUT'}, "User cannot be empty.")
return {'CANCELLED'}
if not email:
self.report({'ERROR_INVALID_INPUT'}, "Email cannot be empty.")
return {'CANCELLED'}
print(f"Dir Path: {filepath}")
self.report({'DEBUG'}, filepath)
# Make directory
try:
os.mkdir(os.path.abspath(filepath))
except FileNotFoundError:
self.report({'ERROR_INVALID_INPUT'}, "Invalid directory path.")
return {'CANCELLED'}
# Make assets directory
os.mkdir(os.path.join(filepath, "assets"))
# Make .gitignore file
gitHelpers.makeGitIgnore(filepath)
# Init git repo
repo = git.init_repository(filepath)
# Configure git repo
gitHelpers.configUser(repo, username, email)
# Clear reports
reports.clearReports()
# Init python file
with open(os.path.join(filepath, f"{filename}.py"), "w") as file:
file.write("import bpy\n\n")
file.write("def executeCommands():\n")
file.write("\tpass\n")
# Save .blend file
bpy.ops.wm.save_mainfile(filepath=os.path.join(filepath, f"{filename}.blend"))
# Initial commit
gitHelpers.commit(repo, "Initial commit - created project")
return {'FINISHED'}
def register():
bpy.utils.register_class(BlenditNewProject)
def unregister():
bpy.utils.unregister_class(BlenditNewProject)