Cleanup: trailing space
[blender-addons.git] / add_mesh_extra_objects / add_mesh_3d_function_surface.py
blobacd272b43eabc8a7c189f2ede5cb8db355c50531
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Original by Buerbaum Martin (Pontiac), Elod Csirmaz
5 import bpy
6 import math
7 import numpy
8 from mathutils import *
9 from math import *
10 from bpy.types import Operator
11 from bpy.props import (
12 StringProperty,
13 IntProperty,
14 FloatProperty,
15 BoolProperty,
19 # List of safe functions for eval()
20 safe_list = ['acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh',
21 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot',
22 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians',
23 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'gcd']
25 # Use the list to filter the local namespace
26 safe_dict = dict((k, globals().get(k, None)) for k in safe_list)
27 safe_dict['math'] = math
28 safe_dict['numpy'] = safe_dict['np'] = numpy
29 safe_dict['lcm'] = numpy.lcm
30 safe_dict['max'] = max
31 safe_dict['min'] = min
34 # Stores the values of a list of properties and the
35 # operator id in a property group ('recall_op') inside the object
36 # Could (in theory) be used for non-objects.
37 # Note: Replaces any existing property group with the same name!
38 # ob ... Object to store the properties in
39 # op ... The operator that should be used
40 # op_args ... A dictionary with valid Blender
41 # properties (operator arguments/parameters)
44 # Create a new mesh (object) from verts/edges/faces
45 # verts/edges/faces ... List of vertices/edges/faces for the
46 # new mesh (as used in from_pydata)
47 # name ... Name of the new mesh (& object)
49 def create_mesh_object(context, verts, edges, faces, name):
51 # Create new mesh
52 mesh = bpy.data.meshes.new(name)
54 # Make a mesh from a list of verts/edges/faces
55 mesh.from_pydata(verts, edges, faces)
57 # Update mesh geometry after adding stuff
58 mesh.update()
60 from bpy_extras import object_utils
61 return object_utils.object_data_add(context, mesh, operator=None)
64 # A very simple "bridge" tool
66 def createFaces(vertIdx1, vertIdx2, closed=False, flipped=False):
67 faces = []
69 if not vertIdx1 or not vertIdx2:
70 return None
72 if len(vertIdx1) < 2 and len(vertIdx2) < 2:
73 return None
75 fan = False
76 if (len(vertIdx1) != len(vertIdx2)):
77 if (len(vertIdx1) == 1 and len(vertIdx2) > 1):
78 fan = True
79 else:
80 return None
82 total = len(vertIdx2)
84 if closed:
85 # Bridge the start with the end
86 if flipped:
87 face = [
88 vertIdx1[0],
89 vertIdx2[0],
90 vertIdx2[total - 1]]
91 if not fan:
92 face.append(vertIdx1[total - 1])
93 faces.append(face)
95 else:
96 face = [vertIdx2[0], vertIdx1[0]]
97 if not fan:
98 face.append(vertIdx1[total - 1])
99 face.append(vertIdx2[total - 1])
100 faces.append(face)
102 # Bridge the rest of the faces
103 for num in range(total - 1):
104 if flipped:
105 if fan:
106 face = [vertIdx2[num], vertIdx1[0], vertIdx2[num + 1]]
107 else:
108 face = [vertIdx2[num], vertIdx1[num],
109 vertIdx1[num + 1], vertIdx2[num + 1]]
110 faces.append(face)
111 else:
112 if fan:
113 face = [vertIdx1[0], vertIdx2[num], vertIdx2[num + 1]]
114 else:
115 face = [vertIdx1[num], vertIdx2[num],
116 vertIdx2[num + 1], vertIdx1[num + 1]]
117 faces.append(face)
119 return faces
122 class AddZFunctionSurface(Operator):
123 bl_idname = "mesh.primitive_z_function_surface"
124 bl_label = "Add Z Function Surface"
125 bl_description = "Add a surface defined defined by a function z=f(x,y)"
126 bl_options = {'REGISTER', 'UNDO', 'PRESET'}
128 equation: StringProperty(
129 name="Z Equation",
130 description="Equation for z=f(x,y)",
131 default="1 - ( x**2 + y**2 )"
133 div_x: IntProperty(
134 name="X Subdivisions",
135 description="Number of vertices in x direction",
136 default=16,
137 min=3,
138 max=256
140 div_y: IntProperty(
141 name="Y Subdivisions",
142 description="Number of vertices in y direction",
143 default=16,
144 min=3,
145 max=256
147 size_x: FloatProperty(
148 name="X Size",
149 description="Size of the x axis",
150 default=2.0,
151 min=0.01,
152 max=100.0,
153 unit="LENGTH"
155 size_y: FloatProperty(
156 name="Y Size",
157 description="Size of the y axis",
158 default=2.0,
159 min=0.01,
160 max=100.0,
161 unit="LENGTH"
164 def execute(self, context):
165 equation = self.equation
166 div_x = self.div_x
167 div_y = self.div_y
168 size_x = self.size_x
169 size_y = self.size_y
171 verts = []
172 faces = []
174 delta_x = size_x / (div_x - 1)
175 delta_y = size_y / (div_y - 1)
176 start_x = -(size_x / 2.0)
177 start_y = -(size_y / 2.0)
179 edgeloop_prev = []
181 if equation:
182 try:
183 expr_args = (
184 compile(equation, __file__, 'eval'),
185 {"__builtins__": None},
186 safe_dict)
187 except:
188 import traceback
189 # WARNING is used to prevent the constant pop-up spam
190 self.report({'WARNING'},
191 "Error parsing expression: {} "
192 "(Check the console for more info)".format(equation))
193 print("\n[Add Z Function Surface]:\n\n", traceback.format_exc(limit=1))
195 return {'CANCELLED'}
197 for row_x in range(div_x):
198 edgeloop_cur = []
199 x = start_x + row_x * delta_x
201 for row_y in range(div_y):
202 y = start_y + row_y * delta_y
203 z = 0.0
205 safe_dict['x'] = x
206 safe_dict['y'] = y
208 # Try to evaluate the equation.
209 try:
210 z = float(eval(*expr_args))
211 except:
212 import traceback
213 self.report({'WARNING'},
214 "Error evaluating expression: {} "
215 "(Check the console for more info)".format(equation))
216 print("\n[Add Z Function Surface]:\n\n", traceback.format_exc(limit=1))
218 return {'CANCELLED'}
220 edgeloop_cur.append(len(verts))
221 verts.append((x, y, z))
223 if len(edgeloop_prev) > 0:
224 faces_row = createFaces(edgeloop_prev, edgeloop_cur)
225 faces.extend(faces_row)
227 edgeloop_prev = edgeloop_cur
229 base = create_mesh_object(context, verts, [], faces, "Z Function")
230 else:
231 self.report({'WARNING'}, "Z Equation - No expression is given")
233 return {'CANCELLED'}
235 return {'FINISHED'}
238 def xyz_function_surface_faces(self, x_eq, y_eq, z_eq,
239 range_u_min, range_u_max, range_u_step, wrap_u,
240 range_v_min, range_v_max, range_v_step, wrap_v,
241 a_eq, b_eq, c_eq, f_eq, g_eq, h_eq, n, close_v):
243 verts = []
244 faces = []
246 # Distance of each step in Blender Units
247 uStep = (range_u_max - range_u_min) / range_u_step
248 vStep = (range_v_max - range_v_min) / range_v_step
250 # Number of steps in the vertex creation loops.
251 # Number of steps is the number of faces
252 # => Number of points is +1 unless wrapped.
253 uRange = range_u_step + 1
254 vRange = range_v_step + 1
256 if wrap_u:
257 uRange = uRange - 1
259 if wrap_v:
260 vRange = vRange - 1
262 try:
263 expr_args_x = (
264 compile(x_eq, __file__.replace(".py", "_x.py"), 'eval'),
265 {"__builtins__": None},
266 safe_dict)
267 expr_args_y = (
268 compile(y_eq, __file__.replace(".py", "_y.py"), 'eval'),
269 {"__builtins__": None},
270 safe_dict)
271 expr_args_z = (
272 compile(z_eq, __file__.replace(".py", "_z.py"), 'eval'),
273 {"__builtins__": None},
274 safe_dict)
275 expr_args_a = (
276 compile(a_eq, __file__.replace(".py", "_a.py"), 'eval'),
277 {"__builtins__": None},
278 safe_dict)
279 expr_args_b = (
280 compile(b_eq, __file__.replace(".py", "_b.py"), 'eval'),
281 {"__builtins__": None},
282 safe_dict)
283 expr_args_c = (
284 compile(c_eq, __file__.replace(".py", "_c.py"), 'eval'),
285 {"__builtins__": None},
286 safe_dict)
287 expr_args_f = (
288 compile(f_eq, __file__.replace(".py", "_f.py"), 'eval'),
289 {"__builtins__": None},
290 safe_dict)
291 expr_args_g = (
292 compile(g_eq, __file__.replace(".py", "_g.py"), 'eval'),
293 {"__builtins__": None},
294 safe_dict)
295 expr_args_h = (
296 compile(h_eq, __file__.replace(".py", "_h.py"), 'eval'),
297 {"__builtins__": None},
298 safe_dict)
299 except:
300 import traceback
301 self.report({'WARNING'}, "Error parsing expression(s) - "
302 "Check the console for more info")
303 print("\n[Add X, Y, Z Function Surface]:\n\n", traceback.format_exc(limit=1))
304 return [], []
306 for vN in range(vRange):
307 v = range_v_min + (vN * vStep)
309 for uN in range(uRange):
310 u = range_u_min + (uN * uStep)
312 safe_dict['u'] = u
313 safe_dict['v'] = v
315 safe_dict['n'] = n
317 # Try to evaluate the equations.
318 try:
319 safe_dict['a'] = float(eval(*expr_args_a))
320 safe_dict['b'] = float(eval(*expr_args_b))
321 safe_dict['c'] = float(eval(*expr_args_c))
322 safe_dict['f'] = float(eval(*expr_args_f))
323 safe_dict['g'] = float(eval(*expr_args_g))
324 safe_dict['h'] = float(eval(*expr_args_h))
326 verts.append((
327 float(eval(*expr_args_x)),
328 float(eval(*expr_args_y)),
329 float(eval(*expr_args_z))))
330 except:
331 import traceback
332 self.report({'WARNING'}, "Error evaluating expression(s) - "
333 "Check the console for more info")
334 print("\n[Add X, Y, Z Function Surface]:\n\n", traceback.format_exc(limit=1))
335 return [], []
337 for vN in range(range_v_step):
338 vNext = vN + 1
340 if wrap_v and (vNext >= vRange):
341 vNext = 0
343 for uN in range(range_u_step):
344 uNext = uN + 1
346 if wrap_u and (uNext >= uRange):
347 uNext = 0
349 faces.append([(vNext * uRange) + uNext,
350 (vNext * uRange) + uN,
351 (vN * uRange) + uN,
352 (vN * uRange) + uNext])
354 if close_v and wrap_u and (not wrap_v):
355 for uN in range(1, range_u_step - 1):
356 faces.append([
357 range_u_step - 1,
358 range_u_step - 1 - uN,
359 range_u_step - 2 - uN])
360 faces.append([
361 range_v_step * uRange,
362 range_v_step * uRange + uN,
363 range_v_step * uRange + uN + 1])
365 return verts, faces
368 # Original Script "Parametric.py" by Ed Mackey.
369 # -> http://www.blinken.com/blender-plugins.php
370 # Partly converted for Blender 2.5 by tuga3d.
372 # Sphere:
373 # x = sin(2*pi*u)*sin(pi*v)
374 # y = cos(2*pi*u)*sin(pi*v)
375 # z = cos(pi*v)
376 # u_min = v_min = 0
377 # u_max = v_max = 1
379 # "Snail shell"
380 # x = 1.2**v*(sin(u)**2 *sin(v))
381 # y = 1.2**v*(sin(u)*cos(u))
382 # z = 1.2**v*(sin(u)**2 *cos(v))
383 # u_min = 0
384 # u_max = pi
385 # v_min = -pi/4,
386 # v max = 5*pi/2
388 class AddXYZFunctionSurface(Operator):
389 bl_idname = "mesh.primitive_xyz_function_surface"
390 bl_label = "Add X, Y, Z Function Surface"
391 bl_description = ("Add a surface defined defined by 3 functions:\n"
392 "x=F1(u,v), y=F2(u,v) and z=F3(u,v)")
393 bl_options = {'REGISTER', 'UNDO', 'PRESET'}
395 x_eq: StringProperty(
396 name="X equation",
397 description="Equation for x=F(u,v). "
398 "Also available: n, a, b, c, f, g, h",
399 default="cos(v)*(1+cos(u))*sin(v/8)"
401 y_eq: StringProperty(
402 name="Y equation",
403 description="Equation for y=F(u,v). "
404 "Also available: n, a, b, c, f, g, h",
405 default="sin(u)*sin(v/8)+cos(v/8)*1.5"
407 z_eq: StringProperty(
408 name="Z equation",
409 description="Equation for z=F(u,v). "
410 "Also available: n, a, b, c, f, g, h",
411 default="sin(v)*(1+cos(u))*sin(v/8)"
413 range_u_min: FloatProperty(
414 name="U min",
415 description="Minimum U value. Lower boundary of U range",
416 min=-100.00,
417 max=0.00,
418 default=0.00
420 range_u_max: FloatProperty(
421 name="U max",
422 description="Maximum U value. Upper boundary of U range",
423 min=0.00,
424 max=100.00,
425 default=2 * pi
427 range_u_step: IntProperty(
428 name="U step",
429 description="U Subdivisions",
430 min=1,
431 max=1024,
432 default=32
434 wrap_u: BoolProperty(
435 name="U wrap",
436 description="U Wrap around",
437 default=True
439 range_v_min: FloatProperty(
440 name="V min",
441 description="Minimum V value. Lower boundary of V range",
442 min=-100.00,
443 max=0.00,
444 default=0.00
446 range_v_max: FloatProperty(
447 name="V max",
448 description="Maximum V value. Upper boundary of V range",
449 min=0.00,
450 max=100.00,
451 default=4 * pi
453 range_v_step: IntProperty(
454 name="V step",
455 description="V Subdivisions",
456 min=1,
457 max=1024,
458 default=128
460 wrap_v: BoolProperty(
461 name="V wrap",
462 description="V Wrap around",
463 default=False
465 close_v: BoolProperty(
466 name="Close V",
467 description="Create faces for first and last "
468 "V values (only if U is wrapped)",
469 default=False
471 n_eq: IntProperty(
472 name="Number of objects (n=0..N-1)",
473 description="The parameter n will be the index "
474 "of the current object, 0 to N-1",
475 min=1,
476 max=100,
477 default=1
479 a_eq: StringProperty(
480 name="A helper function",
481 description="Equation for a=F(u,v). Also available: n",
482 default="0"
484 b_eq: StringProperty(
485 name="B helper function",
486 description="Equation for b=F(u,v). Also available: n",
487 default="0"
489 c_eq: StringProperty(
490 name="C helper function",
491 description="Equation for c=F(u,v). Also available: n",
492 default="0"
494 f_eq: StringProperty(
495 name="F helper function",
496 description="Equation for f=F(u,v). Also available: n, a, b, c",
497 default="0"
499 g_eq: StringProperty(
500 name="G helper function",
501 description="Equation for g=F(u,v). Also available: n, a, b, c",
502 default="0"
504 h_eq: StringProperty(
505 name="H helper function",
506 description="Equation for h=F(u,v). Also available: n, a, b, c",
507 default="0"
509 show_wire : BoolProperty(
510 name="Show wireframe",
511 default=True,
512 description="Add the object’s wireframe over solid drawing"
514 edit_mode : BoolProperty(
515 name="Show in edit mode",
516 default=True,
517 description="Show in edit mode"
520 def execute(self, context):
521 for n in range(0, self.n_eq):
522 verts, faces = xyz_function_surface_faces(
523 self,
524 self.x_eq,
525 self.y_eq,
526 self.z_eq,
527 self.range_u_min,
528 self.range_u_max,
529 self.range_u_step,
530 self.wrap_u,
531 self.range_v_min,
532 self.range_v_max,
533 self.range_v_step,
534 self.wrap_v,
535 self.a_eq,
536 self.b_eq,
537 self.c_eq,
538 self.f_eq,
539 self.g_eq,
540 self.h_eq,
542 self.close_v
544 if not verts:
545 return {'CANCELLED'}
547 obj = create_mesh_object(context, verts, [], faces, "XYZ Function")
549 if self.show_wire:
550 context.active_object.show_wire = True
551 else:
552 context.active_object.show_wire = False
554 if self.edit_mode:
555 bpy.ops.object.mode_set(mode = 'EDIT')
556 else:
557 bpy.ops.object.mode_set(mode = 'OBJECT')
559 return {'FINISHED'}