[Bf-extensions-cvs] [69a91ed9] master: Animated Text: fix register, reload error with module

lijenstina noreply at git.blender.org
Fri Jun 16 03:38:24 CEST 2017


Commit: 69a91ed91ea7b391f25f33264f30a8a519983bb1
Author: lijenstina
Date:   Fri Jun 16 03:37:34 2017 +0200
Branches: master
https://developer.blender.org/rBAC69a91ed91ea7b391f25f33264f30a8a519983bb1

Animated Text: fix register, reload error with module

Bumped version to 0.3.1
Part of the T51547:
Solve the key registration / reload issues related to the
module definining no classes
Pep8 cleanup
Don't register object properties in the Panel
Add polling to operators
Fix Dynamic Counter errors with eval
Fix passing empty strings as text keys

===================================================================

M	animation_text_types.py

===================================================================

diff --git a/animation_text_types.py b/animation_text_types.py
index 27106bcc..3068c013 100644
--- a/animation_text_types.py
+++ b/animation_text_types.py
@@ -15,13 +15,14 @@
 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 #
 # ##### END GPL LICENSE BLOCK #####
-## by LeoMoon Studios, Marcin Zielinski, Martin Wacker, Bassam Kurdali, Jared Felsman, meta-androcto ##
+# by LeoMoon Studios, Marcin Zielinski, Martin Wacker,
+# Bassam Kurdali, Jared Felsman, meta-androcto #
 
 bl_info = {
     "name": "Animated Text",
     "author": "LeoMoon Studios, Marcin Zielinski, Martin Wacker, "
-    "Bassam Kurdali, Jared Felsman, meta-androcto",
-    "version": (0, 3, 0),
+              "Bassam Kurdali, Jared Felsman, meta-androcto",
+    "version": (0, 3, 1),
     "blender": (2, 74, 5),
     "location": "Properties Editor > Font",
     "description": "Typing & Counting Animated Text",
@@ -31,32 +32,51 @@ bl_info = {
 }
 
 import bpy
-import string
 import random
+from bpy.types import (
+        Operator,
+        Panel,
+        PropertyGroup,
+        )
 from bpy.props import (
-    FloatProperty, PointerProperty, BoolProperty,
-    IntProperty, EnumProperty, StringProperty
-)
+        FloatProperty,
+        PointerProperty,
+        BoolProperty,
+        IntProperty,
+        EnumProperty,
+        StringProperty,
+        )
 from bpy.app.handlers import persistent
 
 
-def formatCounter(input, timeSeparators, timeLeadZeroes, timeTrailZeroes, timeModulo):
+# In case of error set it to true so it can be used for console runes
+DEBUG = False
+
+
+# Use just as a regular print (switchable as the UI will be constantly updated
+def debug_print_vars(*args, **kwargs):
+    global DEBUG
+    if DEBUG:
+        print(*args, **kwargs)
+
+
+def formatCounter(inputs, timeSeparators, timeLeadZeroes, timeTrailZeroes, timeModulo):
     f = 0
     s = 0
     m = 0
     h = 0
     out = ''
     neg = ''
-    if input < 0:
+    if inputs < 0:
         neg = '-'
-        input = abs(input)
+        inputs = abs(inputs)
 
     if timeSeparators >= 0:
         if timeSeparators == 0:
-            out = int(input)
+            out = int(inputs)
             out = format(out, '0' + str(timeLeadZeroes) + 'd')
         else:
-            s, f = divmod(int(input), timeModulo)
+            s, f = divmod(int(inputs), timeModulo)
             out = format(f, '0' + str(timeLeadZeroes) + 'd')
 
     if timeSeparators >= 1:
@@ -79,98 +99,143 @@ def formatCounter(input, timeSeparators, timeLeadZeroes, timeTrailZeroes, timeMo
     return neg + out
 
 
-class TextCounter_Props(bpy.types.PropertyGroup):
+class TextCounter_Props(PropertyGroup):
+
+    # A placeholder for the text box
+    default_string = 'Enter a number or an expression'
 
     def val_up(self, context):
         textcounter_update_val(context.object, context.scene)
 
     ifAnimated = BoolProperty(
-        name='Counter Active', default=False, update=val_up
-    )
+        name='Counter Active',
+        default=False,
+        update=val_up
+        )
     counter = FloatProperty(
-        name='Counter', update=val_up
-    )
+        name='Counter',
+        update=val_up
+        )
     padding = IntProperty(
-        name='Padding', update=val_up, min=1
-    )
+        name='Padding',
+        update=val_up,
+        min=1
+        )
     ifDecimal = BoolProperty(
-        name='Decimal', default=False, update=val_up
-    )
+        name='Decimal',
+        default=False,
+        update=val_up
+        )
     decimals = IntProperty(
-        name='Decimal', update=val_up, min=0
-    )
+        name='Decimal',
+        update=val_up,
+        min=0
+        )
     typeEnum = EnumProperty(
         items=[
             ('ANIMATED', 'Animated', 'Counter values from f-curves'),
             ('DYNAMIC', 'Dynamic', 'Counter values from expression')
         ],
-        name='Type', update=val_up, default='ANIMATED'
-    )
+        name='Type',
+        update=val_up,
+        default='ANIMATED'
+        )
     formattingEnum = EnumProperty(
         items=[
             ('NUMBER', 'Number', 'Counter values as numbers'),
             ('TIME', 'Time', 'Counter values as time')
         ],
-        name='Formatting Type', update=val_up, default='NUMBER'
-    )
+        name='Formatting Type',
+        update=val_up,
+        default='NUMBER'
+        )
+    # set to 0 as eval will crash in it's default state
     expr = StringProperty(
-        name='Expression', update=val_up, default=''
-    )
+        name='Expression',
+        description="Enter a number or a numeric expression",
+        update=val_up,
+        default=default_string
+        )
     prefix = StringProperty(
-        name='Prefix', update=val_up, default=''
-    )
+        name='Prefix',
+        update=val_up,
+        default=''
+        )
     sufix = StringProperty(
-        name='Sufix', update=val_up, default=''
-    )
+        name='Sufix',
+        update=val_up,
+        default=''
+        )
     ifTextFile = BoolProperty(
-        name='Override with Text File', default=False, update=val_up
-    )
+        name='Override with Text File',
+        default=False,
+        update=val_up
+        )
     textFile = StringProperty(
-        name='Text File', update=val_up, default=''
-    )
+        name='Text File',
+        update=val_up,
+        default=''
+        )
     ifTextFormatting = BoolProperty(
-        name='Numerical Formatting', default=False, update=val_up
-    )
+        name='Numerical Formatting',
+        default=False,
+        update=val_up
+        )
     timeSeparators = IntProperty(
-        name='Separators', update=val_up, min=0, max=3
-    )
+        name='Separators',
+        update=val_up,
+        min=0, max=3
+        )
     timeModulo = IntProperty(
-        name='Last Separator Modulo', update=val_up, min=1, default=24
-    )
+        name='Last Separator Modulo',
+        update=val_up,
+        min=1,
+        default=24
+        )
     timeLeadZeroes = IntProperty(
-        name='Leading Zeroes', update=val_up, min=1, default=2
-    )
+        name='Leading Zeroes',
+        update=val_up,
+        min=1,
+        default=2
+        )
     timeTrailZeroes = IntProperty(
-        name='Trailing Zeroes', update=val_up, min=1, default=2
-    )
+        name='Trailing Zeroes',
+        update=val_up,
+        min=1,
+        default=2
+        )
 
