c4d.utils.ViewportSelect

This class is used to extract information from a viewport about which objects and object elements are visible at which pixels. It is used internally for stuff like highlighting and modeling.

Members

ViewportSelect.__init__()
Return type:c4d.utils.ViewportSelect
Returns:A new viewport select object.
ViewportSelect.Init(w, h, bd, ops, mode, onlyvisible, flags)

Initializes the viewport information for multiple objects. This has to be done before the pixel information can be retrieved. You have to retrieve the width and height of the viewport manually:

frame = bd.GetFrame()
left = frame["cl"]
right = frame["cr"]
top = frame["ct"]
bottom = frame["cb"]

width = right - left + 1
height = bottom - top +1

viewportSelect.Init(width, height, bd, [op], c4d.Mpolyedgepoint, True, c4d.VIEWPORTSELECTFLAGS_IGNORE_HIDDEN_SEL)
Parameters:
  • w (int) – Width of the viewport in pixels.
  • h (int) – Height of the viewport in pixels.
  • bd (c4d.BaseDraw) – The viewport base draw.
  • ops (list of BaseObject) – A list with objects to get the information for.
  • mode (int) –

    Editor mode:

    Mpoints Point edit mode.
    Medges Edge edit mode.
    Mpolygons Polygon edit mode.
    Mpolyedgepoint Combined poly/edge/point mode. Must only be used in ViewportSelect.
    Medgepoint Combined edge/point mode. Must only be used in ViewportSelect.
  • onlyvisible (bool) – If this is True only visible elements are included.
  • flags (int) –

    Flags:

    VIEWPORTSELECTFLAGS_NONE None.
    VIEWPORTSELECTFLAGS_USE_HN Use HyperNURBS.
    VIEWPORTSELECTFLAGS_USE_DEFORMERS Use deformers.
    VIEWPORTSELECTFLAGS_REGION_SELECT A rectangle, polygon and lasso selection counts the number of vertices of a polygon within its area. Usually hidden vertices are not taken into account. This flag allows hidden vertices to be taken into account, otherwise it would not be possible to select polygons if for example all points are hidden.
    VIEWPORTSELECTFLAGS_IGNORE_HIDDEN_SEL Ignore hidden selection.
    VIEWPORTSELECTFLAGS_USE_DISPLAY_MODE If this flag is set and the display mode is Wireframe or Isoparms only edges and vertices are written into the ViewportSelect buffer. The ViewportSelect mode must be Mpolyedgepoint.
    VIEWPORTSELECTFLAGS_FORCE_USE_DEFORMERS Use deformers even when the viewport DISPLAYEDITSTATE_DEFORM is disabled.
ViewportSelect.GetPixelInfoPoint(x, y)

Retrieves point information at the pixel position (x, y):

viewportSelect = utils.ViewportSelect()
infopoint = viewportSelect.GetPixelInfoPoint(x, y)
if not infopoint:
  return

i = infopoint["i"] #int
op = infopoint["op"] #c4d.BaseObject
z = infopoint["z"] #float
Parameters:
  • x (int) – An X coordinate within the viewport. Must be 0 <= x < w, where w is the width given to Init().
  • y (int) – An Y coordinate within the viewport. Must be 0 <= y < h, where h is the height given to Init().
Return type:

dict{i: int, op: BaseObject, z: float}

i int Element index. The index depends on the element type:  
    point point index
    polygon polygon index
    edge 4*polygon + edge index
    spline point segment offset + point index
op BaseObject Object.  
z float Z coordinate of the current element.  

Returns:

The retrieved pixel information or None if no information could be retrieved.

ViewportSelect.GetPixelInfoPolygon(x, y)

Retrieves polygon information at the pixel position (x, y):

infopolygon = viewportSelect.GetPixelInfoPolygon(x, y)
if not infopolygon:
  return

i = infopolygon["i"] #int
op = infopolygon["op"] #c4d.BaseObject
z = infopolygon["z"] #float
Parameters:
  • x (int) – An X coordinate within the viewport. Must be 0 <= x < w, where w is the width given to Init().
  • y (int) – An Y coordinate within the viewport. Must be 0 <= y < h, where h is the height given to Init().
Return type:

dict{i: int, op: BaseObject, z: float}

i int Element index. The index depends on the element type:  
    point point index
    polygon polygon index
    edge 4*polygon + edge index
    spline point segment offset + point index
op BaseObject Object.  
z float Z coordinate of the current element.  

Returns:

The retrieved pixel information or None if no information could be retrieved.

ViewportSelect.GetPixelInfoEdge(x, y)

Retrieves edge information at the pixel position (x, y):

