1
$\begingroup$

enter image description here

I have two custom sliders; A and B. How can I B refer to A every time I past his name "Duration" on the input B?

In this case B should be 76 in output.

$\endgroup$
7
  • $\begingroup$ What exactly do you mean with refer? That it shows the value of A in the slider bar? like + 30(46)? $\endgroup$
    – Jonathan
    Commented Aug 23, 2015 at 15:54
  • $\begingroup$ Get value from A $\endgroup$ Commented Aug 23, 2015 at 15:54
  • $\begingroup$ So that B gets the value of A if you set A and you can set B from that value? $\endgroup$
    – Jonathan
    Commented Aug 23, 2015 at 15:55
  • $\begingroup$ B should 76 in output $\endgroup$ Commented Aug 23, 2015 at 15:57
  • $\begingroup$ So if A has a variable (call it varA) you should just be able to add +varA to the variable of B or not? $\endgroup$
    – Jonathan
    Commented Aug 23, 2015 at 15:58

1 Answer 1

1
$\begingroup$

If you want both A and B to be completely dynamic, without having a constant relationship between them, this requires a bit of work (otherwise a simple driver would suffice).

To get this to work, you need to define both properties in a propertyGroup, so that you'll be able to assign "B" with an "update function" - a function that is called whenever B's value changes.

That function then needs to parse your expression and assign the correct value back to B.

Here's the code for getting this to work:

bl_info = {
    "name"        : "Test",
    "author"      : "TLousky",
    "blender"     : (2, 74, 0),
    "version"     : (0, 0, 0, 1),
    "location"    : "3D View > Toolbox",
    "description" : "Test 123",
    "category"    : "Test"
}

import bpy
import re

class samplePropertyGroup( bpy.types.PropertyGroup ):
    def update_b( self, context ):
        ''' Update function for property "B". This function is called whenever B's value is altered '''

        # Regular expression pattern for parsing the typed in expression:
        # (?:duration)? --> The word "duration", which might or might not appear
        # (?:\s+)?(?:\+)?(?:\s+)? --> The plus sign, which might exist or not, with or without spaces before or after
        # ([\d\.]+) --> the actual numerical value in the expression, which could have a decimal point or not        
        pat = "(?:duration)?(?:\s+)?(?:\+)?(?:\s+)?([\d\.]+)"

        if "duration" in self.b:                # If the word "duration" exists in the manually assigned value
            m = re.match( pat, self.b )         # Then parse the string to extract the numerical value out of it
            if m:                               # And if parsing was successful
                numVal = float( m.groups()[0] ) # Get the value from the regex match object's capturing group
                self.b = str( self.duration + numVal ) # Then assign the value plus the duration prop's value back to "b"

    duration = bpy.props.FloatProperty(
        description = "This property is a simple float prop that doesn't support expressions",
        name = "duration" # Recommended not to use caps at the beginning of var names as a habit
    )

    # This one needs to be a string to be able to accept strings such as the other prop's name
    b = bpy.props.StringProperty( 
        description = "This property also accepts strings and might actually use them",
        name        = "b",
        update      = update_b # This is the important bit here, a reference to the update function
    )

class samplePanel(bpy.types.Panel):
    ''' Sample Panel '''
    bl_label       = "Sample Panel"
    bl_idname      = "sample_panel"
    bl_category    = "Test"
    bl_space_type  = 'VIEW_3D'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        layout = self.layout
        col    = layout.column()

        P = context.scene.sample_props

        col.prop( P, "duration"      )
        col.prop( P, "b" )

def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.sample_props = bpy.props.PointerProperty(
        type = samplePropertyGroup
    )

def unregister():
    bpy.utils.unregister_module(__name__)

You'll need to install this as an addon to test it, as it creates a new tab in the tools panel called "Test", where you will find both your properties.

$\endgroup$
3
  • 1
    $\begingroup$ This works pretty good!! :)) very nice $\endgroup$ Commented Aug 26, 2015 at 9:16
  • $\begingroup$ @TLousky is it possible to elaborate one what you mean by driver ? I ask as it is the second reference I have seen to these but do not yet know which docs to look up to learn more. $\endgroup$
    – Carel
    Commented Sep 2, 2016 at 5:29
  • $\begingroup$ @Carel, try this video: cgmasters.net/free-tutorials/… $\endgroup$
    – TLousky
    Commented Sep 2, 2016 at 5:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .