c4d.utils – helper functions

To get an overview of the class members, check out the diagram.

Functions

c4d.utils.SendModelingCommand(command, list[, mode=MODIFY_ALL[, bc[, doc[, undo]]]])

With this function one can apply nearly all modeling commands. The function is applied to the objects in list. With the parameter mode you can decide if the current selection should be used to control what points/polygons are affected.

This is a small example how you can use this function:

import c4d
from c4d import utils
from c4d import documents

settings = c4d.BaseContainer()    # settings
settings[2033] = 50.0             # length of the extrusion. 'id' taken from toolextrude.h

document = documents.GetActiveDocument()
return_value = utils.SendModelingCommand(
                            command = utils.ID_MODELING_EXTRUDE_TOOL,
                            list= [document.GetFirstObject()],    # don't forget to set the correct objects
                            mode=utils.MODIFY_POLYGONSELECTION,     # mode
                            bc=settings,
                            doc = document)
if return_value is False:
    print "Something went wrong"
elif return_value is True:
    print "Command successfull"
elif isinstance(return_value, list):
    print "Here you get the new created object"
Parameters:
  • command (int) – The command ID. See the top of this page for the commands.
  • list (list of BaseObjects) – A list with objects which you want to apply the modeling command.
  • bc (BaseContainer) – This is a settings container which contains all the values of the tool you want to apply.
  • mode (int) –
    MODIFY_ALL All points/polygons.
    MODIFY_POINTSELECTION Only the current point selection.
    MODIFY_POLYGONSELECTION Only the polygon point selection.
    MODIFY_EDGESELECTION Only the edge point selection.
  • doc (BaseDocument) – The document for the operation. Should be set if possible. Must be set for MCOMMAND_MAKEEDITABLE, MCOMMAND_CURRENTSTATETOOBJECT and MCOMMAND_SPLINE_PROJECT. If you set the document, the objects which you pass to this function have to be in the same document. So pay attention that you use one send_modeling_command per document for objects.
  • undo (bool) – If you pass a document, you can use this bool flag to set if you want to support an UNDO option in the document. If this is set to True, you get no object back from this function.
Return type:

False, True or list of BaseObject's

Returns:

Returns True if the command succeeded, otherwise False if something went wrong. Some commands don’t apply their task to the passed objects. They return a cloned object so just check for this, too.

c4d.utils.GetAngle(v1, v2)

Calculates the angle of two vectors.

Parameters:
  • v1 (Vector) – Input value in degrees.
  • v2 (Vector) – Input value in degrees.
Return type:

float

Returns:

The angle.

c4d.utils.RangeMap(value, mininput, maxinput, minoutput, maxoutput, clampval[, curve])

Map the value of a range to another. Similiar to the RangeMapper Node. Here is a small example:

print range_map(value=0.5, mininput=0, maxinput=1, minoutput=5, maxoutput=10, clampval=False)
#Output: 7.5
print range_map(value=0.5, mininput=0, maxinput=1, minoutput=5, maxoutput=10, clampval=True)
#Output: 30
Parameters:
  • value (float) – The value
  • mininput (float) – The minimum Input
  • maxinput (float) – The maximum Input
  • minoutput (float) – The minimum Output
  • maxoutput (float) – The maximum Output
  • clampval (bool) – True if value should be clamped.
  • curve (SplineData) – The curve.
c4d.utils.Rad(r)

Convert a degrees value into radians.

Parameter:r (number) – Input value in degrees.
Return type:number
Returns:Converted value in degrees.
c4d.utils.Deg(r)

Convert a radians value into degrees.

Parameter:r (number) – Input value in degrees.
Return type:number
Returns:Converted value in degrees.
c4d.utils.MixNum(v1, v2, t)

Returns a mixed value of v1 and v2 using the parameter t, as calculated by v1+(v2-v1)*t.

Parameters:
  • v1 (float) – Value to mix.
  • v2 (float) – Value to mix.
  • t (number) – Mix amount.
Math:

0 < t < 1.0

Return type:

float

Returns:

Mix amount, with 0 <= t <= 1.0.

c4d.utils.MixVec(v1, v2, t)

Mixes the two vectors together, such as mixing two colours.

Parameters:
  • v1 (Vector) – Vector to mix.
  • v2 (Vector) – Vector to mix.
  • t (number) – Mix amount
Math:

0 < t < 1.0

Return type:

Vector

Returns:

The mixed vector.

c4d.utils.MiNum(n1, n2, t)

Mixes two numbers together.

