analitics

Pages

Friday, June 21, 2013

Simple way to find your script under linux.

Many python users have a lot of scripts.

They use some words for classes or some functions.

Sometime is hard to remember where it's this scripts.

So the easy way to do that is to find the script where is some words.

For example you need to find this : word_in_your_script

To do that just see next linux command:

$ find ~/ -type f -iname "*.py" -exec grep -l 'word_in_your_script' {} \;

Thursday, June 13, 2013

What you need : stdout or print ?

My question is much more complicated than intended and I will show you in this tutorial.

Most users use print or print() - if it used in python 3.

For example you can use this without import any python module.

$ python 
Python 2.6.8 (unknown, Apr 14 2013, 18:10:41) 
[GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "my text"
my text
>>> 

This is simple to show some strings.

What the most people don't know about print function it's the interface of stdout.write.

Let's see another example with stdout.write .

>>> sys.stdout.write(str(my text) + '\n')   
  File "<stdin>", line 1
    sys.stdout.write(str(my text) + '\n')
                               ^
SyntaxError: invalid syntax
>>> sys.stdout.write(str("my text") + '\n')                  
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined

First error tells us about function str.It's need just one arg not this my text.

The next error tells us about sys module is not import by python.

Note: This restriction can help us sometime.

Some example using stdout.write :

First is : my text example , see I add '\n' to go to the next row.

>>> import sys 
>>> sys.stdout.write("my text"+'\n')
my text

Let's try to change the output.

See the next image :

You can say : I can do this with print function.

>>> print "\033[0;32m"+"my text"+"\033[0m"
my text

Yes! Right. Can you do this ? (see the next video)


Wednesday, May 15, 2013

Use python to render scene in Blender 3D.

The python script is very simple.

I used my scene with Mickey Mouse (my boy like this funny cartoon).

This is the python script.

import bpy
bpy.context.scene.render.use_border = False
bpy.context.scene.render.use_crop_to_border = False
bpy.ops.render.render()
R="Render Result"
bpy.data.images[R].save_render("/home/your_username/test_render.png")

See the output image:


The next source code it's used for border and crop.

Only if you want to use it.

bpy.context.scene.render.use_border = False
bpy.context.scene.render.use_crop_to_border = False

Also you can use this to set resolution percentage.

For example if you want to render just 10% of resolution use this:

bpy.context.scene.render.resolution_percentage =10

Saturday, March 23, 2013

Using fnmatch python module ...

The module fnmatch provides support for Unix shell-style wildcards, which are not the same as regular expressions.

Why , because the special characters used are : * , ? , [seq] , [!seq] .

This is default example from fnmach website.


>>> for file in os.listdir('.'):
...  if fnmatch.fnmatch(file, '*.txt'):
...   print file
... 
tableta.txt
verf.txt
a.txt
python-modules.txt
untitled.txt
resetbios.txt
>>> 

Now I want to show you another way to use this module.

Using Blender with multiple objects and python script can be very hard way.

For example if you have many objects or one matrix of objects create manually or with some scripts the named object is like in the next image:


Just use the fnmatch to sort this objects.

>>> import bpy 
>>> import fnmatch

Now get all meshes objects.

>>> all_objects = bpy.data.objects

Put names of all meshes as a list of strings.

>>> list_all_objects= [all_objects[i].name for i in range(len(all_objects))]

Use the python module fnmatch to filter the name of objects.

>>> new_list_objects = fnmatch.filter(list_all_objects, 'Cube.*')]

Now you can use this new list to make some change. I use print to show test the list.

>>> print(new_list_objects)
['Cube.001', 'Cube.002', 'Cube.003'

The goal of fnmatch module in Blender 3D can be use one module to make list of objects and enables searching for files given a file name pattern.

It's two features in one module.

Also this python module can be used to get some

>>> regular_expression_txt = fnmatch.translate('*.txt')
>>> regular_expression_txt
'.*\\.txt\\Z(?ms)'
>>> print(regular_expression_txt)
.*\.txt\Z(?ms)

Just remove \Z(?ms) and use it.

I try to use some regular expression and seam working well.

Saturday, March 2, 2013

Simple way to reverse strings

Just see the next source code:

>>> text='0123456789'
>>> reverse_text=text[::-1]
>>> print reverse_text
9876543210
>>> 

Monday, February 25, 2013

Make Newton fractal with python

A fractal is a mathematical set that has a fractal dimension that usually exceeds its topological dimension , see Fractal wikipedia.

Today I used my mind and also my python skills to make one fractal image.

I use Newton's method to make all points and PIL python module to save the result.

Let's see the source code and comments.

from PIL import Image
#size of image
imgx = 600
imgy = 400
#make image buffer
image = Image.new("RGB", (imgx, imgy))

# area of fractal
xa = -2.0
xb = 2.0
ya = -2.0
yb = 2.0

#define constants
max_iterations = 10 # max iterations allowed
step_derivat = 0.002e-1 # step size for numerical derivative
error = 5e-19 # max error allowed

# function will generate the newton fractal
def f(z): return z * z  + complex(-0.31,0.031)

# draw derivate fractal for each y and x 
for y in range(imgy):
 zy = y * (yb - ya)/(imgy - 1) + ya
 for x in range(imgx):
  zx = x * (xb - xa)/(imgx - 1) + xa
  z = complex(zx, zy)
  for i in range(max_iterations):
   # make complex numerical derivative
   dz = (f(z + complex(step_derivat, step_derivat)) - f(z))/complex(step_derivat,step_derivat)
 # Newton iteration see wikipedia   
   z0 = z - f(z)/dz 
   # stop to the error 
   if abs(z0 - z) < error: 
    break
   z = z0
  #I use modulo operation expression to do RGB colors of the pixels 
  image.putpixel((x, y), (i % 8 * 16, i % 4 * 32,i % 2 * 64))
  
#save the result 
image.save("fractal.png", "PNG")

This is the final result of Newton fractal image: