Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to know how to make a mesh having Vertices and Faces by using python script from mathematical equations, for example, spherical harmonics, and so on.

If the sample code of an easy function is shown, the rest will be made by myself.

Ubuntu version: 12.04

Blender version: 2.62

Added Information (2013/08/02) :

I found very useful site,

Blender Python: Mathematical Mesh

Finally I got an example, this is the case of Normal Distribution.

import bpy
import math

# clear mesh and object
for item in bpy.context.scene.objects:
    if item.type == 'MESH':
        bpy.context.scene.objects.unlink(item)
for item in bpy.data.objects:
    if item.type == 'MESH':
        bpy.data.objects.remove(item)
for item in bpy.data.meshes:
    bpy.data.meshes.remove(item)
for item in bpy.data.materials:
    bpy.data.materials.remove(item)

# mesh arrays
verts = []
faces = []

# mesh variables
numX = 100
numY = 100

# variance and scale variables
variance = .35
scale = 4

# fill verts array
for i in range (0, numX):
    for j in range(0,numY):
        # nomalize range
        u = 2*(i/numX-1/2)
        v = 2*(j/numY-1/2)

        s = variance
        x = scale*u
        y = scale*v
        z = scale*1/math.sqrt(2*math.pi*s*s)*math.exp(-(u*u+v*v)/(2*s*s))

        vert = (x,y,z)
        verts.append(vert)

# fill faces array
count = 0
for i in range (0, numY *(numX-1)):
    if count < numY-1:
        A = i
        B = i+1
        C = (i+numY)+1
        D = (i+numY)
        face = (A,B,C,D)
        faces.append(face)
        count = count + 1
    else:
        count = 0

# create mesh and object
mesh = bpy.data.meshes.new("wave")
object = bpy.data.objects.new("wave",mesh)

# set mesh location
object.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(object)

# create mesh from python data
mesh.from_pydata(verts,[],faces)
mesh.update(calc_edges=True)

Screenshot: Mesh Object by Python Script

share|improve this question
add comment

1 Answer

You should search in similar scripts. As you might know, Blender comes with addons written in Python. There are some available addons about the creation of meshes. You have to enable to use them, but they are installed on your computer. Search in /usr/share/blender/addons, to see them. Maybe you can do some reverse engenieering to answer your question.

Copy your own script to this folder. There is also "install from file" in preferences. You have to enable the scripts in preferences. It's a bit hidden. On the upper left of the default window you can see an exclamation mark. Click on it and select preferences from dropdown menu. Then you have to increase hight of this bar, to see the preferences. On screenshot you can see increased preferences bar. Select addon tab. On the right hand, you can mark "enable addon". To make this change permanent, you have to save them. I have forgotten about the shortcut, search for it, maybe its Ctrl+U.

Well, when I used Blender, there was an option to run Scripts. Now, I can see only a Console.

enter image description here

share|improve this answer
    
Thanks kleinempfaenger. I found those scripts , not in "/usr/share/blender/addons" , but in "/usr/lib/blender/scripts/addons". However, I can not understand well yet, so I'm waiting for any other replies. –  vine_user Jul 28 '13 at 8:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.