how to add an empty and a camera using python script ?

i want to add an empty and a camera using a python script ! next i want to parent the camera to the empty ! please tell me how to do that ? :slight_smile:
it should be like this when i run the script it should add an empty and a camera in the scene and parents the camera to the empty…co-ordinates (0,0,0) :smiley:

Add empty
bpy.ops.object.add(type=‘EMPTY’, view_align=False, enter_editmode=False, location=(12.7992, 6.42344, -4.98939), rotation=(0, 0, 0), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

add camera
bpy.ops.object.camera_add(view_align=False, enter_editmode=False, location=(0.0, 0.0, 0.0), rotation=(0.0, 0.0, 0.0), layers=(False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))¶
http://www.blender.org/documentation/blender_python_api_2_68_2/bpy.ops.object.html?highlight=camera#bpy.ops.object.camera_add

here is example to add camera empty and constraint



   print ('There is not Camera  then need to add one   %%%%%%%%%%% ')
 
   cam = bpy.data.cameras.new("Camera")
   cam_ob = bpy.data.objects.new("Camera", cam)
   bpy.context.scene.objects.link(cam_ob)
 
   # Add an Empty and constraint if not in scene
 
   bpy.context.scene.objects.active = bpy.context.scene.objects["Camera"]
 
   print (  'cam_ob constraints=',cam_ob.constraints)
   print (  'list constraints=',len(cam_ob.constraints))
 
 
   print ()
   print ( '  test for empty here ')
   print ()
 
 
   if "Empty" in types:        # Empty check for constraint
    print ("there is an Empty   #########   ")
 
    ob = bpy.data.objects['Camera']
    target = bpy.data.objects.get('Empty', False)
    print ('ob.constraints =',ob.constraints,'len =',len(ob.constraints))
    print()
    print (' List of constraint')
    for i1 in range(0,len(ob.constraints)):
     print (' Constraint =',ob.constraints[i1])
  #   print (dir(ob.constraints[i1]))
     print()
  #   print (dir(ob.constraints.new))
     print ( 'constraint name =',ob.constraints[i1].name)
    print()
 
    if not bpy.data.objects["Camera"].constraints["TrackTo"].target:
     print (' add target name')
     bpy.data.objects["Camera"].constraints["TrackTo"].target=target
 
 
   else:            # No Empty
    print ('There is not Empty  then need to add one + Constraint')
 
    empty = bpy.data.objects.new("Empty", None)
    bpy.context.scene.objects.link(empty)
    bpy.context.scene.update()
 
    # Add a constraint 
 
    ob = bpy.data.objects['Camera']
    target = bpy.data.objects.get('Empty', False)
 
 #   if not bpy.data.objects["Camera"].constraints["TrackTo"].target:
    if not bpy.data.objects["Camera"].constraints["TRACK_TO"].target:
     print (' add target name')
     bpy.data.objects["Camera"].constraints["TrackTo"].target=target



try to modify the code and use only what you need !

happy bl

when using operators, you can leave out uncessary arguments, such as the layers

i know how to do it from the console window but not form the text editor…please help me on that…

well it’s the same? You might need to import the same stuff as available in the console, e.g.

import bpy
from bpy import context as C
…

i am not able to parent the camera to the empty :frowning: