In this short tutorial we’ll have a look at how to center axis of an object in Cinema 4D with using python…
XPRESSO PYTHON NODE SCRIPT
Create a point object. (In this tutorial we had used text and converted it to an editable spline.)
Create a null object and add a xpresso tag. Double-click on xpresso tag to open xpresso editor.
Add a python node.
Paste the script below inside python node. Change this line to define your object which you want to center axis : obj = doc.SearchObject(“WriteYourObject’sNameHere“)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import c4d from c4d import Vector from c4d import Document def main(): obj = doc.SearchObject("Text") point=obj.GetAllPoints() pcount=obj.GetPointCount() mg=obj.GetMg() axi=obj.GetAbsPos() cen=obj.GetMp() diff=axi-(axi+cen) if diff!=Vector(0): for i in xrange(pcount): obj.SetPoint(i,point[i]+diff) obj.Message(c4d.MSG_UPDATE) obj.SetMg(c4d.Matrix((mg*cen),mg.v1,mg.v2,mg.v3 )) obj.SetAbsPos(Vector(0)) c4d.EventAdd() |
And when you execute your object’s axis will be centered.
PYTHON SCRIPT
If you would like to use it as a user script I have prepared another version which centers selected object’s axis.
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 |
import c4d from c4d import Vector from c4d import documents from c4d import gui def main(): op = doc.GetActiveObject() selected = doc.GetActiveObjects(0) if len(selected) == 0: # If there is no selected object... print "No Object Selected" c4d.gui.MessageDialog("Please Select an Object") return point=op.GetAllPoints() pcount=op.GetPointCount() mg=op.GetMg() axi=op.GetAbsPos() cen=op.GetMp() diff=axi-(axi+cen) if diff!=Vector(0): for i in xrange(pcount): op.SetPoint(i,point[i]+diff) op.Message(c4d.MSG_UPDATE) op.SetMg(c4d.Matrix((mg*cen),mg.v1,mg.v2,mg.v3 )) op.SetAbsPos(Vector(0)) c4d.EventAdd() if __name__ == "__main__": main() |
For future use you may save it with a *.py extension and move script file to Cinema 4D > Library > Scripts folder.
Then you can easily create a shortcut from Window > Customization > Customize Commands > YourScriptName.py and you can drag&drop your script’s icon to anywhere in your layout.
UPDATE: CENTER AXIS OF ALL SELECTED OBJECTS
This script basically set all selected objects axis to center.
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 |
import c4d from c4d import Vector from c4d import documents from c4d import gui def main(): op = doc.GetActiveObject() selected = doc.GetActiveObjects(0) if len(selected) == 0: # we have one or more selected objects in the scene print "No Object Selected" c4d.gui.MessageDialog("Please Select an Object") return else: count = len(selected) for a in xrange(count): point=selected[a].GetAllPoints() pcount=selected[a].GetPointCount() mg=selected[a].GetMg() axi=selected[a].GetAbsPos() cen=selected[a].GetMp() diff=axi-(axi+cen) if diff!=Vector(0): for i in xrange(pcount): selected[a].SetPoint(i,point[i]+diff) selected[a].Message(c4d.MSG_UPDATE) selected[a].SetMg(c4d.Matrix((mg*cen),mg.v1,mg.v2,mg.v3 )) selected[a].SetAbsPos(Vector(0)) c4d.EventAdd() if __name__ == "__main__": main() |
Leave a Reply
Want to join the discussion?Feel free to contribute!