Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("**SCRIPT START**")
- import bpy
- import math
- import time
- bpy.context.scene.frame_set(1)
- frame = 1
- frameStart = 1
- fs = 4
- class Cube(object):
- id = 0
- curX = 0
- curY = 0
- curZ = 0
- destX = 0
- destY = 0
- destZ = 0
- def __init__(self, id, curX, curY, curZ):
- self.id = id
- self.curX = curX
- self.curY = curY
- self.curZ = curZ
- def setDest(self, destX, destY, destZ):
- self.destX = destX
- self.destY = destY
- self.destZ = destZ
- cubes = []
- xsize = 10
- ysize = 10
- zsize = 10
- def moveCube(id, x, y, z, fr):
- ob = bpy.data.objects[id]
- bpy.context.scene.frame_set(fr)
- ob.keyframe_insert(data_path="location", index=-1)
- bpy.context.scene.frame_set(fr + fs)
- ob.location = (x, y, z)
- ob.keyframe_insert(data_path="location", index=-1)
- def isCube(x, y, z):
- for c in cubes:
- if (c.curX == x) and (c.curY == y) and (c.curZ == z):
- return True
- return False
- def createCube(x, y, z, id):
- cubes.append(Cube(id, x, y, z))
- bpy.ops.mesh.primitive_cube_add(location=(x, y, z))
- bpy.context.active_object.name = str(id)
- bpy.context.active_object.scale = (0.5, 0.5, 0.5)
- print(x, ":", y, ":", z, " ", id)
- def createCubes(): #Function cause python is stupid and dosent let you break from loops properly
- i = 0
- numberOfCubes = xsize * ysize * zsize
- topy = 0 #Top y is sutracted
- rightx = xsize #Right x is added
- bottomy = ysize #Bottom y is added
- leftx = 0 #Left x is subracted
- while True: #Infinate loop, the for loops have logic to exit the fucntion
- for x in range(0, xsize): #Top creation, 0 -> xsize
- createCube(x + .5, topy - .5, 0, i)
- i += 1
- numberOfCubes -= 1
- if (numberOfCubes == 0): #Check if we are out of cubes to add
- return #If so, exit the whole loop set (this is why we need a function)
- 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
- #The rest of the for loops work in similar ways, they however are set up to work in diffrent directions
- for y in range(0, ysize): #Right creation, 0 -> ysize
- createCube(rightx + .5, y + .5, 0, i)
- i += 1
- numberOfCubes -= 1
- if (numberOfCubes == 0):
- return
- 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
- for x in range(0, xsize): #Bottom creation, 0 -> xsize
- createCube(x + .5, bottomy + .5, 0, i)
- i += 1
- numberOfCubes -= 1
- if (numberOfCubes == 0):
- return
- bottomy += 1
- for y in range(0, ysize): #Left creation, 0 -> ysize
- createCube(leftx - .5, y + .5, 0, i)
- i += 1
- numberOfCubes -= 1
- if (numberOfCubes == 0):
- return
- leftx -= 1
- #If we make it here, loop back to the first for loop
- createCubes() #Now run the function
- l = 0
- for z in range(0, zsize): #Now we add a destination to each of the cubes
- for y in range(0, ysize):
- for x in range(0, xsize):
- cubes[l].setDest(x + .5, y + .5, z)
- l += 1
- #Now the cubes are set up and in the right posistions
- cZ = 0
- for c in range (0, len(cubes)):
- print(cubes[c].curX, ",", cubes[c].curY, ",", cubes[c].curZ, ":", cubes[c].destX, ",", cubes[c].destY, ",", cubes[c].destZ)
- if (cubes[c].destZ > cZ):
- frameStart = frame
- cZ = cubes[c].destZ
- else:
- frame = frameStart
- while ((cubes[c].curX != cubes[c].destX) or (cubes[c].curY != cubes[c].destY) or (cubes[c].curZ != cubes[c].destZ)):
- error = 0
- if (cubes[c].curX != cubes[c].destX):
- if (cubes[c].destX - cubes[c].curX > 0):
- dist = 1
- else:
- dist = -1
- if (not isCube(cubes[c].curX + dist, cubes[c].curY, cubes[c].curZ)):
- moveCube(str(cubes[c].id), cubes[c].curX + dist, cubes[c].curY, cubes[c].curZ, frame)
- cubes[c].curX += dist
- frame += fs
- else:
- error = 1
- if (cubes[c].curY != cubes[c].destY):
- if (cubes[c].destY - cubes[c].curY > 0):
- dist = 1
- else:
- dist = -1
- if (not isCube(cubes[c].curX, cubes[c].curY + dist, cubes[c].curZ)):
- moveCube(str(cubes[c].id), cubes[c].curX, cubes[c].curY + dist, cubes[c].curZ, frame)
- cubes[c].curY += dist
- frame += fs
- else:
- error = 1
- if (cubes[c].curZ != cubes[c].destZ):
- if (cubes[c].destZ - cubes[c].curZ > 0):
- dist = 1
- else:
- dist = -1
- if (not isCube(cubes[c].curX, cubes[c].curY, cubes[c].curZ + dist)):
- moveCube(str(cubes[c].id), cubes[c].curX, cubes[c].curY, cubes[c].curZ + dist, frame)
- cubes[c].curZ += dist
- frame += fs
- if (error == 1):
- moveCube(str(cubes[c].id), cubes[c].curX, cubes[c].curY, cubes[c].curZ + dist, frame)
- cubes[c].curZ += dist
- frame += fs
- bpy.context.scene.frame_end = frame
- print("Done! :D")
Advertisement
Add Comment
Please, Sign In to add comment