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

July 18, 2011

printing verts, face indices, and their normals

import bpy

current_obj = bpy.context.active_object

print("="*40) # printing marker
for polygon in current_obj.data.polygons:
    verts_in_face = polygon.vertices[:]
    print("face index", polygon.index)
    print("normal", polygon.normal)
    for vert in verts_in_face:
        print("vert", vert, " vert co", current_obj.data.vertices[vert].co)

July 05, 2011

printing verts and faces of object to a text file

May 14, 2011

Blender 2.5 Printing Edge Keys

quick example:
import bpy

for item in bpy.data.objects:
if item.type == 'MESH':
for e in item.data.edge_keys:
print(e)

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])

Blender 2.5 Python Printing Groups and Objects and using Text Editor

# create a named text object 
>>> bpy.data.texts.new(name="dothistext")
bpy.data.texts["dothistext"]
py script to print names of groups in scene and their objects
import bpy

# print a divider
print("="*20)

for item in bpy.data.groups:
    print(item.name)
    for object in item.objects:
        print("--", object.name)
print() statements print to the debug console, if you don't know where that is. read this thread.
( Blender-2.57-User-preferences-and-the-console )

Useful for self when documenting what parts have been optimized manually. someone else might find a use for it. This script makes a textfile inside blender, which will be visible in the 'Text Editor' view.
import bpy
# creates a new text file, named "SceneList"
# writes Groups and contained Objects to it.

bpy.data.texts.new(name="SceneList")
newtext = bpy.data.texts["SceneList"] 
# or in one line
# newtext = bpy.data.texts.new(name="SceneList")

newtext.write("="*20 + "\n") # makes a divider

# your scene must contain groups. (groups are a collection of objects)
for item in bpy.data.groups:
    newtext.write(item.name + "\n")
    for object in item.objects:
        newtext.write("--" + object.name + "\n")
An example of reading a file from disk and writing it to a textobject:
# will create a text file called MyTextName (it will appear in the dropdown)
bpy.data.texts.new(name="MyTextName") 

# give give you a reference to that object, once created.
textobject = bpy.data.texts["MyTextName"] 

with open("yourtexttoread.txt", r) as readfile:
    for line in readfile:
        textobject.write(line)