Parameters:
  • n1 (number) – Number to mix.
  • n2 (number) – Number to mix.
  • t (number) – Mix amount
Math:

0 < t < 1.0

Return type:

number

Returns:

The mixed number.

c4d.utils.Pulse(a, b, x)

Returns 1.0 if x is between or equal to either of a and b, else 0.0.

Parameters:
  • a (number) – The float value.
  • b (number) – The float value.
  • x (number) – The float value.
Return type:

float

Returns:

The pulse value.

c4d.utils.Clamp(a, b, x)

Returns a if x is less than a and b if x is greater than b, else returns x.

Note:

The order of parameters different to Peachey’s definition.

Parameters:
  • a (number) – The float value.
  • b (number) – The float value.
  • x (number) – The float value.
Return type:

float

Returns:

The clamped value.

c4d.utils.Step(a, b)

Returns 1.0 if x is greater than or equal to a, else 0.0.

Parameters:
  • a (number) – The float value.
  • b (number) – The float value.
Return type:

float

Returns:

The step value (1.0 or 0.0).

c4d.utils.Smoothstep(a, b, x)

Returns 0.0 if x is less than a and 1.0 if x is greater than b, else returns x mapped on the range [a,b] (a number between 0.0 and 1.0). The mapping is smoothed using an ease-in/ease-out curve.

Parameters:
  • a (float) – The float value.
  • b (float) – The float value.
  • x (float) – The float value.
Return type:

float

Returns:

The smoothed value.

c4d.utils.Boxstep(a, b, x)

Returns 0.0 if x is less than a and 1.0 if x is greater than b, else returns x mapped on the range [a,b] (a number between 0.0 and 1.0).

Parameters:
  • a (number) – The float value.
  • b (number) – The float value.
  • x (number) – The float value.
Return type:

float

Returns:

The stepped value.

c4d.utils.MatrixRotX(w)

Creates a rotation matrix about the X axis.

Parameter:w (float) – The angle around X.
Return type:Matrix
Returns:The rotation matrix.
c4d.utils.MatrixRotY(w)

Creates a rotation matrix about the Y axis.

Parameter:w (float) – The angle around Y.
Return type:Matrix
Returns:The rotation matrix.
c4d.utils.MatrixRotZ(w)

Creates a rotation matrix about the Z axis.

Parameter:w (float) – The angle around Z.
Return type:Matrix
Returns:The rotation matrix.
c4d.utils.MatrixToRotAxis(m)

Calculates rotation axis and angle from matrix m:

v, w = MatrixToRotAxis(m)
#v is the rotation axis, c4d.Vector
#w is the rotation angle, float
Parameter:m (Matrix) – Rotation matrix.
Return type:list
Returns:The rotation axis, and the angle.
c4d.utils.GetOptimalAngle(hpb_old, hpb_new)

Modify hpb_new so that the “distance” to the last angle hpb_old is at minimum. This helps to avoid HPB singularity effects.

Parameter:hpb_old (Vector) – The old HPB.

:param hpb_new The new HPB. :rtype: Vector :return: The optimal angle.

c4d.utils.PointLineDistance(p0, v, p)

Calculates the distance from a point to a line.

Parameters:
  • p0 (Vector) – The starting point of the line.
  • v (Vector) – The line vector.
  • p (Vector) – The point.
Return type:

Vector

Returns:

Point-line vector.

c4d.utils.ReflectRay(v, n)

Find the ray vector after a reflection about a surface normal.

Parameters:
  • v (Vector) – The incoming ray.
  • n (Vector) – The surface normal.
  • p (Vector) – The reflected ray.
c4d.utils.CalcSpline(x, knots)

Calculates the value of a spline at a point.

Parameters:
  • x (float) – The position on the spline.
  • knots (list of float) – The knots list.
Return type:

float

Returns:

The spline value.

c4d.utils.CalcSplineV(x, knots)

Calculates the value of a spline at a point.

Parameters:
  • x (float) – The position on the spline.
  • knots (list of Vector) – The knots list.
Return type:

Vector

Returns:

The spline value.

c4d.utils.RGBToHSV(col)

Converts RGB into the HSV color space and returns the converted value.

Parameter:col (Vector) – RGB color.
Return type:Vector
Returns:HSV color.
c4d.utils.HSVToRGB(col)

Converts HSV into the RGB color space and returns the converted value.

Parameter:col (Vector) – HSV color.
Return type:Vector
Returns:RGB color.
c4d.utils.VectorEqual(v1, v2[, epsilon=0.01])

