Halloween Costume ideas 2015

SVERCHOK / When Python Plays With Cricket

I am currently re-reading "Generative Algorithms" PDF book for Rhino-Grasshopper learning. It is an excellent read while learning the Sverchok (cricket) sings in Blender. You MUST read this ebook if you are serious about procedural/parametric modeling.

In Sverchok, from the very beginning, we are already exposed to DATA and DATA STRUCTURE. The DATA in Sverchok is simple and seems to be limited for now. Limitation is actually good in this case.

The DATA STRUCTURE in Sverchok is actually quite interesting, in fact Thinking about Data Structure is another advance step to take you to the next level, whether in programming or in procedural workflow. We will be able to modify, convert, re-arrange DATA more correctly if we understand DATA STRUCTURE well.

I do not have programming background, but recently encounter this "data structure" term quite often. I am probably understanding this "not quite right", however this is how I understand it now.

All these are very easy for programmer, not so easy for non-programmer. I am writing from non-programmer perspective.

DATA 

In Sverchok, you will encounter data which are mostly numbers.
  • FLOAT = fractional values, eg. 0.5256, 0.2
  • INT (integer) = whole numbers, eg. -4, 0, 135

DATA STRUCTURE

  • Tree Structure
  • Tuple
  • List
  • List within List
  • List within List within List
I think the analogy of Russian Matryoshka doll is perfect and brilliant to explain this. I am still slowly trying to understand the whole article here at Sverchok main website:

I think at the very basic, we can think of DATA STRUCTURE like TREE with BRANCHES.

In Sverchok, we have this interesting simplification of DATA and STRUCTURE. When we bring a simple object mesh, like Blender default Cube object:


Using Sverchok VIEWER TEXT node, we can see the actual DATA and STRUCTURE of Poly Cube.


vertices: 
(1) object(s)
=0=   (8)
(1.0, 0.9999999403953552, -1.0)
(1.0, -1.0, -1.0)
(-1.0000001192092896, -0.9999998211860657, -1.0)
(-0.9999996423721313, 1.0000003576278687, -1.0)
(1.0000004768371582, 0.999999463558197, 1.0)
(0.9999993443489075, -1.0000005960464478, 1.0)
(-1.0000003576278687, -0.9999996423721313, 1.0)
(-0.9999999403953552, 1.0, 1.0)

data:(1) object(s)
=0=   (12)
(0, 1)
(0, 3)
(0, 4)
(1, 2)
(1, 5)
(2, 3)
(2, 6)
(3, 7)
(4, 5)
(4, 7)
(5, 6)
(6, 7)

matrixes: 
(1) object(s)
=0=   (4)
(1.0, 0.0, 0.0, 0.0)
(0.0, 1.0, 0.0, 0.0)
(0.0, 0.0, 1.0, 0.0)
(0.0, 0.0, 0.0, 1.0)

I highlighted the LIST of numbers above so that we can see it more clearly.

NOTE: Here we are reading the CUBE as Vertices, Edges, Matrixes. Remember that you also pass on the Polygons, or the DATA that makes Faces/Polygons of the mesh.

BREAKING DOWN CUBE DATA

If I were to elaborate the date using my own words, it will be like this: Cube ingredient can be broken into these DATA:
  • Vertices (LIST of 8 values)
  • Edges (LIST of 12 values)
  • Matrixes (LIST of 4 values)
Each Vertice data have 3 FLOAT values, each value representing position in XYZ, in relation to the Matrix.
Each Edge data have 2 INT values, contain pair information on which Vertice Index connected to which.
Matrix data is storing information about Translation, Rotation, Scale, and Rotation Axis.

In Sverchok, just like in Rhino-Grasshopper, we are able to generate or supply our own data to pass on and recreate those DATA STRUCTURE required to make an object parametrically or procedurally.

This means, you can very easily and visually generate LIST of 3 values to represent POINT POSITIONS in XYZ. Or all kind of DATA in STRUCTURE that you like it to be for your own purpose.

Cube Data above is a simple example.

What interesting is when we start to MERGE, MIX, WRAP and JOIN data, you will encounter lots of interesting thing. Sometimes you need to FLATTEN the DATA Tree. Separate 3 values data into each separate value and then combine it back.

EXAMPLE: CREATING RANDOM VALUES XYZ

Generating a single TUPLE with 3 values (Vector) using Sverchok.

vertices: 
(1) object(s)
=0=   (1)
(0.7954123484458212, 0.9915130893390923, 0.3247524107944588)
unknown data: 
None 

matrixes: 
None 

Generating a LIST of some TUPLES with 3 values:

vertices: 
(1) object(s)
=0=   (10)
(0.8800991152954115, 0.2869275644617431, 0.4621446386044473)
(0.1707271599919563, 0.2141409389917276, 0.4621446386044473)
(0.6958015463360391, 0.6714156319278667, 0.4621446386044473)
(0.1252005547937043, 0.7170644202392952, 0.4621446386044473)
(0.1131273587904863, 0.2736442119694806, 0.4621446386044473)
(0.6394349933329605, 0.3203943496951187, 0.4621446386044473)
(0.3210433830317321, 0.4042363684941612, 0.4621446386044473)
(0.2363263605284892, 0.456282186215196, 0.4621446386044473)
(0.7362634528968245, 0.3493000706331532, 0.4621446386044473)
(0.6090024422764354, 0.3397017948731432, 0.4621446386044473)
unknown data: 
None 

matrixes: 
None 

BRINGING THOSE DATA INTO PYTHON

DATA is just DATA.

We can generate those data above in Python obviously. But what exciting here is the fact that we are creating and generating those DATA visually using Sverchok node-tree inside Blender. It is all visual.

Within Sverchok, we can modify, sort, join, and do all sort of interesting data manipulations. Mostly, we will care on how the data will transform back into Mesh geometry that we can use in normal Blender environment.

Current Sverchok is great for modeling. It is not yet directly connected to Blender typical environment, in which we are probably tempted to use Sverchok for Procedural Animation, Procedural Shading, etc.

Don't limit yourself. Either way, those DATA, or NUMBERS, can be exported or imported as we please.

Knowing this truth, we can basically bend the spoon like NEO in the MATRIX.

When we use Viewer Text Node in Sverchok, we already spit out a readable DATA for Python.  Those DATA is separated by LINE, which is not as simple as copy-pasting to create actual list.

Let say, I want to do this:
"Generate random 3 values list using Sverchok, and use that data to create bunch of cubes randomly positioned colored with those values."

Random XYZ => Cube Positions
Random XYZ => Cube Colors

That can be done in Python. Is it easy? Well... it depends, but every step as logical as preparing ingredient for cooking something.

It actually took me a bit of time and I found problems here and there.

STEP 001
Prepare and generate the data in Sverchok. We have done this already simply copy paste list of random 3 values and save it as normal text file (TXT). For example, my data looks like below:

(0.6646910631367325, 0.908184595140585, 0.559640197467031)
(0.6259886197350288, 0.0009782863450561, 0.0635360130887022)
(0.1702762851837583, 0.0303247289132893, 0.4000292985038622)
(0.4247105715299129, 0.0239215297953929, 0.7871877036098419)
(0.253353935095027, 0.4481245252973097, 0.2827014293529476)
(0.329842697601205, 0.8651187090911354, 0.843854411030971)
(0.2707690256035583, 0.7882956210312217, 0.2807732651216759)
(0.4629872528241108, 0.6491480656266136, 0.5343794408560223)
(0.927270058247242, 0.7755588725338894, 0.4961782330878063)
(0.0655122421709582, 0.623794902689959, 0.5508732151641716)

STEP 002 - Import TXT DATA 
We have out TXT data to be imported back into Blender to be used to create Cube Positions and Color. How do we do that using Python? Below is one way to do it.

# Import file
f = open('\\Users\Jimmy\Desktop\listData.txt')
lines = f.readlines()
f.close()

# Read each line one by one
# while converting each into normal tuple
# and appending into list I want

myList = []

counter = 1
for line in lines:
    myList.append(eval(line))
    #print(counter, line)
    counter += 1

print(myList)

I did not come up with that in one go, I actually search around for solution using Google:
  • How to open TXT file in Python? open() and close()
  • How to read file line by line in Python? readlines()
  • When each line is read, I am getting String of values, how to convert those String DATA into normal TUPLE? Apparently eval() can be used as long the String is known to be a valid Tuple structure
  • Our Tuple is (x, y, z) so all good
  • We finally get our LIST containing 3 values
  • We can pass it on
STEP 003 - Use LIST DATA 
From here, it is totally up to you what you want with the DATA.

Bend the spoon if you like.

However, hold your Force, here we just want to create bunch of Cubes, each Cube has POSITION and COLOR based on the DATA values we originally generated in Sverchok, which is now just a LIST DATA in Python.

Python can do the rest with Blender Python (BPY).

# From that list
# Create bunch of cubes stacked in Z position
# and Color them according to List tuple of Random Values

import bpy

for i, col in enumerate(myList):
    bpy.ops.mesh.primitive_cube_add(view_align=False, enter_editmode=False, location=(10 * col[0], 10 * col[1], 10 *  col[2]), rotation=(0, 0, 0))
    bpy.context.object.color = (col[0], col[1], col[2], 1)
    print (i, col)


Here, today, you have learn how we can see DATA and start thinking about DATA STRUCTURE to then used to MANIPULATE the DATA.

Now, you see how much "easier" it is to generate, modify, passing DATA in Sverchok?

Of course in Python it is "easy" as well, if you used to working with DATA in Python, just a lot more typing and Wing-Chun bashing that keyboard. In Sverchok we connect the node. If there is an error, we get nothing. In Python, we probably get error message.

It is not actually that much different, in Sverchok we use Viewer Text and SEE the DATA on Text Editor. In Python, we usually PRINT out the DATA and read what is happening.

Sverchok indeed comes with some useful Sverchok nodes to manipulate data which is useful to do parametric/procedural modeling.

Nikita aka NIKITRON on Blender Artist tried to explain DATA STRUCTURE to me:
- use data level slider to get level right
- if you need increase level = use join node with "wrap" activated. 
- If you need reduce data level = use "List Levels" node to reduce

Anyways, this is when I asked question "Why LIST LENGTH" did not give me DATA that can be passed on to Random node? The answer is below and it is something to do with data structure.

TO GET LENGTH (TOTAL NUMBER OF EDGES):

.
Do not try to bend the spoon — that's impossible. Instead, only try to realize the truth: there is no spoon. - THE MATRIX


LINKS


Post a Comment

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget