Scripting Elements¶
Explains Python scripting elements embedded in Cinema 4D scenes.
Scripting elements are scene elements such as object, tags, or materials in Cinema 4D that have Python script parameters
which control the whole element or aspects of it. Many scripting elements are either miniature versions of full plugins,
the Python Generator object is for example a miniature version of a c4d.plugins.ObjectData plugin, or stand for
plugin types that have never been ported to the Python API, such as the Python Effector and Python Field.
Warning
Because many scripting elements are derived from c4d.plugins.NodeData, they inherit its threading restrictions.
See Threading Restrictions for more details.
Python Generator Object¶
A Python Generator Object is an object that dynamically creates a generator output based on a Python script. It is a
miniature version of a c4d.plugins.ObjectData plugin. The generator has a Optimize Cache parameter that
acts just like the method c4d.plugins.ObjectData.SetOptimizeCache() and causes the main function only to be
called when any of the parameters of the generator changed. Disabling it will rebuild the generator on each scene
update which can be quite expensive and when not paired with a manual dirtiness check.
Warning
The main function of a Python Generator is subject to threading restrictions, i.e., you cannot modify a loaded document from there or perform GUI operations. See Threading Restrictions for more details.
Attributes¶
These are module level attributes which are injected by Cinema 4D into the Python Generator’s module. hh is only
available while main() is being evaluated.
Name |
Datatype |
Description |
|---|---|---|
|
|
The active document. |
|
|
The hierarchy helper used to process child objects of the generator. Available only while |
|
|
The Python Generator object itself. |
Methods¶
-
main() → c4d.BaseObject Override to generate and return the object output for the generator.
Equivalent to the
c4d.plugins.ObjectData.GetVirtualObjects()method. You can return a singlec4d.PolygonObjector a whole hierarchy of objects under a null objects. You can also return splines, and in many cases this will work, but this formally not a spline generator, and some other components of Cinema 4D therefore might fail to recognize your generator as a spline.- Returns
The new cache of the generator (i.e., what the user sees as its geometry).
- Return type
-
message(mid: int, data: dict | object) → bool | None Override to handle messages sent to the Python Generator.
Equivalent to the
c4d.plugins.NodeData.Message()method. This can be used to react to modify to button clicks, modify the UI of the generator, and many other things that are possible with messages. Messages are also the only context in a generator where you are not always bound by threading restrictions. Messages often run on the main thread, but you must check withc4d.threading.GeIsMainThread()to be sure.Warning
Do not consume messages unless you have to, i.e., the default should be to return None. Consuming messages that you do not handle yourself can break the functionality of the node, as data is then now longer reaching its consumer higher up the call chain.
- Parameters
mid (int) – The message ID.
data (dict | object) – The message data, usually a dict.
- Returns
True or False if you either successfully or unsuccessfully consumed the message, or None if not consumed.
- Return type
bool | None
Python Programming Tag¶
A Python Programming Tag executes the Python code stored in its script parameter when the tag is being
executed. The tag acts as a miniature version of a c4d.plugins.TagData plugin.
Warning
The main and draw functions of a Python Programming Tag are subject to threading restrictions. See Threading Restrictions for more details.
Attributes¶
These module-level attributes are injected into the Python Programming Tag’s script scope.
Name |
Datatype |
Description |
|---|---|---|
|
|
The document containing the tag. |
|
|
The Python Programming Tag. |
|
|
The execution flags passed to |
|
|
The execution priority of the tag. See EXECUTIONPRIORITY. |
|
|
The document’s Thinking Particles master system, or |
Methods¶
-
main() → None Override to execute the tag. It corresponds to
c4d.plugins.TagData.Execute()and may be called multiple times per frame.
-
message(mid: int, data: object) → bool | None Override to handle messages sent to the tag. Return
Noneunless the script explicitly consumes the message.- Parameters
mid (int) – The message ID.
data (object) – The message data.
- Return type
bool | None
-
draw(bd: c4d.BaseDraw) → None Override to draw visual elements in the viewport. It corresponds to
c4d.plugins.TagData.Draw().- Parameters
bd (c4d.BaseDraw) – The editor view.
Python Effector¶
A Python Effector controls MoGraph data. Its main() callback is called in full-control mode.
The scope provides doc, op (the effector), and gen (the MoGraph generator).
Warning
The main function of a Python Effector is subject to threading restrictions and should not create or modify objects in the scene. See Threading Restrictions for more details.
Attributes¶
These module-level attributes are injected into the Python Effector’s script scope.
Name |
Datatype |
Description |
|---|---|---|
|
|
The document containing the effector. |
|
|
The Python Effector object. |
|
|
The MoGraph generator executing the effector. |
Methods¶
-
main() → bool | float Override to read and modify the MoGraph data returned by
c4d.modules.mograph.GeGetMoData(). In full-control mode it is called once to handle all clones. In parameter-control mode it is called for each clone.- Returns
Trueor a numeric result as required by the effector mode.- Return type
bool | float
The implementation invokes
main()without positional arguments. In parameter mode it is evaluated for the current clone; in full-control mode it is evaluated once and can modify thec4d.modules.mograph.MoDataobtained fromc4d.modules.mograph.GeGetMoData().
Python Field Object and Layer¶
A Python Field can be a Field Object in the scene or a Field Layer in a c4d.FieldList.
The same callback names are used for both, but Aggregate() is called only for a Field Layer.
The callbacks are called for each sampling pass and must not modify the scene.
Warning
The field callbacks are subject to threading restrictions and should not create or modify objects in the scene. See Threading Restrictions for more details.
Attributes¶
These module-level attributes are injected into the Python Field script scope. The sampling objects and information are passed as callback arguments, not stored as module-level attributes.
Name |
Datatype |
Description |
|---|---|---|
|
|
The document containing the field. The field or layer is passed explicitly as |
Methods¶
-
InitSampling(op: c4d.modules.mograph.FieldObject, info: c4d.modules.mograph.FieldInfo) → bool Called before a sampling pass starts. Use it to allocate resources needed by
Sample(), such as a neighbor lookup or a KD-tree. Free those resources inFreeSampling().- Parameters
op – The Python Field Object or Layer.
info – Information about the current sampling pass.
- Returns
Trueto continue sampling.- Return type
bool
-
Sample(op: c4d.modules.mograph.FieldObject, inputs: c4d.modules.mograph.FieldInput, outputs: c4d.modules.mograph.FieldOutputBlock, info: c4d.modules.mograph.FieldInfo) → bool Called for each field output block. Populate
outputs._valueand update its deactivation state. For a Field Layer, the output can already contain values from preceding fields.- Parameters
op – The Python Field Object or Layer.
inputs – The input positions and transform for the current block.
outputs – The output block to populate.
info – Information about the current sampling pass.
- Returns
Trueto continue sampling.- Return type
bool
-
Aggregate(op: c4d.modules.mograph.FieldObject, inputs: c4d.modules.mograph.FieldInput, outputs: c4d.modules.mograph.FieldOutputBlock, info: c4d.modules.mograph.FieldInfo) → bool Called after sample blocks have been processed for a Python Field Layer. It is not called for a Python Field Object. Use it to post-process or normalize the output, for example by applying a smoothing filter.
- Returns
Trueto continue.- Return type
bool
-
FreeSampling(op: c4d.modules.mograph.FieldObject, info: c4d.modules.mograph.FieldInfo) → bool Called after a sampling pass. Free resources allocated in
InitSampling()here.- Returns
Truewhen cleanup succeeded.- Return type
bool
The callback must return
True. The result is converted to a boolean by the host.
Python Field scripts do not receive op or info as globals. The field or layer and the
sampling information are passed explicitly to every sampling callback. doc is the only injected
document reference.
Python Interaction Tag¶
Python code in an Interaction Tag responds to mouse and selection events on the host object. The tag is intended for interactive viewport behavior and implements the interaction callbacks of a tag plugin.
Warning
The interaction callbacks are subject to threading restrictions. See Threading Restrictions for more details.
Attributes¶
These module-level attributes are injected into the Interaction Tag’s script scope. They are updated by Cinema 4D for the current interaction.
Name |
Datatype |
Description |
|---|---|---|
|
|
The Interaction Tag. |
|
|
The object hosting the tag. |
|
|
The proxy object, or undefined when no proxy is involved. |
|
|
The thread in which the tag is executed. |
|
|
The qualifier keys that were pressed. See QUALIFIER. |
Methods¶
-
mouseDown() → None Called when the user presses the mouse button on the host object.
-
mouseDrag() → None Called for each tick while the user drags with the mouse.
-
mouseUp() → None Called when the user releases the mouse button.
-
doubleClick() → None Called when the user double-clicks the host object.
-
onSelect() → None Called when the host object is selected.
-
onDeselect() → None Called when the host object is deselected.
-
onHighlight() → None Called when the host object is highlighted.
-
onUnhighlight() → None Called when the host object is no longer highlighted.
-
draw(bd: c4d.BaseDraw) → None Called to draw visual elements in the editor view.
- Parameters
bd (c4d.BaseDraw) – The editor view.
The return value is ignored.
-
message(mid: int, data: object) → bool | None Called when the tag receives a message.
- Parameters
mid – The message ID.
data – The message data.
- Return type
bool | None
Python Character Component Tag¶
Python code can customize the components of a Character Object through a Character Component Tag. The callbacks are available in the Python tab of the Character Component Tag and in Python insertion and adjustment rules.
Warning
The Character Component Tag callbacks are subject to threading restrictions. See Threading Restrictions for more details.
Attributes¶
The available callback-local attributes depend on the Character workflow and callback being executed. The following types are the Python types registered by the Character Builder module.
Name |
Datatype |
Description |
|---|---|---|
|
|
The current document. |
|
|
The Character Object. |
|
|
Character template data for template-related callbacks. |
|
|
The object currently processed by the callback. |
|
|
The parent component object. |
|
|
The parent component. |
|
|
The component being processed. |
|
|
The component object being processed. |
|
|
Callback-specific objects used by component insertion and dependency callbacks. |
|
|
Container used by |
|
|
Matrix supplied to or returned by transform-related callbacks. |
|
|
Insertion type, item count, and old/new mode values used by the corresponding callbacks. |
Methods¶
-
BuildTemplate() → None Override to build or modify a character template when it is saved.
- Parameters
doc – The template document.
op – The object with the Component Tag.
-
GetComponents() → c4d.BaseContainer | None Override to control which components are available. Return a
c4d.BaseContainerof component links, orNoneto use the standard inclusion rules.
-
ModeChanged() → None Called when the Character Object changes between Build, Adjust, Bind, and Animate modes. The old and new modes are available as
omodeandnmode.
-
InsertComponent() → c4d.BaseObject Override component insertion. Return the main component object from the Python tab, or the object under which the component should be inserted from an insertion rule. Set the global
itypevariable when using an insertion rule.
-
PostInsertComponent() → None Called after a component has been inserted when
InsertComponent()is not used.
-
MirrorComponent() → bool Called when a mirrored component is added. Return
Truewhen mirroring was handled by Python.
-
IncludeObjects() → bool Define additional objects to include with the
incdatavariable. ReturnTruewhen it was changed.
-
AdjustObject() → c4d.Matrix Return the matrix to set for an object controlled by an adjustment rule. The matrices are local to the Character Object.
-
GetDependency() → c4d.BaseContainer Return a
c4d.BaseContainercontaining the objects on which an adjustment depends. This callback runs beforeAdjustObject().
-
TransformComponent() → c4d.Matrix Return the global matrix for an inserted component. The matrices are local to the Character Object.
-
AllowComponent() → bool Return
Truewhen the component is allowed.
Python Sketch and Toon Material¶
Python code can control the Color, Thickness, and Opacity channels of a Sketch and Toon Material.
Enable the Python option in the channel to execute the script. Return a c4d.Vector for
Color, and a float for Thickness or Opacity.
Warning
The Sketch and Toon callback is evaluated during rendering and is subject to threading restrictions. See Threading Restrictions for more details.
Attributes¶
The following module-level attributes are available while a Sketch and Toon channel script is evaluated. Additional variables depend on the channel and sampled line.
The host supplies the following values for the line being evaluated. Values in the ray/surface group describe the current render sample and are most useful for scripts in the Color channel.
Name |
Datatype |
Description |
|---|---|---|
|
|
The object currently being sampled. |
|
|
Position along the current stroke. |
|
|
Normalized position along the current stroke. |
|
|
Distance from the sampled point to the line. |
|
|
Current stroke thickness. |
|
|
Length of the current stroke. |
|
|
Current point in scene coordinates. |
|
|
Offset of the current line. |
|
|
Current point in world coordinates. |
|
|
Current scene time in seconds. |
|
|
Current frame number, computed from time and frame rate. |
|
|
Current scene frame rate. |
|
|
Screen-space start and end points of the current line segment. |
|
|
Normal of the current line segment. |
|
|
Normal of the source edge. |
|
|
Positions of the line endpoints within their source edges. |
|
|
Join angle at the start and end of the line. The value is |
|
|
Point indices of the line’s start and end vertices. |
|
|
Polygon indices associated with the line’s start and end. |
|
|
Current line color. For the Color channel, return the modified value from |
|
|
Ray-intersection flags for the current render sample. |
|
|
Cosine value for the current render sample. |
|
|
Surface normal at the current render sample. |
|
|
Bump-mapped surface normal at the current render sample. |
|
|
Distance for the current render sample. |
|
|
Polygon index hit by the current render ray. |
|
|
Origin and direction of the current render ray. These are set only when ray data exists. |
|
|
Surface point for the current render sample. |
|
|
Pixel bounds of the current render region. |
|
|
Pixel bounds of the current object’s outline. If no outline is available, these fall back to the render-region bounds. |
Methods¶
-
main() Return a
c4d.Vectorfor Color, or a float for Thickness and Opacity. The callback is evaluated for each sampled line.
Python Visual Selector Tag¶
Python code can respond when a hotspot is clicked. The script is evaluated in the context of the Visual Selector Tag.
Warning
The main function of a Python Visual Selector Tag is subject to threading restrictions. See Threading Restrictions for more details.
Attributes¶
These module-level attributes are injected into the Visual Selector script scope for the selected hotspot.
Name |
Datatype |
Description |
|---|---|---|
|
|
The document containing the tag. |
|
|
The object in the tag’s link field, or |
Methods¶
-
main() Called when the user presses a hotspot.
Python Xpresso Node¶
Python code can be executed in a Python Xpresso Node. The node creates a global variable for each
input and output port using the port name. main() takes no positional arguments. Read input
variables and assign output variables before returning. A Python Xpresso Node should not modify the
scene.
Warning
The main function of a Python Xpresso Node is subject to threading restrictions. See Threading Restrictions for more details.
Attributes¶
These module-level attributes are injected into the Python Xpresso Node’s script scope. The
Thinking Particles reference is set to None when the document has no Thinking Particles system.
Name |
Datatype |
Description |
|---|---|---|
|
|
The document containing the Xpresso tag. |
|
|
The current Python Xpresso Node. |
|
|
The document’s Thinking Particles master system, or |
Methods¶
-
main() Called when the node is evaluated by the GvNodeMaster. The return value is ignored.
Input and output variables are dynamic: their names and types are determined by the ports on the node. The activation input named
Oncontrols whether the callback is evaluated, but is not exposed as a regular input variable.