#-------------------------------- #--Fliping Blocks version 0.1---- #---Functions page--------------- #--Written from scratch,as first #--project by Barak Koren-------- #---------20\03\2009--------------- import maya.cmds as cmds import random import math #---------Restrat Timeline----------------------- cmds.playbackOptions(minTime=1) cmds.playbackOptions(maxTime=1000) #------End of Timeline--------------------------- #------Start Shaders-------------------------------- #--------------------------------------------------- #-------------RED--------------------- cmds.select(cmds.ls(mat=1)) if cmds.objExists('red'): print "Red already exist" else : cmds.shadingNode('lambert',asShader=1,n='red') cmds.setAttr('red.color', 1, 0, 0, type="double3") cmds.sets (renderable=True ,noSurfaceShader = 1 ,empty=1 ,name='redSG') cmds.connectAttr ('red.outColor' ,'redSG.surfaceShader',f=1) #-------------End RED--------------------- #----------Green------- cmds.select(cmds.ls(mat=1)) if cmds.objExists('green'): print "Green already exist" else : cmds.shadingNode('lambert',asShader=1,n='green') cmds.setAttr('green.color', 0, 1, 0, type="double3") cmds.sets (renderable=True ,noSurfaceShader = 1 ,empty=1 ,name='greenSG') cmds.connectAttr ('green.outColor' ,'greenSG.surfaceShader',f=1) #----------End Green------- #-------Blue------------ cmds.select(cmds.ls(mat=1)) if cmds.objExists('blue'): print "Blue already exist" else : cmds.shadingNode('lambert',asShader=1,n='blue') cmds.setAttr('blue.color', 0, 0, 1, type="double3") cmds.sets (renderable=True ,noSurfaceShader = 1 ,empty=1 ,name='blueSG') cmds.connectAttr ('blue.outColor','blueSG.surfaceShader',f=1) #------- end of Blue------------ #------------------------------------------------------------------ #------End of Shaders-------------------------------- #-------Delete All Transforms #-------Beside the first and last three (cameras) def delete_all(): if ((cmds.ls(tr=1))[1:-3]):cmds.delete(cmds.ls(tr=1)[1:-3]) #---------------------------------------------- #-------Delete All Transforms------------------ #----Select diffrent Shader------- #---Input:(Shader name ,name of the object)------------------------------ #----------------------------------------------------- def chnage_color(x,name): cmds.sets (name,e=1, forceElement='%sSG'%x) #----End of Select diffrent Shader------- #--- Geting center points from sides of the box--- #------------------------------------------------- #definition to_be_rotate : #Location=The location of edge center point. #(Name Of Object,Location1,Location2,Location3,Location4) #Input: (to_be_rotate) #Output:(to_be_rotate) def get_sides (to_be_rotate) : #--------------- Array ------ #print to_be_rotate[0][0] victor= cmds.xform ('%s.e[18]'%to_be_rotate[0][0] ,q=1,ws=1,t=1) to_be_rotate[1]=((victor[0]+victor[3])/2,(victor[1]+victor[4])/2,(victor[2]+victor[5])/2) victor= cmds.xform ('%s.e[19]'%to_be_rotate[0][0] ,q=1,ws=1,t=1) to_be_rotate[2]=((victor[0]+victor[3])/2,(victor[1]+victor[4])/2,(victor[2]+victor[5])/2) victor= cmds.xform ('%s.e[4]'%to_be_rotate[0][0] ,q=1,ws=1,t=1) to_be_rotate[3]=((victor[0]+victor[3])/2,(victor[1]+victor[4])/2,(victor[2]+victor[5])/2) victor= cmds.xform ('%s.e[1]'%to_be_rotate[0][0] ,q=1,ws=1,t=1) to_be_rotate[4]=((victor[0]+victor[3])/2,(victor[1]+victor[4])/2,(victor[2]+victor[5])/2) return to_be_rotate #-------------------------------------------------------- #--- End of geting center points from sides of the box--- #-------------------------------------------------------- #----------Make List of Cubes #Making the Cubes list to be used in animation #Input :(Number of Cubes wanted) #Output:List of keyframed in the begining visibility off boxes. #I will have to make option for location chnage (the deafult is (0.5,0,0.5) def make_list_cube(number_of_cube,x,y,z,width,height,depth): boxes=[] for i in range(0,number_of_cube+2): boxes.append(cmds.polyCube( sy=2,w=width,d=depth,h=height )) cmds.move(x,y,z) cmds.setAttr ("%s.visibility" %boxes[i][0], 0) cmds.setKeyframe (t=1) return boxes #-------------------------------------------------------------------- #----------End of Make List of Cubes #--------Choose Direction-------------------------------------------- #Input:(To_be_rotate,side) #Output:[point,speed vector,direction] def choose_direction (to_be_rotate,direction) : #---------------------Array---------String if (direction=='right') : chosen=to_be_rotate[1] #choosing point who represnt side from the list to_be_rotate[1]-to_be_rotate[4] max_v=to_be_rotate[1][0] # in this case of right side checking saveing the x value. for i in range(2,5) : if (to_be_rotate[i][0]>max_v ): # checking each x value from every point. chosen=to_be_rotate[i] max_v=to_be_rotate[i][0] return [chosen,(0,0,-6),direction] # after checking it will returen the point who contain the max x value . if (direction=='down') : chosen=to_be_rotate[1] max_v=to_be_rotate[1][2] for i in range(2,5) : if (to_be_rotate[i][2]>max_v ): chosen=to_be_rotate[i] max_v=to_be_rotate[i][2] return [chosen,(6,0,0),direction] if (direction=='forward') : chosen=to_be_rotate[1] max_v=to_be_rotate[1][2] for i in range(2,5) : if (to_be_rotate[i][2]maxborder) or (ymin>maxborder) or (zmin>maxborder)) or ((xmax>maxborder) or (ymax>maxborder) or (zmax>maxborder)): cmds.select(to_be_rotate[0][0]) return 0 if ((xmin xmin ) and (Txmin < xmax )) or ((Txmax > xmin ) and (Txmax < xmax ) )or((Ax_temp >= xmin ) and (Ax_temp <= xmax ) ): check1+=1 if ((Tymin > ymin ) and (Tymin < ymax )) or ((Tymax > ymin ) and (Tymax < ymax )) or ((Ay_temp >= ymin ) and (Ay_temp <= ymax )): check1+=1 if ((Tzmin > zmin ) and (Tzmin < zmax )) or ((Tzmax > zmin ) and (Tzmax < zmax )) or ((Az_temp >= zmin ) and (Az_temp <= zmax )): check1+=1 #---------------- if some point of bbox check inside some object in space if ((xmin > Txmin ) and (xmin < Txmax )) or ((xmax > Txmin ) and (xmax < Txmax )) or ((Ax_check >= Txmin ) and (Ax_check <= Txmax )): check2+=1 if ((ymin > Tymin ) and (ymin < Tymax )) or ((ymax > Tymin ) and (ymax < Tymax )) or ((Ay_check >= Tymin ) and (Ay_check <= Tymax )): check2+=1 if ((zmin > Tzmin ) and (zmin < Tzmax )) or ((zmax > Tzmin ) and (zmax < Tzmax )) or ((Az_check >= Tzmin ) and (Az_check <= Tzmax )): check2+=1 if ((check1 == 3 ) or (check2 == 3 )) : cmds.select(to_be_rotate[0][0]) return 0 cmds.select(to_be_rotate[0][0]) return 1 #------------------------------------------------------------------------------- #------------------End Of "The checking" function ------------------------------------------ #.............................................................................. #------------------Cheking all sides Function--------------------- #------------------Retrive object and check where it can be rotate---- #input: (To_be_rotate) #output:(List of sides available to flip) def check_all_sides (to_be_rotate,min_border,maxborder): temp=to_be_rotate[0] if ((cmds.ls(sl=1))!=to_be_rotate[0]) : to_be_rotate[0]=(cmds.ls(sl=1)) #cmds.makeIdentity( apply=True, t=0, r=1, s=1, n=0 ) sides=['left','right','forward','down'] side_options=[] for i in sides : if (check_side_ok(to_be_rotate,i,min_border,maxborder)): side_options.append(i) to_be_rotate[0]=temp return side_options #----------------------------------------------------------- #------------------Cheking all sides Function--------------------- #.................................................................. #-------------------------------------------------------------------------- #------------Checking which objects can be rotating or in other words #----in other words wich objects pass the check all sides function #---input(To_Be_rotate) #---Output(List of objects that can be rotate) #-------------------------------------------------------------------------- def free_way_list(to_be_rotate,min_border,maxborder):# return list of aviable object for rotate temp=to_be_rotate[0] list_next_obj=((cmds.ls(tr=1,v=1))) x=[] for i in list_next_obj: cmds.select(i) if (check_all_sides(to_be_rotate,min_border,maxborder)): x.append(i) to_be_rotate[0]=temp cmds.select (to_be_rotate[0]) return (x) #------------End of Checking which objects can be rotating or in other words #----------Arrange function--------------- # Arrange the list of boxes acording standart way of countring from # left to right and then down , left to right and so on..... #--Input(scaning area(border range),the smallest edege in the sence) #--Output(clean list after delete all duplicates in sence) def minmax_clean (border_range,smallest_edge) : # i wish i heard more about list commands before i wrote this script! dammm me. line=0 w=smallest_edge #search area sort_list=[] new_list=[] big_list=[] while (line <= border_range): list_next_obj=((cmds.ls(tr=1,v=1))) num_list=len(list_next_obj) sort_list=[] for i in list_next_obj: point=cmds.exactWorldBoundingBox(i) if (point[5] <= (line+(w/10))) and (point[5] >=( line-(w/10))): sort_list.append(i) while (sort_list) : place=0 smallX=(cmds.exactWorldBoundingBox(sort_list[0]))[3] for i in range(0,len(sort_list)): smallX_temp=(cmds.exactWorldBoundingBox(sort_list[i]))[3] if (smallX > smallX_temp): smallX=smallX_temp place=i new_list.append(sort_list.pop(place)) new_list = clean_list(new_list) big_list=big_list+new_list new_list=[] line+=w return(big_list) #----------------------------------------- #----------End Of Arrange function--------------- #----------------------------------------- #----------------------------------------- #----------Clean Function----------------- #--Clean copies of objects which exist in the same place. def clean_list (new_list) : list01=[] #making same size list as mew_list but with "1" for i in new_list: list01.append(1) for i in range (0,(len(new_list))): # check if there are duplictions and where there are puting "0" in list01 flag=0 for j in range (0,(len(new_list))): bboxI=(cmds.exactWorldBoundingBox(new_list[i]))[3] bboxJ=(cmds.exactWorldBoundingBox(new_list[j]))[3] if (flag) and ((round(bboxI,4))==(round(bboxJ,4))):list01[j]=0 if new_list[i]==new_list[j]:flag=1 del_list=[] temp_list=[] for i in range (0,(len(new_list))): if (list01[i]): temp_list.append(new_list[i]) not_list=filter(lambda x:x not in temp_list,new_list) if (not_list): cmds.delete(not_list) return temp_list #----------------------------------------- #----------End of Clean Function---------- #----------------------------------------- def start_software (number_box,x,y,z,width,height,depth,min_border,maxborder,side): #delete_all() to_be_rotate=[1,2,3,4,5] boxes=[] boxes=make_list_cube(number_box,x,y,z,width,height,depth) #making the first frame! #----------------The first box----------- to_be_rotate[0]=([boxes.pop(0)[0]]) cmds.select(to_be_rotate[0]) if ((cmds.currentTime(q=1))>2) : #this made in case we want to start animation after number of frame cmds.setKeyframe(t=cmds.currentTime(q=1)-1) cmds.setAttr ("%s.visibility" %to_be_rotate[0][0],1) cmds.setKeyframe() to_be_rotate=get_sides(to_be_rotate) #----------------The second box that will be turned----------- to_be_rotate[0]=[boxes.pop(0)[0]] cmds.select(to_be_rotate[0]) if ((cmds.currentTime(q=1))>2) : #this made in case we want to start animation after number of frame cmds.setKeyframe(t=cmds.currentTime(q=1)-1) cmds.setAttr ("%s.visibility" %to_be_rotate[0][0],1) cmds.setKeyframe() #----------------end of The second box that will be turned----------- cmds.currentTime(cmds.currentTime(q=1)+2,e=1) # we moving 2 frame forward so the everything will start move only from frame 2 sides=['left','right','forward','down'] for i in range(0,number_box) : option_list=check_all_sides(to_be_rotate,min_border,maxborder) directions=len(option_list) if (directions): roto(to_be_rotate,side,1,boxes) #option_list[random.randint(0,directions-1)] #SPACE ENGINES 10 pm ET/PT start_software(25,0.5,0,10,1,0.1,1.75,-1,15,'right') #start_software(10,0.5,0,6.5,1,0.1,1.75,-1,15,'right') #-(number flip,(xyz) start position,width,height,depth,min_border,maxborder,side) blocks_rgb=(minmax_clean(7,1)) #general functain rgb=['red','green','blue'] #random color funcation for i in blocks_rgb: chnage_color(rgb[random.randint(0,2)],i) #rgb pattern color='red' for i in blocks_rgb: flag=1 if (color=='red') and (flag): chnage_color(color,i) color='green' flag=0 if (color=='green') and (flag): chnage_color(color,i) color='blue' flag=0 if (color=='blue') and (flag): chnage_color(color,i) color='red' flag=0