-
Notifications
You must be signed in to change notification settings - Fork 49
/
archipack_toolkit.py
325 lines (266 loc) · 9.86 KB
/
archipack_toolkit.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# -*- coding:utf-8 -*-
# ##### 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 #####
# <pep8 compliant>
# ----------------------------------------------------------
# Author: Stephen Leger (s-leger)
#
# ----------------------------------------------------------
import bpy
from bpy.types import Operator, PropertyGroup, Mesh, Panel
# Minimal required property types
from bpy.props import (
FloatProperty, BoolProperty, CollectionProperty
)
from mathutils import Vector
# Bmesh made easy
from .bmesh_utils import BmeshEdit as bmed
# Manipulable
from .archipack_manipulator import Manipulable
# Preset system
from .archipack_preset import ArchipackPreset, PresetMenuOperator
# Base for Propertygroup and create tool Operator
from .archipack_object import ArchipackCreateTool, ArchipackObject
def update(self, context):
self.update(context)
class archipack_myobject(ArchipackObject, Manipulable, PropertyGroup):
""" Archipack toolkit sample"""
x = FloatProperty(
name="Width",
default=2.0, min=0.01,
unit='LENGTH', subtype='DISTANCE',
update=update
)
y = FloatProperty(
name="Depth",
default=2.0, min=0.01,
unit='LENGTH', subtype='DISTANCE',
update=update
)
z = FloatProperty(
name="Height",
default=2.0, min=0.01,
unit='LENGTH', subtype='DISTANCE',
update=update
)
auto_update = BoolProperty(
# Wont save auto_update state in any case
options={'SKIP_SAVE'},
default=True,
update=update
)
@property
def verts(self):
"""
Object vertices coords
"""
x = 0.5 * self.x
y = 0.5 * self.y
z = self.z
return [
(-x, y, 0),
(-x, -y, 0),
(x, -y, 0),
(x, y, 0),
(-x, y, z),
(-x, -y, z),
(x, -y, z),
(x, y, z)
]
@property
def faces(self):
"""
Object faces vertices index
"""
return [
(0, 1, 2, 3),
(7, 6, 5, 4),
(7, 4, 0, 3),
(4, 5, 1, 0),
(5, 6, 2, 1),
(6, 7, 3, 2)
]
@property
def uvs(self):
"""
Object faces uv coords
"""
return [
[(0, 0), (0, 1), (1, 1), (1, 0)],
[(0, 0), (0, 1), (1, 1), (1, 0)],
[(0, 0), (0, 1), (1, 1), (1, 0)],
[(0, 0), (0, 1), (1, 1), (1, 0)],
[(0, 0), (0, 1), (1, 1), (1, 0)],
[(0, 0), (0, 1), (1, 1), (1, 0)]
]
@property
def matids(self):
"""
Object material indexes for each face
"""
return [0, 0, 0, 0, 0, 0]
def setup_manipulators(self):
if len(self.manipulators) < 1:
# add manipulator for x property
s = self.manipulators.add()
s.prop1_name = "x"
s.type_key = 'SIZE'
# add manipulator for y property
s = self.manipulators.add()
s.prop1_name = "y"
s.type_key = 'SIZE'
# add manipulator for z property
s = self.manipulators.add()
s.prop1_name = "z"
s.type_key = 'SIZE'
# draw this one on xz plane
s.normal = Vector((0, 1, 0))
def update(self, context):
# provide support for "copy to selected"
o = self.find_in_selection(context, self.auto_update)
if o is None:
return
# dynamically create manipulators when needed
self.setup_manipulators()
# update your mesh from parameters
bmed.buildmesh(context,
o,
self.verts,
self.faces,
matids=self.matids,
uvs=self.uvs,
weld=False)
# update manipulators location (3d location in object coordsystem)
x, y = 0.5 * self.x, 0.5 * self.y
self.manipulators[0].set_pts([(-x, -y, 0), (x, -y, 0), (1, 0, 0)])
self.manipulators[1].set_pts([(-x, -y, 0), (-x, y, 0), (-1, 0, 0)])
self.manipulators[2].set_pts([(x, -y, 0), (x, -y, self.z), (-1, 0, 0)])
# always restore context
self.restore_context(context)
class ARCHIPACK_PT_myobject(Panel):
bl_idname = "ARCHIPACK_PT_myobject"
bl_label = "MyObject"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'ArchiPack'
@classmethod
def poll(cls, context):
# ensure your object panel only show when active object is the right one
return archipack_myobject.filter(context.active_object)
def draw(self, context):
o = context.active_object
if not archipack_myobject.filter(o):
return
layout = self.layout
# retrieve datablock of your object
props = archipack_myobject.datablock(o)
# Manipulate mode operator
layout.operator('archipack.myobject_manipulate', icon='HAND')
box = layout.box()
row = box.row(align=True)
# Presets operators
row.operator("archipack.myobject_preset_menu",
text=bpy.types.ARCHIPACK_OT_myobject_preset_menu.bl_label)
row.operator("archipack.myobject_preset",
text="",
icon='ZOOMIN')
row.operator("archipack.myobject_preset",
text="",
icon='ZOOMOUT').remove_active = True
row = layout.row()
box = row.box()
box.label(text="Size")
box.prop(props, 'x')
box.prop(props, 'y')
box.prop(props, 'z')
class ARCHIPACK_OT_myobject(ArchipackCreateTool, Operator):
bl_idname = "archipack.myobject"
bl_label = "Myobject"
bl_description = "Create Myobject"
bl_category = 'Archipack'
bl_options = {'REGISTER', 'UNDO'}
def create(self, context):
# Create an empty mesh datablock
m = bpy.data.meshes.new("Myobject")
# Create an object using the mesh datablock
o = bpy.data.objects.new("Myobject", m)
# Add your properties on mesh datablock
d = m.archipack_myobject.add()
# Link object into scene
context.scene.objects.link(o)
# select and make active
o.select = True
context.scene.objects.active = o
# Load preset into datablock
self.load_preset(d)
# add a material
self.add_material(o)
return o
def execute(self, context):
if context.mode == "OBJECT":
bpy.ops.object.select_all(action="DESELECT")
o = self.create(context)
o.location = bpy.context.scene.cursor_location
o.select = True
context.scene.objects.active = o
# Start manipulate mode
self.manipulate()
return {'FINISHED'}
else:
self.report({'WARNING'}, "Archipack: Option only valid in Object mode")
return {'CANCELLED'}
class ARCHIPACK_OT_myobject_preset_menu(PresetMenuOperator, Operator):
bl_idname = "archipack.myobject_preset_menu"
bl_label = "Myobject preset"
preset_subdir = "archipack_myobject"
class ARCHIPACK_OT_myobject_preset(ArchipackPreset, Operator):
"""Add a Myobject Preset"""
bl_idname = "archipack.myobject_preset"
bl_label = "Add Myobject preset"
preset_menu = "ARCHIPACK_OT_myobject_preset_menu"
@property
def blacklist(self):
return ['manipulators']
class ARCHIPACK_OT_myobject_manipulate(Operator):
bl_idname = "archipack.myobject_manipulate"
bl_label = "Manipulate"
bl_description = "Manipulate"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(self, context):
return archipack_myobject.filter(context.active_object)
def invoke(self, context, event):
d = archipack_myobject.datablock(context.active_object)
d.manipulable_invoke(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(archipack_myobject)
Mesh.archipack_myobject = CollectionProperty(type=archipack_myobject)
bpy.utils.register_class(ARCHIPACK_PT_myobject)
bpy.utils.register_class(ARCHIPACK_OT_myobject)
bpy.utils.register_class(ARCHIPACK_OT_myobject_preset_menu)
bpy.utils.register_class(ARCHIPACK_OT_myobject_preset)
bpy.utils.register_class(ARCHIPACK_OT_myobject_manipulate)
def unregister():
bpy.utils.unregister_class(archipack_myobject)
del Mesh.archipack_myobject
bpy.utils.unregister_class(ARCHIPACK_PT_myobject)
bpy.utils.unregister_class(ARCHIPACK_OT_myobject)
bpy.utils.unregister_class(ARCHIPACK_OT_myobject_preset_menu)
bpy.utils.unregister_class(ARCHIPACK_OT_myobject_preset)
bpy.utils.unregister_class(ARCHIPACK_OT_myobject_manipulate)