Script templates, including game logic scripts from 2.4x and new operator template.

Files copied into scripts/templates will automatically appear in the menu.

the operator template is a bit rough but a start.
This commit is contained in:
Campbell Barton 2009-10-29 11:26:44 +00:00
parent e14a8635cc
commit 6f1e9a843e
7 changed files with 227 additions and 1 deletions

@ -278,4 +278,4 @@ menu_func = lambda self, context: self.layout.itemO("export.ply", text="Stanford
menu_item = dynamic_menu.add(bpy.types.INFO_MT_file_export, menu_func)
if __name__ == "__main__":
bpy.ops.EXPORT_OT_ply(path="/tmp/test.ply")
bpy.ops.export.ply(path="/tmp/test.ply")

@ -0,0 +1,78 @@
# This script must be assigned to a python controller
# where it can access the object that owns it and the sensors/actuators that it connects to.
# GameLogic has been added to the global namespace no need to import
# for keyboard event comparison
# import GameKeys
# support for Vector(), Matrix() types and advanced functions like AngleBetweenVecs(v1,v2) and RotationMatrix(...)
# import Mathutils
# for functions like getWindowWidth(), getWindowHeight()
# import Rasterizer
def main():
cont = GameLogic.getCurrentController()
# The KX_GameObject that owns this controller.
own = cont.owner
# for scripts that deal with spacial logic
own_pos = own.worldPosition
# Some example functions, remove to write your own script.
# check for a positive sensor, will run on any object without errors.
print 'Logic info for KX_GameObject', own.name
input = False
for sens in cont.sensors:
# The sensor can be on another object, we may want to use it
own_sens = sens.owner
print ' sensor:', sens.name,
if sens.positive:
print '(true)'
input = True
else:
print '(false)'
for actu in cont.actuators:
# The actuator can be on another object, we may want to use it
own_actu = actu.owner
print ' actuator:', actu.name
# This runs the actuator or turns it off
# note that actuators will continue to run unless explicitly turned off.
if input:
cont.activate(actu)
else:
cont.deactivate(actu)
# Its also good practice to get sensors and actuators by name
# rather then index so any changes to their order wont break the script.
# sens_key = cont.sensors['key_sensor']
# actu_motion = cont.actuators['motion']
# Loop through all other objects in the scene
sce = GameLogic.getCurrentScene()
print 'Scene Objects:', sce.name
for ob in sce.objects:
print ' ', ob.name, ob.worldPosition
# Example where collision objects are checked for their properties
# adding to our objects "life" property
"""
actu_collide = cont.sensors['collision_sens']
for ob in actu_collide.objectHitList:
# Check to see the object has this property
if ob.has_key('life'):
own['life'] += ob['life']
ob['life'] = 0
print own['life']
"""
main()

@ -0,0 +1,15 @@
def main():
cont = GameLogic.getCurrentController()
own = cont.owner
sens = cont.sensors['mySensor']
actu = cont.actuators['myActuator']
if sens.positive:
cont.activate(actu)
else:
cont.deactivate(actu)
main()

@ -0,0 +1,26 @@
# This module can be accessed by a python controller with
# its execution method set to 'Module'
# * Set the module string to "gamelogic_module.main" (without quotes)
# * When renaming the script it MUST have a .py extension
# * External text modules are supported as long as they are at
# the same location as the blendfile or one of its libraries.
import GameLogic
# variables defined here will only be set once when the
# module is first imported. Set object spesific vars
# inside the function if you intend to use the module
# with multiple objects.
def main(cont):
own = cont.owner
sens = cont.sensors['mySensor']
actu = cont.actuators['myActuator']
if sens.positive:
cont.activate(actu)
else:
cont.deactivate(actu)
# dont call main(GameLogic.getCurrentController()), the py controller will

@ -0,0 +1,53 @@
def write_some_data(context, path, use_some_setting):
pass
class ExportSomeData(bpy.types.Operator):
'''This appiers in the tooltip of the operator and in the generated docs.'''
__idname__ = "export.some_data" # this is important since its how bpy.ops.export.some_data is constructed
__label__ = "Export Some Data"
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
# TODO, add better example props
__props__ = [
bpy.props.StringProperty(attr="path", name="File Path", description="File path used for exporting the PLY file", maxlen= 1024, default= ""),
bpy.props.BoolProperty(attr="use_some_setting", name="Apply Modifiers", description="Apply Modifiers to the exported mesh", default= True),
]
def poll(self, context):
return context.active_object != None
def execute(self, context):
if not self.is_property_set("path"):
raise Exception("filename not set")
write(self.path, context, use_setting, SOME_SETTING = self.use_some_setting)
return ('FINISHED',)
def invoke(self, context, event):
wm = context.manager
if True:
# File selector
wm.add_fileselect(self.__operator__) # will run self.execute()
return ('RUNNING_MODAL',)
else if 0:
# Redo popup
wm.invoke_props_popup(self.__operator__, event) #
return ('RUNNING_MODAL',)
else if 0:
return self.execute(context)
bpy.ops.add(ExportSomeData)
# Only needed if you want to add into a dynamic menu
import dynamic_menu
menu_func = lambda self, context: self.layout.itemO("export.some_data", text="Example Exporter...")
menu_item = dynamic_menu.add(bpy.types.INFO_MT_file_export, menu_func)
# Use for running this script directly
if __name__ == "__main__":
bpy.ops.export.some_data(path="/tmp/test.ply")

@ -0,0 +1,20 @@
def main(context):
for ob in context.scene.objects:
print(ob)
class SimpleOperator(bpy.types.Operator):
''''''
__idname__ = "object.simple_operator"
__label__ = "Simple Object Operator"
def poll(self, context):
return context.active_object != None
def execute(self, context):
main(context)
return ('FINISHED',)
bpy.ops.add(SimpleOperator)
if __name__ == "__main__":
bpy.ops.object.simple_operator()

@ -135,11 +135,44 @@ class TEXT_MT_text(bpy.types.Menu):
layout.itemO("text.properties", icon='ICON_MENU_PANEL')
#ifndef DISABLE_PYTHON
# XXX layout.column()
# XXX uiDefIconTextBlockBut(block, text_template_scriptsmenu, NULL, ICON_RIGHTARROW_THIN, "Script Templates", 0, yco-=20, 120, 19, "");
# XXX uiDefIconTextBlockBut(block, text_plugin_scriptsmenu, NULL, ICON_RIGHTARROW_THIN, "Text Plugins", 0, yco-=20, 120, 19, "");
#endif
layout.itemM("TEXT_MT_templates")
class TEXT_MT_templates(bpy.types.Menu):
'''
Creates the menu items by scanning scripts/templates
'''
__label__ = "Script Templates"
def draw(self, context):
import os
def path_to_name(f):
f_base = os.path.splitext(f)[0]
f_base = f_base.replace("_", " ")
return ' '.join([w[0].upper() + w[1:] for w in f_base.split()])
layout = self.layout
template_dir = os.path.join(os.path.dirname(__file__), os.path.pardir, "templates")
for f in sorted(os.listdir(template_dir)):
if f.startswith("."):
continue
path = os.path.join(template_dir, f)
layout.item_stringO("text.open", "path", path, text=path_to_name(f))
class TEXT_MT_edit_view(bpy.types.Menu):
__label__ = "View"
@ -233,6 +266,7 @@ bpy.types.register(TEXT_HT_header)
bpy.types.register(TEXT_PT_properties)
bpy.types.register(TEXT_PT_find)
bpy.types.register(TEXT_MT_text)
bpy.types.register(TEXT_MT_templates)
bpy.types.register(TEXT_MT_format)
bpy.types.register(TEXT_MT_edit)
bpy.types.register(TEXT_MT_edit_view)