facegroup crash

I just created a super simple action and in execute action, I create a super simple plane. after creating the mesh, as soon as I want to hide a facegroup in geometry editor, dazstudio will crash.
What I should do in my code to prevent this?! I can upload any other part of my project if needed.
 

Kodak.cpp

#include "Kodak.h"
#include "dzapp.h"
#include "dzscene.h"
#include "dznode.h"
#include "dzobject.h"
#include "dzfacetshape.h"
#include "dzfacetmesh.h"
#include "dzdefaultmaterial.h"

Kodak::Kodak() : DzAction("Kodak", "Executes the Kodak action")
{
    setObjectName(Kodak::metaObject()->className());
}

void Kodak::executeAction()
{
    DzNode* node = new DzNode();
    node->setName("MinimalMesh");

    DzObject* obj = new DzObject();
    DzFacetShape* shape = new DzFacetShape();
    DzFacetMesh* mesh = new DzFacetMesh();
    DzDefaultMaterial* material = new DzDefaultMaterial();

    obj->addShape(shape);
    shape->setFacetMesh(mesh);

    material->setName("Default");
    material->setLabel(material->getName());
    shape->addMaterial(material);

    node->setObject(obj);

    mesh->beginEdit(false);

    mesh->addVertex(DzVec3(-10.0, 0.0, -10.0));
    mesh->addVertex(DzVec3( 10.0, 0.0, -10.0));
    mesh->addVertex(DzVec3( 10.0, 0.0,  10.0));
    mesh->addVertex(DzVec3(-10.0, 0.0,  10.0));

    int facet_indices[4] = {0, 1, 2, 3};
    mesh->addFacet(facet_indices, facet_indices);

    mesh->finishEdit();

    dzScene->addNode(node);
}

CMakeLists.txt (I want to make sure I used correct references)

cmake_minimum_required(VERSION 3.10)
project(Kodak)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Paths
set(DAZ_SDK_PATH "D:/Software/DazStudio/DAZStudio4.5+ SDK")
set(DAZ_PLUGIN_PATH "C:/Daz 3D/Applications/64-bit/DAZ 3D/DAZStudio4/plugins/Kodak")

# DAZ SDK Paths
set(DAZ_SDK_INCLUDE "${DAZ_SDK_PATH}/include")
set(DAZ_SDK_LIB "${DAZ_SDK_PATH}/lib/x64")
set(DAZ_SDK_BIN "${DAZ_SDK_PATH}/bin/x64")

# Include Directories
include_directories(
    "${DAZ_SDK_INCLUDE}"
    "${DAZ_SDK_INCLUDE}"
    "${DAZ_SDK_INCLUDE}/QtCore"
    "${DAZ_SDK_INCLUDE}/QtGui"
    "${DAZ_SDK_INCLUDE}/QtScript"
)

# Library Directories
link_directories(
    "${DAZ_SDK_LIB}"
    "${DAZ_SDK_BIN}"
)

