Monday, May 4, 2009

How to add a mesh to Blender using Python

There are some useful examples in the API reference on the Mesh class. Here is a complete example that illustrates a minimal number of statements required to add a single triangular mesh.
#!BPY """ Name: 'HowToAddMesh001' Blender: 248 Group: 'Examples' """ ##########################################################3 import Blender import bpy from Blender import * from Blender.Scene import Render ############################################################## # Initialize a new scene # scene = Scene.New() # make this scene the active scene in the screen scene.makeCurrent() ############################################################## # add a camera and set it up # camdata = Camera.New() cam = scene.objects.new(camdata) cam.setLocation(0.0,-7.0,1.0) cam.setEuler(1.2,0.0,-0.15) ############################################################## # add a lamp and set it up # lampdata = Lamp.New() lampdata.setEnergy(10.0) lampdata.setType('Hemi') lamp = scene.objects.new(lampdata) lamp.setLocation(0.0,-1,5) lamp.setEuler(0.0,0.0,0.0) ############################################################## # create the mesh and bind a material to it # coords = [ [-1,-1,-1],[1,-1,-1],[1,1,-1]] faces = [ [2,1,0]] me = bpy.data.meshes.new('myMesh') me.verts.extend(coords) me.faces.extend(faces) ob = scene.objects.new(me,'myObj') # mat = Material.New('newMat') # create a new Material called 'newMat' mat.rgbCol = [0.8, 0.2, 0.2] # change its color # ob.setMaterials([mat]) # Window.RedrawAll() # ####################################### # render the image and save the image # #scn = Scene.GetCurrent() context = scene.getRenderingContext() # enable seperate window for rendering Render.EnableDispWin() # draw the image context.render() # save the image to disk # to the location specified by RenderPath # by default this will be a jpg file context.saveRenderedImage('MeshExample001') ####################################### # Save the blender file # Blender.Save('MeshExample001.blend')

No comments:

Post a Comment