analitics

Pages

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