2
$\begingroup$

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

$\endgroup$
3
  • 1
    $\begingroup$ Cool, would consider using custom properties rather than the name change. Had a go at this using 3 3x3 matrices (x axis alignment) and manipulate them based on 3 axes of revolution. $\endgroup$
    – batFINGER
    Jan 13, 2019 at 9:19
  • $\begingroup$ That's a good idea. I'm thinking more like a cuber than a programmer, and I think the naming conventions reflect that. I'm planning on starting anew with the newly acquired knowledge, and I'll consider doing it that way. $\endgroup$
    – Dan White
    Jan 13, 2019 at 10:20
  • $\begingroup$ Never did work out the cube .. used to break apart and re-assemble lol. $\endgroup$
    – batFINGER
    Jan 13, 2019 at 10:25

1 Answer 1

1
$\begingroup$

It seems that you need to deselect everything in the Rotation() instead of run().

$\endgroup$
1
  • $\begingroup$ Holy cow. I went down a crazy rabbit hole of looking at modals and all kinds of other stuff I didn't need. I feel dumb. What an easy fix. Thank you for your time. $\endgroup$
    – Dan White
    Jan 13, 2019 at 8:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .