7
$\begingroup$

Is there a way to properly set a distance value for an input property of a geometry nodes group with python without loosing the value's unit type?

I have a Geometry Nodes group with various input properties. One of them has a distance type value, hence indicating cm/m (whatever the current system unit is).

Setting the value manually of course isn't a problem. But when setting the value from python the unit type gets lost, meaning the unit indicator (cm/m) is missing and even the number gets converted.

Converted means that if my system units are set to cm the new value gets interpreted as m, because this is the default unit in Blender. Entering 0.1 is then interpreted as 10 cm and not as 0.1 cm as intended.

Node group property comparison and data

Setting all other numerical inputs is no problem, as long as they don't represent any specific units (like degrees).

The simplified python command is:

bpy.context.active_object.modifiers[-1]["Input_3"] = 0.1

So far I haven't found any reference to that issue as it seems to work fine with regular properties on standard objects. But with input properties of node groups it seems to be different.

$\endgroup$
3
  • 1
    $\begingroup$ Interesting problem. I think it could be qualified as a bug or a design limitation. I believe (or at least hope) the developers will overhaul the whole gn inputs / outputs interface because having to access them by name like that is error prone and not elegant $\endgroup$
    – Gorgious
    Commented Apr 8, 2022 at 8:14
  • $\begingroup$ @Gorgious I thought so as well but I first wanted to check if I am missing something. But since all other properties can be set without a problem this sure might be an internal issue. $\endgroup$
    – iclemens
    Commented Apr 8, 2022 at 17:36
  • $\begingroup$ try running mod.node_group.interface_update(context) after setting the value via python. $\endgroup$
    – MACHIN3
    Commented Mar 6 at 23:39

2 Answers 2

4
$\begingroup$

Temporary solution

Reconnect the socket

import bpy

md = bpy.context.active_object.modifiers[-1]
ng = md.node_group

md["Input_2"] = 0.5
x = ng.nodes["Group Input"].outputs["Size X"]

l = None
for link in ng.links:
    if link.from_socket == x:
        l = link
        break

if l is not None:
    to_s = l.to_socket
    from_s = l.from_socket

    ng.links.remove(l)
    ng.links.new(from_s, to_s)

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ It works if your socket is already connected. $\endgroup$
    – X Y
    Commented Apr 8, 2022 at 10:42
  • $\begingroup$ @X Y That's the solution I was thinking about as well. Because I noticed after setting the value and the disappearing of the unit that when the connection gets broken and then reconnected it's all working again. So that really indicates some internal issue. Just the value itself needs to be processed in addition because the value of 0.5 in your example results in 50cm and not 0.5cm as intended. But of course this is something different. Thanks for the great solution even if it's a workaround. $\endgroup$
    – iclemens
    Commented Apr 8, 2022 at 17:45
5
$\begingroup$

As it looks to be a bug or design flaw, this is not a question to answer because it should be reported as a bug in bug tracker. But I can aid with unit conversion. As you mention

Setting all other numerical inputs is no problem, as long as they don't represent any specific units (like degrees).

So Blender has built-in utilities to convert text input to internal units, for example you can set 2cm like this:

bpy.context.active_object.modifiers[-1]["Input_3"] = bpy.utils.units.to_value("METRIC", "LENGTH", "2cm")

Read more: https://docs.blender.org/api/current/bpy.utils.units.html

$\endgroup$
1
  • $\begingroup$ Even though this isn't working in this particular case knowing that values can be set very specifically is very useful and will sure come in handy at one point. Thanks very the speedy reply. $\endgroup$
    – iclemens
    Commented Apr 8, 2022 at 17:34

You must log in to answer this question.

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