0
$\begingroup$

I'm new to blender as I am trying to create a simple Robot skeleton using the Blender Python API (bpy). However, when I run the python script the console spits out the following error:

Traceback (most recent call last): File "", line 1, in NameError: name 'Robot' is not defined

How can I fix this error? I don't understand what it means when it says that the name 'Robot' is now defined. I am also using Blender 2.7.7 on Ubuntu 18.04

The following is my code:

import bpy

#this function will draw a cylinder and cut portions from opposite sides to create the base plate
def Draw_Base_Plate():

        #create two cubes and then we'll "Subtract" them from the base plate
        #then we will delete the cubes
        bpy.ops.mesh.primitive_cube_add(radius = 0.05, location = (0.175, 0, 0.09))
        bpy.ops.mesh.primitive_cube_add(radius = 0.05, location = (-0.175, 0, 0.09))

        ############################################################################

        #adding the base plate itself
        bpy.ops.mesh.primitive_cylinder_add(radius = 0.15, depth = 0.05, location = (0,0,0.09))

        #adding boolean difference modifier from the first cube
        bps.ops.object.modifier_add(type = 'BOOLEAN')
        bpy.context.object.modifiers["Boolean"].operation = 'DIFFERENCE'
        bpy.context.object.modifiers["Boolean"].object = bpy.data.objects["Cube"]
        bpy.ops.object.modifier_apply(modifier = "Boolean")

        ###########################################################################

        #adding boolean difference modifier from the second cube
        bps.ops.object.modifier_add(type = 'BOOLEAN')
        bpy.context.object.modifiers["Boolean"].operation = 'DIFFERENCE'
        bpy.context.object.modifiers["Boolean"].object = bpy.data.objects["Cube.001"]
        bpy.ops.object.modifier_apply(modifier = "Boolean")

        ###########################################################################

        #deselect cylinder and delete cubes
        bpy.ops.object.select_pattern(pattern = "Cube")
        bpy.ops.object.select_pattern(pattern = "Cube.001")
        bpy.data.objects['Cylinder'].select = False
        bpy.ops.object.delete(use_global = False)

        ###########################################################################       

#this function will motors and wheels attached to the base plate    
def Draw_Motor_Wheels():

    #draw a cylinder with 0.045 of radius and 0.01 meters in depth for the wheels
    #once we create the wheels it will be rotated and translated into the
    #cut portion of the base plate

    #create first wheel
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.045, depth = 0.01, location = (0,0,0.07))
    #rotate the first wheel
    bpy.context.object.rotation_euler[1] = 1.5708 #is this pi/2
    #translate
    bpy.context.object.location[0] = 0.135

    #create second wheel
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.045, depth = 0.01, location = (0,0,0.07))
    #rotate the first wheel
    bpy.context.object.rotation_euler[1] = 1.5708 #is this pi/2
    #translate
    bpy.context.object.location[0] = -0.135   

    #now we can create the two dummy motors and add them onto the base plate

    #adding motors
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.018, depth = 0.06, location = (0.075,0,0.075))
    bpy.context.object.rotation_euler[1] = 1.5708 #pi/2 rotation?

    bpy.ops.mesh.primitive_cylinder_add(radius = 0.018, depth = 0.06, location = (-0.075,0,0.075))
    bpy.context.object.rotation_euler[1] = 1.5708 #pi/2 rotation?

    #now we will add the shafts to the motors
    #shaft 1
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.006, depth = 0.04, location = (0.12, 0,0.075))
    bpy.context.object.rotation_euler[1] = 1.5708

    #shaft 2
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.006, depth = 0.04, location = (-0.12, 0,0.075))
    bpy.context.object.rotation_euler[1] = 1.5708

    #now we can add the 2 caster wheels    
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.015, depth = 0.05, location = (0,0.125,0.065))
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.015, depth = 0.05, location = (0,-0.125, 0.065))

    #now we'll add a dummy kinect sensor
    bpy.ops.mesh.primitive_cube_add(radius = 0.04, location = (0,0, 0.26))

#this function will draw the middle plate of the robot

def Draw_Middle_Plate():
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.15, depth = 0.005, location = (0, 0, 0.22))

#this function will draw the top plate of the robot
def Draw_Top_Plate():
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.15, depth = 0.005, location = (0, 0, 0.37))

#this function will draw support tubes connecting the plates of the robot
def Draw_Support_Tubes():
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.007, depth = 0.3, location = (0.09, 0.09, 0.23))
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.007, depth = 0.3, location = (-0.09, 0.09, 0.23))
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.007, depth = 0.3, location = (-0.09, -0.09, 0.23))
    bpy.ops.mesh.primitive_cylinder_add(radius = 0.007, depth = 0.3, location = (0.09, -0.09, 0.23))

#This function will export the newly designed robot to .STL, but we have to change the STL filepath
#executing the script

def Save_to_STL():
    bpy.ops.object.select_all(action = 'SELECT')
    #bpy.ops.object.select_all(action = 'TOGGLE')
    bpy.ops.export_mesh.stl(check_existing = True, filepath = "/home/robertek/Desktop/exported.stl", filter_glob = "*.stl", ascii = False, use_mesh_modifiers = True, axis_forward = 'Y', axis_up = 'Z', global_scale = 1.0)

#Main body code
if __name__ == "__main__":
    Draw_Base_Plate()
    Draw_Motors_Wheels()
    Draw_Middle_Plate()
    Draw_Top_Plate()
    Save_to_STL()

$\endgroup$
  • $\begingroup$ Based on some of the commands used in the script it appears this is intended for Blender version 2.7x could you please confirm that is the intention and correct a few typo's (lines 17, 25, 113) in your script by editing your current question. $\endgroup$ – Ratt May 13 at 8:52
0
$\begingroup$

I solved the problem. I had some typos in the main body of my code and also the boolean modifiers were not correct in the code. I found another post with a similar problem, or at least, the code was utilized from the same book. Here is the stack exchange link for more info: Python script to generate robot model produces wrong result

import bpy # contains all the functionalities of Blender and it can only be
       # accessed from inside the Blender application



# This function will draw base plate
def Draw_Base_Plate(scene=bpy.context.scene):
    #Add cube for cutting sides of base plate

    bpy.ops.mesh.primitive_cube_add(radius=0.05, location=(0.175,0,0.09))
    cube1 = scene.objects.active
    # copy cube1 and link to scene (not required as example howto)

    cube2 = cube1.copy()
    cube2.location.x = -0.175
    scene.objects.link(cube2)


    #Adding base plate
    bpy.ops.mesh.primitive_cylinder_add(radius=0.15,depth=0.005, location=(0,0,0.09))
    cyl = scene.objects.active

    #Adding boolean difference modifiers
    for ob in [cube1, cube1]:
        mod = cyl.modifiers.new("FacePlateBool", type='BOOLEAN')
        mod.operation = 'DIFFERENCE'
        mod.object =  ob        
        bpy.ops.object.modifier_apply(modifier=mod.name)
        ob.location.x = -ob.location.x

    #Deselect cylinder and delete cube        
    cyl.select = False
    scene.objects.unlink(cube1)
    bpy.data.objects.remove(cube1)   

        #Adding booleab difference modifiers
    for ob in [cube2, cube2]:
        mod = cyl.modifiers.new("FacePlateBool", type='BOOLEAN')
        mod.operation = 'DIFFERENCE'
        mod.object =  ob        
        bpy.ops.object.modifier_apply(modifier=mod.name)
        ob.location.x = -ob.location.x

    #Deselect cylinder and delete cube        
    cyl.select = False
    scene.objects.unlink(cube2)
    bpy.data.objects.remove(cube2)   

#This function will draw motors and wheels
def Draw_Motors_Wheels():

    #Create first Wheel

    bpy.ops.mesh.primitive_cylinder_add(radius=0.045,depth=0.01, location=(0,0,0.07))
    #Rotate
    bpy.context.object.rotation_euler[1] = 1.5708
    #Transalation
    bpy.context.object.location[0] = 0.135

    #Create second wheel
    bpy.ops.mesh.primitive_cylinder_add(radius=0.045,depth=0.01, location=(0,0,0.07))
    #Rotate
    bpy.context.object.rotation_euler[1] = 1.5708
    #Transalation
    bpy.context.object.location[0] = -0.135

    #Adding motors

    bpy.ops.mesh.primitive_cylinder_add(radius=0.018,depth=0.06, location=(0.075,0,0.075))
    bpy.context.object.rotation_euler[1] = 1.5708

    bpy.ops.mesh.primitive_cylinder_add(radius=0.018,depth=0.06, location=(-0.075,0,0.075))
    bpy.context.object.rotation_euler[1] = 1.5708


    #Adding motor shaft
    bpy.ops.mesh.primitive_cylinder_add(radius=0.006,depth=0.04, location=(0.12,0,0.075))
    bpy.context.object.rotation_euler[1] = 1.5708

    bpy.ops.mesh.primitive_cylinder_add(radius=0.006,depth=0.04, location=(-0.12,0,0.075))
    bpy.context.object.rotation_euler[1] = 1.5708


#############################################################################################

    #Addubg Caster Wheel

    bpy.ops.mesh.primitive_cylinder_add(radius=0.015,depth=0.05, location=(0,0.125,0.065))
    bpy.ops.mesh.primitive_cylinder_add(radius=0.015,depth=0.05, location=(0,-0.125,0.065))

    #Adding Kinect

    bpy.ops.mesh.primitive_cube_add(radius=0.04, location=(0,0,0.26))    

#Draw middle plate
def Draw_Middle_Plate():
    bpy.ops.mesh.primitive_cylinder_add(radius=0.15,depth=0.005, location=(0,0,0.22))

#Adding top plate
def Draw_Top_Plate():
    bpy.ops.mesh.primitive_cylinder_add(radius=0.15,depth=0.005, location=(0,0,0.37))

#Adding support tubes
def Draw_Support_Tubes():
    #Cylinders
    bpy.ops.mesh.primitive_cylinder_add(radius=0.007,depth=0.30, location=(0.09,0.09,0.23))
    bpy.ops.mesh.primitive_cylinder_add(radius=0.007,depth=0.30, location=(-0.09,0.09,0.23))
    bpy.ops.mesh.primitive_cylinder_add(radius=0.007,depth=0.30, location=(-0.09,-0.09,0.23))
    bpy.ops.mesh.primitive_cylinder_add(radius=0.007,depth=0.30, location=(0.09,-0.09,0.23))

#Exporting into STL    
def Save_to_STL():
    bpy.ops.object.select_all(action='SELECT')
    #    bpy.ops.mesh.select_all(action='TOGGLE')
    bpy.ops.export_mesh.stl(check_existing=True, 
        filepath="/home/robertek/Desktop/exported.stl", 
        filter_glob="*.stl", ascii=False, use_mesh_modifiers=True,   
        axis_forward='Y', axis_up='Z', global_scale=1.0)


#Main code

if __name__ == "__main__":
    Draw_Base_Plate()
    Draw_Motors_Wheels()
    Draw_Middle_Plate()
    Draw_Top_Plate()
    Draw_Support_Tubes()
    Save_to_STL()
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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