I am making an animated Rubik's Cube. The rotations work as intended. After a layer is turned, the pieces must be renamed to reflect their new location before executing another rotation. This works fine when I run my script layer by layer. However, if I want to rotate multiple layers in one execution of my script, the objects are not renamed before the next layer attempts to rotate, causing the cube to not perform as intended.
import bpy
def run():
#deselect everything
for obj in bpy.data.objects:
obj.select = False
#The following rotations can be made:
# Up Clockwise = U
# Up Counterclockwise = U'
# Down Clockwise = D
# Down Counterclockwise = D'
# Right Clockwise = R
# Right Counterclockwise = R'
# Left Clockwise = L
# Left Counterclockwise = L'
# Front Clockwise = F
# Front Counterclockwise = F'
# Back Clockwise = B
# Back Counterclockwise = B'
# First rotation works fine
Rotation("B'")
# Second Rotation causes issues (only when rotations share common pieces)
Rotation("U")
return
def Rotation(type):
objects = [None]*9
#Select layers based on rotation
#Right Layer
if type == "R" or type == "R'":
pieces = ['C.UFR','C.UBR','C.DBR','C.DFR','E.UR','E.BR','E.DR','E.FR','M.R'];
#Left Layer
elif type == "L" or type == "L'":
pieces = ['C.UBL','C.UFL','C.DFL','C.DBL','E.UL','E.FL','E.DL','E.BL','M.L'];
#Up Layer
elif type == "U" or type == "U'":
pieces = ['C.UBL','C.UBR','C.UFR','C.UFL','E.UB','E.UR','E.UF','E.UL','M.U'];
#Down Layer
elif type == "D" or type == "D'":
pieces = ['C.DFL','C.DFR','C.DBR','C.DBL','E.DF','E.DR','E.DB','E.DL','M.D'];
#Front Layer
elif type == "F" or type == "F'":
pieces = ['C.UFL','C.UFR','C.DFR','C.DFL','E.UF','E.FR','E.DF','E.FL','M.F'];
#Back Layer
elif type == "B" or type == "B'":
pieces = ['C.UBR','C.UBL','C.DBL','C.DBR','E.UB','E.BL','E.DB','E.BR','M.B'];
#Select Objects
i = 0
while i < len(pieces):
objects[i] = bpy.data.objects.get(pieces[i])
objects[i].select = True
i += 1
#Rotate Right or Left Layer
if type == "R" or type == "L'":
bpy.ops.transform.rotate(value=-1.5708, axis=(1, 0, 0), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
elif type == "R'" or type == "L":
bpy.ops.transform.rotate(value=1.5708, axis=(1, 0, 0), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
#Rotate Up or Down Layer
elif type == "U" or type == "D'":
bpy.ops.transform.rotate(value=-1.5708, axis=(0, 0, 1), constraint_axis=(False, False, True), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
elif type == "U'" or type == "D":
bpy.ops.transform.rotate(value=1.5708, axis=(0, 0, 1), constraint_axis=(False, False, True), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
#front or Back
elif type == "F" or type == "B'":
bpy.ops.transform.rotate(value=1.5708, axis=(0, 1, 0), constraint_axis=(False, True, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
elif type == "F'" or type == "B":
bpy.ops.transform.rotate(value=-1.5708, axis=(0, 1, 0), constraint_axis=(False, True, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)
#THIS RENAMING CODE NEEDS TO COMPLETE BEFORE THIS FUNCTION CAN BE RUN AGAIN
#rename layers on clockwise turn
if len(type) == 1:
#corners
objects[0].name = ""
objects[3].name = pieces[0]
objects[2].name = pieces[3]
objects[1].name = pieces[2]
objects[0].name = pieces[1]
#edges
objects[4].name = ""
objects[7].name = pieces[4]
objects[6].name = pieces[7]
objects[5].name = pieces[6]
objects[4].name = pieces[5]
#rename layers on counterclockwise turn
if len(type) == 2:
#corners
objects[0].name = ""
objects[1].name = pieces[0]
objects[2].name = pieces[1]
objects[3].name = pieces[2]
objects[0].name = pieces[3]
#edges
objects[4].name = ""
objects[5].name = pieces[4]
objects[6].name = pieces[5]
objects[7].name = pieces[6]
objects[4].name = pieces[7]
return
run()
The issue arises because the cubes are not renamed before the next rotation occurs.
How can I workaround this?
EDIT: Problem was completely unrelated. I simply had my deselect loop in the wrong function. Thanks