[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24155] trunk/blender/release/scripts: Script templates, including game logic scripts from 2. 4x and new operator template.

Campbell Barton ideasman42 at gmail.com
Thu Oct 29 12:26:47 CET 2009


Revision: 24155
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24155
Author:   campbellbarton
Date:     2009-10-29 12:26:44 +0100 (Thu, 29 Oct 2009)

Log Message:
-----------
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.

Modified Paths:
--------------
    trunk/blender/release/scripts/io/export_ply.py
    trunk/blender/release/scripts/ui/space_text.py

Added Paths:
-----------
    trunk/blender/release/scripts/templates/
    trunk/blender/release/scripts/templates/gamelogic.py
    trunk/blender/release/scripts/templates/gamelogic_basic.py
    trunk/blender/release/scripts/templates/gamelogic_module.py
    trunk/blender/release/scripts/templates/operator.py
    trunk/blender/release/scripts/templates/operator_simple.py

Modified: trunk/blender/release/scripts/io/export_ply.py
===================================================================
--- trunk/blender/release/scripts/io/export_ply.py	2009-10-29 11:12:59 UTC (rev 24154)
+++ trunk/blender/release/scripts/io/export_ply.py	2009-10-29 11:26:44 UTC (rev 24155)
@@ -278,4 +278,4 @@
 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")

Added: trunk/blender/release/scripts/templates/gamelogic.py
===================================================================
--- trunk/blender/release/scripts/templates/gamelogic.py	                        (rev 0)
+++ trunk/blender/release/scripts/templates/gamelogic.py	2009-10-29 11:26:44 UTC (rev 24155)
@@ -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()

Added: trunk/blender/release/scripts/templates/gamelogic_basic.py
===================================================================
--- trunk/blender/release/scripts/templates/gamelogic_basic.py	                        (rev 0)
+++ trunk/blender/release/scripts/templates/gamelogic_basic.py	2009-10-29 11:26:44 UTC (rev 24155)
@@ -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()

Added: trunk/blender/release/scripts/templates/gamelogic_module.py
===================================================================
--- trunk/blender/release/scripts/templates/gamelogic_module.py	                        (rev 0)
+++ trunk/blender/release/scripts/templates/gamelogic_module.py	2009-10-29 11:26:44 UTC (rev 24155)
@@ -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

Added: trunk/blender/release/scripts/templates/operator.py
===================================================================
--- trunk/blender/release/scripts/templates/operator.py	                        (rev 0)
+++ trunk/blender/release/scripts/templates/operator.py	2009-10-29 11:26:44 UTC (rev 24155)
@@ -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")
\ No newline at end of file

Added: trunk/blender/release/scripts/templates/operator_simple.py
===================================================================
--- trunk/blender/release/scripts/templates/operator_simple.py	                        (rev 0)
+++ trunk/blender/release/scripts/templates/operator_simple.py	2009-10-29 11:26:44 UTC (rev 24155)
@@ -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()

Modified: trunk/blender/release/scripts/ui/space_text.py
===================================================================
--- trunk/blender/release/scripts/ui/space_text.py	2009-10-29 11:12:59 UTC (rev 24154)
+++ trunk/blender/release/scripts/ui/space_text.py	2009-10-29 11:26:44 UTC (rev 24155)
@@ -135,12 +135,45 @@
 
 		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_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)





More information about the Bf-blender-cvs mailing list