DataNalle

Blender Mesh/Curve Ring Generator

Nov 1st, 2014
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 KB | None | 0 0
  1. # Blender Mesh/Curve Ring Generator
  2. import bpy
  3. import math
  4.  
  5. detail = 2 # [0-2]
  6. maxRing = 28 # [0-28]
  7. checkers = True
  8. curve = False
  9. rooms = True
  10. emptySize = 120
  11.  
  12. ringRadius =    [236.800459869903,
  13.         255.644913604649,
  14.         274.87006337444,
  15.         294.483600008267,
  16.         314.493369705404,
  17.         334.9073771742,
  18.         355.733788834285,
  19.         376.980936083462,
  20.         398.657318630603,
  21.         420.771607895867,
  22.         443.332650479622,
  23.         466.349471701433,
  24.         489.831279210552,
  25.         513.787466669351,
  26.         538.227617511157,
  27.         563.161508774008,
  28.         588.599115011867,
  29.         614.550612284834,
  30.         641.026382229982,
  31.         668.037016214426,
  32.         695.593319572293,
  33.         723.706315927289,
  34.         752.387251602588,
  35.         781.647600119812,
  36.         811.499066788899,
  37.         841.99966606518,
  38.         873.070366389729,
  39.         904.768757629926,
  40.         937.107520410328]
  41.  
  42. ringWidth = [25,
  43.         27,
  44.         27,
  45.         60,
  46.         60,
  47.         60,
  48.         60,
  49.         60,
  50.         60,
  51.         27,
  52.         27,
  53.         27,
  54.         27,
  55.         27,
  56.         60,
  57.         60,
  58.         27,
  59.         27,
  60.         27,
  61.         27,
  62.         27,
  63.         60,
  64.         60,
  65.         27,
  66.         27,
  67.         27,
  68.         27,
  69.         27,
  70.         27]
  71.  
  72. ringHeight =    [16.4712962350476,
  73.         18.8444537347455,
  74.         19.225149769791,
  75.         19.6135366338271,
  76.         20.0097696971367,
  77.         20.414007468796,
  78.         20.8264116600848,
  79.         21.2471472491775,
  80.         21.6763825471405,
  81.         22.1142892652646,
  82.         22.5610425837548,
  83.         23.0168212218106,
  84.         23.4818075091198,
  85.         23.9561874587991,
  86.         24.440150841805,
  87.         24.9338912628517,
  88.         25.4376062378587,
  89.         25.951497272967,
  90.         26.4757699451482,
  91.         27.0106339844441,
  92.         27.5563033578673,
  93.         28.1129963549959,
  94.         28.6809356752989,
  95.         29.2603485172241,
  96.         29.8514666690868,
  97.         30.5005992762809,
  98.         31.070700324549,
  99.         31.6983912401965,
  100.         32.3387627804025]
  101.  
  102. ringMaxRooms =  [14,
  103.         7,
  104.         8,
  105.         9,
  106.         9,
  107.         10,
  108.         11,
  109.         11,
  110.         12,
  111.         25,
  112.         26,
  113.         28,
  114.         29,
  115.         31,
  116.         1,
  117.         1,
  118.         36,
  119.         37,
  120.         39,
  121.         41,
  122.         42,
  123.         1,
  124.         1,
  125.         48,
  126.         49,
  127.         51,
  128.         53,
  129.         55,
  130.         57]
  131.  
  132. ringHalfRotFr = [450,
  133.         468,
  134.         486,
  135.         504,
  136.         522,
  137.         539,
  138.         556,
  139.         573,
  140.         590,
  141.         606,
  142.         623,
  143.         639,
  144.         655,
  145.         671,
  146.         688,
  147.         704,
  148.         720,
  149.         736,
  150.         752,
  151.         768,
  152.         784,
  153.         799,
  154.         815,
  155.         831,
  156.         847,
  157.         863,
  158.         879,
  159.         895,
  160.         911]
  161.  
  162. bpy.context.scene.unit_settings.system='METRIC'
  163. bpy.context.scene.frame_end = max(ringHalfRotFr)*2
  164.  
  165. for i in range(0,maxRing+1):
  166.    
  167.     # Animation
  168.     bpy.ops.object.empty_add(type='PLAIN_AXES')
  169.     bpy.context.object.empty_draw_size = emptySize
  170.     bpy.context.object.name = "RING "+str(i)+" Controller"
  171.    
  172.     bpy.context.scene.frame_current = 1
  173.     bpy.ops.anim.keyframe_insert_menu(type='Rotation')
  174.     bpy.context.scene.frame_current = ringHalfRotFr[i]
  175.     bpy.context.object.rotation_euler[2] = math.pi
  176.     bpy.ops.anim.keyframe_insert_menu(type='Rotation')
  177.    
  178.     # Area type juggling
  179.     # Save old area type
  180.     old_type = bpy.context.area.type
  181.  
  182.     # Switch to graph editor
  183.     bpy.context.area.type = 'GRAPH_EDITOR'
  184.  
  185.     # Do some graph stuff, e.g.
  186.     bpy.ops.graph.interpolation_type(type='LINEAR')
  187.     bpy.ops.graph.select_all_toggle(invert=False)
  188.     bpy.ops.graph.select_all_toggle(invert=False)
  189.     bpy.ops.graph.extrapolation_type(type='LINEAR')
  190.  
  191.     # Switch back again
  192.     bpy.context.area.type = old_type
  193.    
  194.     if curve:
  195.         # Create circle curve
  196.         bpy.ops.curve.primitive_bezier_circle_add(radius=ringRadius[i])
  197.         bpy.context.object.data.resolution_u = 128
  198.         bpy.context.object.data.extrude = ringWidth[i]
  199.     elif rooms:
  200.         if i not in [14,15,21,22]# Special case for PUB rings
  201.             bpy.ops.mesh.primitive_cylinder_add(vertices=ringMaxRooms[i], radius=ringRadius[i], depth=ringWidth[i], end_fill_type='NOTHING')
  202.         else:   # Create circle mesh
  203.             bpy.ops.mesh.primitive_cylinder_add(vertices=4, radius=ringRadius[i], depth=ringWidth[i], end_fill_type='NOTHING')
  204.        
  205.         if checkers and not curve:
  206.             # Checkers materials
  207.             bpy.context.object.data.materials.append(bpy.data.materials['white'])
  208.             bpy.context.object.data.materials.append(bpy.data.materials['black'])
  209.             bpy.ops.object.mode_set(mode='EDIT')
  210.             bpy.ops.mesh.select_nth()
  211.             bpy.context.object.active_material_index = 1
  212.             bpy.ops.object.material_slot_assign()
  213.             bpy.ops.object.mode_set(mode='OBJECT')
  214.     else:
  215.         # Mesh circle with constant resolution
  216.         bpy.ops.mesh.primitive_cylinder_add(vertices=128, radius=ringRadius[i], depth=ringWidth[i], end_fill_type='NOTHING')
  217.        
  218.     if rooms and not curve:
  219.         # Make meshes perfect circles
  220.         sublvl = 1+detail if (i == 0) else 2+detail if (0 < i < 9) else 1+detail if (i not in [14,15,21,22]) else 5+detail
  221.        
  222.         bpy.ops.object.modifier_add(type='SUBSURF')
  223.         bpy.context.object.modifiers["Subsurf"].levels = sublvl
  224.         bpy.context.object.modifiers["Subsurf"].render_levels = sublvl
  225.  
  226.         bpy.ops.object.modifier_add(type='CAST')
  227.         bpy.context.object.modifiers["Cast"].cast_type = 'CYLINDER'
  228.         bpy.context.object.modifiers["Cast"].use_z = False
  229.         bpy.context.object.modifiers["Cast"].factor = 1
  230.         bpy.context.object.modifiers["Cast"].radius = ringRadius[i]
  231.  
  232.     bpy.ops.object.modifier_add(type='SOLIDIFY')
  233.     bpy.context.object.modifiers["Solidify"].thickness = ringHeight[i]
  234.     bpy.context.object.modifiers["Solidify"].use_quality_normals = True
  235.     bpy.context.object.modifiers["Solidify"].use_even_offset = True
  236.  
  237.     bpy.ops.object.modifier_add(type='EDGE_SPLIT')
  238.  
  239.     bpy.ops.object.shade_smooth()
  240.     bpy.context.object.name = "RING "+str(i)
  241.    
  242.     # Parent to controller empty for animation
  243.     bpy.context.object.parent = bpy.data.objects["RING "+str(i)+" Controller"]
Add Comment
Please, Sign In to add comment