From 499e4e239a2e1241a70cff8a5a662d5349600f41 Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 11 Mar 2003 21:31:17 +0000 Subject: [PATCH] getting closer with mayapview --- pandatool/src/maya/mayaApi.cxx | 21 +++- pandatool/src/maya/mayaApi.h | 1 + pandatool/src/mayaprogs/Sources.pp | 5 +- pandatool/src/mayaprogs/mayaPview.cxx | 136 ++++++++++++++++++++++---- pandatool/src/mayaprogs/mayaPview.h | 52 ++++++++++ 5 files changed, 192 insertions(+), 23 deletions(-) create mode 100755 pandatool/src/mayaprogs/mayaPview.h diff --git a/pandatool/src/maya/mayaApi.cxx b/pandatool/src/maya/mayaApi.cxx index 31b17c66db..ad7f951988 100644 --- a/pandatool/src/maya/mayaApi.cxx +++ b/pandatool/src/maya/mayaApi.cxx @@ -41,6 +41,20 @@ MayaApi *MayaApi::_global_api = (MayaApi *)NULL; //////////////////////////////////////////////////////////////////// MayaApi:: MayaApi(const string &program_name) { + if (program_name == "plug-in") { + // In this special case, we are invoking the code from within a + // plug-in, so we need not (and should not) call + // MLibrary::initialize(). + _plug_in = true; + _is_valid = true; + return; + } + + // Otherwise, if program_name is any other name, we are invoking the + // code from a standalone application and we do need to call + // MLibrary::initialize(). + _plug_in = false; + // Beginning with Maya4.5, the call to initialize seems to change // the current directory! Yikes! @@ -100,7 +114,7 @@ operator = (const MayaApi ©) { MayaApi:: ~MayaApi() { nassertv(_global_api == this); - if (_is_valid) { + if (_is_valid && !_plug_in) { // Caution! Calling this function seems to call exit() somewhere // within Maya code. MLibrary::cleanup(); @@ -119,7 +133,10 @@ MayaApi:: // If program_name is supplied, it is passed to Maya as // the name of the currently-executing program. // Otherwise, the current program name is extracted from -// the execution environment, if possible. +// the execution environment, if possible. The special +// program_name "plug-in" is used for code that is +// intended to be invoked as a plug-in only; in this +// case, the maya library is not re-initialized. //////////////////////////////////////////////////////////////////// PT(MayaApi) MayaApi:: open_api(string program_name) { diff --git a/pandatool/src/maya/mayaApi.h b/pandatool/src/maya/mayaApi.h index 508fef4557..81f1823559 100644 --- a/pandatool/src/maya/mayaApi.h +++ b/pandatool/src/maya/mayaApi.h @@ -55,6 +55,7 @@ public: private: bool _is_valid; + bool _plug_in; static MayaApi *_global_api; }; diff --git a/pandatool/src/mayaprogs/Sources.pp b/pandatool/src/mayaprogs/Sources.pp index 0affbed85f..3c23f7ee05 100644 --- a/pandatool/src/mayaprogs/Sources.pp +++ b/pandatool/src/mayaprogs/Sources.pp @@ -81,10 +81,9 @@ #define UNIX_SYS_LIBS \ m + #define BUILDING_DLL BUILDING_MISC #if $[WINDOWS_PLATFORM] - // Explicitly export the maya initialize and shutdown functions. - #define LINKER_FLAGS $[LINKER_FLAGS] /export:initializePlugin /export:uninitializePlugin // On Windows, Maya expects its plugins to be named with a .mll // extension, but it's a perfectly normal dll otherwise. This // ppremake hack achieves that filename. @@ -92,7 +91,7 @@ #endif #define SOURCES \ - mayaPview.cxx + mayaPview.cxx mayaPview.h #end lib_target \ No newline at end of file diff --git a/pandatool/src/mayaprogs/mayaPview.cxx b/pandatool/src/mayaprogs/mayaPview.cxx index 39aeb7d8c2..5e4a3c4f09 100755 --- a/pandatool/src/mayaprogs/mayaPview.cxx +++ b/pandatool/src/mayaprogs/mayaPview.cxx @@ -16,30 +16,45 @@ // //////////////////////////////////////////////////////////////////// -#include "pandaFramework.h" +#include "mayaPview.h" +#include "mayaToEggConverter.h" +#include "eggData.h" +#include "load_egg_file.h" + +// We must define this to prevent Maya from doubly-declaring its +// MApiVersion string in this file as well as in libmayaegg. +#define _MApiVersion #include "pre_maya_include.h" -#include +#include +#include #include "post_maya_include.h" -static PandaFramework framework; -static bool opened_framework = false; - -// This Maya macro sets up a class that meets the plug-in requirements -// for a simple command. -DeclareSimpleCommand(MayaPview, "VR Studio", "1.0"); +//////////////////////////////////////////////////////////////////// +// Function: MayaPview::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +MayaPview:: +MayaPview() { +} +//////////////////////////////////////////////////////////////////// +// Function: MayaPview::doIt +// Access: Public, Virtual +// Description: Called when the plugin command is invoked. +//////////////////////////////////////////////////////////////////// MStatus MayaPview:: doIt(const MArgList &) { - if (!opened_framework) { - int argc = 0; - char **argv = NULL; - framework.open_framework(argc, argv); - framework.set_window_title("Panda Viewer"); - framework.enable_default_keys(); - } + int argc = 0; + char **argv = NULL; + PandaFramework framework; + framework.open_framework(argc, argv); + framework.set_window_title("Panda Viewer"); + framework.enable_default_keys(); - WindowFramework *window = framework.open_window(); + PT(WindowFramework) window; + window = framework.open_window(); if (window == (WindowFramework *)NULL) { // Couldn't open a window. nout << "Couldn't open a window!\n"; @@ -50,11 +65,96 @@ doIt(const MArgList &) { window->enable_keyboard(); window->setup_trackball(); + framework.get_models().instance_to(window->get_render()); + + if (!convert(framework.get_models())) { + return MS::kFailure; + } - window->load_default_model(framework.get_models()); window->loop_animations(); - framework.clear_exit_flag(); framework.main_loop(); return MS::kSuccess; } + +//////////////////////////////////////////////////////////////////// +// Function: MayaPview::creator +// Access: Public, Static +// Description: This is used to create a new instance of the plugin. +//////////////////////////////////////////////////////////////////// +void *MayaPview:: +creator() { + return new MayaPview; +} + +//////////////////////////////////////////////////////////////////// +// Function: MayaPview::convert +// Access: Private +// Description: Actually converts the Maya selection to Panda +// geometry, and parents it to the indicated NodePath. +//////////////////////////////////////////////////////////////////// +bool MayaPview:: +convert(const NodePath &parent) { + // Now make a converter to get all the Maya structures. + MayaToEggConverter converter("plug-in"); + + // We always want polygon output since we want to be able to see the + // results. + converter._polygon_output = true; + + EggData egg_data; + converter.set_egg_data(&egg_data, false); + + if (!converter.convert_maya()) { + nout << "Errors in conversion.\n"; + return false; + } + + // Now the converter has filled up our egg structure with data, so + // convert this egg data to Panda data for immediate viewing. + egg_data.set_coordinate_system(CS_default); + PT(PandaNode) result = load_egg_data(egg_data); + + if (result == (PandaNode *)NULL) { + nout << "Unable to load converted egg data.\n"; + return false; + } + + parent.attach_new_node(result); + return true; +} + + + + +//////////////////////////////////////////////////////////////////// +// Function: initializePlugin +// Description: Called by Maya when the plugin is loaded. +//////////////////////////////////////////////////////////////////// +EXPCL_MISC MStatus +initializePlugin(MObject obj) { + MFnPlugin plugin(obj, "VR Studio", "1.0"); + MStatus status; + status = plugin.registerCommand("pview", MayaPview::creator); + if (!status) { + status.perror("registerCommand"); + } + + return status; +} + +//////////////////////////////////////////////////////////////////// +// Function: uninitializePlugin +// Description: Called by Maya when the plugin is unloaded. +//////////////////////////////////////////////////////////////////// +EXPCL_MISC MStatus +uninitializePlugin(MObject obj) { + MFnPlugin plugin(obj); + MStatus status; + status = plugin.deregisterCommand("pview"); + + if (!status) { + status.perror("deregisterCommand"); + } + return status; +} diff --git a/pandatool/src/mayaprogs/mayaPview.h b/pandatool/src/mayaprogs/mayaPview.h new file mode 100755 index 0000000000..5f34747058 --- /dev/null +++ b/pandatool/src/mayaprogs/mayaPview.h @@ -0,0 +1,52 @@ +// Filename: mayaPview.h +// Created by: drose (11Mar03) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://www.panda3d.org/license.txt . +// +// To contact the maintainers of this program write to +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#ifndef MAYAPVIEW_H +#define MAYAPVIEW_H + +#include "pandatoolbase.h" +#include "pandaFramework.h" + +#include "pre_maya_include.h" +#include +#include +#include +#include "post_maya_include.h" + +//////////////////////////////////////////////////////////////////// +// Class : MayaPview +// Description : This class serves as a plug-in to Maya to allow +// viewing the current Maya selection as it will be +// converted to Panda. +//////////////////////////////////////////////////////////////////// +class MayaPview : public MPxCommand { +public: + MayaPview(); + virtual MStatus doIt(const MArgList &); + + static void *creator(); + +private: + bool convert(const NodePath &parent); +}; + +EXPCL_MISC MStatus initializePlugin(MObject obj); +EXPCL_MISC MStatus uninitializePlugin(MObject obj); + + +#endif