1
'''# ##### BEGIN GPL LICENSE BLOCK #####
3
# This program is free software; you can redistribute it and/or
4
# modify it under the terms of the GNU General Public License
5
# as published by the Free Software Foundation; either version 2
6
# of the License, or (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software Foundation,
15
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
# ##### END GPL LICENSE BLOCK #####
22
"author": "Carbonic Wolf",
26
"location": "View3D > Add > Curve",
27
"description": "Add rectangle",
31
"category": "Add Curve"}
34
##------------------------------------------------------------
37
from bpy.props import *
38
from mathutils import *
41
##------------------------------------------------------------
42
# calculates the matrix for the new object
43
# depending on user pref
44
def align_matrix(context):
45
loc = Matrix.Translation(context.scene.cursor_location)
46
obj_align = context.user_preferences.edit.object_align
47
if (context.space_data.type == 'VIEW_3D'
48
and obj_align == 'VIEW'):
49
rot = context.space_data.region_3d.view_matrix.rotation_part().invert().resize4x4()
52
align_matrix = loc * rot
55
##------------------------------------------------------------
56
#### Curve creation functions
57
# sets bezierhandles to auto
58
def setBezierHandles(obj, mode = 'AUTOMATIC'):
59
scene = bpy.context.scene
60
if obj.type != 'CURVE':
62
scene.objects.active = obj
63
bpy.ops.object.mode_set(mode='EDIT', toggle=True)
64
bpy.ops.curve.select_all(action='SELECT')
65
bpy.ops.curve.handle_type_set(type=mode)
66
bpy.ops.object.mode_set(mode='OBJECT', toggle=True)
68
##------------------------------------------------------------
69
#### Curve creation functions
71
# get array of vertcoordinates acording to splinetype
72
def vertsToPoints(Verts):
80
# create new CurveObject from vertarray and splineType
81
def createCurve(vertArray, props, align_matrix):
87
scene = bpy.context.scene
88
newCurve = bpy.data.curves.new(name, type = 'CURVE') # curvedatablock
89
newSpline = newCurve.splines.new(type = splineType) # spline
91
# create spline from vertarray
92
newSpline.bezier_points.add(int(len(vertArray)*0.33))
93
newSpline.bezier_points.foreach_set('co', vertArray)
96
newCurve.dimensions = '2D'
97
newSpline.use_cyclic_u = True
98
newSpline.use_endpoint_u = True
101
# create object with newCurve
102
new_obj = bpy.data.objects.new(name, newCurve) # object
103
scene.objects.link(new_obj) # place in active scene
104
new_obj.select = True # set as selected
105
scene.objects.active = new_obj # set as active
106
new_obj.matrix_world = align_matrix # apply matrix
108
setBezierHandles(new_obj, 'VECTOR')
112
########################################################################
113
####################### Definitions ###################################
114
########################################################################
117
def rectangle_Curve(rectangle_w=2, rectangle_l=2, rectangle_r=1):
124
newPoints.append([-x+r,y,0])
125
newPoints.append([x-r,y,0])
126
newPoints.append([x,y-r,0])
127
newPoints.append([x,-y+r,0])
128
newPoints.append([x-r,-y,0])
129
newPoints.append([-x+r,-y,0])
130
newPoints.append([-x,-y+r,0])
131
newPoints.append([-x,y-r,0])
133
newPoints.append([-x,y,0])
134
newPoints.append([x,y,0])
135
newPoints.append([x,-y,0])
136
newPoints.append([-x,-y,0])
140
##------------------------------------------------------------
142
def main(context, props, align_matrix):
143
# deselect all objects
144
bpy.ops.object.select_all(action='DESELECT')
147
verts = rectangle_Curve(props.rectangle_w,
151
# turn verts into array
152
vertArray = vertsToPoints(verts)
155
createCurve(vertArray, props, align_matrix)
159
class rectangle(bpy.types.Operator):
161
bl_idname = "curve.rectangle"
162
bl_label = "Rectangle"
163
bl_options = {'REGISTER', 'UNDO'}
164
bl_description = "adds rectangle"
166
# align_matrix for the invoke
167
align_matrix = Matrix()
170
rectangle_w = FloatProperty(name="Width",
174
rectangle_l = FloatProperty(name="Length",
177
description="Length")
178
rectangle_r = FloatProperty(name="Rounded",
181
description="Rounded")
184
def draw(self, context):
185
props = self.properties
189
col = layout.column()
190
#col.prop(props, 'rectangle')
191
col.label(text="Rectangle Parameters")
195
box.prop(props, 'rectangle_w')
196
box.prop(props, 'rectangle_l')
197
box.prop(props, 'rectangle_r')
201
def poll(cls, context):
202
return context.scene != None
205
def execute(self, context):
207
undo = bpy.context.user_preferences.edit.use_global_undo
208
bpy.context.user_preferences.edit.use_global_undo = False
210
props = self.properties
213
main(context, props, self.align_matrix)
215
# restore pre operator undo state
216
bpy.context.user_preferences.edit.use_global_undo = undo
221
def invoke(self, context, event):
222
# store creation_matrix
223
self.align_matrix = align_matrix(context)
224
self.execute(context)
228
################################################################################
231
def rectangle_button(self, context):
232
self.layout.operator(rectangle.bl_idname, text="Rectangle", icon="PLUGIN")
236
bpy.utils.register_module(__name__)
237
bpy.types.INFO_MT_curve_add.append(rectangle_button)
240
bpy.utils.unregister_module(__name__)
241
bpy.types.INFO_MT_curve_add.remove(rectangle_button)
243
if __name__ == "__main__":