pep8 compliance for bpy_ops.py

add bpy.props to the modules so you can do...
 from bpy.props import *
This commit is contained in:
Campbell Barton 2009-10-31 01:23:49 +00:00
parent a8e56a274e
commit 9efc427f80
2 changed files with 454 additions and 368 deletions

@ -18,6 +18,7 @@ context_dict = {
'EXEC_SCREEN': 7,
}
class bpy_ops(object):
'''
Fake module like class.
@ -95,13 +96,16 @@ class bpy_ops_submodule(object):
def __repr__(self):
return "<module like class 'bpy.ops.%s'>" % self.module
class bpy_ops_submodule_op(object):
'''
Utility class to fake submodule operators.
eg. bpy.ops.object.somefunc
'''
__keys__ = ('module', 'func')
def __init__(self, module, func):
self.module = module
self.func = func
@ -114,7 +118,7 @@ class bpy_ops_submodule_op(object):
# Get the operator from blender
if len(args) > 2:
raise ValueError("only 1 or 2 arguments for the execution context is supported")
raise ValueError("1 or 2 args execution context is supported")
C_dict = None
@ -134,7 +138,8 @@ class bpy_ops_submodule_op(object):
try:
context = context_dict[C_exec]
except:
raise ValueError("Expected a single context argument in: " + str(list(context_dict.keys())))
raise ValueError("Expected a single context argument in: " + \
str(list(context_dict.keys())))
if len(args) == 2:
C_dict = args[1]
@ -150,20 +155,24 @@ class bpy_ops_submodule_op(object):
'''
return op_get_rna(self.idname())
def __repr__(self): # useful display, repr(op)
return op_as_string(self.idname())
def __str__(self): # used for print(...)
return "<function bpy.ops.%s.%s at 0x%x'>" % (self.module, self.func, id(self))
return "<function bpy.ops.%s.%s at 0x%x'>" % \
(self.module, self.func, id(self))
import bpy
bpy.ops = bpy_ops()
# TODO, C macro's cant define settings :|
from bpy.props import *
class MESH_OT_delete_edgeloop(bpy.types.Operator):
'''Export a single object as a stanford PLY with normals, colours and texture coordinates.'''
'''Export a single object as a stanford PLY with normals,
colours and texture coordinates.'''
__idname__ = "mesh.delete_edgeloop"
__label__ = "Delete Edge Loop"
@ -173,12 +182,17 @@ class MESH_OT_delete_edgeloop(bpy.types.Operator):
bpy.ops.mesh.remove_doubles()
return ('FINISHED',)
rna_path_prop = bpy.props.StringProperty(attr="path", name="Context Attributes", description="rna context string", maxlen= 1024, default= "")
rna_reverse_prop = bpy.props.BoolProperty(attr="reverse", name="Reverse", description="Cycle backwards", default= False)
rna_path_prop = StringProperty(attr="path", name="Context Attributes",
description="rna context string", maxlen=1024, default="")
rna_reverse_prop = BoolProperty(attr="reverse", name="Reverse",
description="Cycle backwards", default=False)
class NullPathMember:
pass
def context_path_validate(context, path):
import sys
try:
@ -194,7 +208,6 @@ def context_path_validate(context, path):
return value
def execute_context_assign(self, context):
if context_path_validate(context, self.path) == NullPathMember:
return ('PASS_THROUGH',)
@ -202,76 +215,111 @@ def execute_context_assign(self, context):
exec("context.%s=self.value" % self.path)
return ('FINISHED',)
class WM_OT_context_set_boolean(bpy.types.Operator):
'''Set a context value.'''
__idname__ = "wm.context_set_boolean"
__label__ = "Context Set"
__props__ = [rna_path_prop, bpy.props.BoolProperty(attr="value", name="Value", description="Assignment value", default= True)]
__props__ = [
rna_path_prop,
BoolProperty(attr="value", name="Value",
description="Assignment value", default=True)]
execute = execute_context_assign
class WM_OT_context_set_int(bpy.types.Operator): # same as enum
'''Set a context value.'''
__idname__ = "wm.context_set_int"
__label__ = "Context Set"
__props__ = [rna_path_prop, bpy.props.IntProperty(attr="value", name="Value", description="Assignment value", default= 0)]
__props__ = [
rna_path_prop,
IntProperty(attr="value", name="Value",
description="Assignment value", default=0)]
execute = execute_context_assign
class WM_OT_context_set_float(bpy.types.Operator): # same as enum
'''Set a context value.'''
__idname__ = "wm.context_set_int"
__label__ = "Context Set"
__props__ = [rna_path_prop, bpy.props.FloatProperty(attr="value", name="Value", description="Assignment value", default= 0.0)]
__props__ = [
rna_path_prop,
FloatProperty(attr="value", name="Value",
description="Assignment value", default=0.0)]
execute = execute_context_assign
class WM_OT_context_set_string(bpy.types.Operator): # same as enum
'''Set a context value.'''
__idname__ = "wm.context_set_string"
__label__ = "Context Set"
__props__ = [rna_path_prop, bpy.props.StringProperty(attr="value", name="Value", description="Assignment value", maxlen= 1024, default= "")]
__props__ = [
rna_path_prop,
StringProperty(attr="value", name="Value",
description="Assignment value", maxlen=1024, default="")]
execute = execute_context_assign
class WM_OT_context_set_enum(bpy.types.Operator):
'''Set a context value.'''
__idname__ = "wm.context_set_enum"
__label__ = "Context Set"
__props__ = [rna_path_prop, bpy.props.StringProperty(attr="value", name="Value", description="Assignment value (as a string)", maxlen= 1024, default= "")]
__props__ = [
rna_path_prop,
StringProperty(attr="value", name="Value",
description="Assignment value (as a string)",
maxlen=1024, default="")]
execute = execute_context_assign
class WM_OT_context_toggle(bpy.types.Operator):
'''Toggle a context value.'''
__idname__ = "wm.context_toggle"
__label__ = "Context Toggle"
__props__ = [rna_path_prop]
def execute(self, context):
if context_path_validate(context, self.path) == NullPathMember:
return ('PASS_THROUGH',)
exec("context.%s=not (context.%s)" % (self.path, self.path)) # security nuts will complain.
exec("context.%s=not (context.%s)" % (self.path, self.path))
return ('FINISHED',)
class WM_OT_context_toggle_enum(bpy.types.Operator):
'''Toggle a context value.'''
__idname__ = "wm.context_toggle_enum"
__label__ = "Context Toggle Values"
__props__ = [
rna_path_prop,
bpy.props.StringProperty(attr="value_1", name="Value", description="Toggle enum", maxlen= 1024, default= ""),
bpy.props.StringProperty(attr="value_2", name="Value", description="Toggle enum", maxlen= 1024, default= "")
]
StringProperty(attr="value_1", name="Value", \
description="Toggle enum", maxlen=1024, default=""),
StringProperty(attr="value_2", name="Value", \
description="Toggle enum", maxlen=1024, default="")]
def execute(self, context):
if context_path_validate(context, self.path) == NullPathMember:
return ('PASS_THROUGH',)
exec("context.%s = ['%s', '%s'][context.%s!='%s']" % (self.path, self.value_1, self.value_2, self.path, self.value_2)) # security nuts will complain.
exec("context.%s = ['%s', '%s'][context.%s!='%s']" % \
(self.path, self.value_1, self.value_2, self.path, self.value_2))
return ('FINISHED',)
class WM_OT_context_cycle_int(bpy.types.Operator):
'''Set a context value. Useful for cycling active material, vertex keys, groups' etc.'''
'''Set a context value. Useful for cycling active material,
vertex keys, groups' etc.'''
__idname__ = "wm.context_cycle_int"
__label__ = "Context Int Cycle"
__props__ = [rna_path_prop, rna_reverse_prop]
def execute(self, context):
value = context_path_validate(context, self.path)
@ -279,23 +327,29 @@ class WM_OT_context_cycle_int(bpy.types.Operator):
return ('PASS_THROUGH',)
self.value = value
if self.reverse: self.value -= 1
else: self.value += 1
if self.reverse:
self.value -= 1
else:
self.value += 1
execute_context_assign(self, context)
if self.value != eval("context.%s" % self.path):
# relies on rna clamping int's out of the range
if self.reverse: self.value = (1<<32)
else: self.value = -(1<<32)
if self.reverse:
self.value = (1 << 32)
else:
self.value = - (1 << 32)
execute_context_assign(self, context)
return ('FINISHED',)
class WM_OT_context_cycle_enum(bpy.types.Operator):
'''Toggle a context value.'''
__idname__ = "wm.context_cycle_enum"
__label__ = "Context Enum Cycle"
__props__ = [rna_path_prop, rna_reverse_prop]
def execute(self, context):
value = context_path_validate(context, self.path)
@ -307,7 +361,10 @@ class WM_OT_context_cycle_enum(bpy.types.Operator):
# Have to get rna enum values
rna_struct_str, rna_prop_str = self.path.rsplit('.', 1)
i = rna_prop_str.find('[')
if i != -1: rna_prop_str = rna_prop_str[0:i] # just incse we get "context.foo.bar[0]"
# just incse we get "context.foo.bar[0]"
if i != -1:
rna_prop_str = rna_prop_str[0:i]
rna_struct = eval("context.%s.rna_type" % rna_struct_str)
@ -321,18 +378,25 @@ class WM_OT_context_cycle_enum(bpy.types.Operator):
# Have the info we need, advance to the next item
if self.reverse:
if orig_index==0: advance_enum = enums[-1]
else: advance_enum = enums[orig_index-1]
if orig_index == 0:
advance_enum = enums[-1]
else:
if orig_index==len(enums)-1: advance_enum = enums[0]
else: advance_enum = enums[orig_index+1]
advance_enum = enums[orig_index-1]
else:
if orig_index == len(enums) - 1:
advance_enum = enums[0]
else:
advance_enum = enums[orig_index + 1]
# set the new value
exec("context.%s=advance_enum" % self.path)
return ('FINISHED',)
doc_id = bpy.props.StringProperty(attr="doc_id", name="Doc ID", description="ID for the documentation", maxlen= 1024, default= "")
doc_new = bpy.props.StringProperty(attr="doc_new", name="Doc New", description="", maxlen= 1024, default= "")
doc_id = StringProperty(attr="doc_id", name="Doc ID",
description="ID for the documentation", maxlen=1024, default="")
doc_new = StringProperty(attr="doc_new", name="Doc New",
description="", maxlen=1024, default="")
class WM_OT_doc_view(bpy.types.Operator):
@ -352,19 +416,20 @@ class WM_OT_doc_view(bpy.types.Operator):
def execute(self, context):
id_split = self.doc_id.split('.')
# Example url, http://www.graphicall.org/ftp/ideasman42/html/bpy.types.Space3DView-class.html#background_image
# Example operator http://www.graphicall.org/ftp/ideasman42/html/bpy.ops.boid-module.html#boidrule_add
if len(id_split) == 1: # rna, class
url = '%s/bpy.types.%s-class.html' % (self._prefix, id_split[0])
elif len(id_split) == 2: # rna, class.prop
class_name, class_prop = id_split
class_name_full = self._nested_class_string(class_name) # It so happens that epydoc nests these
# It so happens that epydoc nests these
class_name_full = self._nested_class_string(class_name)
if hasattr(bpy.types, class_name.upper() + '_OT_' + class_prop):
url= '%s/bpy.ops.%s-module.html#%s' % (self._prefix, class_name_full, class_prop)
url = '%s/bpy.ops.%s-module.html#%s' % \
(self._prefix, class_name_full, class_prop)
else:
url= '%s/bpy.types.%s-class.html#%s' % (self._prefix, class_name_full, class_prop)
url = '%s/bpy.types.%s-class.html#%s' % \
(self._prefix, class_name_full, class_prop)
else:
return ('PASS_THROUGH',)
@ -380,7 +445,7 @@ class WM_OT_doc_edit(bpy.types.Operator):
__idname__ = "wm.doc_edit"
__label__ = "Edit Documentation"
__props__ = [doc_id, doc_new]
_url = "http://www.mindrones.com/blender/svn/xmlrpc.php"
def _send_xmlrpc(self, data_dict):
print("sending data:", data_dict)
@ -389,27 +454,47 @@ class WM_OT_doc_edit(bpy.types.Operator):
user = 'blenderuser'
pwd = 'blender>user'
docblog = xmlrpc.client.ServerProxy("http://www.mindrones.com/blender/svn/xmlrpc.php")
docblog = xmlrpc.client.ServerProxy(self._url)
docblog.metaWeblog.newPost(1, user, pwd, data_dict, 1)
def execute(self, context):
class_name, class_prop = self.doc_id.split('.')
if self.doc_new:
op_class = getattr(bpy.types, class_name.upper() + '_OT_' + class_prop, None)
if not self.doc_new:
return 'OPERATOR_CANCELLED'
# check if this is an operator
op_name = class_name.upper() + '_OT_' + class_prop
op_class = getattr(bpy.types, op_name, None)
# Upload this to the web server
upload = {}
if op_class:
doc_orig = op_class.__rna__.description
if doc_orig != self.doc_new:
print("operator - old:'%s' -> new:'%s'" % (doc_orig, self.doc_new))
self._send_xmlrpc({'title':'OPERATOR %s:%s' % (self.doc_id,doc_orig),'description':self.doc_new})
rna = op_class.__rna__
doc_orig = rna.description
if doc_orig == self.doc_new:
return 'OPERATOR_CANCELLED'
print("op - old:'%s' -> new:'%s'" % (doc_orig, self.doc_new))
upload["title"] = 'OPERATOR %s:%s' % (self.doc_id, doc_orig)
upload["description"] = self.doc_new
self._send_xmlrpc(upload)
else:
doc_orig = getattr(bpy.types, class_name).__rna__.properties[class_prop].description
if doc_orig != self.doc_new:
rna = getattr(bpy.types, class_name).__rna__
doc_orig = rna.properties[class_prop].description
if doc_orig == self.doc_new:
return 'OPERATOR_CANCELLED'
print("rna - old:'%s' -> new:'%s'" % (doc_orig, self.doc_new))
# Ugh, will run this on every edit.... better not make any mistakes
self._send_xmlrpc({'title':'RNA %s:%s' % (self.doc_id,doc_orig),'description':self.doc_new})
upload["title"] = 'RNA %s:%s' % s(self.doc_id, doc_orig)
upload["description"] = self.doc_new
self._send_xmlrpc(upload)
return ('FINISHED',)

@ -2723,6 +2723,7 @@ PyObject *BPY_rna_props( void )
{
PyObject *submodule;
submodule= PyModule_Create(&props_module);
PyDict_SetItemString(PySys_GetObject("modules"), props_module.m_name, submodule);
/* INCREF since its its assumed that all these functions return the
* module with a new ref like PyDict_New, since they are passed to