deep_computer_overclocked.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import bpy
  2. import mathutils
  3. import math
  4. import random
  5. # load outfittemplate-nocube.blend, then run this
  6. # add an ominous red lamp
  7. bpy.ops.object.add(type='LIGHT', location=(3,-3,0))
  8. lamp = bpy.context.object
  9. lamp.data.type = 'POINT'
  10. lamp.data.energy = 55
  11. lamp.data.color = [1.0,0.2,0.1]
  12. # we need five materials
  13. # - the monitor (image texture)
  14. # - the metal for the case
  15. # - three colors for the buttons
  16. # - green (pressed down)
  17. # - grey/clear (most unpressed)
  18. # - red (unpressed but alert)
  19. monitor_mat = bpy.data.materials.new("ComputerMonitor")
  20. monitor_mat.diffuse_color = [0, 0.02, 0, 1]
  21. monitor_mat.specular_color = [0.9, 1, 0.9]
  22. monitor_mat.roughness = 0.05
  23. monitor_mat.use_nodes = True
  24. mon_nodes = monitor_mat.node_tree.nodes
  25. mon_links = monitor_mat.node_tree.links
  26. mon_mat_output = mon_nodes.get('Material Output')
  27. mon_bsdf = mon_nodes.get('Principled BSDF')
  28. mon_bsdf.inputs['Roughness'].default_value = 0.1
  29. mon_bsdf.inputs['Emission Strength'].default_value = 30
  30. mon_img = bpy.data.images.load(filepath = '/home/jmelesky/code/endless-sky-artificial-crew-models/images/oscilloscreen red.png')
  31. mon_imgtx = mon_nodes.new('ShaderNodeTexImage')
  32. mon_imgtx.image = mon_img
  33. mon_links.new(mon_imgtx.outputs['Color'], mon_bsdf.inputs['Base Color'])
  34. mon_ramp = mon_nodes.new('ShaderNodeValToRGB')
  35. mon_ramp.color_ramp.interpolation = 'B_SPLINE'
  36. mon_ramp.color_ramp.elements[0].position = 0.1
  37. mon_ramp.color_ramp.elements[1].color = [1.0, 0.2, 0.1, 1.0]
  38. mon_links.new(mon_imgtx.outputs['Color'], mon_ramp.inputs['Fac'])
  39. mon_links.new(mon_ramp.outputs['Color'], mon_bsdf.inputs['Emission Color'])
  40. metal_mat = bpy.data.materials.new("ComputerMetal")
  41. metal_mat.use_nodes = True
  42. metal_nodes = metal_mat.node_tree.nodes
  43. metal_links = metal_mat.node_tree.links
  44. metal_mat_output = metal_nodes.get('Material Output')
  45. metal_bsdf = metal_nodes.get('Principled BSDF')
  46. metal_bsdf.inputs['Metallic'].default_value = 1.0
  47. metal_bsdf.inputs['Base Color'].default_value = [0.16, 0.12, 0.08, 1.0]
  48. # metal_noise = metal_nodes.new('ShaderNodeTexNoise')
  49. # metal_noise.inputs['Scale'].default_value = 70.0
  50. # metal_noise.inputs['Detail'].default_value = 15.0
  51. # metal_noise.inputs['Roughness'].default_value = 0.6
  52. # metal_ramp = metal_nodes.new('ShaderNodeValToRGB')
  53. # metal_ramp.color_ramp.elements[0].color = [0.08, 0.07, 0.03, 1.0]
  54. # metal_ramp.color_ramp.elements[1].color = [0.16, 0.155, 0.08, 1.0]
  55. # metal_links.new(metal_noise.outputs['Fac'], metal_ramp.inputs['Fac'])
  56. # metal_links.new(metal_ramp.outputs['Color'], metal_bsdf.inputs['Base Color'])
  57. metal_bump = metal_nodes.new('ShaderNodeBump')
  58. metal_bump.invert = True
  59. metal_bump.inputs['Strength'].default_value = 0.3
  60. metal_noise2 = metal_nodes.new('ShaderNodeTexNoise')
  61. metal_noise2.inputs['Scale'].default_value = 70.0
  62. metal_noise2.inputs['Detail'].default_value = 15.0
  63. metal_noise2.inputs['Roughness'].default_value = 0.6
  64. metal_links.new(metal_noise2.outputs['Fac'], metal_bump.inputs['Height'])
  65. metal_links.new(metal_bump.outputs['Normal'], metal_bsdf.inputs['Normal'])
  66. metal_links.new(metal_bump.outputs['Normal'], metal_bsdf.inputs['Roughness'])
  67. down_button_mat = bpy.data.materials.new("DownButton")
  68. down_button_mat.use_nodes = True
  69. down_button_mat.blend_method = 'BLEND'
  70. down_bsdf = down_button_mat.node_tree.nodes.get('Principled BSDF')
  71. down_bsdf.inputs['Base Color'].default_value = [0.5, 0.5, 0.5, 0.95]
  72. down_bsdf.inputs['Roughness'].default_value = 0.4
  73. down_bsdf.inputs['Emission Color'].default_value = [0.2, 0.85, 0.2, 0.3]
  74. down_bsdf.inputs['Emission Strength'].default_value = 0.8
  75. up_button_mat = bpy.data.materials.new("UpButton")
  76. up_button_mat.diffuse_color = [0.5, 0.5, 0.5, 0.4]
  77. up_button_mat.roughness = 0.8
  78. up_button_mat.blend_method = 'BLEND'
  79. attn_button_mat = bpy.data.materials.new("AttnButton")
  80. attn_button_mat.use_nodes = True
  81. attn_button_mat.blend_method = 'BLEND'
  82. attn_bsdf = attn_button_mat.node_tree.nodes.get('Principled BSDF')
  83. attn_bsdf.inputs['Base Color'].default_value = [0.5, 0.5, 0.5, 0.95]
  84. attn_bsdf.inputs['Roughness'].default_value = 0.4
  85. attn_bsdf.inputs['Emission Color'].default_value = [0.7, 0.1, 0.1, 0.3]
  86. attn_bsdf.inputs['Emission Strength'].default_value = 0.7
  87. # now the big box
  88. back_bottom_left = (0,0,0)
  89. back_top_left = (0,0,8)
  90. front_top_left = (2,0,8)
  91. front_mid_left = (4,0,3)
  92. front_bottom_left = (4,0,0)
  93. back_bottom_right = (0,10,0)
  94. back_top_right = (0,10,8)
  95. front_top_right = (2,10,8)
  96. front_mid_right = (4,10,3)
  97. front_bottom_right = (4,10,0)
  98. box_vertices = [back_bottom_left, back_top_left, back_bottom_right, back_top_right,
  99. front_top_left, front_top_right, front_mid_left, front_mid_right,
  100. front_bottom_left, front_bottom_right]
  101. box_faces = [
  102. (0,1,3,2), # the back of the box: back_bottom_left, back_top_left, back_top_right, back_bottom_right
  103. (0,2,9,8), # the bottom: back_bottom_left, back_bottom_right, front_bottom_right, front_bottom_left
  104. (8,9,7,6), # lower front plate: front_bottom_left, front_bottom_right, front_mid_right, front_mid_left
  105. (9,2,3,5,7), # right side panel (5 edges): front_bottom_right, back_bottom_right, back_top_right, front_top_right, front_mid_right
  106. (0,8,6,4,1), # left side panel: back_bottom_left, front_bottom_left, front_mid_left, front_top_left, back_top_left
  107. (6,7,5,4), # upper front plate: front_mid_left, front_mid_right, front_top_right, front_top_left
  108. (4,5,3,1), # top: front_top_left, front_top_right, back_top_right, back_top_left
  109. ]
  110. box_mesh = bpy.data.meshes.new('MyComputerBoxMesh')
  111. box_mesh.from_pydata(box_vertices, [], box_faces)
  112. box_obj = bpy.data.objects.new('MyComputerBox', box_mesh)
  113. box_obj.data.materials.append(metal_mat)
  114. bpy.context.scene.collection.objects.link(box_obj)
  115. # the oscilloscope-style monitor
  116. monitor_location = mathutils.Vector((3, 2.5, 5.5))
  117. monitor_scale = mathutils.Vector((.4, 1, 1))
  118. monitor_rotation = mathutils.Euler((0, math.radians(203), math.radians(180)))
  119. bpy.ops.mesh.primitive_uv_sphere_add(
  120. segments=32,
  121. ring_count=16,
  122. radius=2,
  123. location=monitor_location,
  124. scale=monitor_scale, # this is relative to the size
  125. rotation=monitor_rotation)
  126. # smooth the monitor, add a texture
  127. monitor_obj = bpy.context.active_object
  128. for f in monitor_obj.data.polygons:
  129. f.use_smooth = True
  130. monitor_obj.data.update()
  131. monitor_obj.data.materials.append(monitor_mat)
  132. # three rows of buttons, randomly depressed
  133. #
  134. # a better way to do this would be to set the buttons in a group,
  135. # then rotate that entire group. i'm not there yet, so we're
  136. # using some magic numbers for positional offsets.
  137. buttons = []
  138. for i in [7, 5.5, 4]:
  139. for j in [6.8, 7.2, 7.6, 8, 8.4, 8.8, 9.2]:
  140. if random.random() > .9:
  141. button_location = mathutils.Vector((5.0 - (i/2.5), j, i-.1))
  142. button_material = down_button_mat
  143. else:
  144. button_location = mathutils.Vector((5.2 - (i/2.5), j, i))
  145. if random.random() > .2:
  146. button_material = attn_button_mat
  147. else:
  148. button_material = up_button_mat
  149. bpy.ops.mesh.primitive_cube_add(
  150. size=1,
  151. location=button_location,
  152. rotation=monitor_rotation,
  153. scale=mathutils.Vector((.8, .2, .6)))
  154. button = bpy.context.active_object
  155. buttons.append(button)
  156. button.data.materials.append(button_material)
  157. box_obj.select_set(True)
  158. monitor_obj.select_set(True)
  159. for b in buttons:
  160. b.select_set(True)
  161. bpy.ops.transform.resize(
  162. value=(0.5, 0.5, 0.5)
  163. )
  164. bpy.ops.transform.rotate(
  165. value=math.radians(90)
  166. )
  167. bpy.ops.transform.translate(
  168. value=(-2.5,-5,-3.3)
  169. )