Iray environment properties

how we can do this using c++?

 

var viewport_manager = MainWindow.getViewportMgr();
var draw_style = viewport_manager.findUserDrawStyle("NVIDIA Iray");
var viewport = viewport_manager.getActiveViewport();
var viewport_3d = viewport.get3DViewport();
viewport_3d.setDrawStyle(draw_style);
var render_manager = App.getRenderMgr();
Scene.addNode(new DzEnvironmentNode());
Scene.addNode(new DzTonemapperNode());
render_manager.getRenderElementObjects().forEach(function (element)
{
    if (element.className() === "DzEnvironmentNode")
    {
        element.findPropertyByLabel("Draw Dome").setDoubleValue(1);
    }
});

Post edited by MehdiZangenehBar on

Comments

  • surrealsurreal Posts: 176
    edited February 27

    A problem is that the SDK 4.5+ does not know about zEnvironmentNode and DzTonemapperNode. They were created after this version of the SDK was released. 

    One option to work around that problem is to create those nodes using a script called by your plugin. If you are so inclined, you could just get your plugin to call your whole script.

    However, since this is an SDK forum let’s do what we can using the SDK.

    Sorry I do not have a great deal of free time so not the best code it is verbose and has redundant checking. Use at your own risk, hopefully it will be of use to you and others.
    CREDIT: apart from any spelling mistakes this code is simply an adaption from DAZ's examples(included with the SDK) and in the SDk documentation and from many previous forum contributors.

    /**********************************************************************
    	Copyright (c) 2002-2020 Daz 3D, Inc. All Rights Reserved.
    	This file is part of the Daz Studio SDK.
    	This file may be used only in accordance with the Daz Studio SDK
    	license provided with the Daz Studio SDK.
    	The contents of this file may not be disclosed to third parties,
    	copied or duplicated in any form, in whole or in part, without the
    	prior written permission of Daz 3D, Inc, except as explicitly
    	allowed in the Daz Studio SDK license.
    	See http://www.daz3d.com to contact Daz 3D, Inc or for more
    	information about the Daz Studio SDK.
    **********************************************************************/
    /// <summary>
    /// Call a script to create class
    /// SDK does not know about some new node types i.e. DzEnvironmentNode and DzTonemapperNode
    /// So we have to use scripting to create those types of nodes
    /// </summary>
    /// <param name="nodeClassName">The class name of the node type to create and add to scene</param>
    void createByClassNodeScript(const QString& nodeClassName) {
    	DzScript script;
    	script.addLine("Scene.addNode(new " + nodeClassName + "());");
    	if (script.execute() == true) {
    		dzApp->statusLine("Script OK", FALSE);
    	}
    	else {
    		dzApp->debug("createByClassNodeScript error message - " + script.errorMessage());
    		dzApp->statusLine(script.errorMessage(), FALSE);
    	}
    }
    
    /// <summary>
    /// Check if there is a node of a particular class in the scene 
    /// by stepping through all the nodes in the scene
    /// and checking if they inherit that class.
    /// </summary>
    /// <param name="nodeClassName">The node class name to find</param>
    /// <returns>Returns first node of requested class type found in scene, returns nullptr(0) if no node of requested class type found in scene</returns>
    DzNode* findSceneNodeByClass(const QString& nodeClassName) {
    	DzNodeListIterator nodeIt(dzScene->nodeListIterator());		// Get iterator for scene node list
    	while (nodeIt.hasNext()) {									// While not at end of node list
    		DzNode* node = nodeIt.next();							// Get next node in scene node list
    		if (node->inherits(nodeClassName)) {					// If environmentalNode not yet found(still null) and node is type DzEnvironmentNode then
    			return node;
    		}
    	}
    	return nullptr;
    }
    
    /// <summary>
    /// Execute action
    /// </summary>
    void NpKeyMoverTestAction::executeAction()
    {
    	const DzMainWindow* mainWindow = dzApp->getInterface();
    	if (mainWindow) {																	// If main window exists then. Should always be true.
    		DzViewportMgr* viewport_manager = mainWindow->getViewportMgr();					// Get viewport manager
    		if (viewport_manager) {															// If successful in getting view port manager then
    			for (int i = 0; i < viewport_manager->getNumUserDrawStyles(); ++i) {		// Step through each of the user draw styles
    				DzUserDrawStyle* draw_style = viewport_manager->getUserDrawStyle(i);	// Get user draw style
    				try {	// getUserDrawStyle is static so we can not test to make sure that it returned 	// getUserDrawStyle is static so can not test if successful
    					QString uStyleName = draw_style->getName();							// Try to get name from user draw style
    					if (uStyleName.compare("NVIDIA Iray", Qt::CaseInsensitive)) {		// If style name is "NVIDIA Iray" then
    						DzViewport* viewport = viewport_manager->getActiveViewport();	// Get the active viewport
    						if (viewport) {													// If there is an active viewport then
    							Dz3DViewport* viewport_3d = viewport->get3DViewport();		// Get 3D viewport
    							if (viewport_3d) {											// If successful then
    								viewport_3d->setDrawStyle(draw_style);					// Set the 3D viewport style to NVIDIA Iray
    							}
    						}
    					}
    				}
    				catch (...) {
    					dzApp->warning("UserDrawStyle " + QString::number(i) + " is not valid.");	// getUserDrawStyle(i) failed, write warning to log file
    				}
    			}
    		}
    	}
    	// Check if there is already a DzEnvironmentNode in the scene
    	DzNode* environmentalNode = findSceneNodeByClass("DzEnvironmentNode");				// Try to get pointer to existing DzEnvironmentNode
    	if (!environmentalNode) {															// If environmentalNode not found then
    		createByClassNodeScript("DzEnvironmentNode");									// Create and add a new DzTonemapperNode to the scene
    		environmentalNode = findSceneNodeByClass("DzEnvironmentNode");					// Try to get pointer to new DzTonemapperNode
    	}
    	if (environmentalNode) {															// If DzTonemapperNode in scene then
    		DzProperty* drawDomeProperty = environmentalNode->findPropertyByLabel("Draw Dome");	// Try to get pointer to Draw Dome property
    		if (drawDomeProperty) {															// If successful then
    			DzBoolProperty* drawDomePropertyOnOff = dynamic_cast<DzBoolProperty*>(drawDomeProperty);	// findPropertyByLabel returned pointer to set value we need a DzBoolProperty
    			if (drawDomePropertyOnOff && !drawDomePropertyOnOff->getBoolValue()) {		// If drawDomeProperty is of type DzBoolProperty and is false then
    				drawDomePropertyOnOff->setBoolValue(true);								// Set value to true
    			}
    		}
    	}
    	if (!findSceneNodeByClass("DzTonemapperNode")) {									// If DzTonemapperNode not found in scene then
    		createByClassNodeScript("DzTonemapperNode");									// Create and add a new DzTonemapperNode to the scene
    	}
    }

     

    Post edited by surreal on
  • MehdiZangenehBarMehdiZangenehBar Posts: 69
    edited February 25

    ok, thanks, code you please wrap your code using <pre> code </pre> tag? it is very hard to read in single line (that is forum problem)

    Post edited by MehdiZangenehBar on
  • OK, I just saw your code, so there no way to instansiate a class by name or any other trick?
    for the first part, checking classname is not better way?

     

        DzViewportMgr *viewport_manager = dzApp->getInterface()->getViewportMgr();
        DzViewport *viewport = viewport_manager->getActiveViewport();
        Dz3DViewport *viewport_3d = viewport->get3DViewport();
        
        for (int i = 0; i < viewport_manager->getNumUserDrawStyles(); ++i)
        {
            DzUserDrawStyle *style = viewport_manager->getUserDrawStyle(i);
            if (style && QString(style->metaObject()->className()) == "DzIrayDrawStyle")
            {
                viewport_3d->setDrawStyle(style);
                break;
            }
        }
    
  • MehdiZangenehBarMehdiZangenehBar Posts: 69
    edited February 25

    I don't undrstand why DzEnvironmentNode is not exposed in SDK!, but don't undrstand more why a scene node used to store environment data!

    Post edited by MehdiZangenehBar on
  • I also don't undrstand why auto creation of the environment and tonemap nodes will be executed synchronously?! the code should not be executed line by line?!

  • surrealsurreal Posts: 176
    edited February 27

    MehdiZangenehBar said:

    so there no way to instansiate a class by name or any other trick?

    You can instance a DzNode by

    #include <dznode.h>
    
    ...
    
    DzNode* aNode = new DzNode();   // Get pointer to node first
                             // so you can modify the node without having to search for it after you have added it to the scene
    
    dzScene->addNode(aNode);
    

    However, because the SDK was created before DzEnvironmentNode there is no "dzenvironmentnode.h" file in the SDK. So you can not instance a DzEnvironmentNode with "new DzEnvironmentNode();"

    When the SDK is next updated, I would expect it to be included then. 

    You asked why don't they include it!
    When you maintain an SDK you have to be very careful of how, what and when you update, or you end up making a lot of extra work for yourself. It is easiest to hold off on minor changes and just issue big changes. It has been said that an update to the SDK is overdue!

    The above is not the only way to instance a class. I provided just one alternate way in the example I gave you. I will leave it to you as homework to read the Qt documentation to find the other ways.

    MehdiZangenehBar said:

    for the first part, checking classname is not better way?

    Yes there are other ways to check an objects class too, I provided just one of them.

    MehdiZangenehBar said:

    don't undrstand more why a scene node used to store environment data!

    A scene node can be used for many purposes. They provide an uncomplicated way to include something in the scene file when the user hits save. You can of course include information in the scene file using other methods however why create complexity when you already have a readymade option.

    MehdiZangenehBar said:

    I also don't undrstand why auto creation of the environment and tonemap nodes will be executed synchronously?

    You could have environment and tonemap nodes stored in an asset file and use code to merge that into the scene. You would need to think of how you are going to manage the situation where one or both environment and tonemap already exists in the scene. There are lots of ways, it’s all in the SDK documentation smiley


    Have fun.

     

     

     

    Post edited by surreal on
  • Some things it is suggested you might want to look at:

    DzScene::findNode()
    DzApp::findClassFactory()
    DzClassFactory::createInstance()
    qobject_cast<DzNode*>()
    DzScene::addNode()

  • MehdiZangenehBarMehdiZangenehBar Posts: 69
    edited February 28

    Richard Haseltine said:

    Some things it is suggested you might want to look at:

    DzScene::findNode()
    DzApp::findClassFactory()
    DzClassFactory::createInstance()
    qobject_cast<DzNode*>()
    DzScene::addNode()

    Brilliant!
    That was what I was looking for!
    Now we can instanciate a class which is not exist in SDK!

        dzScene->addNode(qobject_cast<DzNode*>(dzApp->findClassFactory("DzEnvironmentNode")->createInstance()));

    Post edited by MehdiZangenehBar on
  • MehdiZangenehBar said:

    Richard Haseltine said:

    Some things it is suggested you might want to look at:

    DzScene::findNode()
    DzApp::findClassFactory()
    DzClassFactory::createInstance()
    qobject_cast<DzNode*>()
    DzScene::addNode()

    Brilliant!
    That was what I was looking for!
    Now we can instanciate a class which is not exist in SDK!

        dzScene->addNode(qobject_cast<DzNode*>(dzApp->findClassFactory("DzEnvironmentNode")->createInstance()));

    Don't forget to add error checking

Sign In or Register to comment.