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

April 19, 2016

Getting Index (indices) of selected faces / polygons

One Way

# assuming the object is currently in Edit Mode.
import bpy
import bmesh

obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

for f in bm.faces:
    if f.select:
        print(f.index)

# Show the updates in the viewport
# and recalculate n-gon tessellation.
bmesh.update_edit_mesh(me, True)


Another Way

import bpy
import bmesh
from bmesh.types import BMFace

obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

for geom in bm.select_history:
    if isinstance(geom, BMFace):
        print(geom.index)

bmesh.update_edit_mesh(me, True)

Further Reading

I encourage you to know what the documentation says. blender.org/api/blender_python_api_current/bmesh.html?

March 29, 2014

mathutils.geometry.intersect_point_line

Another note to self. Originally I coded my own routine for this function. it calculates the distance between intx and a, and intx and b and if those two quantities sum up to something larger than the distance between a,b then the point does not lie on the edge.

So now I'll use this instead:

March 22, 2014

EXM Extend Multiple Edges towards Edge

Coming from AutoCAD you might miss _extend in Blender. Extend lets you pick a main edge (Edge Prime) and subsequent selected edges will be extended towards the Edge Prime.

This Addon has two modes.
  • press Comma to move the vertices of the selected edges towards their projected intersection on Edge Prime.
  • press Period to create new edges between the closest vertex on the selected edge to their projected intersection on Edge Prime.


link: place inzipped mesh_tinyCAD folder into scripts/addons or scripts/addons_contrib

The addon uses bgl to draw projected edges onto the mesh.

Other CAD tools can be found on BlenderArtists, here's a quick roundup that Nikita suggested I add. (thanks);

June 03, 2013

Normalize and Quantive

A question on BA.org looking for a script that can scale an object to within the boundaries of -1 and 1 for all axis (called normalizing, maybe erroneously) and then placing further constraint on the geometry that it must fall within grid slices of 0.1 .. so xyz can only have values between the range (-1.0, -0.9 -0.8 ....0.8, 0.9, 1.0), i made the following scripts.

here is the development version:
here is everything chucked into one loop.

Alternative code

May 29, 2013

Adding geometry to existing geometry using BMesh

I resist asking questions until that feeling starts to kick where there has to be a fast but illusive answer. I want to see blender.stackexchange progress out of beta, it has been pretty useful already!
import bpy, bmesh

obj = bpy.context.object
bm = bmesh.from_edit_mesh(obj.data)

bm.verts.new((2.0, 2.0, 2.0))
bm.verts.new((-2.0, 2.0, 2.0))
bm.verts.new((-2.0, -2.0, 2.0))

bm.faces.new((bm.verts[i] for i in range(-3,0)))

bm.verts.new((2.0, 2.0, -2.0))
bm.verts.new((-2.0, 2.0, -2.0))
bm.verts.new((-2.0, -2.0, -2.0))
bm.verts.new((2.0, -2.0, -2.0))

bm.faces.new((bm.verts[i] for i in range(-4,0)))

# update, while in edit mode
# thanks to ideasman42 for making this clear
# http://blender.stackexchange.com/questions/414/
bmesh.update_edit_mesh(obj.data)
Adding edges is also similar
# let's add two verts and connect them as an edge
bm.verts.new((0.0, 1.0, 1.0))
bm.verts.new((0.0, 0.0, 1.0))
bm.edges.new((bm.verts[-2], bm.verts[-1]))

# remember to do a    bmesh.update_edit_mesh(obj.data)

July 09, 2012

Make Unique Geometry (no shared vertices)

This is the basic approach to ripping all geometry apart. doesn't play nice with UV and ignores world matrix (ie, is stuff scaled rotated translated or not..) :)

Pretty soon afterwards I wanted a way to preserve UV information per face. The following script was born. link : todo