I was looking into writing a fairly simple batch file to install some stuff and update environment variables. At some point I wanted to do something like this (constructing a string of ‘;’ separated paths):
set env_vars = \path\to\stuff
if x%user%==xsveinbjorn (
set env_vars = %env_vars%;\another\path
)
if x%hostname%==xgefjun (
set env_vars = %env_vars%;\whoah\this\is\radical
)
echo env_vars
When I would run this I would always get
\path\to\stuff;\whoah\this\is\radical
(Notice that the second path \another\path is missing from the result)
Being very new to writing batch scripts I found this extremely confusing since this sort of stuff would work like a charm in python. So I started the google machine.
The reason this happens is that variables enclosed in ‘%’ are expanded when the script is read, which is quite different from the way python handles this resolving stuff at the last possible time. So when the script is read it will actually look something like this:
set env_vars = \path\to\stuff
if xsveinbjorn==xsveinbjorn (
set env_vars = \path\to\stuff;\another\path
)
if xgefjun==xgefjun (
set env_vars = \path\to\stuff;\whoah\this\is\radical
)
echo env_vars
(I resolved the variables %user% to ‘sveinbjorn’ and %hostname% to ‘gefjun’)
And we can clearly see why we got the result that we got.
So what we have to do is tell the batch file to expand the variables as we go along (something close to how python does things) instead of this expand-it-when-you-read-it nonsense.
To do this we simply have to do three things:
- Add this line to the top of the code
setlocal EnableDelayedExpansion
- Add this to the end of it
- Replace the enclosing ‘%’ to ‘!’ for the variable you want to delay the expansion for.
if x%user%==xsveinbjorn (
set env_vars = !env_vars!;\another\path
)
So my original example would look like this:
setlocal EnableDelayedExpansion
set env_vars = \path\to\stuff
if xsveinbjorn==xsveinbjorn (
set env_vars = \path\to\stuff;\another\path
)
if xgefjun==xgefjun (
set env_vars = \path\to\stuff;\whoah\this\is\radical
)
echo env_vars
endlocal
Now we get the result we were looking for:
\path\to\stuff;\another\path;\whoah\this\is\radical
This also applies to loops and other places where the variable might change between the time you start the batch script and when you actually want to expand it.
Today I was looking at launching programs with some custom environment variables set and if you don’t want to redefine a whole lot of them for this new environment I’d recommend is copying the os.environ dictionary and do your changes on the copy, like so:
new_env = os.environ.copy()
new_env['MEGAVARIABLE'] = 'MEGAVALUE'
subprocess.Popen('path', env=new_env)
Then you should have your program launched with all them fancy environment variables set.
Have you ever called a some scripting function in Softimage and gotten back an XSICollection and printed that return value only to get a ‘None’ value printed to the log?
Don’t worry, you got some data there. Just remember that thats the way python in Softimage prints XSICollections if you would iterate over the collection you will find your values:
for object in returnedCollectin:
# do something with object
...
I have seen some people who just started scripting in xsi run in to this little issue.
This is a simple plug-in command to copy skin weights from one object to another with some significant restrictions:
Download sjtCopySkinWeights
- The meshes must be exactly mirrors of each other vertex-number wise. This plugin simply uses the vertex numbers of the mesh so each vertex of object A must have a corresponding vertex on object B.
- The joint names must be somehow side-differentiated e.g. L_joint and R_joint or left_side_arm_JNT and right_side_arm_JNT or you must somehow be able to map one side of joint names to the other.
This has mostly been useful when dealing with hands, you skin one and then you have to copy the weights over, but sometimes maya does a lousy job. This plug-in is pretty much like going in the component editor and copying each value for each vertex between meshes (which would be insane to do by hand).
Note: this is not a very fast script (I was trying to learn the Maya API while writing this) so give it some time to work through dense meshes.
If you have questions or comments just either send me an email or comment below.
Usage
- Select the object with the correct weights.
- Select the object with the incorrect weights.
- Run the command e.g. ‘sjtCopySkinWeights -s “L_” -r “R_”‘
Arguments
This command accepts two arguments -search/-s and -replace/-r and each argument requires a string to follow it that tells the plugin how to map the right joint names to the left ones. Yes it works kind of backwards. The command first constructs a list of all the influences on the first object, then it goes over every vertex on the other object and tries to map it’s influences to the ones in the influence-list from the first object.
Example
On one side the joints are called L_….._JNT and the other R_……_JNT the proper arguments for this command to work would be (MEL syntax):
sjtCopySkinWeights -s "L_" -r "R_"
(if no arguments are givien, this is the default search pattern)
This would also have worked (since the search strings can be regular expressions):
sjtCopySkinWeights -s "L_" -r "^[rR]_"
This would allow the right side joints to be named either R_…._JNT or r_…._JNT Just remember the regular expression should go with the -r flag, the -s flag is just used to replace the match from the -r flag with.
Example file
To test the command immediately you can load up th test file included in the zip download ‘copyWeights_example.ma’. First try scrubbing in the timeline to see the difference in weighting. The right side (pCube1) bends in the middle but the left side(pCube2) does not. To copy the weights from pCube1 to pCube2 first load the plugin, select pCube1 then pCube2 and type (MEL syntax):
into the command line (or script editor) and then the weights will have been copied from pCube1 to pCube2. Voila.
This is a tiny snippet I threw together today to speed up selecting the object that was controlling my selected object. In this case the object that was being controlled was a curve, but the constraining object (controlling the curve) was an invisible transform node, so instead of going through the hypergraph I wrote this:
import maya.cmds as mc
selection = mc.ls(sl=1)
toSelect = []
for item in selection:
constraint = mc.listConnections( item+'.parentInverseMatrix[0]', d=1, s=0,type='constraint')
if constraint:
constraint = constraint[0]
src = mc.listConnections(constraint+'.target[0].targetParentMatrix', d=0, s=1)
if src:
toSelect.extend(src)
try:
mc.select(toSelect)
except:
pass
Note: this allows you to select multiple constrained objects and this will try and find the driving objects, this does not handle multiple constraints or anything fancy like that. Enjoy
When setting the volume of a quicktime player through COM please keep in mind that the range is from 0-1 NOT 0-100 as I thought (since there doesn’t seem to be any good documentation about the QT COM interface). QT allows you to set the volume to 100 and you end up with painfully loud sound. So set your volume to 1 to allow people to enjoy their hearing for a new day.
Always always always remember wrapping your paths in quotes. I keep running into stuff I wrote break because someone saved a file with a space in it and I forgot to wrap the path in quotes.
This works:
blackwave.exe -d 50 superwave.wav
This doesn’t:
blackwave.exe -d 50 super 2.wav (two file arguments, ‘super’ and ’2.wav’)
To fix this:
blackwave.exe -d 50 "super 2.wav"
So remember: whenever you are using path rembemer the
"\""+ variable + "\""
(or something else that suits your language)
In Softimage on Windows, if you fetch the TEMP environment variable e.g. from witihn python (with the os.environ dictionary), you will get a different directory when you get the TEMP variable from outside Softimage.
Outside Softimage you get:
C:\Users\<username>\some\path\I\cannot\rembemer
within it you get:
C:\Users\<username>\some\path\I\cannot\rembemer\XSI_temp_###
Just in case you wanted to write files to TEMP and read from them later.
After seeing an excellent post (as usual) on Hamish’s site about a profiling utility he wrote I decided to give it a try today, and man is this thing useful or what. It helped me find bottlenecks in two of my scripts today and I can only imagine how much of the user’s time it will save.
I ran into Doug Hellmann’s site the other day doughellmann.com. And he an excellent collection of articles and examples for a bunch of modules from python’s standard library called Python Module of the Week. The one’s I’ve used and had no idea existed were configparser, optparser and the logging module. But there are many, many more there.
Absolute gold for those of us who havent gone through every page of the documentation.