On 24/05/2017 at 00:12, xxxxxxxx wrote:
Hello Andreas,
The plugin is (I think) simple enough and basic enough that it's possible to just put all the code up:
import c4d, os, sys
from c4d import plugins, utils, documents, gui, bitmaps
folder = os.path.dirname( __file__ )
if folder not in sys.path: sys.path.insert( 0, folder )
import k
class ToggleDoSave( plugins.CommandData ) :
def Execute( self, doc ) :
preferences = GetPreferences()
preferences.SetBool( k.PREF_DO_C4D_SAVE_COMMAND, not preferences.GetBool( k.PREF_DO_C4D_SAVE_COMMAND ) )
return True
def GetState( self, doc ) :
preferences = GetPreferences()
if preferences.GetBool( k.PREF_DO_C4D_SAVE_COMMAND ) :
return c4d.CMD_ENABLED | c4d.CMD_VALUE
else:
return c4d.CMD_ENABLED
class GetPath( plugins.CommandData ) :
def Execute( self, doc ) :
documentBaseContainer = GetDocumentPluginData( doc )
defaultString = documentBaseContainer.GetString( k.PATH_FOR_FBX )
print "defaultString in GetPath/Execute: ", defaultString
if defaultString is "": defaultString = doc.GetDocumentName()
# construct a slightly more sensible default etc
path = c4d.gui.InputDialog( "Please enter the path for the FBX", defaultString )
# error check user provided path, eventually have a nice "save file" dialog
documentBaseContainer.SetString( k.PATH_FOR_FBX, path )
return True
class AutomaticFBXExport( plugins.CommandData ) :
def Execute( self, doc ) :
documentBaseContainer = GetDocumentPluginData( doc )
path = documentBaseContainer.GetString( k.PATH_FOR_FBX )
print "path in AutomaticFBXExport: ", path
# error check the path, does it still exist etc ...
preferences = GetPreferences()
if preferences.GetBool( k.PREF_DO_C4D_SAVE_COMMAND ) : c4d.CallCommand( k.COMMAND_SAVE )
c4d.documents.SaveDocument( doc, path, c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST, k.C4D_OPTION_SAVE_AS_FBX )
return True
def GetDocumentPluginData( doc ) :
bc = doc[ k.MAIN_COMMAND_PLUGIN_ID ]
print "bc in GetDocumentPluginData: (before is None)", bc
if bc is None:
bc = c4d.BaseContainer( k.MAIN_COMMAND_PLUGIN_ID )
#bc = c4d.BaseContainer() ??????
doc[ k.MAIN_COMMAND_PLUGIN_ID ] = bc
print "bc in GetDocumentPluginData: ", bc
return bc
def GetPreferences() :
preferences = c4d.plugins.GetWorldPluginData( k.MAIN_COMMAND_PLUGIN_ID )
if preferences == None:
preferences = c4d.BaseContainer( k.MAIN_COMMAND_PLUGIN_ID )
result = c4d.plugins.SetWorldPluginData( k.MAIN_COMMAND_PLUGIN_ID, preferences )
if preferences.GetBool( k.PREF_DO_C4D_SAVE_COMMAND ) == None: preferences.SetBool( k.PREF_DO_C4D_SAVE_COMMAND, False )
return preferences
if __name__ == "__main__":
theBitmap = bitmaps.BaseBitmap()
theDirectoryPath, theFileName = os.path.split( __file__ )
theBitmap.InitWith( os.path.join( theDirectoryPath, "res", "icon.tif" ) )
plugins.RegisterCommandPlugin( id = k.MAIN_COMMAND_PLUGIN_ID,
str = k.MAIN_COMMAND_NAME,
info = 0,
icon = theBitmap,
help = k.MAIN_COMMAND_HELP,
dat = AutomaticFBXExport() )
plugins.RegisterCommandPlugin( id = k.TOGGLE_DO_SAVE_COMMAND_PLUGIN_ID,
str = k.TOGGLE_DO_SAVE_COMMAND_NAME,
info = 0,
icon = None,
help = k.TOGGLE_DO_SAVE_COMMAND_HELP,
dat = ToggleDoSave() )
plugins.RegisterCommandPlugin( id = k.GET_PATH_PLUGIN_ID,
str = k.GET_PATH_NAME,
info = 0,
icon = None,
help = k.GET_PATH_HELP,
dat = GetPath() )
I hope the general flow is clear: check if a base container with main plugin id exists, create it if it does not, then thereafter use it to store the single string. (The export path for the FBX.)
The method GetDocumentPluginData( doc ) checks for the existence of the base container, creates it if it does not exist.
The method Execute( self, doc ) in the (CommandData) plugin class GetPath uses the above method to get the bc, see if a string is already there, use it if it is, request a string from the user, and store their reply.
The method Execute( self, doc ) in the class AutomaticFBXExport uses the same method to get the bc, gets the string, if the string is empty/bad it will return, if good (and after some error checking to be implemented) it will continue.
My assumption is that although they are different Executes, the same "doc" parameter being fed in, and using the same ID, should give an area of common storage?
All a bit crude at the moment, I know, and it's a very basic plugin (although it will be useful for me working with Unity 3D when it's done) but I cannot work out what I am doing wrong. Usually there's enough hints and tips on these forums from all the kind help that's been given to others over the years that I've been able to work things out, but not this time.
Regards, Charlie