miércoles, 5 de agosto de 2015

Blender Cycles: Using a Python Script in a Material Node

Hi. Today I'm going to show you how to execute a python script in a node.

Hola. Hoy voy a mostrar como ejecutar un script hecho en python y dentro de un nodo.

These are the steps to get it:
  1. Create the python script
  2. Register the python script to be used
  3. Add a driver to a node
  4. Reload the blend file
Estos son los pasos para conseguirlo:
  1. Crear el script en lenguaje python
  2. Registrar el script que se desea usar
  3. Añadir un driver al nodo
  4. Volver a carga el archivo blend

1. Create the python script / Crea el script en python


First of all you need to open a Text Editor in any window of your blend file.

Primero de todo necesitas abrir el Editor de Texto en cualquier ventana de tu archivo blend.







Press on the New button to create a new script.

Pulsa el botón New para crear un script nuevo.




Type the name of you script. IMPORTANT: It must be ended with ".py" extension. So Blender can execute the code.

Teclea el nombre de tu script. IMPORTANTE: Debe estar acabado con la extensión ".py". Así Blender puede executar el código.


I recommend you to check these three buttons to make the editor to be friendly with python programming.

Os recomiendo marcar estos tres botones para hacer que el editor sea más amigable con la programación en python.







Type you script. In my example it is the function foo, which returns the value 0.25.

Escribe tu script. En my ejemplo es la función foo, que devuelve el valor 0.25.



2. Register the python script to be used / Registra el script de python para que se pueda usar


The script must include this line at the end:

El script debe incluir esta línea en el final:

     bpy.app.driver_namespace['foo'] = foo

So it lets you to use the function 'foo' in a driver expression.

Así te permite usar la función 'foo' en una expresión de un driver. 

IMPORTANT: Check Register option on the menu bar of the editor. Then the script will be loaded when you open the blend file.

IMPORTANTE: Marca la opción Register (registrar) en la barra de menú del editor. Así el script se cargará cuando abras el archivo blend.


3. Add a driver to a node / Añadir el driver a un nodo

If you want to execute your script in a node you need to add a driver to a node parameter (node input). Press RMB over the parameter and select Add Driver in the menu. In my example I have added the driver in the Roughness parameter of a Diffuse node.

Si quieres ejecutar tu script en un nodo necesitas añadir un driver a un parámetro del nodo. Presiona el botón derecho del ratón sobre el parámetro y selecciona Add Driver (añadir driver) en el menú. En mi ejemplo he añadido el driver al parámetro Roughness de un nodo Diffuse.


The parameter changes its background color, showing that a driver has been added to it.

El parámetro cambia su color de fondo, mostrando que un driver ha sido añadido a él.



Now you need to open a Graph Editor in a window of your blend file.

Ahora necesitas abrir el Editor de Gráficos (Graph Editor) en una ventana de tu archivo blend.




And select Drivers on the menu bar.

Y selecciona Drivers en la barra de menús.



On the left side unfold the tree until you see the Roughness label and press N to show the properties panel. There you can configure the driver.

A la izquierda despliega el árbol hasta que veas la etiqueta Roughness y presiona N para mostrar el panel de propiedades. Ahí puedes configurar el driver.


There are four types of drivers. In this article I select Scripted Expression type and write "foo()" in the Expression textbox. Also you can use variables in your driver. This is very useful to work with arithmetic expressions. In my example I don't use them.

Hay cuatro tipos de drivers. En este artículo selecciono el tipo Scripted Expression (espresión de script) y tecleo 'foo()' en el cuadro de texto Expression (expresión). También puedes añadir variables al tu driver. Esto es muy útil para trabajar con expresiones aritméticas. En mi ejemplo yo no las uso.

4. Reload the blend file / Recargar el archivo blend

The script will be executed when the blend file is loaded. But if you open the file an alert is shown. The message says 'Auto-run disabled', and appears two buttons in the info menu bar. You can reload the file executing its scripts (pressing the button Reload Trusted) or continue working in the file (pressing Ignore). This is to prevent malicious scripts. Also an error message is shown in the Drivers panel.

El script se ejecutará cuando se carge el archivo blend. Pero si abres el archivo se muestra una alerta. El mensaje dice 'Auto-run disabled' (auto-execución deshabilitada), y aparecen dos botones in la barra de menú. Puedes volver a cargar el archivo y ejecutar sus scripts (presionando el botón Reload Trusted o recarga de confianza) o continuar trabajando con el archivo (presionando Ignore o ignorar). Esto previene la ejecución de scripts maliciosos. También se muestra un mensaje de error en el panel Drivers.


By default all blend files are not allowed to execute scripts. If you want to disable this protection you can go File Preferences and check Auto Run Python Scripts.

Por defecto no se permite ejecutar script a todos los archivos blend. Si quieres deshabilitar esta protección puedes ir a las preferencias de archivo y marcar Auto Run Python Scripts o auto ejecutar scripts de python.


Once you have loaded the file and execute the script you will see the Roughness parameter with the value returned by the foo function.

Una vez que hayas cargado el archivo y ejecutado el script verás el parámetro Roughnes con el valor devuelto por la función foo.


In my next article I will show something more interesting to make with drivers.

En mi próximo artículo mostraré algo más interesante que hacer con drivers.

Bye

Hasta la vista




martes, 4 de agosto de 2015

Blender Cycles: Map Range Node for Materials

Hi. Today I'm going to show you how to make a Map Range node for Cycles materials. I have used this node a lot in my materials. This node takes a value in a range (between a maximum and a minimum) and translates it into a value for another range. In CG this is called mapping.

Hola. Hoy os voy a mostrar como hacer un nodo Map Range (mapeo de rango) para materiales en Cycles. Yo he usado mucho este nodo en mis materiales. Este nodo toma un valor dentro de un rango (entre un máximo y un mínimo) y lo traduce en un valor para otro rango. Esto se conoce com mapeo en graficos por ordenador.

The Map Range node exists in Blender, but only for compositing.

El nodo Map Range ya existe in Blender, pero solo para postproducción.



You can make a node group for Cycles materials which works exactly the same as the compositing one. This is its setup:

Tú puedes hacer un grupo de nodos para materiales de Cycles que funcione exactamente igual que el de postproducción. Esta es su configuración:


Now you can use the Map Range node in your materials. For instance, you can experiment with textures.

Ahora puedes usar el nodo Map Range en tus materiales. Por ejemplo, puedes experimentar con texturas.


Bye

Hasta la vista



Este sitio emplea cookies de Google para prestar sus servicios, para personalizar anuncios y para analizar el tráfico. Google recibe información sobre tu uso de este sitio web. Si utilizas este sitio web, se sobreentiende que aceptas el uso de cookies. Más información Entendido