-    def dyn_get(self):
-        context = bpy.context
-        C = context
-        scene = C.scene
+    str_error = False
 
+    def dyn_get(self):
         try:
+            TextCounter_Props.str_error = False
+            if self.expr in [TextCounter_Props.default_string, None, ""]:
+                return '0'
             return str(eval(self.expr))
         except Exception as e:
-            print('Expr Error: ' + str(e.args))
+            TextCounter_Props.str_error = True
+            debug_print_vars('Expr Error: ' + str(e.args))
+            return '0'
 
-    dynamicCounter = StringProperty(name='Dynamic Counter', get=dyn_get, default='')
+    dynamicCounter = StringProperty(
+        name='Dynamic Counter',
+        get=dyn_get,
+        default=''
+        )
 
     def form_up(self, context):
         textcounter_update_val(context.object, context.scene)
 
     def form_get(self):
-        f = 0
-        s = 0
-        m = 0
-        h = 0
-        out = ''
-        input = 0
+        inputs = 0
+
         if self.typeEnum == 'ANIMATED':
-            input = float(self.counter)
+            inputs = float(self.counter)
         elif self.typeEnum == 'DYNAMIC':
-            input = float(self.dynamicCounter)
-        return formatCounter(input, self.timeSeparators, self.timeLeadZeroes, self.timeTrailZeroes, self.timeModulo)
+            inputs = float(self.dynamicCounter)
+        return formatCounter(inputs, self.timeSeparators, self.timeLeadZeroes,
+                             self.timeTrailZeroes, self.timeModulo)
 
     def form_set(self, value):
         counter = 0
@@ -180,7 +245,12 @@ class TextCounter_Props(bpy.types.PropertyGroup):
         counter += int(separators[-1])
         self.counter = float(counter)
 
-    formattedCounter = StringProperty(name='Formatted Counter', get=form_get, set=form_set, default='')
+    formattedCounter = StringProperty(
+        name='Formatted Counter',
+        get=form_get,
+        set=form_set,
+        default=''
+        )
 
 
 def textcounter_update_val(text, scene):
@@ -194,25 +264,32 @@ def textcounter_update_val(text, scene):
     if props.typeEnum == 'ANIMATED':
         counter = props.counter
     elif props.typeEnum == 'DYNAMIC':
+        if props.expr in [props.default_string, None, ""]:
+            return 0
         try:
             counter = eval(props.expr)
         except Exception as e:
-            print('Expr Error: ' + str(e.args))
+            debug_print_vars('Expr Error: ' + str(e.args))
 
     isNumeric = True  # always true for counter not overrided
     if props.ifTextFile:
-        txt = bpy.data.texts[props.textFile]
-        clampedCounter = max(0, min(int(counter), len(txt.lines) - 1))
-        line = txt.lines[clampedCounter].body
-        if props.ifTextFormatting:
-            try:
-                line = float(line)
-            except Exception:
+        txt = bpy.data.texts[props.textFile] if \
+              props.textFile in bpy.data.texts.keys() else None
+        if txt:
+            clampedCounter = max(0, min(int(counter), len(txt.lines) - 1))
+            line = txt.lines[clampedCounter].body
+            if props.ifTextFormatting:
+                try:
+                    line = float(line)
+                except Exception as ex:
+                    debug_print_vars('Expr Error: ' + str(ex.args))
+                    isNumeric = False
+                    out = line
+            else:
                 isNumeric = False
                 out = line
         else:
-            isNumeric = False
-            out = line
+            line = counter
     else:
         line = counter
 
@@ -233,7 +310,10 @@ def textcounter_update_val(text, scene):
             if len(arr) > 1:
                 out += '.' + arr[1]
         elif props.formattingEnu

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list