Posts

Showing posts with the label Python

Calling an external command in Python

Here's a summary of the ways to call external programs and the advantages and disadvantages of each: os.system("some_command with args")  passes the command and arguments to your system's shell. This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection. For example, os.system("some_command < input_file | another_command > output_file") However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, etc. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs. see documentation stream = os.popen("some_command with args")  will do the same thing as  os.system  except that it gives you a file-like object that you can use to access standard input/output for that process. There are 3 other variants of popen that all handle the i/o slightly differently. If you pa...

PyDev with Eclipse on Mac

Image
Step 1 : Install Eclipse Step 2 : Install PyDev Here is the easiest way of drag-n-drop : http://marketplace.eclipse.org/content/pydev-python-ide-eclipse Step 3 : Configure Python Interpreter Path to MayaPy's python interpreter : /Applications/Autodesk/maya2014/Maya.app/Contents/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python Extras : 1. For Autocompletion, add following line in "Forced Builtins" : maya.cmds,maya.mel,maya.standalone,maya.app,maya.OpenMaya,maya.OpenMayaAnim,maya.OpenMayaCloth,maya.OpenMayaFX,maya.OpenMayaMPx,maya.OpenMayaRender,maya.OpenMayaUI which will add following libraries : maya.cmds maya.mel maya.standalone maya.app maya.OpenMaya maya.OpenMayaAnim maya.OpenMayaCloth maya.OpenMayaFX maya.OpenMayaMPx maya.OpenMayaRender maya.OpenMayaUI Another Resource :  http://sourceforge.net/p/pydev/discussion/293649/thread/fb1b2e45

Normalizing one range of values to another range of values.

To add another generic answer. If you want to map the linear range [A..B] to [C..D], you can apply the following steps: Shift the range so the lower bound is 0. (subract A from both bounds: [ A .. B ] -> [ 0. . B - A ] Scale the range so it is [0..1]. (divide by the upper bound): [ 0. . B - A ] -> [ 0. . 1 ] Scale the range so it has the length of the new range which is D-C. (multiply with D-C): [ 0. . 1 ] -> [ 0. . D - C ] Shift the range so the lower bound is C. (add C to the bounds): [ 0. . D - C ] -> [ C .. D ] Combining this to a single formula, we get: ( D - C )*( X - A ) X ' = ----------- + C ( B - A ) Eg : Normalizing a range of 0.5 ----> 1.0   to 0 ------> 1  A=0.5, B=1, C=0, D=1 you get: ( X - 0.5 ) X ' = ------- = 2X - 1 ( 0.5 ) Note, if you have to convert a lot of X to X', you can change the formula to: ( D - C ) C * B - A * D X ' = --...

Sorting Algorithms in Python

 Python Sorting Algorithms and the time taken by each algorithm to sort values.  http://www.daniweb.com/software-development/python/code/216689/sorting-algorithms-in-python

Wrapper functions

A very simple example for writing wrapper functions in Python. def polySphere(*args,**kwargs):     try:         return cmds.polySphere(*args,**kwargs)     except:         print "Could not create polySphere"         print sys.exc_info()  polySphere(r=5,sw=20,sh=20)

Bounding Box in PIL (Python Image Library)

Image
Bounding Box in PIL(Python Image Library) : Bounding Box function of PIL can be very useful for removing unused pixels from an image. Eg. If we want to remove unused pixels based on alpha channel of an image, we can use following code.   im = Image.open(imagePath) r,g,b,a = im.split() left,upper,right,lower = a.getbbox() tempImage = im.crop((left,upper,right,lower)) newPath = imagePath.split(".")[0]+"_Cropped" + "." + imagePath.split(".")[1] tempImage.save(newPath) To understand which values, getbbox() function returns and what do they look like on an image, I created an image showing the values returned by the function. left,upper,right,lower = image.getbbox()   Suppose you have a point in an original image and you want to find out the new position of the same point in the cropped image, lets see how we can do it. Suppose the point is at (300,450) position in an original image, so after cr...

Some Regex Expressions

http://www.tutorialspoint.com/python/python_reg_expressions.htm

Dynamically Generating Curves

These are few lines of code to generate dynamic curves in Maya. I wrote this code while researching about effect of different trigonometrical function on a curve. import math,maya.cmds as cmds,maya.mel as mel points ="[" x= 0 for i in xrange(50):    x+=i    points = points + ("("+str(x)+","+ str(math.sin(i*10)*10) +",0),") points = points.rstrip(",")+"]" cmds.curve(p=eval(points))

Python Performance Tuning and Optmization

Python高级编程(二) from Qiangning Hong