Now that I've gotten to grips with making Python scripts that work, I'm paying more attention to readability and style. I've had the PEP 8 Style guide for Python Code recommended to me as a good standard to follow.
So, my first inelegant-yet-functional PyMel script is done and working. The core of it is in this little snippet here, that digs through a mesh's inputs until it finds the texture node on the surface shader: #Pass through the mesh's shape and return a texture name. import pymel as pm def getTextureFile(geo): sg = [] #Get the shading group from the selected mesh raw_sg = pm.listConnections(geo, type='shadingEngine') #Rapidly check for duplicates in the list- I do this by checking each item #against an empty list. #If the item is not in the empty list I append it. for element in raw_sg: if element not in sg: sg.append(element) #Get some info sgInfo = pm.listConnections(sg[0], type='materialInfo') fileNode = pm.listConnections(sgInfo[0], type='file') textureFile = pm.getAttr(fileNode[0].fileTextureName) print 'This is the file', str(textureFile) return str(textureFile) It st...
Here is a quick code snippet for calling a Python script from Substance Painter and parsing the results. The in/out is very simple, but serves as an example of using the alg.subprocess.check_output function to bridge the JS api and your own Python scripts. // This can be called from the QML UI, or elsewhere in the plugin code. function GetAllFilesInDirectory(root) { if (root == undefined) return; // I put my scripts in a relative path to keep my plugin tidy. var script_path = "Scripts/FileUtils.py"; // Gathering files var ret = "NOSTRING"; try { // The arguments are used as parameter inputs to the Python script. This requires some planning // and well communicated conventions, but works well enough. ret = alg.subprocess.check_output( [ pypath, // Absolute path to interpreter. script_path, // Relative path to the py script. "log_files_of_type", // sys.argv[1], in this case the python ...
Some of the tools I've written allow a user to check out chains of files- like Maya files as well as any exported FBX files and their unity metadata etc, all in one nice pass. This is great, but it has one annoying side effect. If the user presses the button twice the files are added to another change list, but the one they were just in stays there, empty. Press it a number of times and suddenly you have an army of zombie phantom changelists whose only purpose is to clutter up your pending list and annoy you. I have been using the P4Python API and found it to be... well... its kinda crap. It would be great if it had a 'delete all empty pending changelists' function, but it doesn't seem to. So I wrote one of my own. Beware! If you don't like hacking output out of strings the following code will make you cringe... There's my target... import subprocess """Get a list of changelists from the command line. Try to delete them (will delete if empty)...
Comments
Post a Comment