2
$\begingroup$

How to copy BGE logic from one object to another using script?

I use the following code (coppied from an addon) to create a duplicate of an object ...

def copyObject(scene, name, copyobj, location ):

  # Create new mesh
  mesh = bpy.data.meshes.new(name)

  # Create a new object.
  ob_new = bpy.data.objects.new(name, mesh)
  ob_new.data = copyobj.data.copy()
  ob_new.scale = copyobj.scale
  ob_new.location = location
  scene.objects.link(ob_new)

  return ob_new

But this does not copy the game logic from the original object.

Thanks!

$\endgroup$
2
  • $\begingroup$ this answer will show the python paths of the logic bricks data. I don't think you can do a copy() but you can step through the properties of each brick and recreate them. $\endgroup$
    – sambler
    Commented Jan 23, 2015 at 16:17
  • $\begingroup$ thx sambler. selecting the object and using bpy.ops.object.duplicate_move seems to be working. I'll post the code. $\endgroup$
    – Bhupen
    Commented Jan 23, 2015 at 17:11

2 Answers 2

6
$\begingroup$

To copy game logic from one object to one or many others:

Select the other object (or multiples using shift) first (object you would like to copy game logic nodes to)

Select the object you want to copy from (using shift to select)

Click Object->Game->Copy Logic Bricks

( or script as bpy.ops.object.logic_bricks_copy() )

Can be done for Properties as well as Physics properties.

Is this the type of copy you are looking for?

$\endgroup$
1
  • $\begingroup$ I was looking for copying the whole deal (as when you do with shift-D). But this script would be handy if I wanted to copy just the logic bricks. thx! $\endgroup$
    – Bhupen
    Commented Jan 23, 2015 at 23:41
2
$\begingroup$

Following workaround of selecting the object and using bpy.ops.object.duplicate_move works for my case ...

def copyObject( scene, name, copyobj, loc ):
  bpy.ops.object.select_all(action='DESELECT')
  copyobj.select = True
  l = (loc[0] - copyobj.location[0],loc[1] - copyobj.location[1],loc[2] - copyobj.location[2])
  bpy.ops.object.duplicate_move( TRANSFORM_OT_translate={"value":l} )
  obj = scene.objects[0]
  obj.name = name
  return obj
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .