Maya : Unable to Activate Viewport 2.0 by script.

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
  04 April 2013
Maya : Unable to Activate Viewport 2.0 by script.

Hi everybody

I'm trying to activate Viewport 2.0 by script.
( I'm using Maya 2013 x64 on Windows )

I tryed this command :

Nothing happens. No error, but no Viewport 2.0 !
Of course,when i'm using the mouse : no problem.

Hope someone can help me
  04 April 2013
ActivateViewport20 is reliant on a modelPanel having focus. If you do this through the UI with Echo All Commands, you get the following output (formatted horribly):

{  string $currentPanel = `getPanel -withFocus`;   string $panelType = `getPanel -to $currentPanel`;  if ($panelType ==  "modelPanel") {	  setRendererInModelPanel "ogsRenderer" $currentPanel;  } else if ($panelType ==  "scriptedPanel") { 	string $cmd = "setRendererInModelPanel \"ogsRenderer\" "; 
	scriptedPanelRunTimeCmd( $cmd, $currentPanel ); 
updateModelPanelBar MayaWindow|formLayout1|viewPanes|modelPanel4|model  Panel4|modelPanel4;

If you read that code, you'll see that it uses getPanel -withFocus to determine the active viewport panel. Obviously if you're clicking on the UI, you're guaranteed to have the correct panel with focus. If you run it through a script however you are not.

As such, what you need to do by script is decompose that code so that it runs using a given modelPanel and you will need to come up with a way to determine the panel you want to modify, whether that is passed in from another script or hardcoded by name, or you set all modelPanels to VP2.0.

By default, there are 4 model panels for each of the 4 views (front, side, top, persp). I don't recall how they correspond to the particular modelPanel numbers, but you are guaranteed on startup to have 4 modelPanels named:


When you startup with just a single perspective view, that's usually modelPanel4 unless the user has configured things differently in the scene, etc.

Once you know what modelPanel you're looking for just run the subset of the above code:

global proc myEnableVP20( string $currentPanel )
	if ($panelType ==  "modelPanel")
		setRendererInModelPanel  "ogsRenderer" $currentPanel;
	else if ($panelType ==  "scriptedPanel")
		string $cmd = "setRendererInModelPanel \"ogsRenderer\" "; 
		scriptedPanelRunTimeCmd( $cmd, $currentPanel ); 
// Quick test with modelPanel4
myEnableVP20( "modelPanel4" );
  04 April 2013
Keilun, again : Thank you very much

That's great you take time to help me.

I achieve to do as you suggest me (pymel) for all model panel :
from pymel.core import *
for elem  in  getPanel(typ="modelPanel") :
  	mel.eval( 'setRendererInModelPanel  "ogsRenderer" '+ )

And it's working fine.

Now I have a new problem. The first time I run the script on a new scene, I get an error here :

  # MelError: Error during execution of MEL script: file: C:/Program Files/Autodesk/Maya2013/scripts/others/createObjectTypeFilters.mel line 146: Object Type Filter name array size does not match value array size
  # Script:
  #   objectTypeFilterOffCallback 0 # 

But after this error, if I run the script again, I have not any error.
The error is when I set in HardwareRenderingGlobals / Batch Render Options / Object Type Filters some values.
Uncheck Nurbs Curve is : "objectTypeFilterOffCallback 0"

To reproduce the problem first do new scene then execute this :
setRendererInModelPanel  "ogsRenderer" modelPanel1;
 setRendererInModelPanel  "ogsRenderer" modelPanel2;
 setRendererInModelPanel  "ogsRenderer" modelPanel3;
 setRendererInModelPanel  "ogsRenderer" modelPanel4;
 objectTypeFilterOffCallback 0;

At this time you get the error. Again and again, each time you run the previous code.
But if you show in the ui the hardwareRenderingGlobals options windows, then the previous code works fine. It works also if you close the windows !

I guess, perhaps, that is because I need to update something after setting hardware renderer as the renderer.

I see a workaround : Force to show the render option windows by script. Is it possible ?
  04 April 2013
Hi again,

So this is one of those areas where you need to be careful when you just copy paste what's returned via the EchoAllCommands in the ScriptEditor. For methods like:


These routines are callbacks tied to the actual UI that runs them. The only technically safe output to copy are ones that refer to actual Maya commands rather than procedures. The way to debug this is to open up the script that it refers to:


and have a look at what it really does. You'll see that it does this:

  // Description:
 // Callback for changing object type filter value set on
 global proc objectTypeFilterOnCallback(int $id)
 	setObjTypeFilterValueArrayItem($id, 1);
 // Description:
 // Callback for changing object type filter value set off
 global proc objectTypeFilterOffCallback(int $id)
 	setObjTypeFilterValueArrayItem($id, 0);

The key thing to note here is that the callback consists of two parts:

1. It sets the filter value.
2. It tries to update the checkbox in the UI (this is where your error comes from).

So really you just want to invoke:

  int $state = 0; // Set to 0/1 depending on the value you care about
  setObjTypeFilterValueArrayItem( $id, $state );

Make sure the createObjectFiltersType.mel has already been sourced prior to invoking those procedures so that Maya can find them. Until it has been sourced, Maya will only automatically find a script in its path if the procedure's name matches the name of the script.
  04 April 2013
Actually I was wrong. The error is indeed coming from the first part. Which suggests that the filter arrays need to be initialized first. Let me have a look at how it initializes the arrays and see if there's something you can run before you run the code.

[Edit]: Ok so the code should work by invoking the following:

createObjectTypeFilters(0); // Initialize the filter arrays.
   int $state = 0; // Set to 0/1 depending on the value you care about
  setObjTypeFilterValueArrayItem( $id, $state );

The createObjectTypeFilters(0) builds the arrays using a preset array in the same script. The UI uses the preset at index 0 to generate the initial list. So you can do the same. From there you should be able to go off and set the values.

Let me know if that works.

Last edited by Keilun : 04 April 2013 at 09:06 PM.
  04 April 2013
Yes !

You teach me a lot today Keilun. Thank you again.

As you suggest, I search in createObjectTypeFilters.mel
and I found that "setObjTypeFilterValueArrayItem" is a proc that call an other one : resetObjectTypeFilterArrays

In fact this is some convenient ways to update some values in a list But now I know where to search. So I wrote this :

     values =  getAttr("hardwareRenderingGlobals.objectTypeFilterValueArra  y") # get the array of values
     values[0] = 0 # ID 0 is set to OFF
     for i in range(1,4) : # ID 1-3 are set to ON
     	values[i] = 1
     for i in range( 4,15 ) : # ID 4-14 are set to OFF
     	values[i] = 0
     setAttr( "hardwareRenderingGlobals.objectTypeFilterValueArra  y" , values ) # Finaly put the modified array.

And that works perfectly. Thanks again for your help.
  04 April 2013
Great! Happy to help.
  04 April 2013
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 07:54 AM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.