infopoint = viewportSelect.GetPixelInfoPoint(x, y)
if not infopoint:
  return

i = infopoint["i"] #int
op = infopoint["op"] #c4dBaseObject
z = infopoint["z"] #float
Parameters:
  • x (int) – An X coordinate within the viewport. Must be 0 <= x < w, where w is the width given to Init().
  • y (int) – An Y coordinate within the viewport. Must be 0 <= y < h, where h is the height given to Init().
Return type:

dict{i: int, op: BaseObject, z: float}

i int Element index. The index depends on the element type:  
    point point index
    polygon polygon index
    edge 4*polygon + edge index
    spline point segment offset + point index
op BaseObject Object.  
z float Z coordinate of the current element.  

Returns:

The retrieved pixel information or None if no information could be retrieved.

ViewportSelect.GetNearestPoint(op, x, y, maxrad=MAXLONGl, onlyselected=False, ignorelist=None, ignorecnt=0)

Retrieves nearest point information at the pixel position (x, y).

Parameters:
  • op (c4d.BaseObject) – The object to search for the closest element.
  • x (int) – An X coordinate within the viewport. Must be 0 <= x < w, where w is the width given to Init(). If an element was found the reference is updated to reflect the X coordinate of the nearest point.
  • y (int) – An Y coordinate within the viewport. Must be 0 <= y < h, where h is the height given to Init(). If an element was found the reference is updated to reflect the Y coordinate of the nearest point.
  • maxrad (int) – A maximal radius for the search in pixels.
  • onlyselected (bool) – If this is True only selected elements are included in the search.
  • ignorelist (list) – List of points to ignore.
  • ignorecnt (int) – Ignore list count.
Return type:

dict{i: int, op: BaseObject, z: float, x: float, y: float}

i int Element index. The index depends on the element type:  
    point point index
    polygon polygon index
    edge 4*polygon + edge index
    spline point segment offset + point index
op BaseObject Object.  
z float Z coordinate of the current element.  
x float X pixel position.  
y float Y pixel position.  

Returns:

The retrieved pixel information or None if no information could be retrieved.

ViewportSelect.GetNearestPolygon(op, x, y, maxrad=MAXLONGl, onlyselected=False, ignorelist=None, ignorecnt=0)

Retrieves nearest polygon information at the pixel position (x, y).

Parameters:
  • op (c4d.BaseObject) – The object to search for the closest element.
  • x (int) – An X coordinate within the viewport. Must be 0 <= x < w, where w is the width given to Init(). If an element was found the reference is updated to reflect the X coordinate of the nearest point.
  • y (int) – An Y coordinate within the viewport. Must be 0 <= y < h, where h is the height given to Init(). If an element was found the reference is updated to reflect the Y coordinate of the nearest point.
  • maxrad (int) – A maximal radius for the search in pixels.
  • onlyselected (bool) – If this is True only selected elements are included in the search.
  • ignorelist (list) – List of points to ignore.
  • ignorecnt (int) – Ignore list count.
Return type:

dict{i: int, op: BaseObject, z: float, x: float, y: float}

i int Element index. The index depends on the element type:  
    point point index
    polygon polygon index
    edge 4*polygon + edge index
    spline point segment offset + point index
op BaseObject Object.  
z float Z coordinate of the current element.  
x float X pixel position.  
y float Y pixel position.  

Returns:

The retrieved pixel information or None if no information could be retrieved.

ViewportSelect.GetNearestEdge(op, x, y, maxrad=MAXLONGl, onlyselected=False, ignorelist=None, ignorecnt=0)

Retrieves nearest edge information at the pixel position (x, y).

Parameters:
  • op (c4d.BaseObject) – The object to search for the closest element.
  • x (int) – An X coordinate within the viewport. Must be 0 <= x < w, where w is the width given to Init(). If an element was found the reference is updated to reflect the X coordinate of the nearest point.
  • y (int) – An Y coordinate within the viewport. Must be 0 <= y < h, where y is the width given to Init(). If an element was found the reference is updated to reflect the Y coordinate of the nearest point.
  • maxrad (int) – A maximal radius for the search in pixels.
  • onlyselected (bool) – If this is True only selected elements are included in the search.
  • ignorelist (list) – List of points to ignore.
  • ignorecnt (int) – Ignore list count.
Return type:

dict{i: int, op: BaseObject, z: float, x: float, y: float}

i int Element index. The index depends on the element type:  
    point point index
    polygon polygon index
    edge 4*polygon + edge index
    spline point segment offset + point index
op BaseObject Object.  
z float Z coordinate of the current element.  
x float X pixel position.  
y float Y pixel position.  

Returns:

The retrieved pixel information or None if no information could be retrieved.

