Collection of useful python snippets for Cinema 4D…
Conditional Batch Process On Objects
This script cycles through all objects (hierarchies as well) with conditions, makes an array of matching objects and process on them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# Conditional Batch Process on Objects # Python Script Snippet for Cinema 4D by Mehmet Sensoy import c4d from c4d import gui from c4d import documents def GetNextObject(op): if op==None: return None if op.GetDown(): return op.GetDown() while not op.GetNext() and op.GetUp(): op = op.GetUp() return op.GetNext() c4d.EventAdd() def main(): doc = c4d.documents.GetActiveDocument() obj = doc.GetFirstObject() if obj==None: return output = [] while obj and obj!=None: name = obj.GetName() if "Null" in name and obj.GetType() == c4d.Onull : #Example: If it has null name and it's type is a null then... output.append(obj) # Create an Object Array obj = GetNextObject(obj) for obj in output: obj.SetName ("Test") # Change its name to Test... obj.MakeTag(c4d.Texpresso) # Add Xpresso tag for example... # Callcommands are ok as well... c4d.EventAdd() if __name__=='__main__': main() |
Batch Polygon Groups to Object Script
This script will do the same as Polygon Groups to Object command but in batch mode. Select polygon objects and run script. All your objects will be disconnected without preserving polygon groups and placed under an empty null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import c4d from c4d import utils from c4d import documents # Batch Polygon Groups To Object Command Cinema 4D Python Script # Script by villager-and-c4d.cocolog-nifty def GetSelPolygonObject(document): selObjList = [] for objs in document.GetSelection(): if isinstance(objs, c4d.PolygonObject): selObjList.append(objs) return selObjList def main(): objs = GetSelPolygonObject(doc) sets = c4d.BaseContainer() sets[c4d.MDATA_DISCONNECT_PRESERVEGROUPS] = False utils.SendModelingCommand(c4d.MCOMMAND_DISCONNECT, objs, c4d.MODIFY_POLYGONSELECTION, sets, doc) sets = c4d.BaseContainer() utils.SendModelingCommand(c4d.MCOMMAND_EXPLODESEGMENTS, objs, c4d.MODIFY_POLYGONSELECTION, sets, doc) c4d.CallCommand(16388) c4d.CallCommand(1011982) if __name__=='__main__': main() c4d.EventAdd() |
Deleting All Objects with Specific Name
This script cycles through all objects in document and deletes the ones which have predefined string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import c4d from c4d import gui # This script cycles through all objects and deletes the ones with given string # Python Script Snippet for Cinema 4D by Mehmet Sensoy def deletewithname(obj,name): if not obj: return deletewithname(obj.GetDown(),name) deletewithname(obj.GetNext(),name) if not obj.GetDown(): if obj.GetName()== name: if not obj.GetFirstTag(): obj.Remove() def main(): name = "_blank" # String obj = doc.GetFirstObject() deletewithname(obj,name) c4d.EventAdd() if __name__=='__main__': main() |
Assigning All Objects in Document Into an Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import c4d from c4d import Vector def find_objects(op, objectarray): objectarray.append(op) for child in op.GetChildren(): find_objects(child, objectarray) def main(): objects = [] for obj in doc.GetObjects(): find_objects(obj, objects) print len(objects) # Count of Objects print objects[3].GetName() # getting name of your 4th object pos = c4d.Vector(0,100, 0) # Setting Position Value objects[3].SetAbsPos (pos) # make something on your 4th object c4d.EventAdd() main() |
Finding X Sizes of All Objects and Assigning into an Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import c4d from c4d import Vector def find_objects(op, objectarray): objectarray.append(op) for child in op.GetChildren(): find_objects(child, objectarray) def getsize(op,size): size = op.GetRad()*2 position = op.GetMp() + op.GetAbsPos() xsize = int(size.x*2) xsizearray.append((xsize)) def main(): global xsizearray objects = [] size = [] xsizearray = [] for obj in doc.GetObjects(): find_objects(obj, objects) count = len(objects) for i in xrange (count): getsize(objects[i],size) print ("Count of Objects " + str(count) ) print ("Objects' X Size Valuess " + str(xsizearray) ) c4d.EventAdd() main() |
Merge Polygons
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import c4d from c4d.documents import GetActiveDocument #from c4d.gui import GeDialog #from threading import Thread # Version 1.1 def GetNextObject(op): #root only version if not op: return return op.GetNext() def numberofobjects(op): counter = 0 while op: counter += 1 op = GetNextObject(op) return counter def statusbar (counter, secondcounter): # c4d.StatusSetText ('%s - %s Objects are processed.' %(secondcounter, counter)) c4d.StatusSetBar(100*secondcounter/counter) #statusbar def selchildren(obj,next): # Scan obj hierarchy and select children while obj and obj != next: doc.AddUndo(c4d.UNDOTYPE_CHANGE_SMALL,obj) obj.SetBit(c4d.BIT_ACTIVE) selchildren(obj.GetDown(),next) obj = obj.GetNext() c4d.EventAdd() return True def main(undo = True): now = c4d.GeGetTimer() # start stopwatch c4d.CallCommand(13957) # Konsole löschen # c4d.CallCommand(12305) # Konsole... if GetActiveDocument(): doc = GetActiveDocument() if doc.GetFirstObject(): c4d.StatusSetSpin() op = doc.GetFirstObject() myobject = op counter = numberofobjects(op) secondcounter = 0 c4d.StatusSetText ('%s Objects are processed.' %(counter)) opactive = None else: print "Error: No Objects" return False else: print "Error: No Document" return False #doc.StartUndo() # Start undo support # iterate over all objects in the document c4d.CallCommand(12113) # Alles deselektieren while op: optemp = op op = GetNextObject(op) # get next object secondcounter += 1 statusbar(counter, secondcounter) if optemp.CheckType(c4d.Opolygon): optemp.SetBit(c4d.BIT_ACTIVE) if selchildren(optemp,optemp.GetNext()): c4d.CallCommand(16768) #Connect And Delete c4d.CallCommand(16768) #Connect And Delete opactive = doc.GetActiveObject() opactive.DelBit(c4d.BIT_ACTIVE) optemp.DelBit(c4d.BIT_ACTIVE) c4d.StatusClear() #doc.EndUndo() # Do not forget to close the undo support c4d.EventAdd() # update cinema 4d print 'END OF SCRIPT %s ms ' %(c4d.GeGetTimer() - now) return True main() |
Make Editable Command as Python Script
You may use these syntax to insert objects to the object manager:
- BaseDocument.InsertObject ()
- GeListNode.InsertUnder ()
- GeListNode.InsertAfter ()
- GeListNode.InsertBefore ()
There are same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import c4d from c4d import utils def main(): if not isinstance(op, c4d.BaseObject): return pre_obj = op.GetPred() up_obj = op.GetUp() bc = c4d.BaseContainer() doc.StartUndo() objs = utils.SendModelingCommand(c4d.MCOMMAND_MAKEEDITABLE, [op], c4d.MODELINGCOMMANDMODE_ALL, bc, doc, c4d.MODELINGCOMMANDFLAGS_CREATEUNDO) if not objs:return if pre_obj: objs[0].InsertAfter(pre_obj) elif up_obj: objs[0].InsertUnder(up_obj) else:doc.InsertObject(objs[0]) doc.AddUndo(c4d.UNDOTYPE_NEW, objs[0]) doc.EndUndo() if __name__=='__main__': main() c4d.EventAdd() |
Spline Tangent Equalization Using Python Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import c4d from c4d import utils def main(): if not isinstance(op, c4d.SplineObject):return bc = c4d.BaseContainer() doc.StartUndo() utils.SendModelingCommand(c4d.MCOMMAND_SPLINE_EQUALDIRECTION, [op], c4d.MODELINGCOMMANDMODE_POINTSELECTION, bc, doc, c4d.MODELINGCOMMANDFLAGS_CREATEUNDO) doc.EndUndo() if __name__=='__main__': main() c4d.EventAdd() |
Changing Spline Point Type as Linear,Cubic, Akima and others…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import c4d from c4d import utils def main(): if not isinstance(op, c4d.SplineObject):return bc = c4d.BaseContainer() bc.SetData(c4d.MDATA_SPLINEROUND_POINTS, 30) bc.SetData(c4d.MDATA_SPLINEROUND_TYPE, c4d.MDATA_SPLINEROUND_TYPE_LINEAR) # Linear : c4d.MDATA_SPLINEROUND_TYPE_LINEAR # Cubic : c4d.MDATA_SPLINEROUND_TYPE_CUBIC # Akima : c4d.MDATA_SPLINEROUND_TYPE_AKIMA # B-Spline : c4d.MDATA_SPLINEROUND_TYPE_BSPLINE # Bezier : c4d.MDATA_SPLINEROUND_TYPE_BEZIER doc.StartUndo() utils.SendModelingCommand(c4d.ID_MODELING_SPLINE_ROUND_TOOL, [op], c4d.MODELINGCOMMANDMODE_ALL, bc, doc, c4d.MODELINGCOMMANDFLAGS_CREATEUNDO) doc.EndUndo() if __name__=='__main__': main() c4d.EventAdd() |
Get Global Coordinates and Point IDs with Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import c4d def main(): if not isinstance(op, c4d.PointObject):return mg = op.GetMg() pts = op.GetPointS() for id in xrange(op.GetPointCount()): if pts.IsSelected(id): print 'point', id print 'Global 1:', op.GetPoint(id) print 'Global 2:', op.GetPoint(id) * mg print 'Global 3:', mg * op.GetPoint(id) if __name__=='__main__': main() |
Leave a Reply
Want to join the discussion?Feel free to contribute!