Extending Python - Global Constants
-
On 29/07/2016 at 09:33, xxxxxxxx wrote:
User Information:
Cinema 4D Version: 13+
Platform: Mac ;
Language(s) : C++ ; PYTHON ;---------
Howdy,Is the function in the PythonLibrary class called "InitConstant()" the function to use to add global constants, like the coffee extension function AddGlobalSymbol()?
I tried using it but I don't know how to create the parameter "_PyObject *op" to pass to the function. Can you post an example?
Adios,
Cactus Dan -
On 01/08/2016 at 02:16, xxxxxxxx wrote:
Hi,
Yes InitConstant() is the function to use. InitStringConstant() can be used to add string constants too.
The "_PyObject *op" parameter for InitConstant() is the module to add the constant to.
Here's some code:if (pylib.InitModule("c4d.extendpyapi", moduleFunctions, "Extend Python API Module Example")) { ... // Retrieves c4d.extendpyapi module _PyObject* extendpyapi = pylib.GetModule("c4d.extendpyapi"); if (extendpyapi == nullptr) return; // Initializes CUSTOMCONST and CUSTOMSTRINGCONST constants pylib.InitConstant(extendpyapi, "CUSTOMCONST", 4875); pylib.InitStringConstant(extendpyapi, "CUSTOMSTRINGCONST", "String Constant"); }
-
On 01/08/2016 at 06:42, xxxxxxxx wrote:
Howdy,
AHA! That makes sense. Thank you.
Adios,
Cactus Dan -
On 01/08/2016 at 07:08, xxxxxxxx wrote:
Howdy,
Hmmmm, the InitConstant() is returning false.
Here's my code:
// Initializes c4d.cdpy module if(pylib.InitModule("c4d.cdpy", moduleFunctions, "Extend Python API")) { GePrint("\'c4d.cdpy\' module successfully initialized"); // Retrieves c4d.cdpy module _PyObject* cdpy = pylib.GetModule("c4d.cdpy"); if(cdpy) { GePrint("Python module cdpy successfully retrieved"); // Initializes CUSTOMCONST constant if(!pylib.InitConstant(cdpy, "CUSTOMCONST", 4875)) GePrint("Failed to init constant"); } }
Whats wrong?
Adios,
Cactus Dan -
On 02/08/2016 at 14:56, xxxxxxxx wrote:
Howdy,
OK, so I got rid of all of my edits to the code you posted and went back to your copied and pasted code. Then added a couple of print statements:
// Initializes c4d.extendpyapi module if (pylib.InitModule("c4d.extendpyapi", moduleFunctions, "Extend Python API")) { GePrint("\'c4d.extendpyapi\' module successfully initialized"); // Retrieves c4d.extendpyapi module _PyObject* extendpyapi = pylib.GetModule("c4d.extendpyapi"); if (extendpyapi == nullptr) return; GePrint("Successfully retrieved extendpyapi module"); // Initializes CUSTOMCONST and CUSTOMSTRINGCONST constants if(!pylib.InitConstant(extendpyapi, "CUSTOMCONST", 4875)) GePrint("Failed to init constant"); if(!pylib.InitStringConstant(extendpyapi, "CUSTOMSTRINGCONST", "String Constant")) GePrint("Failed to init string constant"); }
... and the console prints "Failed to init constant" but does not print "Failed to init string constant". Is it a bug in the api? I tried this in both R13 and R17 with the same result.
Adios,
Cactus Dan -
On 02/08/2016 at 15:48, xxxxxxxx wrote:
Howdy,
AHA! I've discovered that it is actually NOT failing to initialize the constant, but simply returning false.
When I use the constant in a script:
extendpyapi.PassParameters("a string of characters", extendpyapi.CUSTOMCONST, 10.25)
... it actually works and prints the value 4875 to the console.
Was I just being pannicky?
Adios,
Cactus Dan -
On 03/08/2016 at 02:19, xxxxxxxx wrote:
Hi,
Yes there's an issue in the implementation of PythonLibrary::InitConstant().
The function returns false even if the constant was successfully initialized. -
On 03/08/2016 at 09:56, xxxxxxxx wrote:
Hi,
I've been following these threads with interest and I've got most of it working in R13.
So far I can successfully:
-Initialize the c4d.extendpyapi module.
-Call the extendpyapi_HelloPython() function that was posted on the other thread
-Add new Python constantsBut I'm having trouble with the extendpyapi_PassParameters() function that was in the other thread.
I have added that function to the main.cpp file. And I'm guessing that I need to somehow point to it from inside of the InitExtendPython() method? But I have not been able to figure it out.
And then how do I call it from the script manager?Thanks,
-ScottA -
On 03/08/2016 at 10:12, xxxxxxxx wrote:
Howdy,
First increase the size of the array:
g_extendpyapi_functions->Resize(4);
...then add those other 2 functions to the module:
moduleFunctions[0].Init("HelloPython", (PyFn)extendpyapi_HelloPython, PYFN_FLAGS_NOARGS, "HelloPython() - Extend Python API"); moduleFunctions[1].Init("PassBaseList", (PyFn)extendpyapi_PassBaseList, PYFN_FLAGS_KEYWORDS, "PassBaseList() - Extend Python API"); moduleFunctions[2].Init("PassParameters", (PyFn)extendpyapi_PassParameters, PYFN_FLAGS_KEYWORDS, "PassParameters() - Extend Python API"); moduleFunctions[3].Init(String(), NULL, PYFN_FLAGS_0, String()); // Last dummy element!
Can you say, "DOH!"
I also had trouble figuring out how to call it from the script manager because I didn't realize that everything needed the "extendpyapi." prefix. DOH!
Adios,
Cactus Dan -
On 03/08/2016 at 11:09, xxxxxxxx wrote:
**DOH!
** Thanks Dan.Everything is working **** now.
-ScottA
-
On 03/08/2016 at 11:19, xxxxxxxx wrote:
Howdy,
You're welcome.
Adios,
Cactus Dan