Skip to content

Instantly share code, notes, and snippets.

@PierrickKoch
Created July 12, 2013 09:59
Show Gist options
  • Select an option

  • Save PierrickKoch/5983280 to your computer and use it in GitHub Desktop.

Select an option

Save PierrickKoch/5983280 to your computer and use it in GitHub Desktop.
blender.helper.py
import bpy
# helpers (c/p following in a Blender Text editor, run it, look at your console)
def do_all():
for obj in bpy.data.objects:
# TODO create object
print_all_logic(obj)
print_properties(obj)
def get_properties(obj):
import json
map_prop = {}
properties = obj.game.properties
for name in properties.keys():
map_prop[name] = properties[name].value
if properties[name].type == 'TIMER':
map_prop[name] = "timer(%f)"%map_prop[name]
elif type(map_prop[name]) is str:
map_prop[name] = json.dumps(map_prop[name])
return map_prop
def print_properties(obj):
""" Returns the Game properties of the Blender objects """
map_prop = get_properties(obj)
if not map_prop:
return
str_prop = ", ".join(["%s = %s"%(pname, map_prop[pname]) \
for pname in map_prop.keys()])
print("properties(bpy.data.objects['%s'], %s)"%(obj.name, str_prop))
def print_all_logic(obj):
print("select_only(bpy.data.objects['%s'])"%obj.name)
print("game = bpy.context.object.game")
for brick in obj.game.actuators:
print_logic(brick, 'actuator')
for brick in obj.game.controllers:
print_logic(brick, 'controller')
for act in brick.actuators:
print("game.controllers['%s'].link(game.actuators['%s'])" % \
(brick.name, act.name) )
for brick in obj.game.sensors:
print_logic(brick, 'sensor')
for ctr in brick.controllers:
print("game.sensors['%s'].link(game.controllers['%s'])" % \
(brick.name, ctr.name) )
map_attr_default = {
'sensor': {
'frequency': 0,
'invert': False,
'use_level': False,
'use_pulse_false_level': False,
'use_pulse_true_level': False,
'use_tap': False,
},
'controller': {
'use_priority': False,
},
'actuator': {
},
}
def print_logic(brick, sca):
print("add_%s(type='%s', name='%s')"%(sca, brick.type, brick.name))
for attr in map_attr_default[sca].keys():
value = getattr(brick, attr)
if value != map_attr_default[sca][attr]:
print("game.%ss['%s'].%s = %s" % \
(sca, brick.name, attr, str(value)) )
do_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment