PDA

View Full Version : Auto Switch and Tweak Mode scripts


oliveoyle
01-25-2011, 06:57 PM
Sorry for the double post. I was not sure I posted correctly the first time.

Auto Switch and Tweak Mode scripts
I am very new to c4d scripting. I have trying to learn from seeing other scripts and the script log. I made two simple scripts fro Tweak and Auto switch modes. When I use the default icon for these modes, I constantly needed to switch other things, like how axis off, move tool and so on. These two simple scripts do what I need, but I need some help to add a little more to them and I lack the knowledge.

// Selects Tweak mode with additional selections

CallCommand(1016114); // Tweak mode
CallCommand(200000088); // Move tool
CallCommand(12139); // Point tool
CallCommand(1016176); // Show Axis Tool


second script

// Selects Auto Switch Mode with additional selections
CallCommand(1016118); // Auto Switch mode
CallCommand(200000083); //Live Selection
CallCommand(12139); // Point tool
CallCommand(1016176); // Show Axis Tool



I would like the Tweak script to make sure that the Show Axis is always off. Currently, it flip flops the previous state. I need it to see if the Show Axis is off and to leave it off. Same thing for the Auto Switch script, except the opposite. I need the Auto Switch script to always make sure it will have Show Axis on.

The other problem I have is the Icon images I made. I need them to show they are active when selected. How can I make icon images that change to show which one is selected like all the other icons that show my Preference Theme Colors Pallette Background Active Color? Or even better for me, to make two Icon Images for each script that toggles between them when I select that script icon in the palette. Like one image with a gray background and then shows a red background when it is selected in the palette so it is different looking then the Pallette Background Active Color.


Thanks for any help,
Olive

Cairyn
01-25-2011, 08:44 PM
Check the current state of the Axis tool before switching it:

if (IsCommandChecked(1016176)) CallCommand(1016176);
(switches always off)

if (!IsCommandChecked(1016176)) CallCommand(1016176);
(switches always on)

As for the icons, I doubt that this works that easily. Scripts are commands, they do not have a state and cannot be used as toggle, so their icon works only like a click-button. Maybe a script can manipulate its own icon somehow, but since you can see the current state through the tool icons you toggle, I doubt it's worth investigating.

oliveoyle
01-25-2011, 09:05 PM
Cairyn,

Those two commands did exactly what I needed. I hope to learn more over time with creating new scripts. Every little bit helps.

Thank you,
Olive

Scott Ayers
01-25-2011, 09:24 PM
The icon question was a good one. Something that I've never even thought about before.
When you click on the world/object icon for example. The icon toggles between two different icons.

I'd kind of like to know how that's done myself.

-ScottA

oliveoyle
01-25-2011, 09:26 PM
I forgot to ask, is it possible to now combine those those two scripts I made into one script so I can assign a mouse button to toggle between the new Auto switch and the new Tweak modes? Maybe even as Scott mentions, to have only one icon that switches between those two scripts I made, combined into one script that toggles between them and have the one icon change to reflect that.

I like tweak, but it has limitations that cause me to switch to auto switch. Tweak only allows one element to be selected and acted upon. Auto switch allows me to select more elements, like Live Select.

Scott Ayers
01-25-2011, 09:49 PM
After looking at how the object/world option works. It appears to be done using a menu plugin.
I've written menu plugins before using coffee. But I've only used the default check mark that toggles them on/off.
Using icons as the switching mechanism is something I've never attempted before.

Here is a simple example of a menu plugin. After putting it in your plugins folder. Launch C4D and use the Command Manager to find it and drag it onto your layout.
This will create the same kind of icon like the world/object icon that can toggle your code on/off.

//This plugin adds a menu item to the plugins menu
//It toggles the fillet option if the object has one
//Open the Command Manager and search for "Fillet Enable" .cof
//Drag and drop it onto your interface to create an icon button out of it

//Note: ICONS do not work yet in this version

var bmp;

class MyMenuPlugin : MenuPlugin
{
public:
MyMenuPlugin(); // The constructor

GetID(); // The plugin's ID method
GetIcon(); // The icon image
GetName(); // The plugin's name method
GetState(); // The method used to get the on/off state of our custom menu item
GetHelp(); // The method used to create the help text that's displayed at the bottom of the C4D interface
Execute(doc); // The task we want to perform when executed
}

MyMenuPlugin::MyMenuPlugin()
{
super();
}

MyMenuPlugin::GetID()
{
return 1000003; // TESTING id ONLY!! DO NOT USE THIS ONE PUBLICLY
}