Check if vector v1 and v2 are within epsilon of each other.

Parameters:
  • v1 (Vector) – The first vector.
  • v2 (Vector) – The second vector.
  • epsilon (float) – The epsilon value.
Return type:

bool

Returns:

True if the vectors are equal.

c4d.utils.Bias(b, x)

Returns the bias as the defined in the book “Texturing and Modeling” by Ebert.

The internal code:

def Bias(b, x):
    return math.pow(x, -ln(b) / 0.693147180559945)
Parameters:
  • b (number) – Bias value.
  • x (number) – The real value.
Return type:

float

Returns:

The bias.

c4d.utils.FCut(a, b, c)

Limit the value of a to between b and c.

Parameters:
  • a (float) – Value
  • b (float) – Lower bound
  • c (float) – Upper bound
c4d.utils.CutColor(vec)

Limit a color vector between 0.0 and 1.0.

Parameter:vec (Vector) – Bias value.
Return type:Vector
Returns:The limited color vector.
c4d.utils.Truncate(x)

Limit a color vector between 0.0 and 1.0.

The internal code:

def truncate(x):
    if x >= 0.0:
        return floor(x)
    else:
        return ceil(x)
Parameter:x (number) – The value to truncate.
Return type:Vector
Returns:The truncated value.
c4d.utils.VectorSum(vec)

Sum the vector components.

Parameter:x (Vector) – A color
Return type:float
Returns:The sum of the components.
c4d.utils.VectorGray(vec)

Sum the vector components and multiply by 1/3.

Parameter:vec (Vector) – A color
Return type:float
Returns:The gray value.
c4d.utils.VectorAngle(vec1, vec2)

Calculates the angle between two vectors in radians.

Parameters:
  • vec1 (Vector) – The first vector.
  • vec2 (Vector) – The second vector.
Return type:

float

Returns:

The angle in radians.

c4d.utils.VectorMin(vec)

Find the minimum component of the vector.

Parameter:vec (Vector) – The vector to find the minimum component of.
Return type:float
Returns:The minimum component of the vector.
c4d.utils.VectorMax(vec)

Find the maximum component of the vector.

Parameter:vec (Vector) – The vector to find the maximum component of.
Return type:float
Returns:The maximum component of the vector.
c4d.utils.MatrixMove(vec)

Create a translation matrix.

Parameter:vec (Vector) – The translation vector.
Return type:Matrix
Returns:The translation matrix.
c4d.utils.MatrixScale(vec)

Create a scaling matrix.

Parameter:vec (Vector) – The scaling vector for the axes.
Return type:Matrix
Returns:The scaling matrix.
c4d.utils.VectorToHPB(vec)

Calculate euler angles from the vector p. The bank is always set to 0.0.

Parameter:vec (Vector) – The rotation HPB.
Return type:Vector
Returns:The vector to find the HPB for.
c4d.utils.MatrixToHPB(vec)

Calculate euler angles from the matrix m.

Parameter:vec (Vector) – The rotation HPB.
Return type:Matrix
Returns:The rotation matrix.
c4d.utils.HPBToMatrix(hpb, order)

Construct matrix from the euler angles hpb.

Parameters:
  • hpb (Vector) – The input HPB.
  • order (Vector) –

    The order of rotation:

    ROT_YXZGLOBAL Global YXZ order.
    ROT_YZXGLOBAL Global YZX order.
    ROT_ZYXGLOBAL Global ZYX order.
    ROT_ZXYGLOBAL Global ZXY order.
    ROT_XZYGLOBAL Global XZY order.
    ROT_XYZGLOBAL Global XYZ order.
    ROT_YXZLOCAL Local YXZ order.
    ROT_YZXLOCAL Local YZX order.
    ROT_ZYXLOCAL Local ZYX order.
    ROT_ZXYLOCAL Local ZXY order.
    ROT_XZYLOCAL Local XZY order.
    ROT_XYZLOCAL Local XYZ order.
    ROT_HPB HPB order.
Return type:

Matrix

Returns:

The rotation matrix.

c4d.utils.CalcLOD(val, lod, min, max)

This is a helper function to modify a user chosen subdivision value. For example:

sub = CalcLOD(op[TUBEOBJECT_SUB, 1], hh["lod"], 1, 1000)
Parameters:
  • val (int) – The user chosen lod value.
  • lod (float) – The LOD value.
  • min (int) – The minimum lod.
  • max (int) – The maximum lod.
c4d.utils.CompareFloatTolerant(a, b)

Compares if two floats are close to each other on a bit basis (rather than a fixed epsilon).

Parameters:
  • a (float) – The first parameter.
  • b (float) – The second parameter.
Return type:

bool

Returns:

True if a and b are sufficiently close to each other, otherwise False.

c4d.utils.SinCos(w)

Get sine and cosine of w:

sn, cs = SinCos(80)
Parameter:w (float) – Value.
Return type:list
Returns:Sine and Cosine

Commands for send_modeling_command:

There are several groups of IDs to use:

MCOMMAND_SPLINE_HARDINTERPOLATION Hard interpolation
MCOMMAND_SPLINE_SOFTINTERPOLATION Soft Interpolation
MCOMMAND_SPLINE_REORDER Set first point
MCOMMAND_SPLINE_REVERSE Reverse sequence
MCOMMAND_SPLINE_MOVEDOWN Move down sequence
MCOMMAND_SPLINE_MOVEUP Move up sequence
MCOMMAND_SPLINE_JOINSEGMENT Join segment
MCOMMAND_SPLINE_BREAKSEGMENT Break segment
MCOMMAND_SPLINE_EQUALLENGTH Equal tangent length
MCOMMAND_SPLINE_EQUALDIRECTION Equal tangent directio
MCOMMAND_SPLINE_LINEUP Line up
MCOMMAND_SPLINE_CREATEOUTLINE Create outline
MCOMMAND_SPLINE_PROJECT Project
MCOMMAND_SPLINE_ADDPOINT Add point
MCOMMAND_SELECTALL Select all
MCOMMAND_DESELECTALL Deselect all
MCOMMAND_SELECTINVERSE Invert selection
MCOMMAND_SELECTCONNECTED Select connected
MCOMMAND_SELECTGROW Grow selection
MCOMMAND_SELECTSHRINK Shrink selection
MCOMMAND_SELECTPOINTTOPOLY Polygon selection from points
MCOMMAND_SELECTPOLYTOPOINT Point selection from polygons
MCOMMAND_SELECTADJACENT Select adjacent
MCOMMAND_GENERATESELECTION Set selection
MCOMMAND_HIDESELECTED Hide selected
MCOMMAND_HIDEUNSELECTED Hide unselected
MCOMMAND_HIDEINVERT Invert visibility
MCOMMAND_UNHIDE Unhide all
MCOMMAND_REVERSENORMALS Reverse normals
MCOMMAND_ALIGNNORMALS Align normals
MCOMMAND_SPLIT Split
MCOMMAND_TRIANGULATE Triangulate
MCOMMAND_UNTRIANGULATE Untriangulate
MCOMMAND_DELETE Delete
MCOMMAND_OPTIMIZE Optimize
MCOMMAND_DISCONNECT Disconnect
MCOMMAND_MAKEEDITABLE Make editable (returns object)
MCOMMAND_MIRROR Mirror
MCOMMAND_MATRIXEXTRUDE Matrix extrude. See toolmatrixextrude.h.
MCOMMAND_SUBDIVIDE Subdivide
MCOMMAND_EXPLODESEGMENTS Explode segments
MCOMMAND_KNIFE Knife. See toolknife.h.
MCOMMAND_CURRENTSTATETOOBJECT Current state to object (returns object)
MCOMMAND_JOIN Join (returns object)
MCOMMAND_CONVERTSELECTION Convert selection
MCOMMAND_EDGE_TO_SPLINE Edge to spline
MCOMMAND_BREAKPHONG Break phong.
MCOMMAND_UNBREAKPHONG Unbreak phong.
MCOMMAND_PHONGTOSELECTION Phong to selection.
MCOMMAND_MELT Melt.
MCOMMAND_RESETSYSTEM Reset System.

Further modelling tools:

