sphinx doc gen: multiple examples possible and include the scripts docstring inline in sphinx.
also tag unused vars
This commit is contained in:
43
doc/python_api/examples/bpy.types.Operator.1.py
Normal file
43
doc/python_api/examples/bpy.types.Operator.1.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""
|
||||
Invoke Function
|
||||
+++++++++++++++
|
||||
This example shows how to define an operator which gets mouse input to
|
||||
execute a function and that this operator can be invoked or executed from
|
||||
the python api.
|
||||
|
||||
Also notice this operator defines its own properties, these are different
|
||||
to typical class properties because blender registers them with the
|
||||
operator, to use as arguments when called, saved for operator undo/redo and
|
||||
automatically added into the user interface.
|
||||
"""
|
||||
import bpy
|
||||
|
||||
|
||||
class SimpleMouseOperator(bpy.types.Operator):
|
||||
"""This operator shows the mouse location, this string is used for the tooltip and API docs"""
|
||||
bl_idname = "wm.mouse_position"
|
||||
bl_label = "Invoke Mouse Operator"
|
||||
|
||||
x = bpy.props.IntProperty()
|
||||
y = bpy.props.IntProperty()
|
||||
|
||||
def execute(self, context):
|
||||
# rather then printing, use the report function,
|
||||
# this way the messag appiers in the header,
|
||||
self.report({'INFO'}, "Mouse coords are %d %d" % (self.x, self.y))
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
self.x = event.mouse_x
|
||||
self.y = event.mouse_y
|
||||
return self.execute(context)
|
||||
|
||||
bpy.utils.register_class(SimpleMouseOperator)
|
||||
|
||||
# Test call to the newly defined operator.
|
||||
# Here we call the operator and invoke it, meaning that the settings are taken
|
||||
# from the mouse.
|
||||
bpy.ops.wm.mouse_position('INVOKE_DEFAULT')
|
||||
|
||||
# Another test call, this time call execute() directly with pre-defined settings.
|
||||
bpy.ops.wm.mouse_position('EXEC_DEFAULT', x=20, y=66)
|
||||
36
doc/python_api/examples/bpy.types.Operator.2.py
Normal file
36
doc/python_api/examples/bpy.types.Operator.2.py
Normal file
@@ -0,0 +1,36 @@
|
||||
"""
|
||||
Calling a File Selector
|
||||
+++++++++++++++++++++++
|
||||
This example shows how an operator can use the file selector
|
||||
"""
|
||||
import bpy
|
||||
|
||||
|
||||
class ExportSomeData(bpy.types.Operator):
|
||||
"""Test exporter which just writes hello world"""
|
||||
bl_idname = "export.some_data"
|
||||
bl_label = "Export Some Data"
|
||||
|
||||
filepath = bpy.props.StringProperty(subtype="FILE_PATH")
|
||||
|
||||
def execute(self, context):
|
||||
file = open(self.filepath, 'w')
|
||||
file.write("Hello World")
|
||||
return {'FINISHED'}
|
||||
|
||||
def invoke(self, context, event):
|
||||
context.window_manager.fileselect_add(self)
|
||||
return {'RUNNING_MODAL'}
|
||||
|
||||
|
||||
# Only needed if you want to add into a dynamic menu
|
||||
def menu_func(self, context):
|
||||
self.layout.operator(ExportSomeData.bl_idname, text="Text Export Operator")
|
||||
|
||||
# Register and add to the file selector
|
||||
bpy.utils.register_class(ExportSomeData)
|
||||
bpy.types.INFO_MT_file_export.append(menu_func)
|
||||
|
||||
|
||||
# test call
|
||||
bpy.ops.export.some_data('INVOKE_DEFAULT')
|
||||
20
doc/python_api/examples/bpy.types.Operator.py
Normal file
20
doc/python_api/examples/bpy.types.Operator.py
Normal file
@@ -0,0 +1,20 @@
|
||||
"""
|
||||
Basic Operator Example
|
||||
++++++++++++++++++++++
|
||||
This script is the most simple operator you can write that does something.
|
||||
"""
|
||||
import bpy
|
||||
|
||||
|
||||
class HelloWorldOperator(bpy.types.Operator):
|
||||
bl_idname = "wm.hello_world"
|
||||
bl_label = "Minimal Operator"
|
||||
|
||||
def execute(self, context):
|
||||
print("Hello World")
|
||||
return {'FINISHED'}
|
||||
|
||||
bpy.utils.register_class(SimpleOperator)
|
||||
|
||||
# test call to the newly defined operator
|
||||
bpy.ops.wm.hello_world()
|
||||
@@ -73,7 +73,7 @@ else:
|
||||
"mathutils.geometry",
|
||||
)
|
||||
|
||||
FILTER_BPY_TYPES = ("Mesh", ) # allow
|
||||
FILTER_BPY_TYPES = ("Operator", ) # allow
|
||||
FILTER_BPY_OPS = ("import_scene", ) # allow
|
||||
|
||||
# for quick rebuilds
|
||||
@@ -131,14 +131,60 @@ def range_str(val):
|
||||
return str(val)
|
||||
|
||||
|
||||
def example_extract_docstring(filepath):
|
||||
file = open(filepath, 'r')
|
||||
line = file.readline()
|
||||
line_no = 0
|
||||
text = []
|
||||
if line.startswith('"""'): # assume nothing here
|
||||
line_no += 1
|
||||
else:
|
||||
file.close()
|
||||
return "", 0
|
||||
|
||||
for line in file.readlines():
|
||||
line_no += 1
|
||||
if line.startswith('"""'):
|
||||
break
|
||||
else:
|
||||
text.append(line.rstrip())
|
||||
|
||||
line_no += 1
|
||||
file.close()
|
||||
return "\n".join(text), line_no
|
||||
|
||||
|
||||
def write_example_ref(ident, fw, example_id, ext="py"):
|
||||
if example_id in EXAMPLE_SET:
|
||||
fw("%s.. literalinclude:: ../examples/%s.%s\n\n" % (ident, example_id, ext))
|
||||
|
||||
# extract the comment
|
||||
filepath = "../examples/%s.%s" % (example_id, ext)
|
||||
filepath_full = os.path.join(os.path.dirname(fw.__self__.name), filepath)
|
||||
|
||||
text, line_no = example_extract_docstring(filepath_full)
|
||||
|
||||
for line in text.split("\n"):
|
||||
fw("%s\n" % (ident + line).rstrip())
|
||||
fw("\n")
|
||||
|
||||
fw("%s.. literalinclude:: %s\n" % (ident, filepath))
|
||||
fw("%s :lines: %d-\n" % (ident, line_no))
|
||||
fw("\n")
|
||||
EXAMPLE_SET_USED.add(example_id)
|
||||
else:
|
||||
if bpy.app.debug:
|
||||
print("\tskipping example:", example_id)
|
||||
|
||||
# Support for numbered files bpy.types.Operator -> bpy.types.Operator.1.py
|
||||
i = 1
|
||||
while True:
|
||||
example_id_num = "%s.%d" % (example_id, i)
|
||||
if example_id_num in EXAMPLE_SET:
|
||||
write_example_ref(ident, fw, example_id_num, ext)
|
||||
i += 1
|
||||
else:
|
||||
break
|
||||
|
||||
|
||||
def write_indented_lines(ident, fn, text, strip=True):
|
||||
'''
|
||||
@@ -517,6 +563,8 @@ def pyrna2sphinx(BASEPATH):
|
||||
|
||||
fw(".. module:: bpy.types\n\n")
|
||||
|
||||
write_example_ref("", fw, "bpy.types.%s" % struct.identifier)
|
||||
|
||||
base_ids = [base.identifier for base in struct.get_bases()]
|
||||
|
||||
if _BPY_STRUCT_FAKE:
|
||||
|
||||
Reference in New Issue
Block a user