MyMenuPlugin::GetState()
{
var doc = GetActiveDocument(); if (!doc) return 0;
var obj = doc->GetActiveObject(); if (!obj) return 0;

if (obj#PRIM_CUBE_DOFILLET == true)
return CMD_ENABLED|CMD_CHECKED; // Enable the menu checkmark
else
return CMD_ENABLED; // Turn off the menu checkmark

}

MyMenuPlugin::GetIcon() { return bmp; }

MyMenuPlugin::GetName()
{
return "Enable Fillet"; //The text shown in the menu
}

MyMenuPlugin::GetHelp()
{
return "Toggle Fillet"; //The text shown at the bottom of the C4D interface
}

MyMenuPlugin::Execute(doc)
{
var obj = doc->GetActiveObject(); if (!obj) return 0;
var bc = obj->GetContainer(); // Get the tag's container

bc->SetData(PRIM_CUBE_DOFILLET,!bc->GetData(PRIM_CUBE_DOFILLET)); //Toggles the checkmark on/off in memory
obj->SetContainer(bc); // Make the change actually happen
EventAdd();
}

main()
{
if (!bmp) bmp = new(BaseBitmap, 128, 128);
var fn = new(Filename);
fn->SetFullString("c:/Test1.jpg");// references the c: directory

bmp->Load(fn);
Register(MyMenuPlugin);
}



Hopefully someone who knows how to add icons to this will chime in and offer help for that part of it.
That's the only part I don't know how to do.

-ScottA

oliveoyle
01-25-2011, 11:14 PM
Hello Scott,

you said "Using icons as the switching mechanism is something I've never attempted before."

I am getting in over my head here, but if I understand correctly, in simple terms, what I am thinking is the icon launches a script as usual. The script contains a toggle between the two modes (my new Autoswitch and Tweak scripts combined into one script ) and each mode has a certain set of commands that need to be selected.

you said "Here is a simple example of a menu plugin. After putting it in your plugins folder."
How do I put that into a plugin folder? Excuse my lack of knowledge here.

Olive

Scott Ayers
01-25-2011, 11:57 PM
Lol.
Nothing like throwing you into the deep end of the pool when you're trying to learn how to dog paddle huh? ;)

The way you install plugins is to put the files into a folder called "Plugins" that exists inside your MAXON folder.
When you create menu plugins. The default way to set them up is to make a check mark next to it appear and disappear depending on it's ON/OFF state.
You can also build in an icon image that shows up in the menu too. And while I do know how to do that. I don't know how to make that icon image change depending on the plugin's ON/OFF state.
The icons themselves are just there for show. And don't effect the way the plugin works.

If you're that new to all of this, then I think what I'm talking about is going to be over your head for now. Until you learn more about how things work.
Unfortunately, I don't think there's an easier way to create a two way toggling icon without going the plugin route. So you might have to settle with using two separate scripts. And dragging them into your layout as two separate buttons. And clicking them separately as needed.
That much you can do with just basic scripts.

-ScottA

oliveoyle
01-26-2011, 02:38 AM
I understand how to install plugins. But they are usually a folder with files like dylib, cob, res. What you posted here earlier looks like code for a script. So what I was asking is how do I take this code you posted here and make it a plugin file that I can install in my plugin folder? I also turn off the pref for showing menu icons since I don't feel I need them there.

Scott Ayers
01-26-2011, 04:05 AM
Simple plugins can be made using just one file.
Copy and paste the code into a text file. Then change the file extension to .cof.
*It's important not to change the formatting when doing this. So make sure you don't use word, or word pad when doing this.
Then put that file in your plugins folder. And launch C4D.

As long as you don't have any other plugins on your system that use the same ID#1000003 it should show up in your plugins menu list. And function properly.
By accessing that plugin through the Command Manager. You can then drag and drop it onto your layout. And click on the icon to toggel it on/off. Rather than going into the plugins menu.
You should also see that even though there isn't any icon image on the new button. The icon will change colors when you click on it depending whether it's on or off. Similar to how the tool mode icons change when you click on them.

If you are using a mac. Then I have no idea if this will work. I've never used a mac before.

Your code needs to placed inside the plugin structure where my fillet code currently is in order to work.
Plugins require you to have at least a basic knowlegde of methods and classes. In the scripting manager you don't have to worry so much about that stuff. But with plugins that stuff is important.
Have you ever worked with methods and classes before?

-ScottA

oliveoyle
01-26-2011, 04:53 AM
Scott,

I understand what you are saying and I did exactly as you said. But I am mot seeing it show up in my plugin list as expected. Maybe because I am on a Mac? It also does not show in the Command manager. I used the simple Mac Text Edit app (similar to Notepad on a PC) and saved it as a rtf. There was no txt option. Then I changed the extension to a cof and put it in my plugin folder. Restarted c4d and no show. :-(

I have not worked with methods and classes. I am curiopus if Python would work and if I should start trying to get into that arena.

Olive

Scott Ayers
01-26-2011, 05:52 AM
It's hard for me to say what's wrong because I've never used a mac before.

Python plugins work the same way as Coffee and C++.
There are classes. And inside of these classes there are methods that perform a task.
It's written differently in Python. But the concept is still the same.

You're trying to jump ahead of yourself too fast.
You really need to take some time and learn about classes and methods before you try and work with plugins. You don't need to be an expert on them. But you do need to have a basic understanding of them before tackeling plugins.
Take your time and watch some tutorials about them. Then see if the code I gave you starts to make more sense to you. There's tons of free tutorials about classes and methods.
The ShowMedo website is a great place to start.

Remember. Learning this stuff is not a race. It's marathon. So don't try to learn it all in one day.

In the mean time. While you're learning. Continue to use the scripting manager.
The code you learn to write in there is basically the same code you'll eventually put inside of your plugins. Once you learn how the plugin structure works.

-ScottA

oliveoyle
01-26-2011, 06:29 AM
scott, i will do as you suggest. thank you for your help.

olive

Jorge Arango
01-26-2011, 12:32 PM
I used the simple Mac Text Edit app

Try TextWrangler (free).

oliveoyle
01-26-2011, 03:56 PM
Jorge,

TextWrangler worked. The cof file now shows up in the plugins menu. Thank you.

Olive

Jorge Arango
01-26-2011, 04:26 PM
Glad to help Olive.

oliveoyle
01-26-2011, 04:33 PM
Scott, I have a free plugin called Solo that has icon images that toggle when selected in the palette. Maybe you can see how they used the icon images to make it toggle the display.

The plugin is at http://www.forcedimension.jp/qdg/plugin.html

Scott Ayers
01-26-2011, 05:23 PM
Thanks for the assist Jorge.
I hate this whole pc<-> mac stuff. It really gets in the way sometimes.

Thanks for the link Olive.
Unfortunately, that plugin uses a .cob file. Which is a .cof file that has been converted to binary to hide the code from prying eyes like mine.:sad:

I'll keep looking and see if I can figure it out.
It should just be a matter of setting up the image paths in the proper spot of the plugin structure. I just have to figure out where, and how to do it.


-ScottA

Cairyn
01-26-2011, 07:59 PM
Hopefully someone who knows how to add icons to this will chime in and offer help for that part of it.
That's the only part I don't know how to do.


Well, you already return the "bmp" bitmap in your class, so the only thing that's missing would be the load:

if (!bmp)
{
bmp = new(BaseBitmap, 32, 32);
var fn1 = GeGetRootFilename();
fn1->RemoveLast();
fn1->AddLast("SwitchmodesActive");
fn1->SetSuffix("tif");
bmp->Load(fn1);
println(fn1->GetFullString());
}


Add a file "SwitchmodesActive.tif" at the same directory level as the plugin itself, in the proper size 32x32 pixels. The icon must have transparencies so the background set by C4D is visible.

Or, if you want the initial example made into a full COFFEE plugin, here I blatantly steal from your example:


var bmp;

class MyMenuPlugin : MenuPlugin
{
public:
MyMenuPlugin(); // The constructor

GetID(); // The plugin's ID method
GetIcon(); // The icon image
GetName(); // The plugin's name method
GetState(); // The method used to get the on/off state of our custom menu item
GetHelp(); // The method used to create the help text that's displayed at the bottom of the C4D interface
Execute(doc); // The task we want to perform when executed

var internalstate;
}

MyMenuPlugin::MyMenuPlugin()
{
internalstate = false;
super();
}

MyMenuPlugin::GetID()
{
return 1000003; // TESTING id ONLY!! DO NOT USE THIS ONE PUBLICLY
}

MyMenuPlugin::GetState()
{
if (internalstate)
return CMD_ENABLED|CMD_CHECKED; // Enable the menu checkmark
else
return CMD_ENABLED; // Turn off the menu checkmark
}

MyMenuPlugin::GetIcon()
{
return bmp;
}

MyMenuPlugin::GetName()
{
return "Switch Modes"; // The text shown in the menu
}

MyMenuPlugin::GetHelp()
{
return "Toggles between Tweak and Auto Switch modes and Move and Live Select tools"; // The text shown at the bottom of the C4D interface
}

MyMenuPlugin::Execute(doc)
{
if (internalstate)
{
CallCommand(1016114); // Tweak mode
CallCommand(200000088); // Move tool
CallCommand(12139); // Point tool
if (IsCommandChecked(1016176)) CallCommand(1016176); // Show Axis Tool
}
else
{
CallCommand(1016118); // Auto Switch mode
CallCommand(200000083); //Live Selection
CallCommand(12139); // Point tool
if (!IsCommandChecked(1016176)) CallCommand(1016176); // Show Axis Tool
}
internalstate = !internalstate;
}

main()
{
if (!bmp)
{
bmp = new(BaseBitmap, 24, 24);
var fn1 = GeGetRootFilename();
fn1->RemoveLast();
fn1->AddLast("SwitchmodesActive");
fn1->SetSuffix("tif");
bmp->Load(fn1);
println(fn1->GetFullString());
}
Register(MyMenuPlugin);
}


What I don't see is a method to switch between several icons (not just one icon where the background is set to different colors by C4D itself). Returning different icons in GetIcon depending on the state just doesn't work. (And come to think of it, I haven't seen something like this in the native functions anyway...)

Scott Ayers
01-26-2011, 08:33 PM
You are welcome to steal anything I have my friend. I owe you at least that much for all of your help.:beer:

I also had no luck trying to use use the GetState() function to drive a global variable which was controlling the paths to my image files.
Then I looked at the sdk. And I saw that there's actually a function called GetIconState() that's supposed to be available for doing this image swapping. But it doesn't say anything other than "Overload this function" .

I've been trying to figure out how to use that function. But so far I'm having no luck with it.

-ScottA

oliveoyle
01-26-2011, 08:53 PM
Cairyn,

Cool. It works. I had to delete Scott's plugin since it used the same ID#. I am happy with one icon that toggles the background of the image. It would be nice to have two icons that switch to show Auto and then Twk icons. But one will work for me.

Many thanks to you, Scott and Jorge for helping. I will try to learn and understand it all over time.

Olive

Cairyn
01-26-2011, 09:04 PM
And I saw that there's actually a function called GetIconState() that's supposed to be available for doing this image swapping. But it doesn't say anything other than "Overload this function" .

I've been trying to figure out how to use that function. But so far I'm having no luck with it.


Funnily, the C++ equivalent, CommandData (for RegisterCommandPlugin) looks quite different structurally and does not pretend to have multi-icon capabilities.

GetIconState only returns bool, so I think it may only indicate whether the state is toggled or not, though, this information may clash with what GetState returns. I'm not sure what it's supposed to do (and yes, the documentation is lacking in that respect - you may want to ask on PluginCafe for clarification). I'm too lazy to look up examples right now :-)

Cairyn
01-26-2011, 09:07 PM
Cool. It works. I had to delete Scott's plugin since it used the same ID#.


This # is from the "developer's only" range anyway and for local and temporary purposes only. If you want a permanent one that is guaranteed not to clash with anybody's work, you need to request a unique id at:

http://www.plugincafe.com/forum/default.asp

Scott Ayers
01-26-2011, 09:27 PM
Hmm. Maybe it's not possible to do it then. But I hate saying that because often that ends up not being true.

I set a global variable called "toggleimage". Then inside of the GetState() function. I toggled it's value between 0&1 depending on the state of the plugin.
Then in the main section.
I used a couple of if statements to set the path of the image file depending on the value of that toggleimage global variable.

It seemed to be working. I used print() along the way to make sure there were no errors.
And if I manually set the global variable by hand to 0 or 1. The icon would change correctly relative to their paths upon launching C4D.
But the image would not automatically change when clicked. So I thought it was maybe one of those refresh/redraw issues.:shrug:

Oh well. It was worth a shot.
It's kind of funny that this issue hasn't come up before.

-ScottA

Cairyn
01-26-2011, 10:27 PM
Hmm. Maybe it's not possible to do it then. But I hate saying that because often that ends up not being true.


Yeah, sometimes there are surprising workarounds, or methods that I just couldn't find because they are not named as I would expect... I was just about to say that I have never seen an icon in C4D that behaved like this, but of course, the Object/World icon does! (It is also definitely possible for objects (e.g. lights) and tags (e.g. dynamics).)

Now I am curious. Also, whether it is possible to include more than 2 states and icons when you set these manually...

oliveoyle
02-06-2011, 10:41 PM
hi cairyn,

"Now I am curious. Also, whether it is possible to include more than 2 states and icons when you set these manually..."

did you find anything more on this?

olive

CGTalk Moderation
02-06-2011, 10:41 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.