How to make a mesh by using python script on Blender

11,374

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:
11,374

Related videos on Youtube

vine_user
Author by

vine_user

I'm writing a blog about how to use Desktop Linux. And also, I am managing the Q&A site of the Japanese version relevant to desktop Linux.

Updated on September 18, 2022

Comments

  • vine_user
    vine_user over 1 year

    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

  • vine_user
    vine_user almost 11 years
    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.