analitics

Pages

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.