-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathoperators.py
228 lines (178 loc) · 6.72 KB
/
operators.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import os
import subprocess
import bpy
from bpy_extras.io_utils import ExportHelper
import pman
def update_blender_path():
startdir = os.path.dirname(bpy.data.filepath) if bpy.data.filepath else None
if pman.config_exists(startdir):
user_config = pman.get_user_config(startdir)
if 'blender' not in user_config:
user_config['blender'] = {
'use_last_path': True
}
user_config['blender']['last_path'] = bpy.app.binary_path
pman.write_user_config(user_config)
class ExportBam(bpy.types.Operator, ExportHelper):
"""Export to Panda3D's BAM file format"""
bl_idname = 'panda_engine.export_bam'
bl_label = 'Export BAM'
# copy_images = bpy.props.BoolProperty(
# default=True,
# )
skip_up_to_date = bpy.props.BoolProperty(
default=False,
)
# For ExportHelper
filename_ext = '.bam'
filter_glob = bpy.props.StringProperty(
default='*.bam',
options={'HIDDEN'},
)
def execute(self, _context):
filedir = os.path.dirname(bpy.data.filepath) if bpy.data.filepath else os.path.dirname(self.filepath)
try:
config = pman.get_config(filedir)
except pman.NoConfigError as err:
config = None
if config:
user_config = pman.get_user_config(config['internal']['projectdir'])
else:
user_config = None
try:
pycmd = pman.get_python_program(config)
except pman.CouldNotFindPythonError as err:
self.report({'ERROR'}, str(err))
return {'CANCELLED'}
use_legacy_mats = (
config is None or
config['general']['material_mode'] == 'legacy'
)
material_mode = 'legacy' if use_legacy_mats else 'pbr'
# Check if we need to convert the file
try:
if self.skip_up_to_date and os.stat(bpy.data.filepath).st_mtime <= os.stat(self.filepath).st_mtime:
print('"{}" is already up-to-date, skipping'.format(self.filepath))
return {'FINISHED'}
except FileNotFoundError:
# The file doesn't exist, so we cannot skip conversion
pass
# Create a temporary blend file to convert
tmpfname = os.path.join(filedir, '__bp_temp__.blend')
bpy.ops.wm.save_as_mainfile(filepath=tmpfname, copy=True)
# Now convert the data to bam
blend2bam_args = [
'--blender-dir', os.path.dirname(bpy.app.binary_path),
'--material-mode', material_mode,
]
blend2bam_args += [
tmpfname,
self.filepath
]
retval = {'FINISHED'}
try:
if user_config is not None and user_config['python']['in_venv']:
# Use blend2bam from venv
pman.run_program(config, ['blend2bam'] + blend2bam_args)
else:
# Use bundled blend2bam
scriptloc = os.path.join(
os.path.dirname(__file__),
'blend2bam_wrapper.py'
)
args = [
pycmd,
scriptloc
] + blend2bam_args
if subprocess.call(args) != 0:
retval = {'CANCELLED'}
finally:
# Remove the temporary blend file
os.remove(tmpfname)
return retval
class CreateProject(bpy.types.Operator):
"""Setup a new project directory"""
bl_idname = 'panda_engine.create_project'
bl_label = 'Create New Project'
directory = bpy.props.StringProperty(
name='Project Directory',
subtype='DIR_PATH',
)
switch_dir = bpy.props.BoolProperty(
name='Switch to directory',
default=True,
)
def execute(self, _context):
pman.create_project(self.directory)
config = pman.get_config(self.directory)
user_config = pman.get_user_config(self.directory)
from pman import hooks # pylint:disable=no-name-in-module
hooks.create_blender(self.directory, config, user_config)
if self.switch_dir:
os.chdir(self.directory)
update_blender_path()
return {'FINISHED'}
def invoke(self, context, _event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def draw(self, _context):
layout = self.layout
layout.prop(self, 'switch_dir')
class UpdateProject(bpy.types.Operator):
"""Re-copies any missing project files"""
bl_idname = 'panda_engine.update_project'
bl_label = 'Update Project Files'
def execute(self, _context):
try:
config = pman.get_config(os.path.dirname(bpy.data.filepath) if bpy.data.filepath else None)
pman.create_project(config['internal']['projectdir'], ['blender'])
return {'FINISHED'}
except pman.PManException as err:
self.report({'ERROR'}, str(err))
return {'CANCELLED'}
class SwitchProject(bpy.types.Operator):
"""Switch to an existing project directory"""
bl_idname = 'panda_engine.switch_project'
bl_label = 'Switch Project'
directory = bpy.props.StringProperty(
name='Project Directory',
subtype='DIR_PATH',
)
def execute(self, _context):
os.chdir(self.directory)
return {'FINISHED'}
def invoke(self, context, _event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
class BuildProject(bpy.types.Operator):
"""Build the current project"""
bl_idname = 'panda_engine.build_project'
bl_label = 'Build Project'
def execute(self, _context):
try:
config = pman.get_config(os.path.dirname(bpy.data.filepath) if bpy.data.filepath else None)
pman.build(config)
return {'FINISHED'}
except pman.PManException as err:
self.report({'ERROR'}, str(err))
return {'CANCELLED'}
class RunProject(bpy.types.Operator):
"""Run the current project"""
bl_idname = 'panda_engine.run_project'
bl_label = 'Run Project'
def execute(self, _context):
try:
config = pman.get_config(os.path.dirname(bpy.data.filepath) if bpy.data.filepath else None)
if config['run']['auto_save']:
bpy.ops.wm.save_mainfile()
pman.run(config)
return {'FINISHED'}
except pman.PManException as err:
self.report({'ERROR'}, str(err))
return {'CANCELLED'}
def menu_func_export(self, _context):
self.layout.operator(ExportBam.bl_idname, text="Panda3D (.bam)")
def register():
bpy.types.INFO_MT_file_export.append(menu_func_export)
def unregister():
bpy.types.INFO_MT_file_export.remove(menu_func_export)