View Full Version : Toggle Phong Shading

04 April 2006, 01:22 AM
Having found JDP and Srekís toggle scripts useful, Iíve created a COFFEE expression for quickly turning Phong shading on and off.

A script would be the ideal implementation but it doesnít seem possible to use global variables to store the original Phong Angle and a toggle state.

Alternately creating a plugin tag with parameters accessible via the HUD appears to require C++.

Anyway, hereís the expression so far. Suggestions would be welcome.

04 April 2006, 06:03 AM
As you say it doesn't seem possible to have global variables in a script. You could add a user data field to a plugin tag (which you can reference as you have with the coffee tag) and save it as a tag preset to the library so that every time you call the preset the user data field would be present.


04 April 2006, 06:29 AM
global variables are possible, just by putting the declaration outside of the scope of any functions but they're not really persistant within COFFEE

if you want to store persistant data in COFFEE then your best bet is to store it within an objects basecontainer. this will last just as long as the object itself does. it's like adding it to the objects own global variables. just make sure the ID you use wont conflict with anything inside of the tag itself (generally best to just grab a new ID form plugincafe for this).

04 April 2006, 06:38 AM
global variables are possible, just by putting the declaration outside of the scope of any functions but they're not really persistant within COFFEE

Sorry, this is what I really meant to say :), although global variables are persistant within coffee plugins.

04 April 2006, 10:13 AM
Thanks for the suggestions JDP and mdme_sadie, all very useful information.

It seems to be working okay as a script using a base container (with uniqe IDs) to store the data.

As Phong shading is toggled for both the editor and renderer, it's imporant to remember to toggle shading back on before rendering if it's required.

I don't know about other situations but with the previous version of this expression, global varibles were reset if e.g.:
the Execute button was pressed (COFFEE expression or script);
the object was made editable;
the scene was saved, closed and reopened.

if(!doc->GetActiveObject()) return;

var tag = op->GetFirstTag();
if(!tag) return;

while(tag->GetType() != Tphong)
tag = tag->GetNext();
if(!tag) return;

var bc = op->GetContainer();

var counter = bc->GetData(1019618);
if(!counter) bc->SetData(1019618, 0);
counter = bc->GetData(1019618);
counter = int(Modulo(counter + 1, 2));
bc->SetData(1019618, counter);

var currentAngle = tag#PHONGTAG_PHONG_ANGLE;
if(currentAngle > 0) bc->SetData(1019622, currentAngle);
var phongAngle = bc->GetFloat(1019622);


tag#PHONGTAG_PHONG_ANGLE = phongAngle;

04 April 2006, 12:58 PM
For those who may have copied the script earlier, I've updated it so that the counter doesn't keep incrementing forever, bloating the base container. The earlier version has 'toggle' in the last if statement and should be replaced.

Please let me know if you notice any problems or have any suggestions as I'm no expert.

04 April 2006, 10:17 AM
hi darter,
i don't know jack about programming, but you phong toggle seems usefull especially regarding the latest news about phong tags affecting render time. so my question is how do i use it? where do i write this lines? and after imputing this script in c4d how do i activate it???
excuse my ignorance but since qbasic and turbo pascal i can't stand progamming. :D

04 April 2006, 02:02 PM
Thanks for your interest, jondoe0ne.

Scripting is new to C4D 9.5 so you have to be using this version.

Open the Script Manager, create a new script, paste the code into the editor and click 'Save All'. The script can be tested by clicking the Execute button. The icon at the top left of the Script Manager can be dragged straight onto an icon palette and the script can be run by clicking on the icon. It can also be dragged into the Menu Manager and will appear in the Command Manager under the name assigned when creating the script.

Another option is to import the script into the Script Manager as a CSC file using File->Import.

I hope that you find the script useful, let me know if you have any comments or questions.

04 April 2006, 03:50 PM
as far as i can tell you can toggle the phong tag to one object at the time, as i have tried to select multiple objects and no luck... what if i have let's say 2000 ojects in a scene? can you change this?

04 April 2006, 09:30 PM
I created this primarily as a modelling tool so that an object's Phong shading can be toggled repeatedly as quickly as possible.

An easy way to turn off Phong shading for multiple objects for rendering purposes is to use the Select Tool (Selection->Selection Filter->Select Tool). Just check Phong under the Tags tab and set Phong Angle to 0 in the AM.

If needed, I'm sure that the script could be modified to work globally. I'd have find out how to search through all objects in a scene.

CGTalk Moderation
04 April 2006, 09:30 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.