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

May 31, 2015

Scripted Cyclic Curve

Have received several questions about this, I'll let the code speak.
import bpy  
import math
from math import cos, sin, pi
from mathutils import Vector  

tau = 2 * pi
w = 1 

num_points = 50
amp = lambda i: 5 if (i % 2) else 7
pos = lambda f, i: f(i/num_points*tau) * amp(i)
listOfVectors = [(pos(sin, i), pos(cos, i), 0) for i in range(num_points)]  
  
def MakePolyLine(objname, curvename, cList):  
    curvedata = bpy.data.curves.new(name=curvename, type='CURVE')  
    curvedata.dimensions = '3D'  
  
    objectdata = bpy.data.objects.new(objname, curvedata)  
    objectdata.location = (0, 0, 0)
    bpy.context.scene.objects.link(objectdata)  
  
    polyline = curvedata.splines.new('NURBS')  
    polyline.points.add(len(cList)-1)  
    for num in range(len(cList)):  
        polyline.points[num].co = (cList[num])+(w,)  
  
    polyline.order_u = len(polyline.points)-1
    polyline.use_endpoint_u = True
    polyline.use_cyclic_u = True    
    
  
MakePolyLine("NameOfMyCurveObject", "NameOfMyCurve", listOfVectors)

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

March 26, 2013

Painting Vertex Color Map using selected Faces

This script is obsolete, shift+K will fill all selected faces/ verts with the brush colour.

selected faces

During a recent conversation with Jimmy Gunawan of blendersushi I remembered that I wanted to write a script that lets me paint the vertex color map as a function of which faces are selected. Here's a version of this script that runs in vertex paint mode with face masking mode on. i'll leave refactoring this snippet to you.

I've limited some freedom by forcing the user to be in 'VERTEX_PAINT' and Face Masking Active, but it's safer this way (even after some bugfixing by campbell) -- as it is still easy to crash blender in obscure ways otherwise.

July 18, 2012

Blender 2.6x igs importer

At the moment it only deals with line type 126 (which is BSpline). It can be downloaded here: blender_version_igs_import.py. Warning: only deals with entity type 126.


(model credit: siggraphSpacecraft48.igs -- i have no idea who made it.


At the moment the igs importer probably doesn't do much good beyond importing BSplines. But it happens to do a remarkable job at importing .igs files from
http://www.dgp.toronto.edu/~shbae/ilovesketch.htm, which is why i wrote it.

Behind the scenes

After staring at the .igs files for a while some patterns emerged. It became clear that a relatively uncomplicated extraction pattern would probably suffice to get things started. Without reading through the official file specs i tend to write code for whatever gets flinged my way, until a meta pattern emerges and then things become more sophisticated. Right now the code is procedural, and easily lets me implement new entities as they occur.

After several hours manual labour

I manage to squeeze this one out. Blender has NURBS surfaces but the interface for NURBS specifically is very weak and probably hasn't changed much in the past 4 years. Anyway. here it is (model 83 from the iLOVESKETCH page:
some more hours.

May 27, 2011

Scripted Animation using keyframe_insert

Inspired by a post on BA, it just so happens that i can use this for my own particle script. It's pretty basic but easy enough to elaborate on.

While this first solution will work, it is not optimal on massive datasets because it uses bpy.ops inside the positions loop. The short story about bpy.ops is that ops operations are really meant to be called from the UI directly, where you won't notice the slowdown (blender state refreshes) between clicks.

(using bpy.ops like in the following snippet is not recommended)
import bpy

start_pos = (0,0,0)
bpy.ops.mesh.primitive_uv_sphere_add(segments=32, size=0.3, location=start_pos)
bpy.ops.object.shade_smooth()

positions = (0,0,2),(0,1,2),(3,2,1),(3,4,1),(1,2,1)
frame_num = 0
for position in positions:
    bpy.context.scene.frame_set(frame_num)
    bpy.context.active_object.location = position
    bpy.ops.anim.keyframe_insert(type='Location', confirm_success=True)
    frame_num += 10
    

Here is a version that uses the data_path and avoids using bpy.ops in the loop ( recommended )

May 16, 2011

Blender 2.5 Python Extrude with Vector

import bpy
from mathutils import Vector

myVec = Vector((1.0,2.0,1.0))

bpy.ops.mesh.extrude_region_move(MESH_OT_extrude={"type":'REGION'}, 
                                 TRANSFORM_OT_translate={"value":myVec})
If you execute this script while in edit mode, with a face selected (in face selection mode) -- then the created faces along the extrusion will/may appear inverted, they revert to outside as soon as you perform other operations on a mesh. Experiment!

The 2.6 equivalent code

The best way to figure these things out is to use the reporting console, (ie, set for example a timeline window to display the information window, any user interaction will show up as code)