# This script is an interface to be used with Bassam Kurdali's Rigarule system, providing a faster way to create and apply rules. # I apoligize for the messy, undocumented and unfinished code. So far everthing seems to work as intended here, except the leftside # buttons are not implemented yet. I need add a some checking for bones that are selected but on an invisible layer, sometimes it makes # you wonder why appearently nothing happens. # # written by Ivo Grigull import Blender from Blender import Draw, BGL, Armature, Window, Constraint, Text import marulez reload(marulez) affectedbonename = method = target1 = target2 = target3 = '' tar1 = tar2 = tar3 = mypart = tarpart = 0 option = 0 Xval = Yval = Zval = 0.0 eval = 1 callMethod = [] for ook in [ook for ook in dir(marulez) if "RULE" in ook] : callMethod.append(ook.replace("RULE","")) bonepart = ["","head","tail","roll"] mypartmenu = "Bone Part: %t|all %x0|head %x1|tail %x2|roll %x3" tarpartmenu = "Target Part: %t|all %x0|head %x1|tail %x2|roll %x3" rulemenu = "Rig Rule: %t|" for rule in callMethod: rulemenu = rulemenu + rule +" %x"+str(callMethod.index(rule))+"|" method = callMethod[option] bonemenu = "Selected Bones: %t|" bonelist = [] def getSelectedBones(): # returns a list containing the names (strings) of the selected bones # find the selected bones ob = Blender.Object.GetSelected()[0] selectedbones = [] pose = ob.getPose() for n in pose.bones.values(): if n.sel: selectedbones.append(n.name) return selectedbones def buildBoneMenu(selectedbones): # takes the selected bones and builds a bone menu from them global bonemenu bonemenu = "Selected Bones: %t|" for bonename in selectedbones: bonemenu += bonename +" %x"+str(selectedbones.index(bonename))+"|" def addRuleConstraint( affectedbonename, method=None, target1=None, target2=None, target3=None, mypart=None, tarpart=None, eval=None, Xval=None, Yval=None, Zval=None): ob = Blender.Object.GetSelected()[0] pose = ob.getPose() affectedbone = pose.bones[affectedbonename] BCS=Blender.Constraint.Settings # shorthand dictionary - define with brace, reference with bracket constr = affectedbone.constraints.append( Constraint.Type.PYTHON ) constr[BCS.SCRIPT] = Text.Get('rule.py') if not method == None: constr.name = 'rule ' + method else: constr.name = 'blank' if not method == None: constr[BCS.PROPERTIES]['method'] = method else: constr[BCS.PROPERTIES]['method'] = "copy" if not target1 == None: constr[BCS.PROPERTIES]['target1'] = target1 else: constr[BCS.PROPERTIES]['target1'] = '' if not target2 == None: constr[BCS.PROPERTIES]['target2'] = target2 else: constr[BCS.PROPERTIES]['target2'] = '' if not target3 == None: constr[BCS.PROPERTIES]['target3'] = target3 else: constr[BCS.PROPERTIES]['target3'] = '' if not tarpart == None: constr[BCS.PROPERTIES]['tarpart'] = bonepart[tarpart] else: constr[BCS.PROPERTIES]['tarpart'] = '' if not eval == None: constr[BCS.PROPERTIES]['eval'] = eval else: constr[BCS.PROPERTIES]['eval'] = 1 if not mypart == None: constr[BCS.PROPERTIES]['mypart'] = bonepart[mypart] else: constr[BCS.PROPERTIES]['mypart'] = '' if not Xval == None: constr[BCS.PROPERTIES]['Xval'] = Xval if not Yval == None: constr[BCS.PROPERTIES]['Yval'] = Yval if not Zval == None: constr[BCS.PROPERTIES]['Zval'] = Zval constr.influence=0.0 #might be unneeded Blender.Scene.getCurrent().update() Blender.Window.RedrawAll() def event(evt, val): # the function to handle input events global mypartmenu,tarpartmenu, bonemenu, bonelist,rulemenu,callMethod, affectedbonename , method , target1 , target2 , target3 , mypart , tarpart , eval , Xval , Yval , Zval, option,tar1, tar2, tar3, bonepart if evt == 10: # Apply to selected bone. ob = Blender.Object.GetSelected()[0] if not ob == None: target1 = getSelectedBones()[0] Blender.Redraw() elif evt == 20: eval = val # user did some input to collect elif evt == 40: option = val # ... method = callMethod[option] elif evt == 60: mypart = val elif evt == 80: target1 = val # ... elif evt == 84: tar1 = val target1 = bonelist[val] Draw.Redraw() elif evt == 104: tar2 = val target2 = bonelist[val] Draw.Redraw() elif evt == 100: target2 = val elif evt == 124: tar3 = val target3 = bonelist[val] Draw.Redraw() elif evt == 100: target2 = val elif evt == 120: target3 = val elif evt == 140: tarpart = val elif evt == 160: Xval = val elif evt == 180: Yval = val elif evt == 200: Zval = val if evt == 221: # apply to first selected bone bone = getSelectedBones()[0] if not bone == None: addRuleConstraint( bone , method , target1 , target2 , target3 , mypart , tarpart , eval , Xval , Yval , Zval) if evt == 231: # Preset copy & align bone = getSelectedBones()[0] if not bone == None: addRuleConstraint( bone , 'copy' , target1 , '' , '' , '' , '' , eval) addRuleConstraint( bone , 'align' , target1 , '' , '' , '' , '' , eval) if evt == 232: # Preset copy head bone = getSelectedBones()[0] if not bone == None: addRuleConstraint( bone , 'copy' , target1 , '' , '' , 'head' , 'head' , eval) if evt == Draw.ESCKEY: Draw.Exit() # exit when user presses ESC return if evt == Draw.RKEY: Draw.Draw() # Draw.Redraw(1) def button_event(evt): # the function to handle Draw Button events global mymsg, toggle def gui(): # the function to draw the screen height = 15 width = 200 xstrings = 30 global tarpartmenu, mypartmenu, bonemenu,bonelist, rulemenu,callMethod, affectedbonename , method , target1 , target2 , target3 , mypart , tarpart , eval , Xval , Yval , Zval ,option, tar1,tar2,tar3 print method print option bonelist = getSelectedBones() buildBoneMenu(bonelist) A_tooltip = 'Apply the new setting of this column to all rule constraints of the selcted bones' Draw.PushButton("Copy & Align", 231, xstrings , 175, 100, height, 'Preset: add one copy rule and an align rule immediately. The resulting bone will have the same transformations as target1', event) Draw.PushButton("Copy head", 232, xstrings + 100, 175, 100, height, 'Preset: this only reposition the bone to the target without changing the orientation', event) Draw.PushButton("Sel as Target", 10, xstrings + 100, 155, 100, height, 'Insert selected bone into the target1 field', event) Draw.Number("Priority:", 20, xstrings, 155, 100, height, int(eval), 0, 999, '0 is highest', event) Draw.PushButton("A", 30, 5, 155, 20, height, A_tooltip, event) # Draw.String("Rig Rule:", 40, xstrings, 135, width, height, method, 40, 'Insert the name of the rule that you want here', event) Draw.Menu(rulemenu,40, xstrings, 135, width, height, option, 'Insert the name of the rule that you want here', event) Draw.PushButton("A", 50, 5, 135, 20, height, A_tooltip, event) # Draw.String("Bone Part:", 60, xstrings, 120, width, height, mypart, 40, 'head, tail, roll or blank', event) Draw.Menu(mypartmenu,60, xstrings, 120, width, height, mypart, 'Insert the name of the rule that you want here', event) Draw.PushButton("A", 70, 5, 120, 20, height, A_tooltip, event) # Draw.String("Target 1:", 80, xstrings, 100, width, height, target1, 40, 'First target (.head, .tail allowed)', event) Draw.Menu(bonemenu, 84, xstrings, 100, 20, height,tar1, 'First target (.head, .tail allowed)', event) Draw.String("", 80, xstrings+20, 100, width-20, height, target1, 40, 'First target (.head, .tail allowed)', event) Draw.PushButton("A", 90, 5, 100, 20, height, A_tooltip, event) Draw.Menu(bonemenu, 104, xstrings, 85, 20, height,tar2, 'First target (.head, .tail allowed)', event) Draw.String("", 100, xstrings+20, 85, width-20, height, target2, 40, 'Second target (.head, .tail allowed)', event) Draw.PushButton("A", 110, 5, 85, 20, height, A_tooltip, event) Draw.Menu(bonemenu, 124, xstrings, 70, 20, height,tar3, 'First target (.head, .tail allowed)', event) Draw.String("", 120, xstrings+20, 70, width-20, height, target3, 40, 'Third target (.head, .tail allowed)', event) Draw.PushButton("A", 130, 5, 70, 20, height, A_tooltip, event) #Draw.String("Target Part:", 140, xstrings, 55, width, height, tarpart, 40, 'head, tail, roll or blank', event) Draw.Menu(tarpartmenu, 140,xstrings, 55, width, height, tarpart, 'head, tail, roll or blank', event) Draw.PushButton("A", 150, 5, 55, 20, height, A_tooltip, event) Draw.Number("X:", 160, xstrings, 40, 100, height, Xval, -10, 10, 'X Value', event) Draw.PushButton("A", 170, 5, 40, 20, height, A_tooltip, event) Draw.Number("Y:", 180, xstrings, 25, 100, height, Yval, -10, 10, 'Y Value', event) Draw.PushButton("A", 190, 5, 25, 20, height, A_tooltip, event) Draw.Number("Z:", 200, xstrings, 10, 100, height, Zval, -10, 10, 'Z Value', event) Draw.PushButton("A", 210, 5, 10, 20, height, A_tooltip, event) Draw.PushButton("Add to bone", 221, xstrings+105, 15, 95, (height*3)-10, 'Use the given Settings and apply the rule constraint to the first selected bone', event) Draw.Register(gui, event, button_event) # registering the 3 callbacks print("moo")