Advertisement
KnickKnack

OffsetYSpline

Nov 24th, 2016
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.42 KB | None | 0 0
  1. """Offset-Y Spline
  2. Takes a child-spline as input and offset all its points on the y-axis by a specific value. Tangents are unaffected.
  3.  
  4. Usage Instructions
  5. ------------------
  6. 1. Save in a file called OffsetYSpline.pyp
  7. 2. Locate it in the plugin folder and create the resources needed to have the offset value displayed as a parameter
  8.   with ID 1000 in the Attribute Manager
  9. 3. Start Cinema
  10. 4. Create a generating spline
  11. 5. From the Plugin menu, select OffsetYSpline
  12. 6. Set the generating spline as input child of the OffsetySpline
  13. """
  14.  
  15.  
  16. # =====================================================================================================================#
  17. #   Imports
  18. # =====================================================================================================================#
  19. import c4d
  20.  
  21.  
  22. # =====================================================================================================================#
  23. #   Class Definitions
  24. # =====================================================================================================================#
  25. def OffsetSpline(childSpline, offsetValue):
  26.     # retrieve child points count and type
  27.     pointCnt = childSpline.GetPointCount()
  28.     splineType = childSpline.GetInterpolationType()
  29.  
  30.     # allocate the result
  31.     result = c4d.SplineObject(pointCnt, splineType)
  32.  
  33.     if result is None:
  34.         return None
  35.  
  36.     # set the points position and tangency data
  37.     for i in range(pointCnt):
  38.         currPos = childSpline.GetPoint(i)
  39.         #modify the y-value to make some change to the resulting spline
  40.         result.SetPoint(i, c4d.Vector(currPos.x,currPos.y+offsetValue, currPos.z))
  41.         result.SetTangent(i, childSpline.GetTangent(i)["vl"], childSpline.GetTangent(i)["vr"])
  42.  
  43.     # transfer the matrix information
  44.     result.SetMl(childSpline.GetMl())
  45.  
  46.     # retrieve the spline object
  47.     childRealSpline = childSpline.GetRealSpline()
  48.  
  49.     if childRealSpline:
  50.         #retrieve the real spline BaseContainer and set the result spline parameters accordingly to those found in
  51.         childRealSplineBC = childRealSpline.GetDataInstance()
  52.         result.SetParameter(c4d.SPLINEOBJECT_INTERPOLATION, childRealSplineBC.GetInt32(c4d.SPLINEOBJECT_INTERPOLATION), c4d.DESCFLAGS_SET_FORCESET)
  53.         result.SetParameter(c4d.SPLINEOBJECT_MAXIMUMLENGTH, childRealSplineBC.GetFloat(c4d.SPLINEOBJECT_MAXIMUMLENGTH), c4d.DESCFLAGS_SET_FORCESET)
  54.         result.SetParameter(c4d.SPLINEOBJECT_SUB, childRealSplineBC.GetInt32(c4d.SPLINEOBJECT_SUB), c4d.DESCFLAGS_SET_FORCESET)
  55.         result.SetParameter(c4d.SPLINEOBJECT_ANGLE, childRealSplineBC.GetFloat(c4d.SPLINEOBJECT_ANGLE), c4d.DESCFLAGS_SET_FORCESET)
  56.  
  57.     # return the computed spline
  58.     return result
  59.  
  60.  
  61. class OffsetYSpline(c4d.plugins.ObjectData):
  62.     PLUGIN_ID = 1038342  # TODO: Replace with your own ID from PluginCafe.com
  63.  
  64.     def __init__(self):
  65.         self.countourChildDirty = 0
  66.  
  67.  
  68.     def Init(self, op):
  69.         bc = op.GetDataInstance()
  70.         if bc is None:
  71.             return False
  72.  
  73.         bc.SetInt32(1000, 100)
  74.  
  75.         return True
  76.  
  77.     def CheckDirty(self, op, doc):
  78.         # override the CheckDirty to use it on GetCountour
  79.         childDirty = 0
  80.  
  81.         child = op.GetDown()
  82.  
  83.         if child:
  84.             childDirty = child.GetDirty(c4d.DIRTYFLAGS_DATA | c4d.DIRTYFLAGS_MATRIX)
  85.  
  86.         if (childDirty != self.countourChildDirty):
  87.             op.SetDirty(c4d.DIRTYFLAGS_DATA)
  88.  
  89.         self.countourChildDirty = childDirty;
  90.  
  91.  
  92.     def GetVirtualObject(self, op, hh):
  93.         # check the passed parameters
  94.         if op is None or hh is None:
  95.             return None
  96.  
  97.         # check the cache and allocate other data variables
  98.         cache = op.GetCache()
  99.         child = None
  100.         childSpline = None
  101.         sop = None
  102.  
  103.         # retrieve the current document
  104.         doc = None
  105.         if hh is None:
  106.             doc = op.GetDocument
  107.         else:
  108.             doc = hh.GetDocument
  109.  
  110.         # retrieve the generator BaseContainer
  111.         bc = op.GetDataInstance()
  112.         offsetValue = 0
  113.         if bc:
  114.             offsetValue = bc.GetInt32(1000)
  115.  
  116.         # define dirty variables    
  117.         dirty = False
  118.         cloneDirty = False
  119.  
  120.  
  121.         child = op.GetDown()
  122.  
  123.         #
  124.         if child:
  125.             # check the dirtyness of the child
  126.             temp = op.GetHierarchyClone(hh, child, c4d.HIERARCHYCLONEFLAGS_ASSPLINE, cloneDirty, trans)
  127.  
  128.             # if it's dirty then assign temp the object returned from GetHierarchyClone
  129.             if temp is None:
  130.                 temp = op.GetHierarchyClone(hh, child, c4d.HIERARCHYCLONEFLAGS_ASSPLINE, None, trans)
  131.  
  132.             # sum up the clone flag
  133.             dirty |= cloneDirty
  134.  
  135.             # assign temp object to childSpline
  136.             # note: in C++ this assignment should explicitly cast from BaseObject to SplineObject since
  137.             # childSpline is a SplineObject
  138.             if (temp and (temp.IsInstanceOf(c4d.Ospline) or (temp.GetInfo()&c4d.OBJECT_ISSPLINE) or temp.IsInstanceOf(c4d.Oline))):
  139.                 childSpline = temp
  140.  
  141.         # Touch the child to flag
  142.         child.Touch()
  143.  
  144.         # check the dirtyness of the data in the Attribute Manager
  145.         dirty |= op.IsDirty(c4d.DIRTYFLAGS_DATA)
  146.  
  147.         # return the cache data if not dirty and cache exists
  148.         if (not dirty and cache):
  149.             return cache
  150.  
  151.         # check for childSpline
  152.         if (childSpline is None):
  153.             return None
  154.  
  155.         # compute the resulting spline using a generic function - in this case a simple one changing the value of the points on y-axis
  156.         sop = OffsetSpline(childSpline, offsetValue)
  157.         if sop is None:
  158.             return None
  159.  
  160.         # copy tags from child to resulting spline
  161.         childSpline.CopyTagsTo(sop, True, c4d.NOTOK, c4d.NOTOK)
  162.  
  163.         # advice about the update occurred
  164.         sop.Message(c4d.MSG_UPDATE)
  165.  
  166.         return sop
  167.  
  168.  
  169.     def GetContour(self, op, doc, lod, bt):
  170.         # check passed parameters
  171.         if op is None:
  172.             return None
  173.  
  174.         # retrieve the current active document
  175.         if doc is None:
  176.             doc = op.GetDocument()
  177.  
  178.         if doc is None:
  179.             return None
  180.  
  181.         # allocate other data variables
  182.         child = None
  183.         childSpline = None
  184.         sop = None
  185.  
  186.         # retrieve the generator BaseContainer
  187.         bc = op.GetDataInstance()
  188.         offsetValue = 0
  189.         if bc:
  190.             offsetValue = bc.GetInt32(1000)
  191.  
  192.         child = op.GetDown()
  193.  
  194.         if child:
  195.             temp = child
  196.  
  197.             # a basic emulation of the GetHierarchyClone in order to retrieve the splines as required
  198.             if (temp and not temp.IsInstanceOf(c4d.Ospline) and not temp.IsInstanceOf(c4d.Oline) and ((temp.GetInfo()&c4d.OBJECT_ISSPLINE) or temp.GetType() == c4d.Oinstance)):
  199.                 temp = temp.GetClone(c4d.COPYFLAGS_NO_ANIMATION, None)
  200.  
  201.                 if temp is None:
  202.                     return None
  203.  
  204.             # assign temp object to childSpline
  205.             # note: in C++ this assignment should explicitly cast from BaseObject to SplineObject since
  206.             # childSpline is a SplineObject
  207.             if (temp and (temp.IsInstanceOf(c4d.Ospline) or (temp.GetInfo()&c4d.OBJECT_ISSPLINE) or temp.IsInstanceOf(c4d.Oline))):
  208.                 childSpline = temp
  209.        
  210.         if childSpline is None:
  211.             return None
  212.  
  213.         # compute the resulting spline using a generic function - in this case a simple one changing the value of the points on y-axis
  214.         sop = OffsetSpline(childSpline, offsetValue)
  215.         if sop is None:
  216.             return None
  217.  
  218.         # advice about the update occurred
  219.         sop.Message(c4d.MSG_UPDATE)
  220.  
  221.         return sop
  222.  
  223. # =====================================================================================================================#
  224. #   Plugin registration
  225. # =====================================================================================================================#
  226.  
  227. if __name__ == "__main__":
  228.     c4d.plugins.RegisterObjectPlugin(id=OffsetYSpline.PLUGIN_ID,
  229.                                      str="OffsetYSpline",
  230.                                      g=OffsetYSpline,
  231.                                      description="Omyresource",
  232.                                      icon=None,
  233.                                      info=c4d.OBJECT_GENERATOR | c4d.OBJECT_ISSPLINE | c4d.OBJECT_INPUT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement