~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to release/scripts/addons_contrib/add_curve_objects/add_curve_rectangle_259.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-05-12 20:02:22 UTC
  • mfrom: (14.2.16 sid)
  • Revision ID: package-import@ubuntu.com-20120512200222-lznjs2cxzaq96wua
Tags: 2.63a-1
* New upstream bugfix release
  + debian/patches/: re-worked since source code changed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'''# ##### BEGIN GPL LICENSE BLOCK #####
2
 
#
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.
7
 
#
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.
12
 
#
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.
16
 
#
17
 
# ##### END GPL LICENSE BLOCK #####
18
 
 
19
 
 
20
 
bl_info = {
21
 
    "name": "Rectangle",
22
 
    "author": "Carbonic Wolf",
23
 
    "version": (1,1),
24
 
    "blender": (2, 5, 9),
25
 
    "api": 39933,
26
 
    "location": "View3D > Add > Curve",
27
 
    "description": "Add rectangle",
28
 
    "warning": "",
29
 
    "wiki_url": "",
30
 
    "tracker_url": "",
31
 
    "category": "Add Curve"}
32
 
    
33
 
 '''   
34
 
##------------------------------------------------------------
35
 
#### import modules
36
 
import bpy
37
 
from bpy.props import *
38
 
from mathutils import *
39
 
from math import *
40
 
 
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()
50
 
    else:
51
 
        rot = Matrix()
52
 
    align_matrix = loc * rot
53
 
    return align_matrix
54
 
 
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':
61
 
        return
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)
67
 
 
68
 
##------------------------------------------------------------
69
 
#### Curve creation functions
70
 
 
71
 
# get array of vertcoordinates acording to splinetype
72
 
def vertsToPoints(Verts):
73
 
    vertArray = []
74
 
 
75
 
    for v in Verts:
76
 
        vertArray += v
77
 
 
78
 
    return vertArray
79
 
 
80
 
# create new CurveObject from vertarray and splineType
81
 
def createCurve(vertArray, props, align_matrix):
82
 
    # options to vars
83
 
    splineType = 'BEZIER'
84
 
    name = 'rectangle'
85
 
 
86
 
    # create curve
87
 
    scene = bpy.context.scene
88
 
    newCurve = bpy.data.curves.new(name, type = 'CURVE') # curvedatablock
89
 
    newSpline = newCurve.splines.new(type = splineType)  # spline
90
 
 
91
 
    # create spline from vertarray
92
 
    newSpline.bezier_points.add(int(len(vertArray)*0.33))
93
 
    newSpline.bezier_points.foreach_set('co', vertArray)
94
 
 
95
 
    # Curve settings
96
 
    newCurve.dimensions = '2D'
97
 
    newSpline.use_cyclic_u = True
98
 
    newSpline.use_endpoint_u = True
99
 
    newSpline.order_u = 4
100
 
 
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
107
 
 
108
 
    setBezierHandles(new_obj, 'VECTOR')
109
 
 
110
 
    return
111
 
 
112
 
########################################################################
113
 
#######################  Definitions ###################################
114
 
########################################################################
115
 
 
116
 
#### rectangle
117
 
def rectangle_Curve(rectangle_w=2, rectangle_l=2, rectangle_r=1):
118
 
    newPoints = []
119
 
    x=rectangle_w/2
120
 
    y=rectangle_l/2
121
 
    r=rectangle_r/2
122
 
    
123
 
    if r>0:
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])
132
 
    else:
133
 
       newPoints.append([-x,y,0])
134
 
       newPoints.append([x,y,0])
135
 
       newPoints.append([x,-y,0])
136
 
       newPoints.append([-x,-y,0])
137
 
 
138
 
    return newPoints
139
 
 
140
 
##------------------------------------------------------------
141
 
# Main Function
142
 
def main(context, props, align_matrix):
143
 
    # deselect all objects
144
 
    bpy.ops.object.select_all(action='DESELECT')
145
 
 
146
 
    # get verts
147
 
    verts = rectangle_Curve(props.rectangle_w,
148
 
                            props.rectangle_l,
149
 
                            props.rectangle_r)
150
 
 
151
 
    # turn verts into array
152
 
    vertArray = vertsToPoints(verts)
153
 
 
154
 
    # create object
155
 
    createCurve(vertArray, props, align_matrix)
156
 
 
157
 
    return
158
 
 
159
 
class rectangle(bpy.types.Operator):
160
 
    ''''''
161
 
    bl_idname = "curve.rectangle"
162
 
    bl_label = "Rectangle"
163
 
    bl_options = {'REGISTER', 'UNDO'}
164
 
    bl_description = "adds rectangle"
165
 
 
166
 
    # align_matrix for the invoke
167
 
    align_matrix = Matrix()
168
 
 
169
 
    #### Parameters
170
 
    rectangle_w = FloatProperty(name="Width",
171
 
                default=2,
172
 
                min=0, soft_min=0,
173
 
                description="Width")
174
 
    rectangle_l = FloatProperty(name="Length",
175
 
                default=2,
176
 
                min=0, soft_min=0,
177
 
                description="Length")
178
 
    rectangle_r = FloatProperty(name="Rounded",
179
 
                default=1,
180
 
                min=0, soft_min=0,
181
 
                description="Rounded")
182
 
 
183
 
    ##### DRAW #####
184
 
    def draw(self, context):
185
 
        props = self.properties
186
 
        layout = self.layout
187
 
 
188
 
        # general options        
189
 
        col = layout.column()
190
 
        #col.prop(props, 'rectangle')
191
 
        col.label(text="Rectangle Parameters")
192
 
 
193
 
        # Parameters 
194
 
        box = layout.box()
195
 
        box.prop(props, 'rectangle_w')
196
 
        box.prop(props, 'rectangle_l')
197
 
        box.prop(props, 'rectangle_r')
198
 
 
199
 
    ##### POLL #####
200
 
    @classmethod
201
 
    def poll(cls, context):
202
 
        return context.scene != None
203
 
 
204
 
    ##### EXECUTE #####
205
 
    def execute(self, context):
206
 
        # turn off undo
207
 
        undo = bpy.context.user_preferences.edit.use_global_undo
208
 
        bpy.context.user_preferences.edit.use_global_undo = False
209
 
 
210
 
        props = self.properties
211
 
        
212
 
        # main function
213
 
        main(context, props, self.align_matrix)
214
 
        
215
 
        # restore pre operator undo state
216
 
        bpy.context.user_preferences.edit.use_global_undo = undo
217
 
 
218
 
        return {'FINISHED'}
219
 
 
220
 
    ##### INVOKE #####
221
 
    def invoke(self, context, event):
222
 
        # store creation_matrix
223
 
        self.align_matrix = align_matrix(context)
224
 
        self.execute(context)
225
 
 
226
 
        return {'FINISHED'}
227
 
 
228
 
################################################################################
229
 
##### REGISTER #####
230
 
 
231
 
def rectangle_button(self, context):
232
 
    self.layout.operator(rectangle.bl_idname, text="Rectangle", icon="PLUGIN")
233
 
 
234
 
 
235
 
def register():
236
 
    bpy.utils.register_module(__name__)
237
 
    bpy.types.INFO_MT_curve_add.append(rectangle_button)
238
 
 
239
 
def unregister():
240
 
    bpy.utils.unregister_module(__name__)
241
 
    bpy.types.INFO_MT_curve_add.remove(rectangle_button)
242
 
 
243
 
if __name__ == "__main__":
244
 
    register()