Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

Showing posts with label nodes. Show all posts
Showing posts with label nodes. Show all posts

May 21, 2015

my node view theme

Signifant digits probably don't go beyond 4 or 5

Record your current node theme

import bpy
current_theme = bpy.context.user_preferences.themes.items()[0][0]
node_editor = bpy.context.user_preferences.themes[current_theme].node_editor

types = """\
  color_node 
  converter_node 
  distor_node 
  filter_node 
  frame_node 
  gp_vertex 
  gp_vertex_select 
  group_node 
  group_socket_node 
  input_node 
  layout_node 
  matte_node 
  node_active 
  node_selected 
  output_node 
  pattern_node 
  script_node 
  selected_text 
  shader_node 
  texture_node 
  vector_node 
  wire 
  wire_inner 
  wire_select
"""
for c in types.split():
    k = getattr(node_editor, c)
    print(c, str(k[:])[1:-1].replace(' ', ''))

Set the Theme only

import bpy

current_theme = bpy.context.user_preferences.themes.items()[0][0]
node_editor = bpy.context.user_preferences.themes[current_theme].node_editor

settings = """\
color_node 0.8941177129745483,1.0,0.7882353663444519
converter_node 0.9098039865493774,1.0,0.960784375667572
distor_node 0.4549019932746887,0.5921568870544434,0.5921568870544434
filter_node 0.6784313917160034,0.6039215922355652,0.7529412508010864
frame_node 1.0,1.0,1.0,0.501960813999176
gp_vertex 0.0,0.0,0.0
gp_vertex_select 1.0,0.5215686559677124,0.0
group_node 0.5411764979362488,0.6117647290229797,0.572549045085907
group_socket_node 0.874509871006012,0.7921569347381592,0.20784315466880798
input_node 1.0,1.0,1.0
layout_node 0.6784313917160034,0.6039215922355652,0.7529412508010864
matte_node 0.5921568870544434,0.4549019932746887,0.4549019932746887
node_active 1.0,0.6666666865348816,0.250980406999588
node_selected 0.9450981020927429,0.3450980484485626,0.0
output_node 1.0,1.0,1.0
pattern_node 0.6784313917160034,0.6039215922355652,0.7529412508010864
script_node 0.6784313917160034,0.6039215922355652,0.7529412508010864
selected_text 0.7019608020782471,0.6117647290229797,0.6117647290229797
shader_node 0.6392157077789307,0.9098039865493774,1.0
texture_node 0.2392157018184662,0.9764706492424011,1.0
vector_node 0.6784313917160034,0.6039215922355652,0.7529412508010864
wire 1.0,1.0,1.0
wire_inner 1.0,1.0,1.0
wire_select 1.0,0.46274513006210327,0.0
"""

for configurable in settings.split('\n'):
    if not configurable.strip():
        continue
    print(configurable)
    attr_name, attr_value = configurable.split(' ')
    attr_floats = [float(i) for i in attr_value.split(',')]
    setattr(node_editor, attr_name, attr_floats)

August 10, 2014

Sverchok, the basics

What is Sverchok?

The quick answer: Geometry Nodes for Blender.
The elaborate answer: An open-source and Free visual node based coding environment aimed at creating geometry. It is written in Python and uses Blender's custom PyNodes API for the user interface. Currently (August 2014) Sverchok has about 100 Nodes, and some nodes have 10 or more unique behaviours. It is hosted on GitHub

Nodes

I'm going to assume you know how to enable the add-on, how to add Nodes to the node view, and how to connect them together. Team Sverchok compiled illustrated documentation for every available Sverchok-node, some references are more extensive when the node performs complex operations.

How to use Sverchok

The following examples will introduce you to some trigonometry and vector math. I'm going to start with the idea of a circle, and build from there. If you see small/minimized Nodes, they are usually Math nodes - minimizing them keeps things less cluttered.

sverchok basic circle

The first example shows a common problem; first and last vertex share the same location in space, because the last vertex is created at 360 degrees which is the same as 0 degrees. Same as sin(0) == sin(2*PI).

download file

download file
To avoid the double vertex we limit the rotation to 360-(360/n). This subtracts one segment from the total rotation. This already allows you to adjust n verts and the amplitude, but it might be time to add some edges to connect the vertices.

connecting vertices


download file
In this scenario, the code shown in the text field of Formula node will output a list of n items. Often we want the number of vertex lists to match the number of edge lists, but here you see the verts count in the viz node is 1 and the edges count is 5. The solution is to wrap the edge-list before it hits the viz node, this is done with the Join List node.

download file
Here you see how to wrap a list and also how to use the BGL debug node (this node might change in the future, but the essence should stay the same)

more?

Creating edges with formula node relies on some python knowledge, formula node allows you to write list comprehensions, this might put some people off -- it shouldn't. There are alternatives to generating Edge lists, the UV Connection node will do it in one go.


download file
With UV connection node replacing the Formula Node for edge creation, you get a sense that some commonly performed operations can be abstracted into their own Node. Concepts like creating edges, edge loops, and even polygons from a list of vertices come up very often and deserve specialized Nodes. In this file, if you tick the Polygon button, you will see the UV connection node creates a single polygon from the edge list. Internally it makes a fan of triangles or more complex arrangement of triangles to display the polygon.

But ..5 verts doesn't make a circle

Nope, but even 1 million verts only approximates a Circle. I used 5 verts to this point to show that in circular trigonometry you want to pay attention to first and last vertex and the potential of doubles in space. My tip is to keep vertex counts low as often as possible so you can see easily if your numbers correspond to what's being created, or if there is some overlap.

vertex count == 22

Circle Node

Because Circle geometry is something we use often, it also has a dedicated node. Generators -> Circle .

Shows nicely how many nodes are involved in something as simple as a Circle. Similar nodes exist for Plane (grid), UV Sphere, Cube, Cylinder, even line.

Circle Node + Vectorization

This gives opportunity to show what we can do with an atomic element like the Circle. We can feed it multiple values for the number of verts, and out the other side will come a nested list of vertices that represent several circles.

download file
You'll notice that feeding the No. Vertices socket of the Circle node an Int Range node, with settings for Start, Step and Count produces a multitude of circles with the same radius but different vertex counts. The Circle node takes care of the edge or polygon definition list for every outputted circle.


download file
We can also provide a set of different radii and a variety of vertex counts, and you will get n different circles with each a different radius. n=4 in this case

May 10, 2012

Cycles materials from dribbble generated swatch

Materials and Nodes

Dribbble generates .aco (swatch) schemes from any upload, that's great for archiving my favourite color schemes. To avoid headache and lowlevel python i've resorted to leeching the color values directly from the html, instead of decoding the .aco file. Currently the snippet creates cycles node materials from swatches using urllib and extracts it using regex. The idea is to keep things simple in the event that dribbble changes their html.

This will facilitate using cool palettes to generate abstract visuals (but cycles only)

But Wait! There is more!

The following version also creates primitive cubes and assigns them each a swatch colour.
becomes

And more!

Here's another version It's a small rewrite to get an idea of what kind of code juggling is permitted within python - it's a bit more modular but might take more intuition to read. This was an intermediate step to the next iteration.

This revision demonstrates the evils of using ops, i would prefer to make the geometry mathematically or part math part lathe operation.

Programmed Lathing!

If we look at the previous snippet, it leaves a nasty taste because it amounts to no more than a macro coding. Let's see what a more low level coded interpretation looks like: using from_pydata