SOLVED How to get Data unit

Hi,
in picture object Position gui unit is real, Rotation gui unit is Degree , User data gui unit is Percent. how to know gui unit? and how to directly obtain the value displayed on the panel(Data = 0.1745, want to directly get 17.453)?
无标题.jpg

Thanks for any help!

Hello @chuanzhen,

thank you for reaching out to us. I will answer in bullet points, since there are sort of two questions within you question.

  1. The unit type of a parameter is being stored in the Description of the node that parameter is a part of. Each parameter is being described as a BaseContainer in the description of a node. The unit of the parameter is then stored in the field c4d.DESC_UNIT in such container. See example at the end for details.
  2. There are no prebuilt functions which would convert the value of a parameter into its display value. You would have to do that yourself with the unit type and the parameter value.

Cheers,
Ferdinand

The example code:

"""Attempts to evaluate the unit type of the first user data parameter of the 
currently selected object.

Must be run in the Script Manger while having an object selected with at least
one user data parameter. When the parameter has one of the unit types float,
percent, degree or meter (there are other unit types), it will print out a
matching message. If not, it will report that there is "another" unit type or
no unit type when the parameter has no unit type set at all.
"""

import c4d

def main() -> None:
    """Attempts to evaluate the unit type of the first user data parameter of
    the currently selected object.
    """
    # op is a predefined module attribute in scripts referencing the currently
    # selected object.
    if not isinstance(op, c4d.BaseObject):
        raise RuntimeError("Please select an object.")

    # The id of the parameter we are interested in, the first user data
    # parameter of a node.
    pid = (c4d.ID_USERDATA, 1)

    # Get the description of the node.
    description = op.GetDescription(c4d.DESCFLAGS_DESC_NONE)
    # Get the first user data element in the description.
    parameterData = description.GetParameter(pid)

    # Bail when the container is empty, i.e., there is no such parameter in
    # the node.
    if len(parameterData) < 1:
        raise RuntimeError(f"Retrieved empty data container for {pid}.")

    # How to iterate over the flags of that parameter container.
    # for cid, value in data:
    #     print (cid, value)

    # Get the unit type flag for the element and bail if there is no value
    # set for that flag.
    unitType = parameterData[c4d.DESC_UNIT]
    if unitType is None:
        print (f"The parameter with the id {pid} has no unit type.")
        return

    # Do some stuff depending on the unit type.
    if unitType == c4d.DESC_UNIT_FLOAT:
        print (f"The unit type for {pid} is float.")
    elif unitType == c4d.DESC_UNIT_PERCENT:
        print (f"The unit type for {pid} is percent.")
    elif unitType == c4d.DESC_UNIT_DEGREE:
        print (f"The unit type for {pid} is degree.")
    elif unitType == c4d.DESC_UNIT_METER:
        print (f"The unit type for {pid} is meter, i.e., length.")
    else:
        print (f"The unit type for {pid} is another unit type.")

if __name__ == '__main__':
    main()

Hello @chuanzhen,

thank you for reaching out to us. I will answer in bullet points, since there are sort of two questions within you question.

  1. The unit type of a parameter is being stored in the Description of the node that parameter is a part of. Each parameter is being described as a BaseContainer in the description of a node. The unit of the parameter is then stored in the field c4d.DESC_UNIT in such container. See example at the end for details.
  2. There are no prebuilt functions which would convert the value of a parameter into its display value. You would have to do that yourself with the unit type and the parameter value.

Cheers,
Ferdinand

The example code:

"""Attempts to evaluate the unit type of the first user data parameter of the 
currently selected object.

Must be run in the Script Manger while having an object selected with at least
one user data parameter. When the parameter has one of the unit types float,
percent, degree or meter (there are other unit types), it will print out a
matching message. If not, it will report that there is "another" unit type or
no unit type when the parameter has no unit type set at all.
"""

import c4d

def main() -> None:
    """Attempts to evaluate the unit type of the first user data parameter of
    the currently selected object.
    """
    # op is a predefined module attribute in scripts referencing the currently
    # selected object.
    if not isinstance(op, c4d.BaseObject):
        raise RuntimeError("Please select an object.")

    # The id of the parameter we are interested in, the first user data
    # parameter of a node.
    pid = (c4d.ID_USERDATA, 1)

    # Get the description of the node.
    description = op.GetDescription(c4d.DESCFLAGS_DESC_NONE)
    # Get the first user data element in the description.
    parameterData = description.GetParameter(pid)

    # Bail when the container is empty, i.e., there is no such parameter in
    # the node.
    if len(parameterData) < 1:
        raise RuntimeError(f"Retrieved empty data container for {pid}.")

    # How to iterate over the flags of that parameter container.
    # for cid, value in data:
    #     print (cid, value)

    # Get the unit type flag for the element and bail if there is no value
    # set for that flag.
    unitType = parameterData[c4d.DESC_UNIT]
    if unitType is None:
        print (f"The parameter with the id {pid} has no unit type.")
        return

    # Do some stuff depending on the unit type.
    if unitType == c4d.DESC_UNIT_FLOAT:
        print (f"The unit type for {pid} is float.")
    elif unitType == c4d.DESC_UNIT_PERCENT:
        print (f"The unit type for {pid} is percent.")
    elif unitType == c4d.DESC_UNIT_DEGREE:
        print (f"The unit type for {pid} is degree.")
    elif unitType == c4d.DESC_UNIT_METER:
        print (f"The unit type for {pid} is meter, i.e., length.")
    else:
        print (f"The unit type for {pid} is another unit type.")

if __name__ == '__main__':
    main()

@ferdinand Thanks,great!