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 List. Show all posts
Showing posts with label List. Show all posts

June 04, 2013

two ways to min/max an xyz coordinate list

July 08, 2011

creating a mesh using primitives, polysphere


import bpy
import math

# constant
r90 = math.radians(90)

# variables
subdiv = 7

# add an new MESH object, place it in 'EDIT' mode
bpy.ops.object.add(type='MESH')
bpy.context.object.name = 'PolySphere'
bpy.ops.object.mode_set(mode='EDIT')

# define locations and rotations of the mesh grid objects
loc_list = (0, 0, 1), (0, 0, -1), (1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0)
rot_list = (0, 0, 0),(0, r90*2, 0),(0, r90, 0),(0, -r90, 0),(-r90, 0, 0), (r90, 0, 0)

# add 6 faces
for i in range(6):
bpy.ops.mesh.primitive_grid_add( x_subdivisions=subdiv,
y_subdivisions=subdiv,
size=1,
view_align=False,
enter_editmode=True,
location=loc_list[i],
rotation=rot_list[i])

# select all verts, and remove doubles, end up with neat cube
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.select_all(action='TOGGLE')
bpy.ops.mesh.remove_doubles(limit=0.0001)

# set
bpy.ops.transform.tosphere(value=1)
bpy.ops.mesh.vertices_smooth(repeat=20, xaxis=True, yaxis=True, zaxis=True)
bpy.ops.transform.tosphere(value=1)

July 03, 2011

List Comprehension Quick example


>>> m = [["sta",[2,1]],["stab",[22,11]],["stam",[24,14]]]
>>> en = [f[1] for f in m]
>>> en
[[2, 1], [22, 11], [24, 14]]

July 01, 2011

Sorting Edge keys Part I

this post has a second part to it here

This code is dying for a rewrite. The code takes a jumbled up sequence of edges that would normally describe a polyline or 'edgeloop', where the vertex sequence is comparable to edge_key_list below. This code assumes a few things.

1) no interuptions
2) no duplicates
3) you have already figured out the index of one of the end vertices.
4) the edge loop is not closed.

edge_key_list = [[5, 2],[2, 3],[3, 10],[10, 24],[24, 19],
[19, 6],[6, 1],[1, 20],[20, 13],[13, 9],[9, 12],[12, 8],[8, 23],[23, 15],
[15, 18],[18, 17],[17, 14],[11, 22],[22, 4],[4, 21],[14, 11],[0, 5],[0, 16],
[16, 7]]

def find_vert_connected(vert, mlist):
    if len(mlist) == 1:
       for g in mlist:
            for k in g:
                if k is not vert:
                    return(k, -1)
        
    for i in mlist:
        if vert in i:
            idx = mlist.index(i)
            for m in i:
                if m is not vert:
                    return(m, idx)

def generate_ladder(starter, edge_key_list):
    stairs = []
    while(True):
        stairs.append(starter)
        starter, idx = find_vert_connected(starter,  edge_key_list)
        if idx == -1:
            stairs.append(starter)
            break
        edge_key_list.pop(idx)

    return(stairs)

print(generate_ladder(7, edge_key_list))
#
# output will be this
'''
[7, 16, 0, 5, 2, 3, 10, 24, 19, 6, 1, 20, 13, 9, 12, 8, 23, 15, 18, 17, 14, 11, 22, 4, 21]
'''
inside blender this might look like

#mesh_edge_sequence_rectifier.py
'''
Sometimes when doing a lathe like operation such as with the Screw Modifier,
the order in which the verts are stringed together to create the profile
(polyline, edgeloop) becomes important to the calculation of face normals for
the screwed geometry. 

''' 
# assumption 1  : edge loop is not closed.
# assumptino 2  : no duplicates ,make sure no verts are identical?
# assumption 3  : includes non consequtive edge keys, ie [0,1],[3,2],..[2,1]
# assertion 1   : edge has a definite start/end, can be either of the two end points.

import bpy

edge_key_list = []
for i in bpy.context.active_object.data.edges:
    edge_key_list.append([i.vertices[0], i.vertices[1]])

'''
old
edge_key_list = [[5, 2],[2, 3],[3, 10],[10, 24],[24, 19],
[19, 6],[6, 1],[1, 20],[20, 13],[13, 9],[9, 12],[12, 8],[8, 23],[23, 15],
[15, 18],[18, 17],[17, 14],[11, 22],[22, 4],[4, 21],[14, 11],[0, 5],[0, 16],
[16, 7]]

'''
# count vertices. ( remember to add 1, because we start counting from 0 )
vert_count = len(edge_key_list)

'''
new 
new_key_list = [0,1],[1,2],[2,3],....[23,24]
'''
# new key list should look like the new one
new_key_list = []
for k in range(vert_count):
    new_key_list.append([k, k+1])

# find all verts that only occur once in the edge key list.
sort_list = []
for i in edge_key_list:
    for l in i:
        sort_list.append(l)


# could use this to figure out if there are interuptions in the edge loop
# here i will only prototype using count == 1.
start_vert = 0
for i in range(len(set(sort_list))):
    if sort_list.count(i) == 1:
        #print("use :", i)
        start_vert = i
        break
    

# performing a copy of the list
shrink_list = []
for i in edge_key_list:
    shrink_list.append(i)


# something about the looks of this function suggests it could be condensed to 4 lines
def find_vert_connected(vert, mlist):
    if len(mlist) == 1:
       for g in mlist:
            for k in g:
                if k is not vert:
                    return(k, -1)
        
    for i in mlist:
        if vert in i:
            idx = mlist.index(i)
            for m in i:
                if m is not vert:
                    return(m, idx)


def generate_ladder(starter, edge_key_list):
    stairs = []
    while(True):
        stairs.append(starter)
        starter, idx = find_vert_connected(starter,  edge_key_list)
        if idx == -1:
            stairs.append(starter)
            break
        edge_key_list.pop(idx)

    return(stairs)

numerically_sorted_list = generate_ladder(7, shrink_list)
print(numerically_sorted_list)
To find out which operations are the most costly on lists, check the python wiki: http://wiki.python.org/moin/TimeComplexity

May 30, 2011

Using set to determin the difference between two lists

in a previous bit of code i offered a suggestion to use [-1] to find the latest created textfile object in blender, this works fine if you don't have existing textfiles with names that are alphabetically 'higher' than the default 'Text' name. When this is the case, and you add a new textfile data object you might consider doing something like:
  • first store the old list of textnames (text_names)
  • create the textfile
  • store the new list of textnames (text_names2)
  • run get_latest_lext(text_names, text_names2) over them, it will return the 'difference', being the newly created file.
def get_latest_text(list1, list2):
    return set(list2)-set(list1)

text_names = ["Text", "Text.001", "Text.002", "Text.003", "Ukraine"]
text_names2 = ["Text", "Text.001", "Text.002", "Text.003", "Text.004", "Ukraine"]

print(get_latest_text(text_names, text_names2))
# >>> "Text.004" 

May 16, 2011

Blender 2.5 Python Sorting Through A List

Sometimes it's worth a few moments to sort a list procedurally.. there is probably a more pythonic way of sorting it. Here's what works for me, this list happens to be sorted. if it wasn't we'd do a sorted() on it, sorting by the value in the first index of each member list.

'''
if we want a histogram type list like this
[[0, 34, 345], [0, 36, 345]]
[[1, 34, 345], [1, 36, 345], [1, 37, 345]]
[[2, 34, 345]]
[[4, 34, 345], [4, 34, 345]]
[[7, 34, 345]]

from a list like this:
[[0,34,345],[0,36,345],[1,34,345],[1,36,345],[1,37,345],
[2,34,345],[4,34,345],[4,34,345],[7,34,345]]
we can write:
'''

jax = [[0,34,345],[0,36,345],[1,34,345],[1,36,345],[1,37,345],
[2,34,345],[4,34,345],[4,34,345],[7,34,345]]

multi_list = []
temp_list = []
# or multi_list, temp_list = [],[]

# get first track num, this is a one off necessity
tracknum = jax[0][0]

# start digging through the data
for i in range(len(jax)):
    tracknum_line = jax[i][0]
    if not tracknum_line == tracknum:

        #we aren't adding more items for this track
        multi_list.append(temp_list) 
        temp_list = [] # empty the storage list
        
    temp_list.append(jax[i])
    tracknum = tracknum_line

    # if last line, then add templist to multilist
    # we aren't adding more items for this track
    if i == len(jax)-1:
        multi_list.append(temp_list) 
        
for track in multi_list:
    print(track)

May 14, 2011

Blender 2.5 Python Find Connected Vertices

given a list of edges, how do you find what vertices are connected to a given vertex?
Edges = [   
                [0,1],[1,2],[2,3],[3,0],
                [4,5],[5,6],[6,7],[7,4],
                [0,4],[1,5],[2,6],[3,7]
            ]
numbered_vertex = 1   # where 1 is an arbitrary choice of vertex
for i in Edges:
    if numbered_vertex in i:
        print(i)

or this way..a little bit more confusing at first.. but ultimately not too unwieldy.
Edges = [   
                [0,1],[1,2],[2,3],[3,0],
                [4,5],[5,6],[6,7],[7,4],
                [0,4],[1,5],[2,6],[3,7]
            ]
n = 1   # where 1 is an arbitrary choice of vertex
print([x for x in Edges if x[0] == n or x[1] == n])
# or 
print([x for x in Edges if n in x])