Simple dialog in a script: how to focus a field?

The goal: since you can’t use the standard “G” shortcut to set the absolute X, Y, or Z location/scale/rotation of an object (only move it relative), I thought to make a script and bind it to a hot key.

So I wanted to make a simple script that pops up a dialog to take a number entry and uses that to set the X/Y/Z whatever.

I got this far, but I can’t make the number entry field have focus when the dialog opens. (I’m not yet using the value to do anything, I know.) I set bl_property but it doesn’t seem to be working. Disclaimer: I know nothing about scripting in Blender and have cobbled this together from a few examples online and a little knowledge of python:

import bpy
from bpy.props import *
theFloat = 0.0

class DialogOperator(bpy.types.Operator):
    bl_idname = "object.dialog_operator"
    bl_label = "Simple Dialog Operator"
    bl_property = "my_float"

    my_float: FloatProperty(name="Some Floating Point")

    def execute(self, context):
        global theFloat
        theFloat = self.my_float
        return {'FINISHED'}

    def invoke(self, context, event):
        global theFloat
        self.my_float = theFloat
        return context.window_manager.invoke_props_dialog(self)

bpy.utils.register_class(DialogOperator)
bpy.ops.object.dialog_operator('INVOKE_DEFAULT')

What am I doing wrong? (And if there is an easier way to do this, I’m all ears. E.g. I don’t need to pop up a dialog – I’d be happy to just read in from the keyboard like the normal G action does. I just didn’t know any other way. Or maybe there is a scriptable way to focus the existing object position fields so my custom hotkey can just pop in to edit those directly, etc.)

One way to set absolute values is simply first set object location to zero. After that you can use G command to set absolute values. Use Tab-key to jump between xyz axis.
Example: set object location to x=1, y=2, z=3. Type: 1 Tab 2 Tab 3

import bpy

o = bpy.context.object
o.location = (0,0,0)
bpy.ops.transform.translate('INVOKE_DEFAULT',value=(0, 0, 0), orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', mirror=True, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False)

Thank you – I appreciate the help. I don’t think I’ll go that route, as I just want to set the value of one axis at a time. Otherwise it would probably be faster to just mouse over to the X/Y/Z vales in the N menu to set the value I want…

Do you know if my script is at least correct in terms of its use of bl_property? I.e. should I file a bug if it’s not working?

I’m pretty surprised there isn’t a built in way to set the absolute values, or to e.g. set the length of an edge, the area of a face, etc. I made a RCS post a while back: https://blender.community/c/rightclickselect/btdbbc/

1 Like

In your script, there is no need for theFloat and bl_property.
So in invoke there is no need for global float and setting it. As well as in execute.
In execute you can access the active object location by context.active_object.location and set it to my_float
context.active_object.location.x = self.my_float.
Notice that I used location.x, because my_float can take only one value. If you want 3 values use FloatVectorProperty

Thank you – I know that the script doesn’t make sense right now, and I know how to set the location.x… my problem is that the dialog which appears doesn’t have focus for the value. Is there some way to make the float input value have focus when the dialog appears?

I have asked a stackexchange question here.

Well, I’m not sure about focusing field. If you find anything let me know.

I get it working using macros : ). I’ll be posting code here after some cleanup and possibly add-on so you can go absolute mode.

Nevermind. It doesn’t seem to work properly

Ok, bl_property is the correct way to do this, but it does not work on FloatProperty. It works on StringProperty. I reported a bug, but I’m not sure if they will consider it a bug or a feature request:

This issue has really stumped me and I would like to start up a hunt for a solution to this. I made a new post here How to ‘Focus’ on an Integer in an Operator in an Addon - Coding / Python Support - Blender Artists Community that details my findings so far which is basically exactly what you have found here.

But with a silver lining that maybe using a String=Float conversion my resolve this.

I was able to get the string to float workaround to work

Basically when you draw your operator draw a String with something like
number = bpy.props.StringProperty(name= "Numb", default= "")

Then you can convert that into a float using float(numb) where numb is the name of your string property defined earlier in your code. This way you can draw an operator pop up panel that is focused, but still apply that value as a Float on the other side. I have much more detail/code examples and gifs in this post How to ‘Focus’ on an Integer in an Operator in an Addon - Coding / Python Support - Blender Artists Community

Thanks – I don’t know if it’s useful to the devs to have people “me too” on the bug reports (in this case a closed report), but in case it is, you might +1 over there: https://developer.blender.org/T84563

It may be more appropriate to make this a feature request. As I have researched this more and more it makes more sense to me why its like this and why no one cares. Put simply you can convert the string to a float so quickly and its such a basic concept to experienced programmers I think, that in their eyes it doesn’t matter. And in the grand scheme of things there are why more important features that the devs are probably working on that making this a bug report probably muddies the water on real bugs. I did leave a comment on the bug report about it.

Yes, it is properly a feature request, but it will likely just get lost on e.g. rightclickselect. I took a peek at the source code (this inspired me to compile Blender for the first time :slight_smile: ), and it’s apparently not a simple thing to change. The string-to-float conversion is a workaround, which is great, but there are nice advantages to a real float property (e.g. using expressions in the field, “6.3/2.7”, etc.) At any rate, it’s clear that there isn’t much developer interest and I can’t figure out how to implement it myself, so unless someone wants to put up a cash bounty we’ll just have to accept that it isn’t likely to happen. Maybe some day there will be a big push for API improvements and they will solicit suggestions. These kinds of small/localized improvements are hard to make happen otherwise.