2
$\begingroup$

I have an addon in development, and create various meshes in it. I have several physically based parameters, that are often extremely small, but make sense in the context they are. I have in my GUI class, for example:

poly_0: FloatProperty(
        name="POLY_0",
        description="Some description...",
        default=1.26587922531743e-16,
        min=-1000.0,
        max=+1000.0,
        )

In the GUI, I would like to force this Float member to always be displayed in scientific notation, and always with some set precision (number of digits). Looking in the API docs, I cant seem to find this option.

$\endgroup$
2
  • $\begingroup$ AFAIK setting the precision argument to 6 (max) is the only thing you can do... $\endgroup$
    – brockmann
    Dec 8, 2020 at 12:39
  • $\begingroup$ @brockmann, very valuable information. I thought the precision of FloatProperty was per default 64bit, as with python floats. When you wrote 6 as display-preciscion max, I looked up the documentation. It does state that it is single precision link. This puts my addon development in trouble. I am going to ask a new question about this instead $\endgroup$ Dec 22, 2020 at 11:57

1 Answer 1

4
$\begingroup$

Make a Scientific Notation Property Group.

As a workaround could set up a property group to handle scientific notation. Restricted to the significant digits of the float property, but allows to set the the power.

Here I've used a generic python property with getter and setter. can do same with bpy.props. properties and expose to the UI.

import bpy
from bpy.props import FloatProperty, IntProperty, PointerProperty
from bpy.types import PropertyGroup
from math import pow, log10, floor

class ScientificNotation(PropertyGroup):

    def set_num(self, value):
        self.power = floor(log10(abs(value)))
        self.number = value / pow(10, self.power)
        
    def get_num(self):
        return self.number *  pow(10, self.power)
    number : FloatProperty(min=-10, max=10)
    power : IntProperty()
    value = property(get_num, set_num)

bpy.utils.register_class(ScientificNotation)
bpy.types.Scene.foo = PointerProperty(type=ScientificNotation)

Layout with both number and power to set. Use the value property in calculation. Could use a string property for the number, to get more sig digits.

strong text

Example of use in python console.

>>> C.scene.foo.number = 6.62607004
>>> C.scene.foo.power = -34
>>> C.scene.foo.value
6.626070022583007e-34

>>> C.scene.foo.value = -344233.44e-44
>>> C.scene.foo.value
-3.442334413528442e-39

>>> C.scene.foo.number
-3.4423344135284424

>>> C.scene.foo.power
-39

Can add pythonic methods as well, eg repr or str

class ScientificNotation(PropertyGroup):
    def __repr__(self):
        return f"{self.value :6e}"

to

>>> C.scene.foo
3.442334e-39

See also https://blender.stackexchange.com/a/134310/15543

$\endgroup$
1
  • $\begingroup$ is there a way to have multiple instances of the ScientificNotation class in a menu? Right now I'm having to specify separate classes, as otherwise the value from one shows up as the suggested for another in a different part of the menu. Is there some kind of PropertyGroup collection I could use to avoid that pattern? $\endgroup$
    – stagermane
    May 22, 2021 at 5:28

You must log in to answer this question.

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