PolygonReduction Manual¶
About¶
The c4d.utils.PolygonReduction
class allows to reduce the polygon count of a given c4d.PolygonObject
while retaining its overall shape. The class gives access to the functionality used within the “Polygon Reduction” generator.
Create¶
c4d.utils.PolygonReduction
objects are created with PolygonReduction.__init__()
/ ()
.
"""
Copyright: MAXON Computer GmbH
Author: Yannick Puech
Description:
- Creates a new PolygonReduction object.
Class/method highlighted:
- c4d.utils.PolygonReduction
Compatible:
- Win / Mac
- R19, R20, R21, S22, R23
"""
import c4d
polyReduction = c4d.utils.PolygonReduction()
Init¶
A c4d.utils.PolygonReduction
object has to be initialized. It will pre-process the given c4d.PolygonObject
.
PolygonReduction.PreProcess()
: Pre-processes thec4d.PolygonObject
defined in the given data argument.
The data dictionary argument has these keys:
_doc:
BaseDocument
: The document._op:
c4d.PolygonObject
: The polygon object to be reduced._thread:
c4d.threading.BaseThread
: The caller thread. Set to None if synchronous pre-processing calculation is needed (e.g. during rendering)._settings:
c4d.BaseContainer
: The reduction constraints settings. See opolyreduxgen.h
Note
If it’s possible to abort the reduction operation and to retain the original mesh, it is advised to operate on a copy of the original c4d.PolygonObject
.
"""
Copyright: MAXON Computer GmbH
Author: Yannick Puech
Description:
- Configures the given PolygonReduction object.
- Reduces the given PolygonObject to 25%.
Class/method highlighted:
- c4d.utils.PolygonReduction
Compatible:
- Win / Mac
- R19, R20, R21, S22, R23
"""
import c4d
def main():
# Checks if selected object is valid
if op is None:
raise ValueError("op is none, please select one object.")
# Check if it's a polygon object
if not op.IsInstanceOf(c4d.Opolygon):
raise TypeError("Selected object is not a polygon Object.")
# Defines settings for PolygonReduction.PreProcess()
settings = c4d.BaseContainer()
settings[c4d.POLYREDUXOBJECT_PRESERVE_3D_BOUNDARY] = True
settings[c4d.POLYREDUXOBJECT_PRESERVE_UV_BOUNDARY] = True
# Defines data for PolygonReduction.PreProcess()
data = dict()
data['_op'] = op
data['_doc'] = doc
data['_settings'] = settings
data['_thread'] = None
# Creates PolygonReduction object
polyReduction = c4d.utils.PolygonReduction()
if polyReduction is None:
raise RuntimeError("Failed to create the PolygonReduction.")
# Pre-process the data
if not polyReduction.PreProcess(data):
raise RuntimeError("Failed to Pre-Process the PolygonReduction with data.")
# Runs the reduce process of the polygon object
polyReduction.SetReductionStrengthLevel(0.75)
# Updates original PolygonObject
op.Message(c4d.MSG_UPDATE)
# Pushes an update event to Cinema 4D
c4d.EventAdd()
if __name__ == '__main__':
main()
If the _thread key is set the pre-process is running asynchronously in a background thread.
PolygonReduction.IsPreprocessing()
: Returns True if preprocessing is running.PolygonReduction.StopPreprocessing()
: Aborts preprocessing.PolygonReduction.Reset()
: Aborts preprocessing and frees all temporary data.
Note
This asynchronous mode may be used e.g. in a generator object.
After the pre-process the c4d.utils.PolygonReduction
object is ready:
PolygonReduction.IsValid()
: Returns True if a valid object and a valid document are associated with the object.PolygonReduction.GetData()
: Returns the dictionary data associated with the object.
Reduce¶
The linked c4d.PolygonObject
can be reduced by defining the overall reduction strength or the target triangle, vertex or edge count.
PolygonReduction.GetMaxReductionStrengthLevel()
: Returns the maximum reduction strength (1.0)PolygonReduction.SetReductionStrengthLevel()
: Sets the overall reduction strength.PolygonReduction.GetReductionStrengthLevel()
: Returns the current overall reduction strength.
The desired triangle count is defined with:
PolygonReduction.SetTriangleLevel()
: Sets the desired triangle count.
Note
The desired triangle count must be between the Max and Min values retrieve with:
PolygonReduction.GetMaxTriangleLevel()
: Returns the maximum number of triangles possible.PolygonReduction.GetMinTriangleLevel()
: Returns the minimum number of triangles possible.
The actual triangle count can be retrieve with:
PolygonReduction.GetTriangleLevel()
: Returns the actual current triangle count.
"""
Copyright: MAXON Computer GmbH
Author: Yannick Puech
Description:
- Reduces the active PolygonObject to the given triangle count.
Class/method highlighted:
- c4d.utils.PolygonReduction
Compatible:
- Win / Mac
- R19, R20, R21, S22, R23
"""
import c4d
def main():
# Checks if selected object is valid
if op is None:
raise ValueError("op is none, please select one object.")
# Check if it's a polygon object
if not op.IsInstanceOf(c4d.Opolygon):
raise TypeError("Selected object is not a polygon Object.")
# Defines settings for PolygonReduction.PreProcess()
settings = c4d.BaseContainer()
settings[c4d.POLYREDUXOBJECT_PRESERVE_3D_BOUNDARY] = True
settings[c4d.POLYREDUXOBJECT_PRESERVE_UV_BOUNDARY] = True
# Defines data for PolygonReduction.PreProcess()
data = dict()
data['_op'] = op
data['_doc'] = doc
data['_settings'] = settings
data['_thread'] = None
# Creates PolygonReduction object
polyReduction = c4d.utils.PolygonReduction()
if polyReduction is None:
raise RuntimeError("Failed to create the PolygonReduction.")
# Pre-process the data
if not polyReduction.PreProcess(data):
raise RuntimeError("Failed to Pre-Process the PolygonReduction with data.")
# Asks for number of triangle level
while True:
# Opens a Dialog where user can enter a text
userInput = c4d.gui.InputDialog("Enter number of triangle level:")
# Checks if operation was cancelled
if userInput == "":
return
# Tries to convert to integer
try:
triangleLevel = int(userInput)
break
except ValueError:
c4d.gui.MessageDialog("Please enter a number.")
# Checks entered number of triangle level is valid
if not polyReduction.GetMinTriangleLevel() < triangleLevel < polyReduction.GetMaxTriangleLevel():
raise RuntimeError("Entered value is not valid.")
# Sets the triangle level
polyReduction.SetTriangleLevel(triangleLevel)
# Gets triangle level count after reduction
realTriangleResult = polyReduction.GetTriangleLevel()
print("Triangle Result: {0}".format(realTriangleResult))
# Updates original PolygonObject
op.Message(c4d.MSG_UPDATE)
# Pushes an update event to Cinema 4D
c4d.EventAdd()
if __name__ == '__main__':
main()
The vertex count is defined with:
PolygonReduction.SetVertexLevel()
: Sets the desired vertex count.
Note
The desired vertex count must be between the Max and Min values retrieve with:
PolygonReduction.GetMaxVertexLevel()
: Returns the maximum number of vertices possible.PolygonReduction.GetMinVertexLevel()
: Returns the minimum number of vertices possible.
The actual vertex count can be retrieve with:
PolygonReduction.GetVertexLevel()
: Returns the actual current vertex count.
"""
Copyright: MAXON Computer GmbH
Author: Yannick Puech
Description:
- Reduces the active PolygonObject to the given vertex count.
Class/method highlighted:
- c4d.utils.PolygonReduction
Compatible:
- Win / Mac
- R19, R20, R21, S22, R23
"""
import c4d
def main():
# Checks if selected object is valid
if op is None:
raise ValueError("op is none, please select one object.")
# Check if it's a polygon object
if not op.IsInstanceOf(c4d.Opolygon):
raise TypeError("Selected object is not a polygon Object.")
# Defines settings for PolygonReduction.PreProcess()
settings = c4d.BaseContainer()
settings[c4d.POLYREDUXOBJECT_PRESERVE_3D_BOUNDARY] = True
settings[c4d.POLYREDUXOBJECT_PRESERVE_UV_BOUNDARY] = True
# Defines data for PolygonReduction.PreProcess()
data = dict()
data['_op'] = op
data['_doc'] = doc
data['_settings'] = settings
data['_thread'] = None
# Creates PolygonReduction object
polyReduction = c4d.utils.PolygonReduction()
if polyReduction is None:
raise RuntimeError("Failed to create the PolygonReduction.")
# Pre-process the data
if not polyReduction.PreProcess(data):
raise RuntimeError("Failed to Pre-Process the PolygonReduction with data.")
# Asks for number of vertex level
while True:
# Opens a Dialog where user can enter a text
userInput = c4d.gui.InputDialog("Enter number of vertex level:")
# Checks if operation was cancelled
if userInput == "":
return
# Tries to convert to integer
try:
vertexLevel = int(userInput)
break
except ValueError:
c4d.gui.MessageDialog("Please enter a number.")
# Checks entered number of vertex level is valid
if not polyReduction.GetMinVertexLevel() < vertexLevel < polyReduction.GetMaxVertexLevel():
raise RuntimeError("Entered value is not valid.")
# Sets the vertex level
polyReduction.SetVertexLevel(vertexLevel)
# Gets vertex level count after reduction
realVertexResult = polyReduction.GetVertexLevel()
print("Vertex Result: {0}".format(realVertexResult))
# Updates original PolygonObject
op.Message(c4d.MSG_UPDATE)
# Pushes an update event to Cinema 4D
c4d.EventAdd()
if __name__ == '__main__':
main()
Finally the edge count can be defined with:
PolygonReduction.SetRemainingEdgesLevel()
: Sets the desired edge count.
Note
The desired edge count must be less than the Max value retrieve with:
PolygonReduction.GetMaxRemainingEdgesLevel()
: Returns the number of remaining edges.
The actual edge count can be retrieve with:
PolygonReduction.GetRemainingEdgesLevel()
: Returns the actual edge count.
"""
Copyright: MAXON Computer GmbH
Author: Yannick Puech
Description:
- Reduces the active PolygonObject to the given edge count.
Class/method highlighted:
- c4d.utils.PolygonReduction
Compatible:
- Win / Mac
- R19, R20, R21, S22, R23
"""
import c4d
def main():
# Checks if selected object is valid
if op is None:
raise ValueError("op is none, please select one object.")
# Check if it's a polygon object
if not op.IsInstanceOf(c4d.Opolygon):
raise TypeError("Selected object is not a polygon Object.")
# Defines settings for PolygonReduction.PreProcess()
settings = c4d.BaseContainer()
settings[c4d.POLYREDUXOBJECT_PRESERVE_3D_BOUNDARY] = True
settings[c4d.POLYREDUXOBJECT_PRESERVE_UV_BOUNDARY] = True
# Defines data for PolygonReduction.PreProcess()
data = dict()
data['_op'] = op
data['_doc'] = doc
data['_settings'] = settings
data['_thread'] = None
# Creates PolygonReduction object
polyReduction = c4d.utils.PolygonReduction()
if polyReduction is None:
raise RuntimeError("Failed to create the PolygonReduction.")
# Pre-process the data
if not polyReduction.PreProcess(data):
raise RuntimeError("Failed to Pre-Process the PolygonReduction with data.")
# Asks for number of edges level
while True:
# Opens a Dialog where user can enter a text
userInput = c4d.gui.InputDialog("Enter number of edges level:")
# Checks if operation was cancelled
if userInput == "":
return
# Tries to convert to integer
try:
edgesLevel = int(userInput)
break
except ValueError:
c4d.gui.MessageDialog("Please enter a number.")
# Sets edges level number
polyReduction.SetRemainingEdgesLevel(min(edgesLevel, polyReduction.GetMaxRemainingEdgesLevel()))
polyReduction.SetRemainingEdgesLevel(min(edgesLevel, polyReduction.GetMaxRemainingEdgesLevel()))
# Retrieves edges level count after reduction
realEdgeResult = polyReduction.GetRemainingEdgesLevel()
print("Edge Result: {0}".format(realEdgeResult))
# Updates the original PolygonObject
op.Message(c4d.MSG_UPDATE)
# Pushes an update event to Cinema 4D
c4d.EventAdd()
if __name__ == '__main__':
main()