Writing Custom Perforce Plugins in Python
I recently wrote a custom tool to diff CryEngine layer files in P4, and was surprised how simple it was. What follows is a quick tutorial on adding custom python tools to Perforce.
Start by heading over to Tools>Manage Custom Tools… Then click ‘New’:
You can pass a lot of information to an external tool, here is a detailed rundown. As you see above, we pass the client spec (local) file name (%f) to a python script, let’s create a new script called ‘custom_tool.py’:
import sys from PyQt4 import QtGui class custom_tool(QtGui.QMessageBox): def __init__(self, parent=None): QtGui.QMessageBox.__init__(self) self.setDetailedText(str(sys.argv)) self.show() if __name__ == "__main__": app = QtGui.QApplication(sys.argv) theTool = custom_tool() theTool.show() sys.exit(app.exec_()) |
What this does is simply spits out the sys.argv in a way you can see it. So now you can feed any file you right click in Perforce into a python script:
If you would like to actually do something with a file or revision on the server and are passing the %F flag to get the depot file path, you then need to use p4 print to redirect the file contents (non-binary) to a local file:
p4.run_print('-q', '-o', depotFile, localFile) |
Just want to say how informative these Python\P4 posts have been. Nice to see someone else melding the two. I do a lot of P4 management at work with Python, but nothing yet with a gui (suppose I should learn pyQt..). keep it up 😉
Comment by Eric Pavey — 2010/10/04 @ 7:05 PM
I have found this presented on another site, but I got more from this insightful approach to it.
Comment by Dr. Paul Santos — 2011/09/20 @ 10:46 AM
Awesome.. Thank you so much
Comment by Altaf — 2013/08/26 @ 1:53 PM