# Headers for MOC processing
set(MOC_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/Kodak.h")

add_custom_command(
    OUTPUT moc_Kodak.cpp
    COMMAND "${DAZ_SDK_BIN}/moc.exe" "${MOC_HEADER}" -o moc_Kodak.cpp
    DEPENDS "${MOC_HEADER}"
)

add_library(Kodak SHARED
    Plugin.cpp
    Kodak.cpp
    moc_Kodak.cpp
    Kodak.def
)

set_target_properties(Kodak PROPERTIES LINK_FLAGS "/DEF:Kodak.def")

# Linking Libraries
target_link_libraries(Kodak
    dzcore.lib
    QtCore4.lib
    QtGui4.lib
    QtScript4.lib
)

# Copy Plugin to DAZ Studio Plugins Folder
add_custom_command(
    TARGET Kodak
    POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy
    "$"
    "${DAZ_PLUGIN_PATH}/$"
)

Comments

  • I added any update/invalidate/referesh that i found, but still crash in geometry editor, the only working solution is saveing and loading file again.

     

        DzNode* node = new DzNode();
        DzObject* obj = new DzObject();
        DzFacetShape* shape = new DzFacetShape();
        DzFacetMesh* mesh = new DzFacetMesh();
        DzDefaultMaterial* material = new DzDefaultMaterial();
    
        node->beginEdit();
        mesh->beginEdit(false);
        shape->beginEdit();
    
        mesh->addVertex(DzVec3(-10.0, 0.0, -10.0));
        mesh->addVertex(DzVec3( 10.0, 0.0, -10.0));
        mesh->addVertex(DzVec3( 10.0, 0.0,  10.0));
        mesh->addVertex(DzVec3(-10.0, 0.0,  10.0));
    
        int facet_indices[4] = {0, 1, 2, 3};
        mesh->addFacet(facet_indices, facet_indices);
    
        node->setName("MinimalMesh");
        material->setName("Default");
        material->setLabel(material->getName());
        obj->addShape(shape);
        shape->setFacetMesh(mesh);
        node->setObject(obj);
        shape->addMaterial(material);
        dzScene->addNode(node);
    
        node->finishEdit();
        mesh->finishEdit();
        shape->finishEdit();
        
        dzScene->invalidate();
        dzScene->update();
        obj->invalidateAllCachedGeom();
        obj->invalidateCache();
        shape->invalidateWorkingMesh();
    
  • surrealsurreal Posts: 198
    edited March 8

    You should use DEBUG in Visual Studio to find exactly where your code is crashing.

    Both your executeAction() code samples did not crash for me.

    However, I did not use your CMakeLists.txt code. Assuming you do not have anything complex in your Kodak.h file, your problem may be in your CMakeLists.txt code.

    My project's CMakeLists.txt looks like that below. And because my action(Kodak.h) file uses the 'Q_OBJECT' macro I make sure I have 

    #include "moc_Kodak.cpp"

    at the bottom of my Kodak.cpp file. See the 'The MOC (Meta-Object Compiler) pre-compiler:' and 'When to use MOC (and when not to):'
    sections in the SDK documentation.

    #**********************************************************************
    #	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 or for more
    #	information about the DAZ Studio SDK.
    #**********************************************************************
    
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(DZ_PLUGIN_TGT_NAME MyTestplugin)
    set(DZ_PLUGIN_PROJECT_NAME "MyTestplugin")
    
    if (WIN32)
    	set(OS_SOURCES
    		MyTestplugin.rc
    		MyTestplugin.def
    	)
    elseif(APPLE)
    	set(OS_SOURCES "")
    endif()
    
    add_library( ${DZ_PLUGIN_TGT_NAME} SHARED
    	Kodak.cpp
    	Kodak.h
    	MyTestplugin.cpp
    	MyTestplugin.h
    	pluginmain.cpp
    	version.h
    	${OS_SOURCES}
    )
    
    target_link_libraries(${DZ_PLUGIN_TGT_NAME}
    	PRIVATE
    	dzcore
    	${DZSDK_QT_CORE_TARGET}
    	${DZSDK_QT_GUI_TARGET}
    	${DZSDK_QT_SCRIPT_TARGET}
    )
    
    set_target_properties (${DZ_PLUGIN_TGT_NAME}
    	PROPERTIES
    	FOLDER "My Plugins/MyTestplugin"
    	PROJECT_LABEL ${DZ_PLUGIN_PROJECT_NAME}
    )
    
    source_group("Resource Files" REGULAR_EXPRESSION ".*\\.(rc)")

     

    Post edited by surreal on
  • surrealsurreal Posts: 198
    edited March 8

    If I wanted a plugin called "Kodak" that only contained an action I would use a CMakeLists.txt that looked like this:

    #**********************************************************************
    #	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 or for more
    #	information about the DAZ Studio SDK.
    #**********************************************************************
    
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(DZ_PLUGIN_TGT_NAME Kodak)
    set(DZ_PLUGIN_PROJECT_NAME "Kodak")
    
    if (WIN32)
    	set(OS_SOURCES
    		Kodak.rc
    		Kodak.def
    	)
    elseif(APPLE)
    	set(OS_SOURCES "")
    endif()
    
    add_library( ${DZ_PLUGIN_TGT_NAME} SHARED
    	Kodak.cpp
    	Kodak.h
    	pluginmain.cpp
    	version.h
    	${OS_SOURCES}
    )
    
    target_link_libraries(${DZ_PLUGIN_TGT_NAME}
    	PRIVATE
    	dzcore
    	${DZSDK_QT_CORE_TARGET}
    	${DZSDK_QT_GUI_TARGET}
    	${DZSDK_QT_SCRIPT_TARGET}
    )
    
    set_target_properties (${DZ_PLUGIN_TGT_NAME}
    	PROPERTIES
    	FOLDER "My Plugins/Kodak"
    	PROJECT_LABEL ${DZ_PLUGIN_PROJECT_NAME}
    )
    
    source_group("Resource Files" REGULAR_EXPRESSION ".*\\.(rc)")

     

    and a Kodak.cpp file that looked like

    #include "dzapp.h"
    #include "dzscene.h"
    #include "dznode.h"
    #include "dzobject.h"
    #include "dzfacetshape.h"
    #include "dzfacetmesh.h"
    #include "dzdefaultmaterial.h"
    #include "Kodak.h"
    
    Kodak::Kodak() : DzAction("Kodak", "Executes the Kodak action")
    {
        setObjectName(Kodak::metaObject()->className());
    }
    
    void Kodak::executeAction()
    {
        DzNode* node = new DzNode();
        node->setName("MinimalMesh");
    
        DzObject* obj = new DzObject();
        DzFacetShape* shape = new DzFacetShape();
        DzFacetMesh* mesh = new DzFacetMesh();
        DzDefaultMaterial* material = new DzDefaultMaterial();
    
        obj->addShape(shape);
        shape->setFacetMesh(mesh);
    
        material->setName("Default");
        material->setLabel(material->getName());
        shape->addMaterial(material);
    
        node->setObject(obj);
    
        mesh->beginEdit(false);
    
        mesh->addVertex(DzVec3(-10.0, 0.0, -10.0));
        mesh->addVertex(DzVec3( 10.0, 0.0, -10.0));
        mesh->addVertex(DzVec3( 10.0, 0.0,  10.0));
        mesh->addVertex(DzVec3(-10.0, 0.0,  10.0));
    
        int facet_indices[4] = {0, 1, 2, 3};
        mesh->addFacet(facet_indices, facet_indices);
    
        mesh->finishEdit();
    
        dzScene->addNode(node);
    }
    
    #include "moc_Kodak.cpp"
    

     

    Post edited by surreal on
  • surreal said:

    You should use DEBUG in Visual Studio to find exactly where your code is crashing.

    Both your executeAction() code samples did not crash for me.

    However, I did not use your CMakeLists.txt code. Assuming you do not have anything complex in your Kodak.h file, your problem may be in your CMakeLists.txt code.

    My project's CMakeLists.txt looks like that below. And because my action(Kodak.h) file uses the 'Q_OBJECT' macro I make sure I have 

    #include "moc_Kodak.cpp"

    at the bottom of my Kodak.cpp file. See the 'The MOC (Meta-Object Compiler) pre-compiler:' and 'When to use MOC (and when not to):'
    sections in the SDK documentation.

    #**********************************************************************
    #	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 or for more
    #	information about the DAZ Studio SDK.
    #**********************************************************************
    
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(DZ_PLUGIN_TGT_NAME MyTestplugin)
    set(DZ_PLUGIN_PROJECT_NAME "MyTestplugin")
    
    if (WIN32)
    	set(OS_SOURCES
    		MyTestplugin.rc
    		MyTestplugin.def
    	)
    elseif(APPLE)
    	set(OS_SOURCES "")
    endif()
    
    add_library( ${DZ_PLUGIN_TGT_NAME} SHARED
    	Kodak.cpp
    	Kodak.h
    	MyTestplugin.cpp
    	MyTestplugin.h
    	pluginmain.cpp
    	version.h
    	${OS_SOURCES}
    )
    
    target_link_libraries(${DZ_PLUGIN_TGT_NAME}
    	PRIVATE
    	dzcore
    	${DZSDK_QT_CORE_TARGET}
    	${DZSDK_QT_GUI_TARGET}
    	${DZSDK_QT_SCRIPT_TARGET}
    )
    
    set_target_properties (${DZ_PLUGIN_TGT_NAME}
    	PROPERTIES
    	FOLDER "My Plugins/MyTestplugin"
    	PROJECT_LABEL ${DZ_PLUGIN_PROJECT_NAME}
    )
    
    source_group("Resource Files" REGULAR_EXPRESSION ".*\\.(rc)")

     

    Thank you for your response, Actually it will not crash in code at all, when the mesh is created, if we click on Geometry Editor Face/Surface it will crash. so if your code will not crash I should compare with my code, to see where is the problem 

  • i don't undrstand, my cmake is indipendent from sdk cmake, so there is no DZSDK_QT_CORE_TARGET and other variables, why you address them?

  • MehdiZangenehBarMehdiZangenehBar Posts: 139
    edited March 8

    mesh->hideAllFacets();
    cause crash, any change to face/surface(material) group, cause crash 

    Post edited by MehdiZangenehBar on
  • MehdiZangenehBarMehdiZangenehBar Posts: 139
    edited March 8

    so, this will crash even in execute action, no need to change visibility manually.
     

    #include "Kodak.h"
    #include "dzapp.h"
    #include "dzscene.h"
    #include "dznode.h"
    #include "dzobject.h"
    #include "dzfacetshape.h"
    #include "dzfacetmesh.h"
    #include "dzdefaultmaterial.h"
    
    Kodak::Kodak() : DzAction("Kodak", "Executes the Kodak action")
    {
        setObjectName(Kodak::metaObject()->className());
    }
    
    void Kodak::executeAction()
    {
        DzNode* node = new DzNode();
        node->setName("MinimalMesh");
    
        DzObject* obj = new DzObject();
        DzFacetShape* shape = new DzFacetShape();
        DzFacetMesh* mesh = new DzFacetMesh();
        DzDefaultMaterial* material = new DzDefaultMaterial();
    
        obj->addShape(shape);
        shape->setFacetMesh(mesh);
    
        material->setName("Default");
        material->setLabel(material->getName());
        shape->addMaterial(material);
    
        node->setObject(obj);
    
        mesh->beginEdit(false);
    
        mesh->addVertex(DzVec3(-10.0, 0.0, -10.0));
        mesh->addVertex(DzVec3( 10.0, 0.0, -10.0));
        mesh->addVertex(DzVec3( 10.0, 0.0,  10.0));
        mesh->addVertex(DzVec3(-10.0, 0.0,  10.0));
    
        int facet_indices[4] = {0, 1, 2, 3};
        mesh->addFacet(facet_indices, facet_indices);
    
        mesh->finishEdit();
    
        dzScene->addNode(node);
    
        mesh->hideAllFacets();
    }
    
    
    Post edited by MehdiZangenehBar on
  • Richard HaseltineRichard Haseltine Posts: 107,867
    edited March 9

    It has been pointed out to me that you are using CMAKE_CXX_STANDARD 17 which means it is not the same compiler as for the base application, and you use new to construct several types which your plug-in does not fully control the memory handling for, potentially causing issues (I hope that is the correct explanation).

    Post edited by Richard Haseltine on
  • Richard Haseltine said:

    It has been pointed out to me that you are using CMAKE_CXX_STANDARD 17 which means it is not the same compiler as for the base application, and you use new to construct several types which your plug-in does not fully control the memory handling for, potentially causing issues (I hope that is the correct explanation).

    It's not. It has nothing to do with the C++ standard, but rather the ABI.

    Compiling against a different C++ standard does not change the compiler; it is the same compiler respecting a different C++ standard.

    The concept of C++ standard is divorced from the ABI.

    As long as the compiler supports the same ABI as the compiler DAZ used, there's no problem. The bug is elsewhere.

     

  • TheMysteryIsThePointTheMysteryIsThePoint Posts: 3,216
    edited March 9

    MehdiZangenehBar said:

    so, this will crash even in execute action, no need to change visibility manually.
     

    #include "Kodak.h"
    #include "dzapp.h"
    #include "dzscene.h"
    #include "dznode.h"
    #include "dzobject.h"
    #include "dzfacetshape.h"
    #include "dzfacetmesh.h"
    #include "dzdefaultmaterial.h"
    
    Kodak::Kodak() : DzAction("Kodak", "Executes the Kodak action")
    {
        setObjectName(Kodak::metaObject()->className());
    }
    
    void Kodak::executeAction()
    {
        DzNode* node = new DzNode();
        node->setName("MinimalMesh");
    
        DzObject* obj = new DzObject();
        DzFacetShape* shape = new DzFacetShape();
        DzFacetMesh* mesh = new DzFacetMesh();
        DzDefaultMaterial* material = new DzDefaultMaterial();
    
        obj->addShape(shape);
        shape->setFacetMesh(mesh);
    
        material->setName("Default");
        material->setLabel(material->getName());
        shape->addMaterial(material);
    
        node->setObject(obj);
    
        mesh->beginEdit(false);
    
        mesh->addVertex(DzVec3(-10.0, 0.0, -10.0));
        mesh->addVertex(DzVec3( 10.0, 0.0, -10.0));
        mesh->addVertex(DzVec3( 10.0, 0.0,  10.0));
        mesh->addVertex(DzVec3(-10.0, 0.0,  10.0));
    
        int facet_indices[4] = {0, 1, 2, 3};
        mesh->addFacet(facet_indices, facet_indices);
    
        mesh->finishEdit();
    
        dzScene->addNode(node);
    
        mesh->hideAllFacets();
    }
    
    

    @MehdiZangenehBar

    DzFacetMesh::addFacet() accepts a pointer and you pass it one that'll be dangling when your function exits. The function could have taking 4 const floats, but it didn't, kind of making me wonder if the function expects to take ownership of the memory? This is an instanceof when it's not quite clear. I know it would just trade a crash for a memory leak, but try allocating it on the heap and see what happens?

    But more troubling is that you pass DzFacetMesh::addFacet() UV indices, but you don't seem to have set the UVs up anywhere, i.e. what are those indices referencing?

     

     

    Post edited by TheMysteryIsThePoint on
  • I just changed CMAKE_CXX_STANDARD 11.
    Added UV.
    Corrected possible dangling.
    but still crash, would you please check in your side to see if this crash is hapenning? or it is just for me!

    cmake_minimum_required(VERSION 3.10)
    project(Kodak)
    
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    set(DAZ_SDK_PATH "D:/Software/DazStudio/DAZStudio4.5+ SDK")
    set(DAZ_PLUGIN_PATH "C:/Daz 3D/Applications/64-bit/DAZ 3D/DAZStudio4/plugins/Kodak")
    
    set(DAZ_SDK_INCLUDE "${DAZ_SDK_PATH}/include")
    set(DAZ_SDK_LIB "${DAZ_SDK_PATH}/lib/x64")
    set(DAZ_SDK_BIN "${DAZ_SDK_PATH}/bin/x64")
    
    include_directories(
        "${DAZ_SDK_INCLUDE}"
        "${DAZ_SDK_INCLUDE}/QtCore"
        "${DAZ_SDK_INCLUDE}/QtGui"
        "${DAZ_SDK_INCLUDE}/QtScript"
    )
    
    link_directories(
        "${DAZ_SDK_LIB}"
        "${DAZ_SDK_BIN}"
    )
    
    set(MOC_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/Kodak.h")
    
    add_custom_command(
        OUTPUT moc_Kodak.cpp
        COMMAND "${DAZ_SDK_BIN}/moc.exe" "${MOC_HEADER}" -o moc_Kodak.cpp
        DEPENDS "${MOC_HEADER}"
    )
    
    add_library(Kodak SHARED
        Plugin.cpp
        Kodak.cpp
        moc_Kodak.cpp
        Kodak.def
    )
    
    set_target_properties(Kodak PROPERTIES LINK_FLAGS "/DEF:Kodak.def")
    
    target_link_libraries(Kodak
        dzcore.lib
        QtCore4.lib
        QtGui4.lib
        QtScript4.lib
    )
    
    add_custom_command(
        TARGET Kodak
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        "$"
        "${DAZ_PLUGIN_PATH}/$"
    )
    
    

    #include "Kodak.h"
    #include "dzapp.h"
    #include "dzscene.h"
    #include "dznode.h"
    #include "dzobject.h"
    #include "dzfacetshape.h"
    #include "dzfacetmesh.h"
    #include "dzdefaultmaterial.h"
    
    Kodak::Kodak() : DzAction("Kodak", "Executes the Kodak action")
    {
        setObjectName(Kodak::metaObject()->className());
    }
    
    void Kodak::executeAction()
    {
        DzNode* node = new DzNode();
        node->setName("MinimalMesh");
    
        DzObject* obj = new DzObject();
        DzFacetShape* shape = new DzFacetShape();
        DzFacetMesh* mesh = new DzFacetMesh();
        DzDefaultMaterial* material = new DzDefaultMaterial();
    
        obj->addShape(shape);
        shape->setFacetMesh(mesh);
    
        material->setName("Default");
        material->setLabel(material->getName());
        shape->addMaterial(material);
    
        node->setObject(obj);
    
        mesh->beginEdit(false);
    
        mesh->addVertex(DzVec3(-10.0, 0.0, -10.0));
        mesh->addVertex(DzVec3(10.0, 0.0, -10.0));
        mesh->addVertex(DzVec3(10.0, 0.0, 10.0));
        mesh->addVertex(DzVec3(-10.0, 0.0, 10.0));
    
        DzMap* uvMap = mesh->getUVs();
        uvMap->appendPnt2Vec(DzVec3(0.0, 0.0, 0.0));
        uvMap->appendPnt2Vec(DzVec3(1.0, 0.0, 0.0));
        uvMap->appendPnt2Vec(DzVec3(1.0, 1.0, 0.0));
        uvMap->appendPnt2Vec(DzVec3(0.0, 1.0, 0.0));
    
        mesh->preSizeFacets(1);
    
        int* facetIndices = new int[4];
        facetIndices[0] = 0;
        facetIndices[1] = 1;
        facetIndices[2] = 2;
        facetIndices[3] = 3;
        int* uvIndices = new int[4];
        uvIndices[0] = 0;
        uvIndices[1] = 1;
        uvIndices[2] = 2;
        uvIndices[3] = 3;
        mesh->addFacet(facetIndices, uvIndices);
    
        mesh->finishEdit();
    
        dzScene->addNode(node);
    
        mesh->hideAllFacets();
    }
    
    


     
  • surrealsurreal Posts: 198

    It crashes for me.

     

    The sample script @ http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/samples/geometry/generate_sphere/star
    works fine as a script. So I ported the script to C++ and it also crashes at hideAllFacets(). The problem has to be a fault with the SDK.

     

    void testCreateSphere() {
    	// Get the user input
    	QString sOrigin = "Object Center"; // "World Center"
    	float nSize = 10.f;
    	//QString sUnit = "cm"; // "m", "yd", "ft", "in"
    	int nSegments = 2;
    	int nSides = 3;
    
    	// Convert the input value to centimeters
    	//nSize = convertToCM(nSize, sUnit);
    
    	// Sanity check the input values
    	if (nSize <= 0.0 || nSegments < 2 || nSides < 3)
    	{
    		// We are done...
    		return;
    	}
    
    	// Let the user know we are busy
    	//setBusyCursor();
    
    	// Create new node
    	DzNode* oNode = new DzNode();
    
    	// Set the node name;
    	// use a name that is consistent with the create primitive action
    	oNode->setName("Sphere");
    
    	// Build the label of the node
    	QString sLabel = QString("Sphere %1x%2 %3%4")
    		.arg(nSides)
    		.arg(nSegments)
    		.arg(nSize)
    		//.arg(sUnit)
    		;
    	//oNode->setLabel(getUniqueLabel(sLabel));
    
    	// Create a new object
    	DzObject* oObject = new DzObject();
    
    	// Set the object name;
    	// use a name that is consistent with the create primitive action
    	oObject->setName(QString("pSphere(%1)%2_%3")
    		.arg(nSize)
    		.arg(nSides)
    		.arg(nSegments));
    
    	// Create a new polygonal shape
    	DzFacetShape* oFacetShape = new DzFacetShape();
    
    	// Set the shape name and label;
    	// use a name that is consistent with the create primitive action
    	oFacetShape->setName("Default");
    	oFacetShape->setLabel(oFacetShape->getName());
    
    	// Create a new polygonal mesh
    	DzFacetMesh* oFacetMesh = new DzFacetMesh();
    
    	// Create a new default material
    	DzDefaultMaterial* oMaterial = new DzDefaultMaterial();
    
    	// Set the material name and label;
    	// use a name that is consistent with the create primitive action
    	oMaterial->setName("Default");
    	oMaterial->setLabel(oMaterial->getName());
    
    	// Add the material to the shape
    	oFacetShape->addMaterial(oMaterial);
    
    	// Begin editing the mesh
    	oFacetMesh->beginEdit();
    
    	// Get the UV map
    	DzMap* oMap = oFacetMesh->getUVs();
    
    	// Activate the material; all new geometry will be added to this
    	oFacetMesh->activateMaterial(oMaterial->getName());
    
    	// Declare some variables for generating the mesh
    	int i = 0;
    	int j = 0;
    	int nNext = 0;;
    	float x = 0.f;
    	float y = 0.f;
    	float z = 0.f;
    	float nAngle = 0.f;
    	float nSinAngle = 0.f;
    	bool bAtSeam = false;
    	DzVec3 vecUVs(0.f, 0.f, 0.f);
    
    	int nVerts = nSegments - 1;
    	int nPoleSegments = nSegments - 2;
    	int nUvOffset = (nSides - 1) * 2;
    	int nHalfSize = nSize / 2;
    
    	// Pre-size the vertex array; faster than dynamic resizing
    	oFacetMesh->preSizeVertexArray(nSides * nVerts + 2);
    
    	// Create vertices for the poles
    	oFacetMesh->addVertex(0.0, 0.0, 0.0);
    	oFacetMesh->addVertex(0.0, nSize, 0.0);
    
    	// Create UVs for the poles
    	float nOffset = 0.5 / nSides;
    	for (i = 0; i < nSides; i += 1) {
    		vecUVs.m_x = i / nSides + nOffset;
    		vecUVs.m_y = 0;
    		oMap->appendPnt2Vec(vecUVs);
    		vecUVs.m_y = 1;
    		oMap->appendPnt2Vec(vecUVs);
    	}
    
    	// Create the vertices and UVs for the sides
    	for (i = 0; i < nSides; i += 1) {
    		nAngle = ((PIf * 2) * i) / nSides;
    		x = sinf(nAngle) * nHalfSize;
    		z = cosf(nAngle) * nHalfSize;
    		vecUVs.m_x = i / nSides;
    		for (j = 0; j < nVerts; j += 1) {
    			nAngle = PIf * (j + 1) / nSegments;
    			y = -cosf(nAngle) * nHalfSize + nHalfSize;
    			nSinAngle = sinf(nAngle);
    			oFacetMesh->addVertex(x * nSinAngle, y, z * nSinAngle);
    			vecUVs.m_y = (j + 1) / nSegments;
    			oMap->appendPnt2Vec(vecUVs);
    		}
    	}
    
    	// Create UVs for the seam
    	int nSeam = oMap->getNumValues();
    	vecUVs.m_x = 1;
    	for (i = 0; i < nVerts; i += 1) {
    		vecUVs.m_y = (i + 1) / nSegments;
    		oMap->appendPnt2Vec(vecUVs);
    	}
    
    	// Pre-size the facet array; faster than dynamic resizing
    	oFacetMesh->preSizeFacets(nSides * (nPoleSegments + 2));
    
    	int aVertexIndices[4];
    	int aUvIndices[4];
    
    	// Create the faces
    	for (i = 0; i < nSides; i += 1) {
    		nNext = i + 1;
    		if (nNext >= nSides) {
    			bAtSeam = true;
    			nNext = 0;
    		}
    
    		aVertexIndices[0] = 0;
    		aUvIndices[0] = i * 2;
    
    		aVertexIndices[1] = nNext * nVerts + 2;
    		aUvIndices[1] = bAtSeam ? nSeam : aVertexIndices[1] + nUvOffset;
    
    		aVertexIndices[2] = i * nVerts + 2;
    		aUvIndices[2] = aVertexIndices[2] + nUvOffset;
    
    		aVertexIndices[3] = -1;
    		aUvIndices[3] = -1;
    
    		oFacetMesh->addFacet(aVertexIndices, aUvIndices);
    
    		for (j = 0; j < nPoleSegments; j += 1) {
    			aVertexIndices[0] = i * nVerts + 2 + j;
    			aUvIndices[0] = aVertexIndices[0] + nUvOffset;
    
    			aVertexIndices[1] = nNext * nVerts + 2 + j;
    			aUvIndices[1] = bAtSeam ? nSeam + j : aVertexIndices[1] + nUvOffset;
    
    			aVertexIndices[2] = nNext * nVerts + 3 + j;
    			aUvIndices[2] = bAtSeam ? nSeam + j + 1 : aVertexIndices[2] + nUvOffset;
    
    			aVertexIndices[3] = i * nVerts + 3 + j;
    			aUvIndices[3] = aVertexIndices[3] + nUvOffset;
    
    			oFacetMesh->addFacet(aVertexIndices, aUvIndices);
    		}
    
    		aVertexIndices[0] = 1;
    		aUvIndices[0] = i * 2 + 1;
    
    		aVertexIndices[1] = i * nVerts + nVerts + 1;
    		aUvIndices[1] = aVertexIndices[1] + nUvOffset;
    
    		aVertexIndices[2] = nNext * nVerts + nVerts + 1;
    		aUvIndices[2] = bAtSeam ? nSeam + nPoleSegments : aVertexIndices[2] + nUvOffset;
    
    		aVertexIndices[3] = -1;
    		aUvIndices[3] = -1;
    
    		oFacetMesh->addFacet(aVertexIndices, aUvIndices);
    	}
    
    	// Finish editing the mesh
    	oFacetMesh->finishEdit();
    
    	// Set the mesh for the shape
    	oFacetShape->setFacetMesh(oFacetMesh);
    
    	// Add the shape to the object
    	oObject->addShape(oFacetShape);
    
    	// Add the object to the node
    	oNode->setObject(oObject);
    
    	// Get the local bounding box
    	DzBox3 boxLocal = oNode->getLocalBoundingBox();
    	DzVec3 vecMax = boxLocal.getMax();
    	DzVec3 vecMin = boxLocal.getMin();
    
    	// If the user chose the object center for the origin
    	if (sOrigin == "Object Center") {
    		// Get the middle of the height of the box
    		float nMid = (vecMax.m_y + vecMin.m_y) * 0.5;
    
    		// Set the origin; default and current
    		DzVec3 vecOrigin(0, nMid, 0);
    		oNode->setOrigin(vecOrigin, true);
    		oNode->setOrigin(vecOrigin);
    	}
    
    	// If the height of the bounding box is less than
    	// 1 unit (1cm) tall, set it to be 1 unit tall
    	if (vecMax.m_y < 1) {
    		vecMax.m_y = 1;
    	}
    
    	// Set the end point; default and current
    	DzVec3 vecEndPoint(0, vecMax.m_y, 0);
    	oNode->setEndPoint(vecEndPoint, true);
    	oNode->setEndPoint(vecEndPoint);
    
    	// Get the presentation for the node
    	DzPresentation* oPresentation = oNode->getPresentation();
    
    	// If the node did not have a presentation,
    	// create one and assign it to the node
    	if (!oPresentation) {
    		oPresentation = new DzPresentation();
    		oNode->setPresentation(oPresentation);
    	}
    
    	// Set the type of node
    	oPresentation->setType("Prop");
    
    	// Add the node to the scene
    	dzScene->addNode(oNode);
    
    	// Let the user know we are done
    	//clearBusyCursor();
    
    	DzFaceGroup* faceGrp = oFacetMesh->getFaceGroup(0);
    	oFacetMesh->showAllFacets();
    	if (faceGrp) {
    		oFacetMesh->hideFacets(faceGrp);///CRASH HERE
    		oFacetMesh->showAllFacets();
    	}
    	oFacetMesh->selectAllFacets();
    	oFacetMesh->hideSelectedFacets();///CRASH HERE
    	oFacetMesh->deselectAllFacets();
    	oFacetMesh->showAllFacets();
    	oFacetMesh->hideAllFacets();///CRASH HERE
    	oFacetMesh->showAllFacets();
    }
  • that proves that the crash is not related to my project settings

  • MehdiZangenehBarMehdiZangenehBar Posts: 139
    edited March 9

    Where we can report the bug?
    Any trick/hack to solve it?
    The only solution is to save/load file (but I don't want to do it!)
    I think someone from SDK guys should create a working example. (even super simple plane)

    Post edited by MehdiZangenehBar on
  • surrealsurreal Posts: 198
    edited March 11

    DzFacetMesh::getUVs() appears to be static, 

    In my ported code
    DzMap* oMap = oFacetMesh->getUVs();  //oMap is not null however it is also not a valid pointer. Thus oMap->appendPnt2Vec(vecUVs); does not add a vertex

    Before using DzFacetMesh::getUVs() we have to test using DzFacetMesh::getNumUVSets() 
    If there are zero UVSets then we need to create one.

    As Richard Haseltine and TheMysteryIsThePoint pointed out it is crashing because the mesh does not have a valid UVSet.

    We have to create a UV set and then add it to the mesh. The documentation does not say if the mesh[DzFacetMesh::addUVSet(DzUVSet *uvSet)] takes ownership of the UV set, that is something to findout.

    The SDK is not faulty, it just does not function exactly the same as the script API. It is always a trap when porting code there are always supple differences between SDK and script, that we too often forget about smiley

    Lots to find out to make it work,
    have fun.

    Post edited by surreal on
  • surreal said:

    DzFacetMesh::getUVs() appears to be static, 

    In my ported code
    DzMap* oMap = oFacetMesh->getUVs();  //oMap is not null however it is also not a valid pointer. Thus oMap->appendPnt2Vec(vecUVs); does not add a vertex

    Before using DzFacetMesh::getUVs() we have to test using DzFacetMesh::getNumUVSets() 
    If there are zero UVSets then we need to create one.

    As Richard Haseltine and TheMysteryIsThePoint pointed out it is crashing because the mesh does not have a valid UVSet.

    We have to create a UV set and then add it to the mesh. The documentation does not say if the mesh[DzFacetMesh::addUVSet(DzUVSet *uvSet)] takes ownership of the UV set, that is something to findout.

    The SDK is not faulty, it just does not function exactly the same as the script API. It is always a trap when porting code there are always supple differences between SDK and script, that we too often forget about smiley

    Lots to find out to make it work,
    have fun.

    Would you please update my super minimal sample with your suggestions and prove that it will not crash on your side? 

  • surrealsurreal Posts: 198
    edited March 11

    You are correct. It still crashes. The problem has noting to do with the UVSet.

    The problem relates to the edge visibility. I have submitted a support request and included the code sample below.
    Have included link to this thread.

    Your sample updated with UVSet constructor, as requested.
    I took the liberty of changing it to a triangle and using variables for vertex index instead of absolute values.
    I have included DzMap* uvMap = mesh->getUVs(); even though getNumUVSets will return 0 when mesh is new.

    #include "Kodak.h"
    #include "dzapp.h"
    #include "dzscene.h"
    #include "dznode.h"
    
    
    #include "dzobject.h"
    #include "dzfacetshape.h"
    #include "dzfacetmesh.h"
    #include "dzdefaultmaterial.h"
    
    Kodak::Kodak() : DzAction("Kodak", "Executes the Kodak action")
    {
        setObjectName(Kodak::metaObject()->className());
    }
    
    void Kodak::executeAction()
    {
    	DzNode* node = new DzNode();
    	node->setName("MinimalMesh");
    
    	DzObject* obj = new DzObject();
    	DzFacetShape* shape = new DzFacetShape();
    	DzFacetMesh* mesh = new DzFacetMesh();
    	DzDefaultMaterial* material = new DzDefaultMaterial();
    
    	obj->addShape(shape);
    	shape->setFacetMesh(mesh);
    
    	material->setName("Default");
    	material->setLabel(material->getName());
    	shape->addMaterial(material);
    
    	node->setObject(obj);
    
    	mesh->beginEdit(false);
    
    	int v0 = mesh->addVertex(DzVec3(0.0, 0.0, 0.0));
    	int v1 = mesh->addVertex(DzVec3(10.0, 10.0, 0.0));
    	int v2 = mesh->addVertex(DzVec3(10.0, 0.0, 0.0));
    	//mesh->addVertex(DzVec3(-10.0, 0.0, 10.0));
    
    	if (mesh->getNumUVSets()) {		//If UVSet exists then
    		DzMap* uvMap = mesh->getUVs();	//Use existing UVSet. Note: a new mesh has no UVSets.	
    		uvMap->appendPnt2Vec(DzVec3(0.0, 0.0, 0.0));
    		uvMap->appendPnt2Vec(DzVec3(1.0, 1.0, 0.0));
    		uvMap->appendPnt2Vec(DzVec3(1.0, 0.0, 0.0));
    	}
    	else {					//If UVSet does not exist
    		DzUVSet* newDVSet = new DzUVSet();	//Create new UVSet
    		newDVSet->appendPnt2Vec(DzVec3(0.0, 0.0, 0.0));
    		newDVSet->appendPnt2Vec(DzVec3(1.0, 1.0, 0.0));
    		newDVSet->appendPnt2Vec(DzVec3(1.0, 0.0, 0.0));
    		mesh->addUVSet(newDVSet);		//Add UVSet to mesh
    	}
    
    	mesh->preSizeFacets(1);
    
    	int* facetIndices = new int[4];
    	facetIndices[0] = v0;
    	facetIndices[1] = v1;
    	facetIndices[2] = v2;
    	facetIndices[3] = -1;
    	int* uvIndices = new int[4];
    	uvIndices[0] = v0;
    	uvIndices[1] = v1;
    	uvIndices[2] = v2;
    	uvIndices[3] = -1;
    	mesh->addFacet(facetIndices, uvIndices);
    
    	mesh->finishEdit();
    
    	dzScene->addNode(node);
    	DzEdge* edgePtr = mesh->getEdgesPtr();
    	if (!edgePtr) {
    		dzApp->debug("edgePtr is null");
    	}
    	else {
    		dzApp->debug("numEdges " + QString::number(mesh->getNumEdges()));
    		for (int i = 0; i < mesh->getNumEdges(); ++i) {
    			dzApp->debug("edgePtr idx " + QString::number(i) +
    				", facetA " + QString::number(mesh->getEdge(i).m_facetA) +
    				", facetB " + QString::number(mesh->getEdge(i).m_facetB) +
    				", vert1 " + QString::number(mesh->getEdge(i).m_vert1) +
    				", vert2 " + QString::number(mesh->getEdge(i).m_vert2));
    		}
    	}
    	mesh->hideAllFacets();	//You are correct it still crashes
    }

     

    Post edited by surreal on
  • MehdiZangenehBarMehdiZangenehBar Posts: 139
    edited March 11

    surreal said:

    You are correct. It still crashes. The problem has noting to do with the UVSet.

    That's what I am saying, nothing wrong with my initial code, it should work without even UV or UVSet setup, (as same code will not crash in DazScript)

    Post edited by MehdiZangenehBar on
Sign In or Register to comment.