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

@ -8,16 +8,17 @@ from bpy.__ops__ import get_rna as op_get_rna
# Keep in sync with WM_types.h # Keep in sync with WM_types.h
context_dict = { context_dict = {
'INVOKE_DEFAULT':0, 'INVOKE_DEFAULT': 0,
'INVOKE_REGION_WIN':1, 'INVOKE_REGION_WIN': 1,
'INVOKE_AREA':2, 'INVOKE_AREA': 2,
'INVOKE_SCREEN':3, 'INVOKE_SCREEN': 3,
'EXEC_DEFAULT':4, 'EXEC_DEFAULT': 4,
'EXEC_REGION_WIN':5, 'EXEC_REGION_WIN': 5,
'EXEC_AREA':6, 'EXEC_AREA': 6,
'EXEC_SCREEN':7, 'EXEC_SCREEN': 7,
} }
class bpy_ops(object): class bpy_ops(object):
''' '''
Fake module like class. Fake module like class.
@ -95,13 +96,16 @@ class bpy_ops_submodule(object):
def __repr__(self): def __repr__(self):
return "<module like class 'bpy.ops.%s'>" % self.module return "<module like class 'bpy.ops.%s'>" % self.module
class bpy_ops_submodule_op(object): class bpy_ops_submodule_op(object):
''' '''
Utility class to fake submodule operators. Utility class to fake submodule operators.
eg. bpy.ops.object.somefunc eg. bpy.ops.object.somefunc
''' '''
__keys__ = ('module', 'func') __keys__ = ('module', 'func')
def __init__(self, module, func): def __init__(self, module, func):
self.module = module self.module = module
self.func = func self.func = func
@ -114,7 +118,7 @@ class bpy_ops_submodule_op(object):
# Get the operator from blender # Get the operator from blender
if len(args) > 2: 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 C_dict = None
@ -127,19 +131,20 @@ class bpy_ops_submodule_op(object):
C_dict = args[1] C_dict = args[1]
else: else:
if type(args[0]) != str: if type(args[0]) != str:
C_dict= args[0] C_dict = args[0]
else: else:
C_exec= args[0] C_exec = args[0]
try: try:
context = context_dict[C_exec] context = context_dict[C_exec]
except: 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: if len(args) == 2:
C_dict= args[1] C_dict = args[1]
return op_call(self.idname() , C_dict, kw, context) return op_call(self.idname(), C_dict, kw, context)
else: else:
return op_call(self.idname(), C_dict, kw) return op_call(self.idname(), C_dict, kw)
@ -150,20 +155,24 @@ class bpy_ops_submodule_op(object):
''' '''
return op_get_rna(self.idname()) return op_get_rna(self.idname())
def __repr__(self): # useful display, repr(op) def __repr__(self): # useful display, repr(op)
return op_as_string(self.idname()) return op_as_string(self.idname())
def __str__(self): # used for print(...) 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 import bpy
bpy.ops = bpy_ops() bpy.ops = bpy_ops()
# TODO, C macro's cant define settings :| # TODO, C macro's cant define settings :|
from bpy.props import *
class MESH_OT_delete_edgeloop(bpy.types.Operator): 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" __idname__ = "mesh.delete_edgeloop"
__label__ = "Delete Edge Loop" __label__ = "Delete Edge Loop"
@ -173,12 +182,17 @@ class MESH_OT_delete_edgeloop(bpy.types.Operator):
bpy.ops.mesh.remove_doubles() bpy.ops.mesh.remove_doubles()
return ('FINISHED',) return ('FINISHED',)
rna_path_prop = bpy.props.StringProperty(attr="path", name="Context Attributes", description="rna context string", maxlen= 1024, default= "") rna_path_prop = StringProperty(attr="path", name="Context Attributes",
rna_reverse_prop = bpy.props.BoolProperty(attr="reverse", name="Reverse", description="Cycle backwards", default= False) description="rna context string", maxlen=1024, default="")
rna_reverse_prop = BoolProperty(attr="reverse", name="Reverse",
description="Cycle backwards", default=False)
class NullPathMember: class NullPathMember:
pass pass
def context_path_validate(context, path): def context_path_validate(context, path):
import sys import sys
try: try:
@ -194,7 +208,6 @@ def context_path_validate(context, path):
return value return value
def execute_context_assign(self, context): def execute_context_assign(self, context):
if context_path_validate(context, self.path) == NullPathMember: if context_path_validate(context, self.path) == NullPathMember:
return ('PASS_THROUGH',) return ('PASS_THROUGH',)
@ -202,76 +215,111 @@ def execute_context_assign(self, context):
exec("context.%s=self.value" % self.path) exec("context.%s=self.value" % self.path)
return ('FINISHED',) return ('FINISHED',)
class WM_OT_context_set_boolean(bpy.types.Operator): class WM_OT_context_set_boolean(bpy.types.Operator):
'''Set a context value.''' '''Set a context value.'''
__idname__ = "wm.context_set_boolean" __idname__ = "wm.context_set_boolean"
__label__ = "Context Set" __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 execute = execute_context_assign
class WM_OT_context_set_int(bpy.types.Operator): # same as enum class WM_OT_context_set_int(bpy.types.Operator): # same as enum
'''Set a context value.''' '''Set a context value.'''
__idname__ = "wm.context_set_int" __idname__ = "wm.context_set_int"
__label__ = "Context Set" __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 execute = execute_context_assign
class WM_OT_context_set_float(bpy.types.Operator): # same as enum class WM_OT_context_set_float(bpy.types.Operator): # same as enum
'''Set a context value.''' '''Set a context value.'''
__idname__ = "wm.context_set_int" __idname__ = "wm.context_set_int"
__label__ = "Context Set" __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 execute = execute_context_assign
class WM_OT_context_set_string(bpy.types.Operator): # same as enum class WM_OT_context_set_string(bpy.types.Operator): # same as enum
'''Set a context value.''' '''Set a context value.'''
__idname__ = "wm.context_set_string" __idname__ = "wm.context_set_string"
__label__ = "Context Set" __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 execute = execute_context_assign
class WM_OT_context_set_enum(bpy.types.Operator): class WM_OT_context_set_enum(bpy.types.Operator):
'''Set a context value.''' '''Set a context value.'''
__idname__ = "wm.context_set_enum" __idname__ = "wm.context_set_enum"
__label__ = "Context Set" __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 execute = execute_context_assign
class WM_OT_context_toggle(bpy.types.Operator): class WM_OT_context_toggle(bpy.types.Operator):
'''Toggle a context value.''' '''Toggle a context value.'''
__idname__ = "wm.context_toggle" __idname__ = "wm.context_toggle"
__label__ = "Context Toggle" __label__ = "Context Toggle"
__props__ = [rna_path_prop] __props__ = [rna_path_prop]
def execute(self, context): def execute(self, context):
if context_path_validate(context, self.path) == NullPathMember: if context_path_validate(context, self.path) == NullPathMember:
return ('PASS_THROUGH',) 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',) return ('FINISHED',)
class WM_OT_context_toggle_enum(bpy.types.Operator): class WM_OT_context_toggle_enum(bpy.types.Operator):
'''Toggle a context value.''' '''Toggle a context value.'''
__idname__ = "wm.context_toggle_enum" __idname__ = "wm.context_toggle_enum"
__label__ = "Context Toggle Values" __label__ = "Context Toggle Values"
__props__ = [ __props__ = [
rna_path_prop, rna_path_prop,
bpy.props.StringProperty(attr="value_1", name="Value", description="Toggle enum", maxlen= 1024, default= ""), StringProperty(attr="value_1", name="Value", \
bpy.props.StringProperty(attr="value_2", name="Value", description="Toggle enum", maxlen= 1024, default= "") description="Toggle enum", maxlen=1024, default=""),
]
StringProperty(attr="value_2", name="Value", \
description="Toggle enum", maxlen=1024, default="")]
def execute(self, context): def execute(self, context):
if context_path_validate(context, self.path) == NullPathMember: if context_path_validate(context, self.path) == NullPathMember:
return ('PASS_THROUGH',) 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',) return ('FINISHED',)
class WM_OT_context_cycle_int(bpy.types.Operator): 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" __idname__ = "wm.context_cycle_int"
__label__ = "Context Int Cycle" __label__ = "Context Int Cycle"
__props__ = [rna_path_prop, rna_reverse_prop] __props__ = [rna_path_prop, rna_reverse_prop]
def execute(self, context): def execute(self, context):
value = context_path_validate(context, self.path) value = context_path_validate(context, self.path)
@ -279,23 +327,29 @@ class WM_OT_context_cycle_int(bpy.types.Operator):
return ('PASS_THROUGH',) return ('PASS_THROUGH',)
self.value = value self.value = value
if self.reverse: self.value -= 1 if self.reverse:
else: self.value += 1 self.value -= 1
else:
self.value += 1
execute_context_assign(self, context) execute_context_assign(self, context)
if self.value != eval("context.%s" % self.path): if self.value != eval("context.%s" % self.path):
# relies on rna clamping int's out of the range # relies on rna clamping int's out of the range
if self.reverse: self.value = (1<<32) if self.reverse:
else: self.value = -(1<<32) self.value = (1 << 32)
else:
self.value = - (1 << 32)
execute_context_assign(self, context) execute_context_assign(self, context)
return ('FINISHED',) return ('FINISHED',)
class WM_OT_context_cycle_enum(bpy.types.Operator): class WM_OT_context_cycle_enum(bpy.types.Operator):
'''Toggle a context value.''' '''Toggle a context value.'''
__idname__ = "wm.context_cycle_enum" __idname__ = "wm.context_cycle_enum"
__label__ = "Context Enum Cycle" __label__ = "Context Enum Cycle"
__props__ = [rna_path_prop, rna_reverse_prop] __props__ = [rna_path_prop, rna_reverse_prop]
def execute(self, context): def execute(self, context):
value = context_path_validate(context, self.path) 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 # Have to get rna enum values
rna_struct_str, rna_prop_str = self.path.rsplit('.', 1) rna_struct_str, rna_prop_str = self.path.rsplit('.', 1)
i = rna_prop_str.find('[') 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) 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 # Have the info we need, advance to the next item
if self.reverse: if self.reverse:
if orig_index==0: advance_enum = enums[-1] if orig_index == 0:
else: advance_enum = enums[orig_index-1] advance_enum = enums[-1]
else: else:
if orig_index==len(enums)-1: advance_enum = enums[0] advance_enum = enums[orig_index-1]
else: 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 # set the new value
exec("context.%s=advance_enum" % self.path) exec("context.%s=advance_enum" % self.path)
return ('FINISHED',) return ('FINISHED',)
doc_id = bpy.props.StringProperty(attr="doc_id", name="Doc ID", description="ID for the documentation", maxlen= 1024, default= "") doc_id = StringProperty(attr="doc_id", name="Doc ID",
doc_new = bpy.props.StringProperty(attr="doc_new", name="Doc New", description="", maxlen= 1024, default= "") 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): class WM_OT_doc_view(bpy.types.Operator):
@ -352,19 +416,20 @@ class WM_OT_doc_view(bpy.types.Operator):
def execute(self, context): def execute(self, context):
id_split = self.doc_id.split('.') 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 if len(id_split) == 1: # rna, class
url= '%s/bpy.types.%s-class.html' % (self._prefix, id_split[0]) url = '%s/bpy.types.%s-class.html' % (self._prefix, id_split[0])
elif len(id_split) == 2: # rna, class.prop elif len(id_split) == 2: # rna, class.prop
class_name, class_prop = id_split 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): 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: 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: else:
return ('PASS_THROUGH',) return ('PASS_THROUGH',)
@ -380,7 +445,7 @@ class WM_OT_doc_edit(bpy.types.Operator):
__idname__ = "wm.doc_edit" __idname__ = "wm.doc_edit"
__label__ = "Edit Documentation" __label__ = "Edit Documentation"
__props__ = [doc_id, doc_new] __props__ = [doc_id, doc_new]
_url = "http://www.mindrones.com/blender/svn/xmlrpc.php"
def _send_xmlrpc(self, data_dict): def _send_xmlrpc(self, data_dict):
print("sending data:", data_dict) print("sending data:", data_dict)
@ -389,27 +454,47 @@ class WM_OT_doc_edit(bpy.types.Operator):
user = 'blenderuser' user = 'blenderuser'
pwd = 'blender>user' 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) docblog.metaWeblog.newPost(1, user, pwd, data_dict, 1)
def execute(self, context): def execute(self, context):
class_name, class_prop = self.doc_id.split('.') class_name, class_prop = self.doc_id.split('.')
if self.doc_new: if not self.doc_new:
op_class = getattr(bpy.types, class_name.upper() + '_OT_' + class_prop, None) 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: if op_class:
doc_orig = op_class.__rna__.description rna = op_class.__rna__
if doc_orig != self.doc_new: doc_orig = rna.description
print("operator - old:'%s' -> new:'%s'" % (doc_orig, self.doc_new)) if doc_orig == self.doc_new:
self._send_xmlrpc({'title':'OPERATOR %s:%s' % (self.doc_id,doc_orig),'description':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: else:
doc_orig = getattr(bpy.types, class_name).__rna__.properties[class_prop].description rna = getattr(bpy.types, class_name).__rna__
if doc_orig != self.doc_new: 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)) print("rna - old:'%s' -> new:'%s'" % (doc_orig, self.doc_new))
# Ugh, will run this on every edit.... better not make any mistakes upload["title"] = 'RNA %s:%s' % s(self.doc_id, doc_orig)
self._send_xmlrpc({'title':'RNA %s:%s' % (self.doc_id,doc_orig),'description':self.doc_new})
upload["description"] = self.doc_new
self._send_xmlrpc(upload)
return ('FINISHED',) return ('FINISHED',)

@ -2723,6 +2723,7 @@ PyObject *BPY_rna_props( void )
{ {
PyObject *submodule; PyObject *submodule;
submodule= PyModule_Create(&props_module); 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 /* INCREF since its its assumed that all these functions return the
* module with a new ref like PyDict_New, since they are passed to * module with a new ref like PyDict_New, since they are passed to