-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoperators.py
More file actions
executable file
·279 lines (212 loc) · 9.32 KB
/
operators.py
File metadata and controls
executable file
·279 lines (212 loc) · 9.32 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# BEGIN GPL LICENSE BLOCK #####
#
# 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 2
# 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
# MERCHANTABILITY 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# END GPL LICENSE BLOCK #####
import bpy
import mathutils
from math import radians
from os.path import join, splitext
from .functions import *
from .database import *
class AddAssetToDatabase(bpy.types.Operator):
"""Add the current group to the database"""
bl_idname = "ropy.add_asset_to_database"
bl_label = "Add to database"
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def execute(self, context):
dbPath = context.user_preferences.addons[__package__].preferences.dbPath
curFile = bpy.data.filepath
if not dbPath:
self.report({'ERROR'},'Please set up the database path in the user preferences')
return {'FINISHED'}
if not curFile:
self.report({'ERROR'},'You have to save your file first !')
return {'FINISHED'}
relFilePath = get_relative_file_path(dbPath,curFile)
groupName = context.scene.build_props.current_groups_in_files
if is_group_in_database(dbPath,groupName,relFilePath):
self.report({'ERROR'},'Asset is already in database')
return {'FINISHED'}
catId = context.scene.build_props.assets_categories
groupDimX= get_group_dimension_x(context,groupName)
groupOffsetX= get_group_min_x_offset(context,groupName)
if is_groupName_used_in_other_file(dbPath,groupName,relFilePath):
#TODO Make sure it's the biggestName in current scene too
biggestName = get_biggest_groupName(dbPath,groupName)
newGroupName = set_new_groupName(biggestName)
#we have to rename the current group with its new name
bpy.data.groups[groupName].name = newGroupName
status, message = add_new_asset(dbPath,catId,newGroupName,relFilePath,groupDimX,groupOffsetX)
status = {'WARNING'}
else:
status, message = add_new_asset(dbPath,catId,groupName,relFilePath,groupDimX,groupOffsetX)
self.report(status,message)
return {'FINISHED'}
class UpdateAssetInDatabase(bpy.types.Operator):
"""Update the current group in the database"""
bl_idname = "ropy.update_asset_to_database"
bl_label = "Update in database"
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def execute(self, context):
dbPath = context.user_preferences.addons[__package__].preferences.dbPath
groupName = context.scene.build_props.current_groups_in_files
catId = context.scene.build_props.assets_categories
groupDimX= get_group_dimension_x(context,groupName)
groupOffsetX= get_group_min_x_offset(context,groupName)
status, message = update_asset(dbPath,catId,groupName,groupDimX,groupOffsetX)
self.report(status,message)
return {'FINISHED'}
class LinkGroupsToFile(bpy.types.Operator):
"""Link all the groups from the current category to the blender file"""
bl_idname = "ropy.link_groups_to_file"
bl_label = "Link category to file"
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def execute(self, context):
link_category_to_file(context)
return {'FINISHED'}
class AddCategoryToDatabase(bpy.types.Operator):
"""Add a new asset category into the database"""
bl_idname = "ropy.add_cat_to_database"
bl_label = "Create a new category"
cat_name = bpy.props.StringProperty(name="cat_name")
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def invoke(self,context,event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
def execute(self, context):
db_path = context.user_preferences.addons[__package__].preferences.dbPath
status, message = add_new_category(db_path,self.cat_name)
self.report(status,message)
return {'FINISHED'}
def draw(self,context):
layout = self.layout
row = layout.row()
row.prop(self,"cat_name")
class InitDatabase(bpy.types.Operator):
"""Initialize the database"""
bl_idname = "ropy.init_database"
bl_label = "Initialize Database"
dbPath = bpy.props.StringProperty(
name="Database Path",
subtype='FILE_PATH')
dbName = bpy.props.StringProperty(
name="Database Name",
subtype='FILE_NAME',
default='assets')
setAsDefault = bpy.props.BoolProperty(name='set_default',default = False)
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def invoke(self,context,event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
def execute(self, context):
relPath = join(self.dbPath,splitext(self.dbName)[0]+'.db')
absPath= bpy.path.abspath(relPath)
status, message = init_assets_database(absPath)
self.report(status,message)
if status is not {'ERROR'}:
if self.setAsDefault:
context.user_preferences.addons[__package__].preferences.dbPath = absPath
return {'FINISHED'}
def draw(self,context):
layout = self.layout
row = layout.row()
row.prop(self,"dbPath")
row = layout.row()
row.prop(self,"dbName")
row = layout.row()
row.prop(self,"setAsDefault",text="Use the database as default database")
class ExportScene(bpy.types.Operator):
"""Export the current scene"""
bl_idname = "ropy.export_scene"
bl_label = "Export Scene"
use_selection = bpy.props.BoolProperty(name='use_selection',default = False)
only_meshes = bpy.props.BoolProperty(name="only_meshes",default = False)
export_instances = bpy.props.BoolProperty(name="export_instances", default = True)
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
def invoke(self,context,event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
def execute(self, context):
instancePrefix = context.user_preferences.addons[__package__].preferences.instancePrefix
for obj in context.scene.objects:
if obj.type == 'EMPTY' and obj.name.startswith(instancePrefix):
obj.dupli_type = 'NONE'
exportPath = context.scene.build_props.export_path
file_name = bpy.path.display_name_from_filepath(bpy.context.blend_data.filepath)
exportPath = join(exportPath,file_name+'.fbx') #TODO make sure we use absolute path
obj_types = {'EMPTY','MESH'}
if not self.only_meshes:
obj_types = {'EMPTY','CAMERA','LAMP','ARMATURE','MESH'}
bpy.ops.export_scene.fbx(filepath=exportPath,use_selection=self.use_selection,object_types= obj_types)
if self.export_instances:
selected_ojbects = context.selected_objects
for obj in context.scene.objects:
obj.select = False
for group in bpy.data.groups:
if group.users > 0:
#we have to export this group
o = add_prop_instance(context,group.name)
o.select = True
exportPath = context.scene.build_props.export_path
exportPath = join(exportPath,extract_groupName(o)+".fbx")
bpy.ops.export_scene.fbx(filepath=exportPath,use_selection=True)
bpy.data.objects.remove(o, True)
for obj in context.scene.objects:
if obj.type == 'EMPTY' and obj.name.startswith(instancePrefix):
obj.dupli_type = 'GROUP'
return {'FINISHED'}
def draw(self,context):
layout = self.layout
row = layout.row()
row.prop(self,"use_selection",text="Export only selected objects")
row = layout.row()
row.prop(self,"only_meshes",text="Export only meshes and empties")
row = layout.row()
row.prop(self,"export_instances",text="Export instances as separate files")
class GenerateRoomOperator(bpy.types.Operator):
"""Generate walls and roof from selection"""
bl_idname = "edit.generate_room"
bl_label = "Generate Room"
bl_options = {'REGISTER', 'UNDO'}
height = bpy.props.FloatProperty(
name="Height",
description="Box Height",
min=0.01,
max=100.0,
default=1.0,
)
@classmethod
def poll(cls, context):
return context.mode == 'EDIT_MESH'
def draw(self,context):
layout = self.layout
row = layout.row()
row.prop(self, "height")
def execute(self, context):
generate_room(context,self.height)
return {'FINISHED'}