ViewportSelect.DrawPolygon(p, i, op, onlyvisible=-1)

Draws a polygon into the internal pixel structure, so that pixels inside the polygon will be associated with i and op in the viewport pixel retrieved.

Parameters:
  • p (array (Vector)) –

    An array of points that make up the polygon.

    Note

    All coordinates are in screen space.

  • i (int) – The polygon index to associate with this polygon.
  • op (c4d.BaseObject) – The object to associate with this polygon.
  • onlyvisible (int) – If this is True, only visible parts of the polygon are drawn.
Return type:

bool

Returns:

True if successful, otherwise False.

ViewportSelect.DrawHandle(p, i, op, onlyvisible=-1)

Draws a handle into the internal pixel structure, so that pixels inside the handle will be associated with i and op in the viewport pixel retrieved.

Parameters:
  • p (c4d.Vector) –

    The position of the handle.

    Note

    All coordinates are in screen space.

  • i (int) – The polygon index to associate with this polygon.
  • op (c4d.BaseObject) – The object to associate with this polygon.
  • onlyvisible (int) – If this is True, only visible parts of the handle are drawn.
Return type:

bool

Returns:

True if successful, otherwise False.

ViewportSelect.ShowHotspot(bw, x, y)

Draws an XOR circle to the view bw at the pixel position (x, y). The radius is set by SetBrushRadius().

Parameters:
  • bw (c4d.gui.EditorWindow) – The editor window to draw to.
  • x (int) – An X coordinate within the viewport. Must be 0 <= x < w, where w is the width given to Init().
  • y (int) – An Y coordinate within the viewport. Must be 0 <= y < h, where h is the height given to Init().
ViewportSelect.SetBrushRadius(r)

Sets the radius for ShowHotspot().

Parameters:r (float) – The new radius in pixels.
ViewportSelect.ClearPixelInfo(x, y, mask)

Deletes the pixel information at (x, y) according to mask.

This is used for example in the live edge selection with “tolerant” deactivated to find out how many pixels of a certain edge was selected. If an edge is determined under the cursor, the pixel counter is decremented and the edge information is deleted afterwards.

Parameters:
  • x (int) – An X coordinate within the viewport. Must be 0 <= x < w, where w is the width given to Init().
  • y (int) – An Y coordinate within the viewport. Must be 0 <= y < h, where h is the height given to Init().
  • mask (int) –

    A bitfield of what to clear:

    VIEWPORT_CLEAR_POINT Clear points.
    VIEWPORT_CLEAR_POLYGON Clear polygons.
    VIEWPORT_CLEAR_EDGE Clear edges.
ViewportSelect.GetCameraCoordinates(x, y, z)

Converts the pixel position (x, y, z) to camera coordinates.

Parameters:
  • x (float) – An X coordinate within the viewport.
  • y (float) – An Y coordinate within the viewport.
  • z (float) – A Z coordinate.
Return type:

c4d.Vector

Returns:

The calculated camera coordinate.

static ViewportSelect.PickObject(bd, doc, x, y, rad, flags)

Pick objects.

Parameters:
  • bd (c4d.BaseDraw) – The viewport base draw.
  • doc (c4d.documents.BaseDocument) – Document.
  • x (int) – An X coordinate within the viewport. Must be 0 <= x < w, where w is the width given to Init().
  • y (int) – An Y coordinate within the viewport. Must be 0 <= y < h, where h is the height given to Init().
  • rad (int) – A radius for the search in pixels.
  • flags (int) –

    Flags:

    VIEWPORT_PICK_FLAGS_NONE None.
    VIEWPORT_PICK_FLAGS_ALLOW_OGL Allow OpenGL.
    VIEWPORT_PICK_FLAGS_DONT_STOP_THREADS Do not stop threads.
    VIEWPORT_PICK_FLAGS_USE_SEL_FILTER Use selection filter.
    VIEWPORT_PICK_FLAGS_OGL_ONLY_TOPMOST Picks only topmost object. Use this only when the object pointer is not needed. Only works with OpenGL.
    VIEWPORT_PICK_FLAGS_OGL_ONLY_VISIBLE Picks only visible. Only works with OpenGL. Note: Not supported by Python API.
    VIEWPORT_PICK_FLAGS_OGL_IGNORE_Z Picks ignore Z position. Set this to only check if an object (and which) was hit, not its Z position. Only works with OpenGL.
    VIEWPORT_PICK_FLAGS_OGL_ONLY_TOPMOST_WITH_OBJ Picks only topmost object but returns the topmost object with its Z position. Only works with OpenGL.
Return type:

list[BaseObject]

Returns:

Object list or None on failure.