2
$\begingroup$

In Blender Game Engine :

I want to join two spheres without losing the logic from the other but I don't know how.I knew that its possible via python. As I am not an expert in blender python can any one tell me how can I do it ? - Or Is There any other method ?

Here Is the file

$\endgroup$
2
  • $\begingroup$ Could you please add a sample file. With two objects with the logic you want joined. $\endgroup$
    – batFINGER
    Commented Sep 9, 2017 at 10:27
  • $\begingroup$ @batFINGER I added a sample file with what i want to join $\endgroup$ Commented Sep 9, 2017 at 12:13

1 Answer 1

3
$\begingroup$

Copy sensors.

Here is some pretty untested code to copy sensors from active object to other selected objects.

import bpy
context = bpy.context

def copy_props(v1, v2):
    ''' copy props from v1 to v2 '''
    props = v1.bl_rna.properties
    for k, v in props.items():
        if (k in ["rna_type", "type", "name"]
            or v.is_readonly):
            continue
        setattr(v2, k, getattr(v1, k))    

def copy_actuators(actuators, c):
    ob = c.id_data
    for a in actuators:
        bpy.ops.logic.actuator_add(type=a.type, 
                name=a.name,
                object=ob.name)
        a2 = ob.game.actuators[-1]
        a2.link(c)
        copy_props(a, a2)
    return c.actuators

def copy_controllers(controllers, s):
    ob = s.id_data
    for c in controllers:
        bpy.ops.logic.controller_add(
               type=c.type,
               name=c.name,
               object=ob.name)
        c2 = ob.game.controllers[-1]
        s.link(c2)
        copy_actuators(c.actuators, c2)
        copy_props(c, c2)
    return s.controllers

# sensors
def copy_sensors(o1, o2):
    ''' copy sensors from ob1 to ob2'''
    for s in o1.game.sensors:
        bpy.ops.logic.sensor_add(type=s.type, name=s.name, object=o2.name) 
        s2 = o2.game.sensors[-1]
        copy_controllers(s.controllers, s2)
        copy_props(s, s2)

scene = context.scene
o1 = context.active_object
to_objects = [o for o in context.selected_objects 
                if o != o1]
for o2 in to_objects:                
    copy_sensors(o1, o2)  

Result on your file.

enter image description here

$\endgroup$

You must log in to answer this question.

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