Search and Replace
Search and Replace
Using a Python “wrapper” script you can catch the output of an executing application, which is very useful when you want to perform certain tasks depending on what is being printed to stdout by the application.
This is a simplified example of how I do it:
# Imports import os, sys, subprocess # Build command command = [ 'python', os.join.path('/path/to', 'scriptFile.py') ] # Execute command p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Read stdout and print each new line sys.stdout.flush() for line in iter(p.stdout.readline, b''): # Print line sys.stdout.flush() print(">>> " + line.rstrip()) # Look for the string 'Render done' in the stdout output if 'Render done' in line.rstrip(): # Write something to stdout sys.stdout.write('Nice job on completing the render, I am executing myFunction()\n' ) sys.stdout.flush() # Execute something myFunction()
Here’s an example of catching the output of a V-Ray for Maya render and outputting TR_PROGRESS nnn% to stdout whenever a percentage is being printed to stdout, which makes Pixar’s Tractor show a task progress in the task node tree.
# Imports import os, sys, subprocess, re # Build command command = ['python', os.join.path('/path/to', 'scriptFile.py'), '-r', 'vray'] # Execute command p = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Read stdout and print each new line sys.stdout.flush() for line in iter(p.stdout.readline, b''): sys.stdout.flush() print(">>> " + line.rstrip()) # Tractor Progress match = re.search('\d\d\d %', line.rstrip() ) # Look for nnn %, vray for maya if not match: match = re.search('\d\d %', line.rstrip() ) # Look for nn %, vray for maya if not match: match = re.search('\d\d.\d\d%', line.rstrip() ) # Look for nn.nn%, vray standalone if not match: match = re.search('\d.\d\d%', line.rstrip() ) # Look for n.nn%, vray standalone if not match: match = re.search('\d.\d%', line.rstrip() ) # Look for n.n%, mentalray if not match: match = re.search('\d\d.\d%', line.rstrip() ) # Look for nn.n%, mentalray if match: percent = match.group(0).replace('%','').replace(' ','') percent = float(percent) percent = int(percent) percent = str(percent) sys.stdout.write('TR_PROGRESS ' + str(percent) + '%\n' ) sys.stdout.flush()
Catching string from stdout with Python
Ever wanted to be able to run the same user interface in Maya, Nuke as well as completely standalone (with or without app-specific modifications) and on any platform, using PySide and/or PyQt?
– That’s exactly why I created a boilerplate Python script, which could serve as a good starting point for most PySide/PyQt projects to be used in the VFX pipeline.
As my experience is limited to some VFX applications, you are most welcome to fork or contribute to make it also run in other PyQt/PySide enabled applications.
QtTypeto either ‘PySide’ or ‘PyQt’
uiFileto point to your .ui file
import sys sys.path.append('c:/path/to/dir/containing/the/script') import boilerplate boilerplate.runMaya()
import sys sys.path.append('c:/path/to/dir/containing/the/script') import boilerplate boilerplate.runNuke()
More on usage and customization over at the GitHub project’s wiki.
A PySide/PyQt boilerplate for Maya, Nuke or standalone
Send MEL/Python code from Sublime Text to Maya
There are some official Autodesk instructions floating around on how to get PyQt4 support in of Maya 2014, but I found them a bit unclear and hard to follow. I also found an error (although minor)… so I’ve typed down my own little step-by-step reminder below, based off that.
PyQt4 in Maya 2014 on OS X
Here’s how to install Maya Bonus Tools 2014 in an arbitrary location. I’ve covered two different methods (which can actually be mixed, if you will). The simple method (#1) is probably the best bet for most people while the “thorough” method (#2) could be interesting for some other folks. Take your pick.
Maya Bonus Tools 2014: arbitrary installation
The Fujifilm X100S – compared to the X100
Recently we ended up with a Maya scene with locked, read-only, nodes which prohibited us from deleting them from the scene. This usually happens to nodes having been part of a locked reference which has been imported into the scene. In our case, we had no access to the original referenced file, so we had to unlock these nodes from being read-only.
For some reason it didn’t work to unlock them using the lockNode command from within Maya and I had to write a Python script which would read each line of the Maya ASCII file and strip out any lines containing “lockNode”.
This is the script, which I’ve saved as
unlock.py in the same directory as the scene file:
import re infile = open('original_file.ma', 'r') outfile = open('new_file.ma', 'w') for line in infile: lockNodeFound = False for word in re.findall(r"\w+", line): if (word == 'lockNode'): lockNodeFound = True if (lockNodeFound): #print 'Line removed: ' + line pass else: outfile.write(line)
Switch out the file name
"original_file.ma" to match the name of your file. Back up your original Maya scene file somewhere safe and run the script in a command line window (not inside of Maya) like this:
Although a new file is created, I take no responsibility for how you use this script, of course. You’re on your own :)
If you want visual feedback of what’s going on, just uncomment the line that says
#print 'Line removed: ' + line but this will make large scenes take significantly longer to process.
And, just in case, why not first try to see if you can unlock the nodes from within Maya with this simple script:
import maya.cmds as cmds unlockError = False nodes = cmds.ls() for node in nodes: lockStatus = cmds.lockNode( node, q=True ) for response in lockStatus: if response != False: try: cmds.lockNode( node, lock=False ) print 'Unlocked: ' + node except: print 'Error: Could not unlock ' + node
Fixing “Read-only” nodes in Maya
Transfer game save data between iOS devices