User Tools

Site Tools


blender:main-blender-simple-shapes

Back to Blender Help Design

Now, we will start, for example, to build the robot frame with tubes. The Python code is below. Note that we start by killing the existing tubes, otherwise, each time we run the script we have now tubes created !!

import bpy
import logging
import math
 
deg2rad = math.pi/180.0
 
def define_sphere (name, diameter, x, y, z):
    # Create a simple sphere.
    bpy.ops.mesh.primitive_ico_sphere_add(radius=diameter/2.0)
    # Get the sphere object and rename it.
    sph = bpy.context.object
    #bpy.ops.transform.resize(value=(diameter,diameter,diameter))
    sph.name = name 
    # Change the location of the sphere.
    sph.location = (x, y, z)
    print (dir(sph))
    return sph
 
def define_cylinder (name, diameter, length, x, y, z, angx, angy , angz):
    # Create a simple cylinder.
    bpy.ops.mesh.primitive_cylinder_add(radius = diameter/2.0, depth=length)
    # Get the cylinder object and rename it.
    cyl = bpy.context.object
    cyl.name = name 
    # Change the orientation of the cylinder.
    cyl.rotation_euler = (angx*deg2rad, angy*deg2rad, angz*deg2rad)
    # Change the location of the cylinder.
    cyl.location = (x, y, z)
    #print (dir(cyl))
    return cyl
 
 
logging.basicConfig(level=logging.DEBUG,format='(%(threadName)-10s) %(message)s',)
 
# clean scene , remove objects created last time
context = bpy.context
scene = context.scene
for o in scene.objects:
    logging.debug (o.name)
    logging.debug (o.type)
    if o.type == "MESH":
        #o.select = True   # Blender 2.7x
        o.select_set(True) # Blender 2.8x
bpy.ops.object.delete() 
 
logging.debug("build chassis")
 
cylAvTopAv = define_cylinder ("CylAvTopAv", 0.05, 0.5, 0.8, 0.0, 0.6, 90.0, 0.0 , 0.0)
#cylAvTopAr = define_cylinder ("CylAvTopAr", 0.05, 0.5, 0.2, 0.0, 0.6, 90.0, 0.0 , 0.0)
cylAvTopLeft = define_cylinder ("CylAvTopLeft", 0.05, 0.5, 0.5, 0.25, 0.6, 90.0, 0.0 , 90.0)
cylAvTopRight = define_cylinder ("CylAvTopRight", 0.05, 0.5, 0.5, -0.25, 0.6, 90.0, 0.0 , 90.0)
shpAvTopLeftFront = define_sphere ("ShpAvTopLeftFront", 0.05, 0.8, 0.25, 0.6)

Back to top

blender/main-blender-simple-shapes.txt · Last modified: 2020/04/13 18:36 by admin