A GeoOp node creates or modifies 3D data. The output is visible in NUKE’s built-in 3D viewer, can be saved out to an OBJ or FBX file via the WriteGeo node, or rendered into a 2D image using (for example) the ScanlineRender node.
Writing a 3D op is similar to writing a 2D op. You will need to override many of the same functions to describe the op to nuke: Class(), knobs, test_input and so on. Refer to the section on implementing 2D ops for details about these. The main differences are:
To write a GeoOp you should, broadly, follow these steps:
The GeoOp class has a method, geometry_engine, which does the bulk of the geometry processing work. Both SourceGeo and ModifyGeo override this method to provide simpler interfaces for creating or modifying geometry respectively.
All GeoOps inherit ultimately from the DD::Image::GeoOp class. There are two additional subclasses which you can inherit from instead, DD::Image::SourceGeo and DD::Image::ModifyGeo; these take care of some extra details for you.
Deciding which op to inherit from depends on what you want your op to do. Some examples to help guide you:
Purpose | Base class to inherit from |
---|---|
Create new geometry from scratch. | DD::Image::SourceGeo |
Create new geometry based on 2D inputs. | |
Transform or distort existing geometry. | DD::Image::ModifyGeo |
Produce a more finely tesselated version of some existing geometry. | |
Calculations based on analysis of the whole scene, rather than just one object at a time. | DD::Image::GeoOp |
Filter existing objects. |
A common reason for inheriting from GeoOp rather than ModifyGeo is that the latter provides an interface which operates on one 3D object at a time. If you are doing calculations which depend on having access to all the objects at once, it may be easier to inherit from GeoOp. You can still access information about other 3D objects in ModifyGeo; it just comes down to convenience.
SourceGeo provides a simplified interface for creating geometry. It provides some useful default behaviour:
The actual creation of 3D geometry is delegated to the create_geometry function, which you must override in your subclass:
Fills the out parameter with newly created geometry. This will be called to create geometry for the first time, or to rebuild it after something has changed.
If you want to change the default values which are set up for each 3D object, you can override the init_geoinfo_parms method:
Sets up the default values for created objects, including the transform matrix and the material.
There are further things which are common to all GeoOps that you can do. See the Common parts of all GeoOps section for details.
The NDK includes Sphere.cpp as an example of how to implement a SourceGeo subclass.
The ModifyGeo class provides a single method that you must override:
This function is called once per GeoInfo from the input node. The obj parameter is the index of the input object to be processed.
The data for the object being processed will already be populated in the out parameter. Your implementation of this function should modify the data in place by calling (e.g.) out.writable_points(obj) and overwriting the entries in the list it returns.
There are further things which are common to all GeoOps that you can do. See the Common parts of all GeoOps section for details.
The NDK includes LogGeo.cpp as an example of how to implement a ModifyGeo subclass.
The GeoOp base class does most of it’s work in a single method which you can override:
Creates or modifies geometry and ensures the op’s geometry cache is up to date.
Your implementation of this function should ensure that it calls out.synchronize_objects() to get the op’s geometry cache up to date. Calling this more than once is relatively cheap, because it exits quickly if the cache is up to date. It must be called at least once after all the geometry creation or modification.
There are further things which are common to all GeoOps that you can/must do. See the Common parts of all GeoOps section for details.
The NDK includes UVProject.cpp as an example of how to implement a GeoOp subclass.
Generally speaking, all GeoOps should calculate suitable hashes for each of the groups by overriding this method:
Recalculates and updates the stored hashes for each group.
This function is called by NUKE to get the hashes up to date, so it can use them to check which groups need to be rebuilt and set the appropriate rebuild flags.
All GeoOps have a few features in common:
They store a separate hash for each group.
They perform caching
Operations to implement (what, when and why).
Geometry hashes
Updates the stored hashes for each of the groups.
The call order for a GeoOp is different depending on whether it’s being rendered through a ScanlineRender (or similar) node, or being viewed in the 3D viewer.
When rendering a GeoOp, pretty much all of the work happens inside the renderers own _validate(bool) method (the renderer itself will be a subclass of Iop, so will be following the usual Iop call sequence):
In terms of the functions you’ll be overriding:
In the case of a SourceGeo op, that will instead look like:
And for a ModifyGeo op it will be:
When you’re viewing the output of a GeoOp node in NUKE’s 3D viewer, the call order is different to the above. Geometry is generated first, without any materials; then textures are generated in a background thread for each of the materials and the viewer is updated as they complete.
Another difference between rendering 3D and viewing 3D is the amount that gets drawn. For rendering it’s the connected inputs only; for the viewer, it’s the connected inputs and also anything which has a panel open in the properties bin.
The call order in the 3D viewer looks like this, for every op connected to the viewers primary input and for every op with an open panel in the properties bin:
This gathers the information necessary for NUKE to do its drawing. Next we do a series of render passes; for each pass we set the ViewerContext’s event to a different value then, after drawing all the geometry, invoke the draw_handles callbacks:
If your op doesn’t draw any custom handles, then build_handles should return false and you can ignore the draw_handle function. Likewise, if you’re not using any custom knobs you can ignore the knob build_handles and draw_handle functions; the built-in knobs already have suitable implementations of these.
To create a new 3D object, you’ll need to follow these steps:
It may not always be necessary to rebuild all of the geometry data; only some of the data groups may have been affected. The implementation should check the state of the rebuild flags to determine which groups need to be rebuilt. You can do this using (for example):
if (rebuild(Mask_Points)) {
...
}
Note that it’s possible to set the rebuild flags explicitly by calling set_rebuild(); but you should avoid doing this while inside your geometry_engine, create_geometry or modify_geometry method as it could lead to a memory leak.
Call out.add_object(obj) to make space for the new object in the geometry list.