ID_MODELING_EDGECUT_TOOL Edge cut tool. See tooledgecut.h.
ID_MODELING_FILL_SELECTION_TOOL Fill selection tool.
ID_MODELING_OUTLINE_SELECTION_TOOL Outline selection tool.
ID_MODELING_LOOP_TOOL Loop selection tool. See toolloopselection.h.
ID_MODELING_RING_TOOL Ring selection tool.
ID_MODELING_EXTRUDE_TOOL Extrude tool. See toolextrude.h.
ID_MODELING_MELT_COMMAND Melt.
ID_CONVERT_SELECTION_TOOL Normal move tool. See toolnormalmove.h.
ID_VIEW_SCENEHOOKHIGHLIGHT Normal scale tool. See toolnormalscale.h.
ID_VIEW_SCENEHOOKHUD Normal rotate tool. See toolnormalrotate.h.
ID_MODELING_EDGE_SPLINE_COMMAND Smooth shift tool. See toolsmoothshift.h.
ID_MODELING_MATRIX_EXTRUDE_TOOL Matrix extrude tool. See toolmatrixextrude.h.
ID_MODELING_NORMALMOVE_TOOL Normal move tool. See toolnormalmove.h.
ID_MODELING_NORMALSCALE_TOOL Normal scale tool. See toolnormalscale.h.
ID_MODELING_NORMALROTATE_TOOL Normal rotate tool. See toolnormalrotate.h.
ID_MODELING_SMOOTH_SHIFT_TOOL Smooth shift tool. See toolsmoothshift.h.
ID_MODELING_EXTRUDE_INNER_TOOL Extrude inner tool. See toolextrudeinner.h.
ID_MODELING_BEVEL_TOOL Bevel tool. See toolbevel.h.
ID_MODELING_COLLAPSE_COMMAND Collapse.
ID_MODELING_POLYGON_CREATE_TOOL Create polygon tool. See toolcreatepolygon.h.
ID_MODELING_POINT_ADD_TOOL Add point tool. See tooladdpoint.h.
ID_MODELING_BRIDGE_TOOL Bridge tool. See toolbridge.h.
ID_MODELING_WELD_TOOL Weld tool. See toolpointweld.h.
ID_MODELING_CLOSEHOLE_TOOL Close polygon hole tool. See toolclosehold.h.
ID_MODELING_STITCHANDSEW_TOOL Stich and sew tool. See toolstitchandsew.h.
ID_MODELING_SLIDE_TOOL Slide tool. See toolslide.h.
ID_MODELING_IRON_TOOL Iron tool. See tooliron.h.
ID_MODELING_SETVALUE_TOOL Set value tool. See toolsetvalue.h.
ID_MODELING_DUPLICATE_TOOL Duplicate tool. See toolduplicate.h.
ID_MODELING_ARRANGE_TOOL Arrange tool. See toolarrange.h.
ID_MODELING_TRANSFER_TOOL Transfer tool. See tooltransfer.h.
ID_MODELING_RANDOMIZE_TOOL Randomize tool. See toolrandomize.h.
ID_MODELING_CENTER_TOOL Center tool. See toolcenter.h.
ID_MODELING_KNIFE_TOOL Knife tool. See toolknife.h.
ID_MODELING_LIVESELECTION Live selection tool. See toolliveselection.h.
ID_MODELING_RECTSELECTION Rectangle selection tool. See toolrectselection.h.
ID_MODELING_FREESELECTION Free selection tool. See toolfreeselection.h.
ID_MODELING_POLYSELECTION Polygon selection tool. See toolpolyselection.h.
ID_MODELING_SPLINE_ROUND_TOOL Spline round tool. See toolsplineround.h.
ID_MODELING_CLONE_TOOL Clone tool. See toolclone.h.
ID_MODELING_SPLINE_CHAMFER_TOOL Spline chamfer tool. See toolsplinechamfer.h.
ID_MODELING_ARRAY_TOOL Array tool. See toolarray.h.
ID_MODELING_SPLINE_PROJECT_TOOL Spline project tool. See toolsplineproject.h.
ID_MODELING_MODIFY_TOOL Modify tool. See toolmodify.h.
ID_MEASURE_TOOL Measure tool. See toolmeasure.h.
ID_MODELING_BRUSH_TOOL Brush tool. See toolbrush.h.
ID_MODELING_OM_SELECT_INVERT Invert object manager selection.

Finally there are the modeling library menu commands. These have no parameters:

ID_NGON_RETRI_MENU Retriangulate N-gons.
ID_NGON_REMOVE_MENU Remove N-gons.
ID_NGON_FORCERETRI_MENU Force N-gon triangulation.
ID_OBJECTHANDLES_ONOFF_MENU Object handles toggle.
ID_SDS_INC_MENU Increment SDS.
ID_SDS_DEC_MENU Decrement SDS.
ID_SDS_ONFFF_MENU Toggle SDS.
ID_MODELING_OM_SELECT_HIDE Hide selected objects.
ID_MODELING_OM_SELECT_SHOW Show selected objects.
ID_MODELING_SHORTCUT_SELECTVISIBLE Toggle select visible.

Table Of Contents

This Page