Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login
    1. Maxon Developers Forum
    2. zauhar
    3. Topics
    Z
    • Profile
    • Following 0
    • Followers 0
    • Topics 4
    • Posts 16
    • Best 0
    • Controversial 0
    • Groups 0

    Topics created by zauhar

    • Z

      Apply material to individual faces of a polygon

      Cinema 4D SDK
      • python sdk • • zauhar
      8
      0
      Votes
      8
      Posts
      2.1k
      Views

      ferdinandF

      Hey @zauhar,

      I do not think so that I have told you that, at least I hope so, because that what you say there is not quite correct 🙂

      A vertex map or a vertex color tag can be rendered. When you use the standard renderer, you will have to use the Vertex Map shader, and when you use Redshift, you will need the Vertex Attribute node (just drag and drop the tag in both cases in the respective reference fields of the shaders). Below is an example for the Redshift case:

      Screenshot 2023-06-16 at 19.41.37.png

      Other renderers support Cinema 4D vertex colors too, but you will have to ask their support how that works 🙂

      Vertex Map Shader
      Vertex Attribute Node

      Cheers,
      Ferdinand

    • Z

      Trying to programmatically create vertex color tag

      Cinema 4D SDK
      • • • zauhar
      2
      0
      Votes
      2
      Posts
      660
      Views

      ferdinandF

      Hello @zauhar,

      thank you for reaching out to us. Your code does look mostly fine and is working for me. The line op = Default makes little sense and you are of course missing things like imports, a context guard and a main function. Not all of them are absolutely necessary, but it is strongly advised to use them all, especially for novice users. Find a code example below.

      Vertex colors are also not automatically rendered as the diffuse color in Cinema 4D, one must use a VertexMap shader for that. Please consult the user manual or end-user support for end-user questions, we cannot deliver such support here.

      Cheers,
      Ferdinand

      The result for two polygon objects, the color of a vertex depends on its position in global space.
      0e9b3117-e70a-4495-8001-cbe0a6b0a2e4-image.png

      Code

      """Creates vertex-color tag on the currently selected polygon object and makes it the active tag. Must be run as a Script Manager scrip with a polygon object selected. The color of each vertex will depend on its position in global space. """ import c4d import typing GRADIENT_MIN: float = -200.0 # The lower boundary of the gradient. GRADIENT_MAX: float = 200.0 # The upper boundary of the gradient. op: typing.Optional[c4d.BaseObject] # The active object, can be `None`. def main() -> None: """Runs the example. """ # Check that there is indeed a selected object and that it is a polygon object. if not isinstance(op, c4d.PolygonObject): raise TypeError("Please select a polygon object.") # Get its point count and allocate a vertex color tag with that count. count: int = op.GetPointCount() tag: c4d.VertexColorTag = c4d.VertexColorTag(count) if not isinstance(tag, c4d.VertexColorTag): raise MemoryError("Could not allocate tag.") # We are going to make it a little bit more exciting and write a gradient based on the global # coordinate y-component of a vertex. # Set the tag to defining colors only once per vertex instead of having N colors per vertex, # where N is the number of polygons attached to it. tag.SetPerPointMode(True) # Get the global matrix of the polygon object and all its points in global space. mg: c4d.Matrix = op.GetMg() globalPoints: list[c4d.Vector] = [mg * p for p in op.GetAllPoints()] # Define a callable with which we can get the gradient color of a point over its index. GetGradientColor = lambda i : c4d.Vector4d( c4d.utils.RangeMap(globalPoints[i].y, GRADIENT_MIN, GRADIENT_MAX, 0, 1, True), 0, 0, 1) # Get the data pointer of the tag and start writing data. dataW: object = tag.GetDataAddressW() for i in range(count): c4d.VertexColorTag.SetPoint(dataW, None, None, i, GetGradientColor(i)) # Insert the tag into the selected object, make it the active tag, and push an update event. op.InsertTag(tag) tag.SetBit(c4d.BIT_ACTIVE) c4d.EventAdd() if __name__ == "__main__": main()
    • Z

      Look At Camera Tag and text orientation

      Cinema 4D SDK
      • python • • zauhar
      12
      0
      Votes
      12
      Posts
      2.6k
      Views

      Z

      @ferdinand

      Thanks, that is exactly correct - in fact I did not need to make the orthonormal vector, all one needs do is set the objects Y-axis equal to the camera, and get the x-axis by cross product .

      I think I correctly handle the pathological situation after looking at the limiting cases.

      The modified function is below.

      Randy

      EPSILON = 1E-5 # The floating point precision we are going to assume, i.e., 0.00001 # This is based on the python Look at Camera example # https://github.com/PluginCafe/cinema4d_py_sdk_extended/tree/master/plugins/py-look_at_camera_r13 def GetLookAtTransform(host: c4d.Matrix, target: c4d.Matrix, reverseZ=True) -> c4d.Matrix: """Returns a transform which orients its z/k/v3 component from #host to #target. """ # Get the position of both transforms. p: c4d.Vector = host.off q: c4d.Vector = target.off # The normalized offset vector between 'host' (object to be reoriented) and 'target' (the camera) # will become the z-axis of the modified frame for the object . # # If reverseZ = True, the new z-axis is points from camera toward object, if False the reverse # I turn reverseZ on by default, as my initial application is to text splines, which are meant to be # viewed looking down the object z-axis. # In the original implementation # (https://github.com/PluginCafe/cinema4d_py_sdk_extended/tree/master/plugins/py-look_at_camera_r13 ) # the modified y-axisis computed using the global y-axis, and this does not consistently # keep the text upright in the view of the camera. Instead, simply take the object y-axis same as camera y. # # In the pathological case of new object z-axis parallel to camera y : # If reverseZ : set object z = camera y , object y = -camera Z # else : set object z = -camera y, object y = -camera z # if reverseZ : z: c4d.Vector = ~(p - q) if 1. - abs(z * target.v2) > EPSILON : y = target.v2 else : z = target.v2 y = -target.v3 else : z: c4d.Vector = ~(q - p) if 1. - abs(z * target.v2) > EPSILON : y = target.v2 else : z = -target.v2 y = -target.v3 # get x using cross product x: c4d.Vector = ~(y % z) # Return the frame (x, y, z) plus the offset of #host as the look-at transform. return c4d.Matrix(off=p, v1=x, v2=y, v3=z)
    • Z

      Particles from python SDK

      Cinema 4D SDK
      • • • zauhar
      5
      0
      Votes
      5
      Posts
      947
      Views

      Z

      @ferdinand

      That is great! I did not imagine that working so simply, that's a wonderful feature.

      Regarding the 'broken' part, I did 'something' and spheres stopped appearing in the viewport, just the 'tracers'. I cannot reproduce that, I must have screwed up. All is right with the world now.

      In case it helps others, the demo code below now does everything I currently need. Programmatically changes position of the emitter, and following this discussion (https://forums.cgsociety.org/t/c4d-animation-via-python/1546556) I also change the color over the course of each 10-frame segment.

      Thanks so much for your help, I am really sold on 4D.

      import c4d import math import random mat = c4d.BaseMaterial(c4d.Mmaterial) mat.SetName('emitter sphere') mat[c4d.MATERIAL_COLOR_COLOR] = c4d.Vector(0.8, 0.0, 0.0) ## Get RGB tracks for continuous color update redtrack = c4d.CTrack(mat, c4d.DescID(c4d.DescLevel(c4d.MATERIAL_COLOR_COLOR, c4d.DTYPE_COLOR, 0, ), c4d.DescLevel(c4d.VECTOR_X, c4d.DTYPE_REAL, 0))) mat.InsertTrackSorted(redtrack) greentrack = c4d.CTrack(mat, c4d.DescID(c4d.DescLevel(c4d.MATERIAL_COLOR_COLOR, c4d.DTYPE_COLOR, 0, ), c4d.DescLevel(c4d.VECTOR_Y, c4d.DTYPE_REAL, 0))) mat.InsertTrackSorted(greentrack) bluetrack = c4d.CTrack(mat, c4d.DescID(c4d.DescLevel(c4d.MATERIAL_COLOR_COLOR, c4d.DTYPE_COLOR, 0, ), c4d.DescLevel(c4d.VECTOR_Z, c4d.DTYPE_REAL, 0))) mat.InsertTrackSorted(bluetrack) doc.InsertMaterial(mat) sph = c4d.BaseObject(5160) rad = sph.GetRad() particleRad = 2.0 scale = particleRad/rad[0] sph.SetAbsScale((scale,scale,scale)) ttag = c4d.TextureTag() ttag.SetMaterial(mat) sph.InsertTag(ttag) sph.SetBit(c4d.BIT_ACTIVE) emitter = c4d.BaseObject(5109) emitter.SetBit(c4d.BIT_ACTIVE) doc.InsertObject(emitter) sph.InsertUnder(emitter) # emit particles at rate 500 emitter[c4d.PARTICLEOBJECT_BIRTHEDITOR] = 500 emitter[c4d.PARTICLEOBJECT_BIRTHRAYTRACER ] = 500 emitter[c4d.PARTICLEOBJECT_RENDERINSTANCES] = 500 emitter[c4d.PARTICLEOBJECT_SIZEX] = 0.2 emitter[c4d.PARTICLEOBJECT_SIZEY] = 0.2 emitter[c4d.PARTICLEOBJECT_TYPE] = c4d.PARTICLEOBJECT_TYPE_PYRAMID emitter[c4d.PARTICLEOBJECT_ANGLEH] = 2 * math.pi emitter[c4d.PARTICLEOBJECT_ANGLEV] = math.pi emitter[c4d.PARTICLEOBJECT_SHOWOBJECTS] = True fps = 24 emitter[c4d.PARTICLEOBJECT_START] = c4d.BaseTime(0, fps) emitter[c4d.PARTICLEOBJECT_STOP] = c4d.BaseTime(500, fps) emitter[c4d.PARTICLEOBJECT_LIFETIME] = c4d.BaseTime(5, fps) ## Animate 500 frames, new position every ten frames, ## transition to next color (cycle red->green->blue) ## First set key frames for color change nextRGB = [1.,0.,0.] redcurve = redtrack.GetCurve() greencurve = greentrack.GetCurve() bluecurve = bluetrack.GetCurve() for segment in range(50) : frame = 50*segment redkey = redcurve.AddKey(c4d.BaseTime(frame, fps))['key'] redkey.SetValue(redcurve, nextRGB[0]) greenkey = greencurve.AddKey(c4d.BaseTime(frame, fps))['key'] greenkey.SetValue(greencurve, nextRGB[1]) bluekey = bluecurve.AddKey(c4d.BaseTime(frame, fps))['key'] bluekey.SetValue(bluecurve, nextRGB[2]) # # rotate RGB values nextRGB.append(nextRGB.pop(0)) ## run animation frame = 0 pos = c4d.Vector(0,0,0) emitter.SetAbsPos(pos) doc.SetTime(c4d.BaseTime(frame, fps)) c4d.CallCommand(12410) for segment in range(50) : frame = 10 * segment mat.Update(True, True) sph.Message(c4d.MSG_UPDATE) for k in range(3) : pos[k] = -30 + 60*random.random() emitter.SetAbsPos(pos) doc.SetTime(c4d.BaseTime(frame, fps)) c4d.CallCommand(12410) # record