Module for values of base types (int, float, …) that can store metadata.
|author:||Mischa Kolbe <firstname.lastname@example.org>|
The stored metadata on NcValues is essential to keep track of where values came from when the NodeCalculator is tracing.
When a value is queried in a NodeCalculator formula it returns an NcValue instance, which has the value-variable attached to it. For example:
a = noca.Node("pCube1") with noca.Tracer(pprint_trace=True): a.tx.get() # Printout: # val1 = cmds.getAttr('pCube1.tx')
“val1” is the stored variable name of this queried value. When it is used in a calculation later in the formula the variable name is used instaed of the value itself. For example:
a = noca.Node("pCube1") b = noca.Node("pSphere1") with noca.Tracer(pprint_trace=True): curr_tx = a.tx.get() b.ty = curr_tx # Printout: # val1 = cmds.getAttr('pCube1.tx') # cmds.setAttr('pSphere1.translateY', val1) # Rather than plugging in the queried value (making it very unclear # where that value came from), value-variable "val1" is used instead.
Furthermore: Basic math operations performed on NcValues are stored, too! This allows to keep track of where values came from as much as possible:
a = noca.Node("pCube1") b = noca.Node("pSphere1") with noca.Tracer(pprint_trace=True): curr_tx = a.tx.get() b.ty = curr_tx + 2 # Adding 2 doesn't break the origin of curr_tx! # Printout: # val1 = cmds.getAttr('pCube1.tx') # cmds.setAttr('pSphere1.translateY', val1 + 2) # <-- !!! # Note that the printed trace contains the correct calculation # including the value-variable "val1".
# This works: a = value(1, "my_metadata") # This does NOT work: a = 1 a.metadata = "my_metadata" # >>> AttributeError: 'int' object has no attribute 'metadata'
BaseClass inherited by all NcValue-classes that are created on the fly.
Only exists for inheritance check: isinstance(XYZ, NcValue) NcIntValue, NcFloatValue, etc. are otherwise hard to identify.
list of weak references to the object (if defined)
_concatenate_metadata(operator, input_a, input_b)¶
Concatenate the metadata for the given NcValue(s).
Check docString of this module for more detail why this is important.
Concatenated metadata for performed operation.
a = noca.Node("pCube1") b = noca.Node("pSphere1") with noca.Tracer(pprint_trace=True): curr_tx = a.tx.get() b.ty = curr_tx + 2 # >>> val1 = cmds.getAttr('pCube1.tx') # >>> cmds.setAttr('pSphere1.translateY', val1 + 2) # <-- !!!
Closure to create value class of any type.
Check docString of value function for more details.
Parameters: class_type (any builtin-type) – Type to create a new NcValue-class for. Returns:
- New class constructor for a NcValue class of appropriate
- type to match given class_type
Return type: NcValueClass
value(in_val, metadata=None, created_by_user=True)¶
Create a new value with metadata of appropriate NcValue-type.
For clarity: The given in_val is of a certain type & an appropriate type of NcValue must be used. For example: - A value of type <int> will become a <NcIntValue> - A value of type <float> will become a <NcFloatValue> - A value of type <list> will become a <NcListValue> The first time a certain NcValue class is required (meaning: if it’s not in the globals yet) the function _create_metadata_val_class is called to create and add the necessary class to the globals. Any subsequent time that particular NcValue class is needed, the existing class constructor in the globals is used.
The reason for all this is that each created NcValue class is an instance of the appropriate base type. For example: - An instance of <NcIntValue> inherits from <int> - An instance of <NcFloatValue> inherits from <float> - An instance of <NcListValue> inherits from <list>
- in_val (any type) – Value of any type
- metadata (any type) – Any data that should be attached to this value
- created_by_user (bool) – Whether this value was created manually
- New instance of appropriate NcValue-class
Read Note for details.
a = value(1, "some metadata") print(a) # >>> 1 print(a.metadata) # >>> "some metadata" print(a.basetype) # >>> <type 'int'> a.maya_node = "pCube1" # Not only .metadata can be stored! print(a.maya_node) # >>> pCube1 b = value([1, 2, 3], "some other metadata") print(b) # >>> [1, 2, 3] print(b.metadata) # >>> "some other metadata" print(b.basetype) # >>> <type 'list'>