cmk20

Cube asm script

Sep 25th, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. print("**SCRIPT START**")
  2. import bpy
  3. import math
  4. import time
  5. bpy.context.scene.frame_set(1)
  6. frame = 1
  7. frameStart = 1
  8. fs = 4
  9. class Cube(object):
  10. id = 0
  11. curX = 0
  12. curY = 0
  13. curZ = 0
  14. destX = 0
  15. destY = 0
  16. destZ = 0
  17.  
  18. def __init__(self, id, curX, curY, curZ):
  19. self.id = id
  20. self.curX = curX
  21. self.curY = curY
  22. self.curZ = curZ
  23.  
  24. def setDest(self, destX, destY, destZ):
  25. self.destX = destX
  26. self.destY = destY
  27. self.destZ = destZ
  28.  
  29. cubes = []
  30. xsize = 10
  31. ysize = 10
  32. zsize = 10
  33. def moveCube(id, x, y, z, fr):
  34. ob = bpy.data.objects[id]
  35. bpy.context.scene.frame_set(fr)
  36. ob.keyframe_insert(data_path="location", index=-1)
  37. bpy.context.scene.frame_set(fr + fs)
  38. ob.location = (x, y, z)
  39. ob.keyframe_insert(data_path="location", index=-1)
  40.  
  41. def isCube(x, y, z):
  42. for c in cubes:
  43. if (c.curX == x) and (c.curY == y) and (c.curZ == z):
  44. return True
  45. return False
  46.  
  47. def createCube(x, y, z, id):
  48. cubes.append(Cube(id, x, y, z))
  49. bpy.ops.mesh.primitive_cube_add(location=(x, y, z))
  50. bpy.context.active_object.name = str(id)
  51. bpy.context.active_object.scale = (0.5, 0.5, 0.5)
  52. print(x, ":", y, ":", z, " ", id)
  53.  
  54. def createCubes(): #Function cause python is stupid and dosent let you break from loops properly
  55. i = 0
  56. numberOfCubes = xsize * ysize * zsize
  57. topy = 0 #Top y is sutracted
  58. rightx = xsize #Right x is added
  59. bottomy = ysize #Bottom y is added
  60. leftx = 0 #Left x is subracted
  61. while True: #Infinate loop, the for loops have logic to exit the fucntion
  62. for x in range(0, xsize): #Top creation, 0 -> xsize
  63. createCube(x + .5, topy - .5, 0, i)
  64. i += 1
  65. numberOfCubes -= 1
  66. if (numberOfCubes == 0): #Check if we are out of cubes to add
  67. return #If so, exit the whole loop set (this is why we need a function)
  68. topy -= 1 #If the for loop finishes then we decrement the topy value so the next time we enter this for loop, the cube wont be placed on top of eachother
  69. #The rest of the for loops work in similar ways, they however are set up to work in diffrent directions
  70.  
  71. for y in range(0, ysize): #Right creation, 0 -> ysize
  72. createCube(rightx + .5, y + .5, 0, i)
  73. i += 1
  74. numberOfCubes -= 1
  75. if (numberOfCubes == 0):
  76. return
  77. rightx += 1 #The above dose the same thing as the first loop, it simply makes a live on the right of the cube instaed of the top
  78.  
  79. for x in range(0, xsize): #Bottom creation, 0 -> xsize
  80. createCube(x + .5, bottomy + .5, 0, i)
  81. i += 1
  82. numberOfCubes -= 1
  83. if (numberOfCubes == 0):
  84. return
  85. bottomy += 1
  86.  
  87. for y in range(0, ysize): #Left creation, 0 -> ysize
  88. createCube(leftx - .5, y + .5, 0, i)
  89. i += 1
  90. numberOfCubes -= 1
  91. if (numberOfCubes == 0):
  92. return
  93. leftx -= 1
  94. #If we make it here, loop back to the first for loop
  95.  
  96. createCubes() #Now run the function
  97. l = 0
  98. for z in range(0, zsize): #Now we add a destination to each of the cubes
  99. for y in range(0, ysize):
  100. for x in range(0, xsize):
  101. cubes[l].setDest(x + .5, y + .5, z)
  102. l += 1
  103. #Now the cubes are set up and in the right posistions
  104. cZ = 0
  105. for c in range (0, len(cubes)):
  106. print(cubes[c].curX, ",", cubes[c].curY, ",", cubes[c].curZ, ":", cubes[c].destX, ",", cubes[c].destY, ",", cubes[c].destZ)
  107. if (cubes[c].destZ > cZ):
  108. frameStart = frame
  109. cZ = cubes[c].destZ
  110. else:
  111. frame = frameStart
  112. while ((cubes[c].curX != cubes[c].destX) or (cubes[c].curY != cubes[c].destY) or (cubes[c].curZ != cubes[c].destZ)):
  113. error = 0
  114. if (cubes[c].curX != cubes[c].destX):
  115. if (cubes[c].destX - cubes[c].curX > 0):
  116. dist = 1
  117. else:
  118. dist = -1
  119. if (not isCube(cubes[c].curX + dist, cubes[c].curY, cubes[c].curZ)):
  120. moveCube(str(cubes[c].id), cubes[c].curX + dist, cubes[c].curY, cubes[c].curZ, frame)
  121. cubes[c].curX += dist
  122. frame += fs
  123. else:
  124. error = 1
  125. if (cubes[c].curY != cubes[c].destY):
  126. if (cubes[c].destY - cubes[c].curY > 0):
  127. dist = 1
  128. else:
  129. dist = -1
  130. if (not isCube(cubes[c].curX, cubes[c].curY + dist, cubes[c].curZ)):
  131. moveCube(str(cubes[c].id), cubes[c].curX, cubes[c].curY + dist, cubes[c].curZ, frame)
  132. cubes[c].curY += dist
  133. frame += fs
  134. else:
  135. error = 1
  136. if (cubes[c].curZ != cubes[c].destZ):
  137. if (cubes[c].destZ - cubes[c].curZ > 0):
  138. dist = 1
  139. else:
  140. dist = -1
  141. if (not isCube(cubes[c].curX, cubes[c].curY, cubes[c].curZ + dist)):
  142. moveCube(str(cubes[c].id), cubes[c].curX, cubes[c].curY, cubes[c].curZ + dist, frame)
  143. cubes[c].curZ += dist
  144. frame += fs
  145. if (error == 1):
  146. moveCube(str(cubes[c].id), cubes[c].curX, cubes[c].curY, cubes[c].curZ + dist, frame)
  147. cubes[c].curZ += dist
  148. frame += fs
  149. bpy.context.scene.frame_end = frame
  150. print("Done! :D")
Advertisement
Add Comment
Please, Sign In to add comment