Skip to content

Commit

Permalink
Cleanup: localize creator.c definitions
Browse files Browse the repository at this point in the history
As creator.c is used for the 'main' function,
avoid obscure declarations being placed so prominently.

- Move Blender as a Python module declarations into it's own section.
- Move USD declaration to an 'extern' just before it's called.
  • Loading branch information
ideasman42 committed Aug 2, 2020
1 parent 6a44483 commit 8305dd9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
3 changes: 3 additions & 0 deletions source/blender/python/intern/bpy_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -921,8 +921,11 @@ int BPY_context_member_get(bContext *C, const char *member, bContextDataResult *
/* TODO, reloading the module isn't functional at the moment. */

static void bpy_module_free(void *mod);

/* Defined in 'creator.c' when building as a Python module. */
extern int main_python_enter(int argc, const char **argv);
extern void main_python_exit(void);

static struct PyModuleDef bpy_proxy_def = {
PyModuleDef_HEAD_INIT,
"bpy", /* m_name */
Expand Down
59 changes: 36 additions & 23 deletions source/creator/creator.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,10 @@
#include "creator_intern.h" /* own include */

/* Local Function prototypes. */
#ifdef WITH_PYTHON_MODULE
int main_python_enter(int argc, const char **argv);
void main_python_exit(void);
#endif

#ifdef WITH_USD
/**
* Workaround to make it possible to pass a path at runtime to USD.
*
* USD requires some JSON files, and it uses a static constructor to determine the possible
* file-system paths to find those files. This made it impossible for Blender to pass a path to the
* USD library at runtime, as the constructor would run before Blender's main() function. We have
* patched USD (see usd.diff) to avoid that particular static constructor, and have an
* initialization function instead.
*
* This function is implemented in the USD source code, `pxr/base/lib/plug/initConfig.cpp`.
*/
void usd_initialise_plugin_path(const char *datafiles_usd_path);
#endif
/* -------------------------------------------------------------------- */
/** \name Local Application State
* \{ */

/* written to by 'creator_args.c' */
struct ApplicationState app_state = {
Expand All @@ -143,6 +128,8 @@ struct ApplicationState app_state = {
},
};

/** \} */

/* -------------------------------------------------------------------- */
/** \name Application Level Callbacks
*
Expand Down Expand Up @@ -199,20 +186,35 @@ static void callback_clg_fatal(void *fp)
/** \} */

/* -------------------------------------------------------------------- */
/** \name Main Function
/** \name Blender as a Stand-Alone Python Module (bpy)
*
* While not officially supported, this can be useful for Python developers.
* See: https://wiki.blender.org/wiki/Building_Blender/Other/BlenderAsPyModule
* \{ */

#ifdef WITH_PYTHON_MODULE
/* allow python module to call main */

/* Called in `bpy_interface.c` when building as a Python module. */
int main_python_enter(int argc, const char **argv);
void main_python_exit(void);

/* Rename the 'main' function, allowing Python initialization to call it. */
# define main main_python_enter
static void *evil_C = NULL;

# ifdef __APPLE__
/* Environment is not available in macOS shared libraries. */
# include <crt_externs.h>
char **environ = NULL;
# endif
#endif
# endif /* __APPLE__ */

#endif /* WITH_PYTHON_MODULE */

/** \} */

/* -------------------------------------------------------------------- */
/** \name Main Function
* \{ */

/**
* Blender's main function responsibilities are:
Expand Down Expand Up @@ -441,10 +443,21 @@ int main(int argc,
BKE_materials_init();

#ifdef WITH_USD
/* Workaround to make it possible to pass a path at runtime to USD.
*
* USD requires some JSON files, and it uses a static constructor to determine the possible
* file-system paths to find those files. This made it impossible for Blender to pass a path to
* the USD library at runtime, as the constructor would run before Blender's main() function.
* We have patched USD (see usd.diff) to avoid that particular static constructor, and have an
* initialization function instead.
*
* This function is implemented in the USD source code, `pxr/base/lib/plug/initConfig.cpp`. */
extern void usd_initialise_plugin_path(const char *datafiles_usd_path);

/* Tell USD which directory to search for its JSON files. If 'datafiles/usd'
* does not exist, the USD library will not be able to read or write any files. */
usd_initialise_plugin_path(BKE_appdir_folder_id(BLENDER_DATAFILES, "usd"));
#endif
#endif /* WITH_USD */

if (G.background == 0) {
#ifndef WITH_PYTHON_MODULE
Expand Down

0 comments on commit 8305dd9

Please sign in to comment.