Guest User

Untitled

a guest
Jul 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. import bpy;
  2. import numpy as np
  3. import bmesh;
  4. import math;
  5.  
  6. # Connectivity
  7. Point1List = [[0.0,0.0,0.0],[3.0,0.0,0.0],[2.0,2.0,0.0]]
  8. Point2List = [[3.0,0.0,0.0],[2.0,2.0,0.0],[0.0,0.0,0.0]]
  9.  
  10. mylayers = [False] * 20;
  11. mylayers[0] = True;
  12.  
  13. bpy.ops.mesh.primitive_uv_sphere_add(
  14. location = (0.0, 0.0, 0.0),
  15. size = 0.4,
  16. layers=mylayers);
  17.  
  18. bpy.context.active_object.name = "Point1ListSphere1";
  19.  
  20. bpy.ops.mesh.primitive_uv_sphere_add(
  21. location = (3.0, 0.0, 0.0),
  22. size = 0.4,
  23. layers=mylayers);
  24.  
  25. bpy.context.active_object.name = "Point2ListSphere1";
  26.  
  27. bpy.ops.mesh.primitive_uv_sphere_add(
  28. location = (2.0, 2.0, 0.0),
  29. size = 0.4,
  30. layers=mylayers);
  31.  
  32. bpy.context.active_object.name = "Point2ListSphere2";
  33.  
  34. def poly(z,L):
  35. return (1.2/(L*L)) * z * z - (1.2/L)*z + 0.4
  36.  
  37. def rotation_matrix(axis, theta):
  38. axis = np.asarray(axis)
  39. axis = axis/math.sqrt(np.dot(axis, axis))
  40. a = math.cos(theta/2.0)
  41. b, c, d = -axis*math.sin(theta/2.0)
  42. aa, bb, cc, dd = a*a, b*b, c*c, d*d
  43. bc, ad, ac, ab, bd, cd = b*c, a*d, a*c, a*b, b*d, c*d
  44. return np.array([[aa+bb-cc-dd, 2*(bc+ad), 2*(bd-ac)],
  45. [2*(bc-ad), aa+cc-bb-dd, 2*(cd+ab)],
  46. [2*(bd+ac), 2*(cd-ab), aa+dd-bb-cc]])
  47.  
  48. for i in range(len(Point1List)):
  49. if Point1List[i] != Point2List[i]:
  50. Dist = 0.0
  51. for j in range(3):
  52. Dist+=(Point1List[i][j]-Point2List[i][j])**2
  53.  
  54. Dist=math.sqrt(Dist)
  55.  
  56. # Calculate ratational axis
  57. u = [0.0,0.0,Dist]
  58. v = [Point2List[i][0]-Point1List[i][0],Point2List[i][1]-
  59. Point1List[i][1],Point2List[i][2]-Point1List[i][2]]
  60. R_axis = np.cross(u,v)
  61. R_axis = R_axis/np.linalg.norm(R_axis)
  62.  
  63. # Calculate theta radians
  64. theta = math.acos(np.dot(u,v)/(np.linalg.norm(u)*np.linalg.norm(v)));
  65.  
  66. # Create a new object with name
  67. mesh = bpy.data.meshes.new("Branch"+str(i+1))
  68. object = bpy.data.objects.new("Branch"+str(i+1), mesh)
  69.  
  70. # Show the object in the scene
  71. scene = bpy.context.scene
  72. scene.objects.link(object) # put the object into the scene (link)
  73. scene.objects.active = object # set as the active object in the scene
  74.  
  75. # Set the layer
  76. object.layers[0] = True
  77. for j in range(1,20):
  78. object.layers[j] = False
  79.  
  80. object.select = True # select object
  81. mesh = bpy.context.object.data
  82. bm = bmesh.new()
  83.  
  84. # Translate and rotate the created column to the correct position
  85.  
  86. v_coord=[poly(0,Dist), 0, 0]
  87. R_matrix = rotation_matrix(R_axis,theta)
  88. v_coord = np.dot(R_matrix,v_coord)
  89. # Point translation
  90. v_coord = v_coord + Point1List[i]
  91.  
  92. v0 = bm.verts.new((v_coord[0], v_coord[1], v_coord[2]))
  93.  
  94. for dz in range(1, 1000):
  95. z = dz / (1000/Dist)
  96. if z>=Dist:
  97. break
  98. v_coord=[poly(z,Dist), 0, z]
  99.  
  100. v_coord = np.dot(R_matrix,v_coord)
  101. v_coord = v_coord + Point1List[i]
  102. v_new = bm.verts.new((v_coord[0], v_coord[1], v_coord[2]))
  103.  
  104.  
  105. edge = bm.edges.new((v0, v_new))
  106. v0 = v_new
  107.  
  108. bmesh.ops.spin(bm, geom=bm.edges, axis=(v[0], v[1], v[2]), angle=math.radians(360), steps=20, cent=(Point1List[i][0],Point1List[i][1],Point1List[i][2]))
  109. # Remove the duplicate vertices
  110. bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.001)
  111.  
  112. bm.to_mesh(mesh)
  113. mesh.update()
  114.  
  115. # Boolean operation
  116. def UnionBoolean(target, opObj, i):
  117.  
  118. bpy.ops.object.select_all(action='DESELECT')
  119. target.select = True
  120.  
  121. bpy.context.scene.objects.active = target
  122. bpy.ops.object.modifier_add(type='BOOLEAN')
  123.  
  124. mod = target.modifiers
  125. mod[0].name = "Boon"+str(i)
  126. mod[0].object = opObj
  127. mod[0].operation = 'UNION'
  128.  
  129. bpy.ops.object.modifier_apply(apply_as='DATA', modifier=mod[0].name)
  130.  
  131. for i in range (1,len(Point1List)):
  132. target = bpy.data.objects['Branch1']
  133. opObj = bpy.data.objects['Branch'+str(i+1)]
  134.  
  135. UnionBoolean(target,opObj, i)
Add Comment
Please, Sign In to add comment