#!/usr/local/bin/pythono from Tkinter import * import tkFileDialog, os, gzip global sourceFolder, Files, comp, reg, figDict, File_lines, actorList, parmDict,\ figname, fignum, actname, addList, ext, version, injpose, rempose, File, useProps sourceFolder = "C:\\Program Files\\Curious Labs\\Poser 5\\Runtime\\libraries" comp = [".crz", ".ppz",".pzz"] reg = [".cr2", ".pp2",".pz3"] figDict = {} File_lines = [] actorList = [] parmDict = {} figname = "" fignum = "" actname = "" addList = [] ext = "" version = "" injpose = 1 rempose = 0 File = "" useProps = 0 def commentHelp(): """ This script creates JP insertion poses from .cr2/.crz, .pp2/.ppz, or .pz3/.pzz files. It will save a .pz2 if the source file is uncompressed and a .p2z file if the source is compressed. One or more figures, with or without props, can be selected. The script will save a separate pose for each figure, but can either include props with the parent figure or create separate poses for them. The GUI uses a series of listboxes to display figures, actors, morphs, and the final pose outline. Use: 1) "Select File" to browse for a file. 2) Once the figures are displayed in the first listbox, highlight one and either double-click it or press "Select Figure" to display that figure's actors and props in listbox 2. 3) Once the selected figure's actors and props are displayed in the second listbox, either select an actor or prop as with the first listbox, or: A) Click "All Actors" to add all actors in the current figure and all their morphs to the outline list, OR... B) Click "All Parms" to add all parameters for the currently selected actor to the outline list. 4) If an actor or prop was selected via double-click or "Select Actor", the third listbox will now display of all the available parameters for the selected actor. Morphs without deltas will not be listed. Select as many of these listed morphs as desired (using shift or ctrl for multiple selections) and click "Add Parameters" (or double-click on the selection (while holding down ctrl for multiples)) to add the selected joint parameters to the outline listbox. 5) The outline list in listbox 4 can be cleared using the "Clear All" button. Items can be removed by selecting them (shift or ctrl for multiples) and double RIGHT-clicking on the selection (hold down ctrl while clicking, for multiples). The outline list shows all figures, actors and morphs which will be included in the final pose. (Note that if all morphs are removed from an actor or prop in this list, but the actor is left in the list, the actor will be entered in the final pose with an empty channels section. A listed figure with no actors will not generate a pose. A listed figure for which all actors lack listed morphs will generate a pose with no channels content. When deleting items from the outline list, it is best to remove the parents when appropriate.) 6) Check the "Include Props with figure" checkbutton if figure poses should include origin data for props. Props will be written to separate poses, by default. 7) Finally, click "Run". The resulting pose(s) will be saved to the source folder from which the Poser file being processed was loaded. Poses will be named after the source file, with the pose's target figure name added and a numerical suffix as needed, to prevent accidental overwriting of any existing poses. Cagedrei@aol.com """ print commentHelp.__doc__ #The warning: this code is disorganized, tangled, inexpert, and kludge-ridden. Don't be like Cage. Don't write #miserable obfuscated spaghetti Python. If you are looking at this hoping to learn how to do something, beware #of what this code may teach. Cage is a bad influence. Bad dog! But it works. def processIt(): global version, addList if version.split("version")[0].count("{") == 0: version = "{\n"+version lines2 = [] lines2.append(version) figname = "" actname = "" parmname = "" parm = "" props = 0 figline = "" isProp = 0 centers = "" for linenum,line in enumerate(addList): if line.find("!@!_") != -1: figname = line.replace("!@!_","").split("<:>")[0] line = line.replace("!@!_","figurename ").split("<:>")[0] figline = line if line.find("!#!_") != -1 and addList[linenum-1].find("!%!_") != -1 or\ line.find("!#!_") != -1 and addList[linenum-1].find("!#!_") != -1: #in case an actor has no listed morphs lines2.append("\t\t}\n\t}\n") #close the previous actor section if line.find("!#!_") != -1: origin = "" endPoint = "" orientation = "" if isProp == 1: #Add figline after prop section, to split and re-assemble later lines2.append(figline) isProp = 0 actname = line.replace("!#!_","").split("<:>")[0] if actname.find("prop ") == -1: line = line.replace("!#!_","").split("<:>")[0] else: lines2.append(line.replace("!#!_prop","propname ").split("<:>")[0]) #prop flag line = line.replace("!#!_","").split("<:>")[0] props = 1 isProp = 1 line += "\n\t{\n\tchannels\n\t\t{\n" #add the channels header to the actor if figname != "" and actname != "": #Don't run orphaned morph listings if line.find("!%!_") != -1: #proces morphs for actor or prop parmname = line.replace("!%!_","").split("<:>")[0] parms = parmDict[figname+" "+actname] for i in parms: if parmname == i[0]: parm = i[1] if parm.find("\torigin ") == -1 and parm.find("\tendPoint ") == -1 and\ parm.find("\torientation ") == -1: line = processMorph(parm) if parm.find("\torigin ") != -1: origin = parm.replace("!%!_","").split("<:>")[0] if parm.find("\tendPoint ") != -1: endPoint = parm.replace("!%!_","").split("<:>")[0] if parm.find("\torientation ") != -1: orientation = parm.replace("!%!_","").split("<:>")[0] tempsplit = line.replace("\r","\n").split("\n") for i in tempsplit: if i != "": if i.find("<:>") == -1: lines2.append(i+"\n") tempsplit = "" line = "" if line.find("**JOINT_CENTERS_FLAG**") != -1: centers = "\t\t}\n%s%s%s\t}\n" %(origin,endPoint,orientation) line = centers lines2.append(line) lines2.append("}") if props == 1 and useProps == 0: readfix(lines2,1) #Run props alone in a separate pass readfix(lines2,0) #Run figure(s), with or without included props def processMorph(morph): lines = morph.replace("\r","\n").split("\n") morph2 = "" del_lines = 0 for linenum, line in enumerate(lines): if del_lines == 0: line += "\n" morph2 += line morph2 += "\t\t\t}\n" return morph2 def readfix(lines2, onlyProps): global version sep = "\\" splitstring = "" splitname = "" splitlist = [] splitcount = 0 splitlist2 = [] props = {} figs = {} group = {} #split lines2 into parts (fig morphs, prop section, fig mats) and keep a dict of prop parenting #This part is kind of messy.... for linenum, line in enumerate(lines2): if line.find("MORPHSLINE:FIGURE") != -1: line = "" #remove remaining morph line flags if line.find("<:>") != -1: line = "" if line.find("figurename") != -1 or line.find("propname") != -1 or linenum+1 == len(lines2): nametemp = line.replace("","").replace("","") splitlist.append(nametemp) splitlist2.append(splitstring) splitstring = "" if nametemp.find("figurename") != -1: #Track prop parenting newname = nametemp.replace("\n","").replace("\r","").replace("figurename ","") if newname not in group.keys(): group[newname] = [] if nametemp.find("propname") != -1: newname2 = nametemp.replace("\n","").replace("\r","").replace("propname ","") group[newname].append(newname2) else: splitstring += line #organize the prop and fig lists created above. Compile fig morphs and mats. Keep props separate for count,name in enumerate(splitlist): if name.find("figurename") != -1: newname = name.replace("\n","").replace("\r","").replace("figurename ","") if newname not in figs.keys(): figs[newname] = splitlist2[count+1] elif newname in figs.keys(): figs[newname] += splitlist2[count+1] elif name.find("propname") != -1: newname = name.replace("\n","").replace("\r","").replace("propname ","") if newname not in props.keys(): props[newname] = splitlist2[count+1] elif newname in props.keys(): props[newname] += splitlist2[count+1] if onlyProps == 1: #Saving only props for i in props.keys(): splitname = "_" + i splitstring = version + props[i] + "}\n" saveit(File, ext, splitstring, splitname) elif onlyProps == 0: #Multiple figures with or without props for i in figs.keys(): splitname = "_" + i splitstring = version if useProps == 1: if i in group.keys() and group[i] != []: for j in group[i]: #print j,props.keys() splitstring += props[j] splitstring += figs[i] + "}\n" if splitstring != version + "\t\t}\n\t}\n}": #don't run a figure which has no actors listed saveit(File, ext, splitstring, splitname) def saveit(File, ext, savetext, splitname): #Write the file(s) posetype = "_JP" if splitname.find(":") != -1: splitname = splitname.split(":")[0] File = File.split(".")[0] + posetype + splitname if ext in reg: File += ".pz2" if os.path.exists(os.path.join(sourceFolder,File)): numb = 0 while os.path.exists(os.path.join(sourceFolder,os.path.splitext(File)[0]+"_"+str(numb)+".pz2")): numb += 1 File = os.path.splitext(File)[0]+"_"+str(numb)+".pz2" save = open(os.path.join(sourceFolder,File),"w") #create new elif ext in comp: File += ".p2z" if os.path.exists(os.path.join(sourceFolder,File)): numb = 0 while os.path.exists(os.path.join(sourceFolder,os.path.splitext(File)[0]+"_"+str(numb)+".p2z")): numb += 1 File = os.path.splitext(File)[0]+"_"+str(numb)+".p2z" save = gzip.GzipFile(os.path.join(sourceFolder,File),"wb") save.write(savetext) save.flush() save.close() print "Created",posetype.replace("_",""),"File ", os.path.join(sourceFolder,File) #----GUI-------------------- def die(): root.destroy() root.quit() root = Tk() class App: def __init__(self, master): self.propVar = IntVar() self.propVar.set(0) self.master = master master.title("Joint Insertion Pose Extractor") self.ButtonFrame = Frame(self.master,borderwidth=2,relief=RIDGE) self.ButtonFrame.grid(row = 1, column = 0) self.ButtonFrame2 = Frame(self.master,borderwidth=2,relief=RIDGE) self.ButtonFrame2.grid(row = 1, column = 1) self.ButtonFrame3 = Frame(self.master,borderwidth=2,relief=RIDGE) self.ButtonFrame3.grid(row = 1, column = 2) self.ButtonFrame4 = Frame(self.master,borderwidth=2,relief=RIDGE) self.ButtonFrame4.grid(row = 1, column = 3) self.ListFrame = Frame(self.master,borderwidth=2,relief=RIDGE) self.ListFrame.grid(row = 0, column = 0) self.ListFrame2 = Frame(self.master,borderwidth=2,relief=RIDGE) self.ListFrame2.grid(row = 0, column = 1) self.ListFrame3 = Frame(self.master,borderwidth=2,relief=RIDGE) self.ListFrame3.grid(row = 0, column = 2) self.ListFrame4 = Frame(self.master,borderwidth=2,relief=RIDGE) self.ListFrame4.grid(row = 0, column = 3) self.listLabel = Label(self.ListFrame, text="Select Figure\nDisplay Actors --->" , anchor=N, justify=LEFT) self.listLabel.grid(row=1, column=1) self.listLabel2 = Label(self.ListFrame2, text="Select Actor\nDisplay Parms--->" , anchor=N, justify=LEFT) self.listLabel2.grid(row=1, column=1) self.listLabel3 = Label(self.ListFrame3, text="Select JP(s)\nAdd to outline --->" , anchor=N, justify=LEFT) self.listLabel3.grid(row=1, column=1) self.listLabel4 = Label(self.ListFrame4, text="Pose Outline\n(Double right-click to remove selected)", anchor=N, justify=LEFT) self.listLabel4.grid(row=1, column=1) # Lists self.ListScroll = Scrollbar(self.ListFrame, orient=VERTICAL) self.ListScroll.grid( row=2, column=0,sticky=N+S+E) self.List = Listbox(self.ListFrame, height=20, width=20, selectmode=SINGLE,exportselection=0, yscrollcommand=self.ListScroll.set) self.List.grid( row=2, column=1) self.ListScroll["command"] = self.List.yview self.ListScroll2 = Scrollbar(self.ListFrame2, orient=VERTICAL) self.ListScroll2.grid( row=2, column=0,sticky=N+S+E) self.List2 = Listbox(self.ListFrame2, height=20, width=20, selectmode=SINGLE,exportselection=0, yscrollcommand=self.ListScroll2.set) self.List2.grid( row=2, column=1) self.ListScroll2["command"] = self.List2.yview self.ListScroll3 = Scrollbar(self.ListFrame3, orient=VERTICAL) self.ListScroll3.grid( row=2, column=0,sticky=N+S+E) self.List3 = Listbox(self.ListFrame3, height=20, width=20, selectmode=EXTENDED,exportselection=0, yscrollcommand=self.ListScroll3.set) self.List3.grid( row=2, column=1) self.ListScroll3["command"] = self.List3.yview self.ListScroll4 = Scrollbar(self.ListFrame4, orient=VERTICAL) self.ListScroll4.grid( row=2, column=0,sticky=N+S+E) self.List4 = Listbox(self.ListFrame4, height=20, width=30, selectmode=EXTENDED,exportselection=0, yscrollcommand=self.ListScroll4.set) self.List4.grid( row=2, column=1) self.ListScroll4["command"] = self.List4.yview self.List4.bind("",self.handleDelete) self.List.bind("",self.handleEvent) self.List2.bind("",self.handleEvent2) self.List3.bind("",self.handleEvent3) self.buttonSource = Button(self.ButtonFrame, text="Select File", command=self.handleFile) self.buttonSource.grid(row=1, column=0) self.buttonFig = Button(self.ListFrame, text="Select Figure", command=self.handleFig) self.buttonFig.grid(row=3, column=1) self.buttonAct = Button(self.ListFrame2, text="Select Actor", command=self.handleAct) self.buttonAct.grid(row=3, column=1) self.buttonAll = Button(self.ButtonFrame2, text="All Actors", command=self.handleAll) self.buttonAll.grid(row=1, column=1) self.buttonAll = Button(self.ButtonFrame2, text="All Parms", command=self.handleActParms) self.buttonAll.grid(row=1, column=2) self.buttonParms = Button(self.ListFrame3, text="Add Parameters", command=self.handleParms) self.buttonParms.grid(row=3, column=1) self.buttonClear = Button(self.ListFrame4, text="Clear All", command=self.handleClear) self.buttonClear.grid(row=3, column=1) self.buttonGo = Button(self.ButtonFrame, text="Run", command=self.handleGo) self.buttonGo.grid(row=1, column=3) self.buttonQuit = Button(self.ButtonFrame, text="Quit", command=die) self.buttonQuit.grid(row=1, column=4) self.buttonHelp = Button(self.ButtonFrame, text="??", command=commentHelp) self.buttonHelp.grid(row=1, column=2) self.propCheck = Checkbutton(self.ButtonFrame3,text="Include props with figure", variable=self.propVar,command=self.toggleIt) self.propCheck.grid(row = 1, column = 1) #mouse-click runners for various listboxes def handleEvent(self,event): self.handleFig() def handleEvent2(self,event): self.handleAct() def handleEvent3(self,event): self.handleParms() def toggleIt(self): global useProps useProps = self.propVar.get() #Process the data added to List4 def handleGo(self): global addList if figDict != {} and actorList != [] and addList != []: grumble = [] for i,j in enumerate(addList): if j.find("!@!") != -1 and i != 0: grumble.append("**JOINT_CENTERS_FLAG**") grumble.append(j) elif j.find("!#!") != -1 and addList[i-1].find("!@!") == -1: grumble.append("**JOINT_CENTERS_FLAG**") grumble.append(j) else: grumble.append(j) if grumble[len(grumble)-1] != "**JOINT_CENTERS_FLAG**": grumble.append("**JOINT_CENTERS_FLAG**") addList = grumble #for i in addList: print i processIt() ##Clear List4 def handleClear(self): global addList self.List4.delete(0,END) addList = [] #Select and open a file, set basic variables, find figures in file and fill List def handleFile(self): global figDict,File_lines,actorList,parmDict, ext, version, File, addList, sourceFolder openList = "" File_type = "" exts2 = [] figDict.clear() actorList = [] addList = [] parmDict.clear() File_lines = [] for i in range(0,len(reg)): exts2.append(reg[i]) exts2.append(comp[i]) for ext1 in exts2: openList += "*"+ext1+" " File = tkFileDialog.askopenfilename(filetypes=[("All Associated Files", openList)],initialdir=sourceFolder) if File: self.List.delete(0,END) self.List2.delete(0,END) self.List3.delete(0,END) self.List4.delete(0,END) self.List.insert(END,"PROCESSING FILE") self.List.insert(END,"PLEASE WAIT...") sourceFolder = os.path.dirname(File)#Store the folder location print "Reading",File,"..." File = os.path.basename(File) ext = os.path.splitext(File)[1] if ext in comp: temptext = gzip.GzipFile(os.path.join(sourceFolder,File)) elif ext in reg: temptext = open(os.path.join(sourceFolder,File),"r") lines = temptext.readlines() if ext == ".pp2" or ext == ".ppz": File_type = "prop" else: File_type = "figure" for linenum,line in enumerate(lines): if line.find("version") != -1: #Store version header if lines[linenum+2].find("number") != -1: version = "version\n\t{\n"+lines[linenum+2]+"\t}\n" else: version = "version\n\t{\n"+"\tnumber 5\n"+"\t}\n" if line.find("name ") != -1 and lines[linenum-2].find(File_type) != -1 and lines[linenum-2].find("Scale") == -1: if not (File_type != "prop" and lines[linenum-2].find("prop") != -1) and lines[linenum-2].find("actor") == -1: if len(line.split("name ")) > 1: figname = line.split("name ")[1].replace("\n","").replace("\r","") elif len(line.split("name ")) == 1: figname = File_type.capitalize() + " 1" if lines[linenum+1].find(":") != -1: fignum = ":"+lines[linenum+1].split(":")[1].replace("\n","").replace("\r","") else: #A new .phi-generated cr2 may have no figure number. It will be the only fig in its file. fignum = ":1" figDict[figname] = fignum File_lines.append(line) temptext.flush() temptext.close() self.List.delete(0,self.List.size()-1) for i in figDict.keys(): self.List.insert(END,i+" ("+figDict[i]+")") #Read the list made from the file, locate actors with morphs for each fig, fill List2, save morph data in parmDict def handleFig(self): global actorList,parmDict,figname,fignum if figDict != {} and self.List.curselection() != (): actorList = [] self.List3.delete(0,self.List3.size()-1) self.List2.delete(0,self.List2.size()-1) self.List2.insert(END,"CHECKING ACTORS") self.List2.insert(END,"PLEASE WAIT...") if ext == ".pp2" or ext == ".ppz": File_type = "doc" else: File_type = "figure" fig = self.List.curselection()[0] fig = int(fig) selected = zip(figDict.keys(),figDict.values()) figname = selected[fig][0].lstrip() fignum = selected[fig][1] print "Finding actors and morphs for",figname parms = [] for linenum,line in enumerate(File_lines): if (line.find("actor ") != -1 or line.find("prop ") != -1) and\ line.find(fignum) != -1 and File_lines[linenum+2].find("name") != -1: actor = line.replace("\t","").replace("\n","").replace("\r","") if figname+" "+actor not in parmDict.keys(): parms = [] templine = linenum+1 while File_lines[templine].find("actor ") == -1 and File_lines[templine].find("prop ") == -1 and\ File_lines[templine].find(File_type) == -1: if (File_lines[templine].find("joint") != -1 or File_lines[templine].find("twist") != -1 or\ File_lines[templine].find("_smoo") != -1 or File_lines[templine].find(" curve") != -1) and\ File_lines[templine].find("jointMult") == -1 and File_lines[templine].find("name ") == -1: templine2 = templine+1 if File_lines[templine2+1].find("GetStringRes") == -1 and File_lines[templine2+1].find("name ") != -1: parmName = File_lines[templine2+1].split("name ")[1].replace("\n","").replace("\r","") else: parmName = File_lines[templine].split(" ")[1].replace("\n","").replace("\r","") parm = File_lines[templine] while not (File_lines[templine2].find("\t\t\t}") != -1 and File_lines[templine2].count("\t") == 3): parm += File_lines[templine2] templine2 += 1 if File_lines[templine2].find("\tendPoint ") != -1 and File_lines[templine2].find("\torigin ") != -1: break parms.append([parmName,parm,""]) if File_lines[templine].find("\torigin ") != -1 or File_lines[templine].find("\tendPoint ") != -1 or\ File_lines[templine].find("\torientation ") != -1: parmName = File_lines[templine].replace("\t","").split(" ")[0] parm = File_lines[templine] parms.append([parmName,parm,""]) templine += 1 if (File_lines[templine].find("actor ") != -1 or File_lines[templine].find("prop ") != -1 or\ File_lines[templine].find(File_type) != -1) and parms != []: parmDict[figname+" "+actor] = parms actorList.append((actor,linenum)) else: actorList.append((actor,linenum)) self.List2.delete(0,self.List2.size()-1) if len(actorList) > 0: for actor in actorList: self.List2.insert(END,actor[0]) else: self.List2.insert(END,"NO MORPHS FOUND IN") self.List2.insert(END,figname.upper()) #Add all actors and parms for a figure def handleAll(self): global parmDict, actname, addList if figDict != {} and actorList != []: self.List3.delete(0,self.List3.size()-1) for count in range(len(actorList)-1,-1,-1): actname = actorList[count][0] parms = parmDict[figname+" "+actname] self.List4.delete(0,END) fill = " " for p in range(len(parms)-1,-1,-1): parmname = parmDict[figname+" "+actname][p][0] fig2 = "!@!_"+figname+"<:>"+fignum act2 = "!#!_"+actname+"<:>"+figname+fignum parm2 = "!%!_"+parmname+"<:>"+actname+fignum if not fig2 in addList: addList.append(fig2) if not act2 in addList: addList.insert(addList.index(fig2)+1,act2) if not parm2 in addList: addList.insert(addList.index(act2)+1,parm2) for i in addList: i2 = i.replace("!@!_","").replace("!#!_",fill*2).replace("!%!_",fill*4) i2 = i2.split("<:>")[0] self.List4.insert(END,i2) def handleActParms(self): self.handleAct() self.List3.select_set(0,END) self.handleParms() self.List3.select_clear(0,END) #Display the morph parameters for selected actor in List3 def handleAct(self): global parmDict, actname if figDict != {} and actorList != [] and self.List2.curselection() != (): self.List3.delete(0,self.List3.size()-1) act = self.List2.curselection()[0] act = int(act) actname = actorList[act][0] parms = parmDict[figname+" "+actname] for parm in parms: self.List3.insert(END,parm[0]) #Add List3 parm/morph selections to List4 outline def handleParms(self): global addList if self.List3.curselection() != (): self.List4.delete(0,self.List4.size()-1) fill = " " for p in range(len(self.List3.curselection())-1,-1,-1): num = self.List3.curselection()[p] num = int(num) #parmDict.values() is a list of lists parmname = parmDict[figname+" "+actname][num][0] fig2 = "!@!_"+figname+"<:>"+fignum act2 = "!#!_"+actname+"<:>"+figname+fignum parm2 = "!%!_"+parmname+"<:>"+actname+fignum if not fig2 in addList: addList.append(fig2) if not act2 in addList: addList.insert(addList.index(fig2)+1,act2) if not parm2 in addList: addList.insert(addList.index(act2)+1,parm2) for i in addList: i2 = i.replace("!@!_","").replace("!#!_",fill*2).replace("!%!_",fill*4) i2 = i2.split("<:>")[0] self.List4.insert(END,i2) # "!@!_" = FIGURE FLAG # "!#!_" = ACTOR FLAG # "!%!_" = PARM FLAG #List4 handling is still bolluxed.... #List4 display handling----------------------------------------- def handleDelete(self,event): #Removes selected lines, but doesn't delete parents when appropriate. A blank figure will not be #written to a pz2, but a blank actor or prop will be written with an empty channels section. global addList if self.List4.curselection() != (): fill = " " acts = 0 mts = 0 fig_name = "" act_name = "" fig_line = 0 act_line = 0 nums = list(int(i) for i in self.List4.curselection()) nums.reverse() for c,i in enumerate(nums): num = i self.List4.activate(num) self.List4.delete(ACTIVE) addList.remove(addList[i]) app = App(root) root.mainloop()