-
首页
-
C4D R23.110 C++ SDK
Substance Functions Manual
内容表
关于
The Substance functions allow to import and handle Allegorithmic Substance assets into a
BaseDocument
. In a document they can be used with shaders and materials. The Substance functions are defined in the
lib_substance.h
header file.
Access Assets
Substance assets store a reference to a Substance archive file (.sbsar) and the current value set of its parameters. These assets are stored in the
BaseDocument
.
// This example accesses the first Substance to loop through all Substances.
BaseList2D
* substance =
GetFirstSubstance
(doc);
while
(substance !=
nullptr
)
{
ApplicationOutput
(
"Substance: "
+ substance->
GetName
());
substance = substance->
GetNext
();
}
// This example accesses all selected Substances
AutoAlloc<AtomArray>
substances;
if
(substances ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
GetSubstances
(doc, substances,
true
);
const
Int32
substanceCount = substances->
GetCount
();
for
(
Int32
i = 0; i < substanceCount; ++i)
{
C4DAtom
*
const
atom = substances->
GetIndex
(i);
const
BaseList2D
*
const
asset =
static_cast<
BaseList2D
*
>
(atom);
if
(asset ==
nullptr
)
return
maxon::UnexpectedError(
MAXON_SOURCE_LOCATION
);
ApplicationOutput
(
"Selected Substance: "
+ asset->
GetName
());
}
Add Assets
A Substance asset can be imported using the
Filename
of a Substance archive file (.sbsar).
// This example imports a Substance asset with the given filename.
// Then a copy of that asset is created.
BaseList2D
* substance =
nullptr
;
SUBSTANCE_IMPORT_COPY
copy =
SUBSTANCE_IMPORT_COPY::NO
;
// import Substance
const
SUBSTANCE_IMPORT_RESULT
res =
ImportSubstance
(doc, filename, copy,
false
,
false
,
true
, &substance);
// check if the Substance was imported successfully
const
Bool
importFailure = res !=
SUBSTANCE_IMPORT_RESULT::SUCCESS
;
const
Bool
invalidSubstance = substance ==
nullptr
;
if
(importFailure || invalidSubstance)
return
maxon::UnexpectedError(
MAXON_SOURCE_LOCATION
);
// copy Substance
BaseList2D
*
const
copySubstance =
static_cast<
BaseList2D
*
>
(substance->
GetClone
(
COPYFLAGS::NONE
,
nullptr
));
if
(copySubstance ==
nullptr
)
return
maxon::OutOfMemoryError(
MAXON_SOURCE_LOCATION
);
const
String
originalName = substance->
GetName
();
const
String
copyName = originalName +
" Copy"
;
copySubstance->
SetName
(copyName);
// insert copy
if
(!
InsertLastSubstance
(doc, copySubstance))
return
maxon::UnknownError(
MAXON_SOURCE_LOCATION
);
Get Assets
A Substance asset can contain one or multiple graphs, input parameters and output bitmaps.
// This example loops through the graphs of the given Substance asset.
// The graph is then used to loop through the input parameters and output images.
void
* graph =
nullptr
;
// loop through graphs
do
{
String
name;
graph =
GetSubstanceGraph
(substance, graph, name);
if
(graph !=
nullptr
)
{
ApplicationOutput
(
"Graph Name: "
+ name);
// loop through inputs
void
* input =
nullptr
;
do
{
String
inputName;
UInt32
inputID;
Int32
numberElements;
Int32
descID;
SUBSTANCE_INPUT_TYPE
type;
input =
GetSubstanceInput
(substance, graph, input, inputID, descID, numberElements, type, inputName);
if
(input !=
nullptr
)
ApplicationOutput
(
"Input: "
+ inputName);
}
while
(input !=
nullptr
);
// loop through outputs
void
* output =
nullptr
;
do
{
UInt32
outputID;
SUBSTANCE_OUTPUT_TYPE
type;
String
outputName;
output =
GetSubstanceOutput
(substance, graph, output, outputID, type, outputName,
nullptr
);
if
(output !=
nullptr
)
ApplicationOutput
(
"Output: "
+ outputName);
}
while
(output !=
nullptr
);
}
}
while
(graph !=
nullptr
);
Materials and Shaders
Substance assets are used in shaders and are typically applied (but not limited) to materials:
// This example creates a material from a given Substance asset.
const
SUBSTANCE_MATERIAL_MODE
mode =
PrefsGetMaterialModeSetting
();
BaseMaterial
*
const
material =
CreateMaterial
(substance, 0, mode);
if
(material ==
nullptr
)
return
maxon::UnknownError(
MAXON_SOURCE_LOCATION
);
const
String
substanceName = substance->
GetName
();
const
String
materialName = substanceName +
" Material"
;
material->
SetName
(materialName);
doc->
InsertMaterial
(material);
An example how to configure a Substance shader can be found on
Substance Elements Manual
.
Substance Preferences
These utility functions allow fast access to the most important Substance preferences.
Utility
Further utility functions are:
// This example checks if the given scene uses Substance assets in the materials.
// get first material
BaseMaterial
* material = doc->
GetFirstMaterial
();
// check all materials
bool
useSubstances =
false
;
while
(material !=
nullptr
)
{
// check if the given material uses Substances
if
(
MaterialUsesSubstance
(material))
{
useSubstances =
true
;
break
;
}
material = material->
GetNext
();
}
if
(useSubstances)
ApplicationOutput
(
"This scene uses Substances."
);
延伸阅读
BaseList2D * GetFirstSubstance(BaseDocument *const doc)
C4DAtom * GetClone(COPYFLAGS flags, AliasTrans *trn)
定义:
c4d_baselist.h:1417
void * GetSubstanceInput(BaseList2D *const asset, void *const graph, void *const prevInput, UInt32 &inputUid, Int32 &firstId, Int32 &numElements, SUBSTANCE_INPUT_TYPE &type, String &name)
maxon::UInt32 UInt32
定义:
ge_sys_math.h:59
BaseList2D * GetNext()
定义:
c4d_baselist.h:2183
BaseMaterial * GetFirstMaterial(void)
BaseMaterial * CreateMaterial(BaseList2D *const asset, Int32 graphIndex, SUBSTANCE_MATERIAL_MODE mode)
Int32 GetCount() const
定义:
c4d_baselist.h:1619
#define MAXON_SOURCE_LOCATION
定义:
memoryallocationbase.h:66
void * GetSubstanceGraph(BaseList2D *const asset, void *const prevGraph, String &name)
SUBSTANCE_MATERIAL_MODE PrefsGetMaterialModeSetting()
SUBSTANCE_IMPORT_RESULT ImportSubstance(BaseDocument *const doc, const Filename &fn, SUBSTANCE_IMPORT_COPY ©File, Bool errPopup, Bool addUndo, Bool createMaterial, BaseList2D **assetPtr)
SUBSTANCE_OUTPUT_TYPE
定义:
lib_substance.h:314
void InsertMaterial(BaseMaterial *mat, BaseMaterial *pred=nullptr, Bool checknames=false)
void * GetSubstanceOutput(BaseList2D *const asset, void *const graph, void *const prevOutput, UInt32 &outputUid, SUBSTANCE_OUTPUT_TYPE &type, String &name, BaseBitmap **bmpPtr)
SUBSTANCE_IMPORT_RESULT
定义:
lib_substance.h:265
SUBSTANCE_MATERIAL_MODE
定义:
lib_substance.h:251
@ NO
Do not copy file to project directory (absolute file path).
Bool MaterialUsesSubstance(BaseMaterial *const mat)
void SetName(const maxon::String &name)
定义:
c4d_baselist.h:2324
maxon::Int32 Int32
定义:
ge_sys_math.h:58
#define ApplicationOutput(formatString,...)
定义:
debugdiagnostics.h:207
SUBSTANCE_INPUT_TYPE
定义:
lib_substance.h:291
Bool InsertLastSubstance(BaseDocument *const doc, BaseList2D *asset)
void GetSubstances(BaseDocument *const doc, AtomArray *arr, Bool onlySelected)
BaseMaterial * GetNext(void)
定义:
c4d_basematerial.h:60
maxon::Bool Bool
定义:
ge_sys_math.h:53
String GetName() const
定义:
c4d_baselist.h:2318
SUBSTANCE_IMPORT_COPY
定义:
lib_substance.h:279
定义:
c4d_basematerial.h:27
C4DAtom * GetIndex(Int32 idx) const