PDA

View Full Version : api varable size problem


fbitonti
11-02-2006, 01:23 AM
I am trying to use the number of edges in a mesh to define the size of varable. however whever i compile it I get an error. If i enter in two numbers instead of the varriables. it lines with the error look like this.

edgeIndex = edgeIter.index();

char vertex[edgeIndex][edgeIndex];


if any one knows how to make this work please let me know. I'm kind of new to c++ so this is most likley a simple question.

Robert Bateman
11-02-2006, 10:34 AM
yeah, you can't really do that in C++, you need to allocate the data dynamically. The easiest way would be to use std::vector, ie :


#include <vector>
typedef std::vector< char > charArray;
typedef std::vector< charArray > char2Array;

edgeIndex = edgeIter.index();

char2Array vertex;
vertex.resize(edgeIndex);
for(int i=0;i=vertex.size();++i)
{
vertex[i].resize(edgeIndex);
}

// should now be able to do :
//
// vertex[0][0] = 0;

fbitonti
11-02-2006, 02:42 PM
Thats interesting and i'll definatly keep this post but it turns out that I didn't need to do that but I am having trouble doing something similar I am trying to do something like this. I want to be able to use a variable to designate the position of data in an array. for example in this one i use a counter to set the location of data in an array. this is a mel script that shows the structure of what I want to do but not exactly this. I would like to do something similar in the api.

global proc sortColorZone1 (){

FEMresultArray;
selFEMresult;
string $FEM_selected[] = `ls -sl`;
$femSize=size($FEM_selected);
global string $stressOne[];
global string $stressTwo[];
global string $stressThree[];
global string $stressFour[];
global string $stressFive[];
global string $stressSix[];
global string $stressSeven[];
global string $stressEight[];
int $a = 0;

for ($i = 0; $i < $femSize; $i++) {

string $FEM_shapeNode[] = `pickWalk -d "down" $FEM_selected[$i]`;
int $FEM_getTheColor = getAttr ($FEM_shapeNode[$i] + ".overrideColor");

//sort edges
if ($FEM_getTheColor == 24){

$stressOne[$a] = "strutMesh.e[" + $i + "]";
$a++;
}

}
}

Robert Bateman
11-02-2006, 05:34 PM
Yes, arrays don't resize in C++! however, std::vector's do.....

I'm wondering why it is you are concatonating things into strings though?

anyhow, with std::vector you can do :


#include <vector>

std::vector< int > intArray;
for(int i=0;i!=10;++i)
{
intArray.push_back(i);
}


or, if for some reason you want to use a string, you can do :


#include <string>

std::string str;
for(int i='a'; i<='z';++i)
{
str += (char) i;
}

std::cout << str.c_str() << std::endl;


In C++, arrays are close to useless. std::vector<> is the standard tool to do dynamic arrays in C++. If you are in the Maya API, there are a few other types like MString or MIntArray which may be useful - but essentially they do the same thing.

fbitonti
11-02-2006, 07:16 PM
basicaly i'm very new to this so i'm trying to modify some code that came with david gould's book. I marked where i added your code.

I am still getting errors when I compile.

//
// This file accompanies the book "Complete Maya Programming (Volume 2)"
// For further information visit http://www.davidgould.com
//
// Copyright (C) 2004 David Gould
// All rights reserved.
//
#include <maya/MSimple.h>
#include <maya/MGlobal.h>
#include <maya/MItSelectionList.h>
#include <maya/MItMeshEdge.h>
#include <maya/MDagPath.h>

//added this after your post
#include <vector>

std::vector< float > intArray;
typedef std::vector< floatArray > float2Array;

DeclareSimpleCommand( meshInfo2, "David Gould", "1.0" );

MStatus meshInfo2::doIt( const MArgList& args )
{
MStatus stat = MS::kSuccess;

MSelectionList selection;
MGlobal::getActiveSelectionList( selection );

MDagPath dagPath;
MObject component;

int edgeCount, v0Index, v1Index, edgeIndex;

int count;
count = 0;

MPoint v0, v1;

MString txt;
MItSelectionList iter( selection );
for ( ; !iter.isDone(); iter.next() )
{
iter.getDagPath( dagPath, component );

MItMeshEdge edgeIter( dagPath, component, &stat );
if( stat == MS::kSuccess )
{
txt += dagPath.fullPathName() + "\n";

edgeCount = edgeIter.count();

txt += MString("# Edges: ") + edgeCount + "\n";

for( ; !edgeIter.isDone(); edgeIter.next() )
{
edgeIndex = edgeIter.index();

char vertex[10000][4];

v0Index = edgeIter.index(0);
v1Index = edgeIter.index(1);

v0 = edgeIter.point( 0, MSpace::kWorld );
v1 = edgeIter.point( 1, MSpace::kWorld );

txt = txt + "Edge " + edgeIndex + ": " +
v0Index + " (" + v0.x + ", " + v0.y + ", " + v0.z + ") " +
v1Index + " (" + v1.x + ", " + v1.y + ", " + v1.z + ", " + count ")\n";

//ADDED THIS AFTER YOUR POST
float2Array[count*2+1].push_back(v0.x);

count = count+1;
}
}

}


MGlobal::displayInfo( txt );

return MS::kSuccess;
}





these are the error messages


c:\documents and settings\frank\my documents\scripts\curveinfo1\curveinfo1cmd.cpp(18) : error C2065: 'floatArray' : undeclared identifier
c:\documents and settings\frank\my documents\scripts\curveinfo1\curveinfo1cmd.cpp(68) : error C2143: syntax error : missing ';' before 'string'
c:\documents and settings\frank\my documents\scripts\curveinfo1\curveinfo1cmd.cpp(70) : error C2143: syntax error : missing ';' before '['
c:\documents and settings\frank\my documents\scripts\curveinfo1\curveinfo1cmd.cpp(70) : error C2337: 'count' : attribute not found
c:\documents and settings\frank\my documents\scripts\curveinfo1\curveinfo1cmd.cpp(70) : error C2143: syntax error : missing ']' before '*'
c:\documents and settings\frank\my documents\scripts\curveinfo1\curveinfo1cmd.cpp(70) : error C2143: syntax error : missing ';' before '*'
c:\documents and settings\frank\my documents\scripts\curveinfo1\curveinfo1cmd.cpp(70) : error C2100: illegal indirection
c:\documents and settings\frank\my documents\scripts\curveinfo1\curveinfo1cmd.cpp(70) : error C2143: syntax error : missing ';' before ']'
c:\documents and settings\frank\my documents\scripts\curveinfo1\curveinfo1cmd.cpp(70) : error C2143: syntax error : missing ';' before ']'

fbitonti
11-02-2006, 07:18 PM
//
// This file accompanies the book "Complete Maya Programming (Volume 2)"
// For further information visit http://www.davidgould.com
//
// Copyright (C) 2004 David Gould
// All rights reserved.
//
#include <maya/MSimple.h>
#include <maya/MGlobal.h>
#include <maya/MItSelectionList.h>
#include <maya/MItMeshEdge.h>
#include <maya/MDagPath.h>

//added this after your post
#include <vector>

std::vector< float > intArray;
typedef std::vector< floatArray > float2Array;

DeclareSimpleCommand( meshInfo2, "David Gould", "1.0" );

MStatus meshInfo2::doIt( const MArgList& args )
{
MStatus stat = MS::kSuccess;

MSelectionList selection;
MGlobal::getActiveSelectionList( selection );

MDagPath dagPath;
MObject component;

int edgeCount, v0Index, v1Index, edgeIndex;

int count;
count = 0;

MPoint v0, v1;

MString txt;
MItSelectionList iter( selection );
for ( ; !iter.isDone(); iter.next() )
{
iter.getDagPath( dagPath, component );

MItMeshEdge edgeIter( dagPath, component, &stat );
if( stat == MS::kSuccess )
{
txt += dagPath.fullPathName() + "\n";

edgeCount = edgeIter.count();

txt += MString("# Edges: ") + edgeCount + "\n";

for( ; !edgeIter.isDone(); edgeIter.next() )
{
edgeIndex = edgeIter.index();

char vertex[10000][4];

v0Index = edgeIter.index(0);
v1Index = edgeIter.index(1);

v0 = edgeIter.point( 0, MSpace::kWorld );
v1 = edgeIter.point( 1, MSpace::kWorld );

txt = txt + "Edge " + edgeIndex + ": " +
v0Index + " (" + v0.x + ", " + v0.y + ", " + v0.z + ") " +
v1Index + " (" + v1.x + ", " + v1.y + ", " + v1.z + ", " + count ")\n";

//ADDED THIS AFTER YOUR POST
float2Array[count*2+1].push_back(v0.x);

count = count+1;
}
}

}


MGlobal::displayInfo( txt );

return MS::kSuccess;
}

fbitonti
11-02-2006, 07:22 PM
//
// This file accompanies the book "Complete Maya Programming (Volume 2)"
// For further information visit http://www.davidgould.com
//
// Copyright (C) 2004 David Gould
// All rights reserved.
//
#include <maya/MSimple.h>
#include <maya/MGlobal.h>
#include <maya/MItSelectionList.h>
#include <maya/MItMeshEdge.h>
#include <maya/MDagPath.h>

//added this after your post
#include <vector>

std::vector< float > intArray;
typedef std::vector< floatArray > float2Array;

DeclareSimpleCommand( meshInfo2, "David Gould", "1.0" );

MStatus meshInfo2::doIt( const MArgList& args )
{
MStatus stat = MS::kSuccess;

MSelectionList selection;
MGlobal::getActiveSelectionList( selection );

MDagPath dagPath;
MObject component;

int edgeCount, v0Index, v1Index, edgeIndex;

int count;
count = 0;

MPoint v0, v1;

MString txt;
MItSelectionList iter( selection );
for ( ; !iter.isDone(); iter.next() )
{
iter.getDagPath( dagPath, component );

MItMeshEdge edgeIter( dagPath, component, &stat );
if( stat == MS::kSuccess )
{
txt += dagPath.fullPathName() + "\n";

edgeCount = edgeIter.count();

txt += MString("# Edges: ") + edgeCount + "\n";

for( ; !edgeIter.isDone(); edgeIter.next() )
{
edgeIndex = edgeIter.index();

char vertex[10000][4];

v0Index = edgeIter.index(0);
v1Index = edgeIter.index(1);

v0 = edgeIter.point( 0, MSpace::kWorld );
v1 = edgeIter.point( 1, MSpace::kWorld );

txt = txt + "Edge " + edgeIndex + ": " +
v0Index + " (" + v0.x + ", " + v0.y + ", " + v0.z + ") " +
v1Index + " (" + v1.x + ", " + v1.y + ", " + v1.z + ", " + count ")\n";

//ADDED THIS AFTER YOUR POST
float2Array[count*2+1].push_back(v0.x);

count = count+1;
}
}

}


MGlobal::displayInfo( txt );

return MS::kSuccess;
}

Robert Bateman
11-03-2006, 10:48 AM
the first problem is that it should be :

typedef std::vector< float > floatArray;
typedef std::vector< floatArray > float2Array;

the 2nd problem is that you are still not allocating any memory to the standard vectors. Take a look at the code i posted that resized the 2D array. If you dont allocate enough elements in the array before inserting data into it, it will crash.

fbitonti
11-03-2006, 04:13 PM
Could you explain more about allocating any memory: like you did in this example

#include <vector>
typedef std::vector< char > charArray;
typedef std::vector< charArray > char2Array;

edgeIndex = edgeIter.index();

char2Array vertex;
vertex.resize(edgeIndex);
for(int i=0;i=vertex.size();++i)
{
vertex[i].resize(edgeIndex);
}

Do I have to use a loop or can I do this staticaly. secondly I don't realy understand how to make this a two dimensional array all the examples we talked about where one dimensional..it seems to me... like i said i am new to this so please excuse me for being a bit dense. my code right now looks like this.


#include <maya/MSimple.h>
#include <maya/MGlobal.h>
#include <maya/MItSelectionList.h>
#include <maya/MItMeshEdge.h>
#include <maya/MDagPath.h>
#include <vector>
typedef std::vector< float > floatArray;
typedef std::vector< floatArray > float2Array;

DeclareSimpleCommand( meshInfo2, "new_loop_test, "1" );

MStatus meshInfo2::doIt( const MArgList& args )
{
MStatus stat = MS::kSuccess;

MSelectionList selection;
MGlobal::getActiveSelectionList( selection );

MDagPath dagPath;
MObject component;

int edgeCount, v0Index, v1Index, edgeIndex;

int count;
count = 0;

MPoint v0, v1;

MString txt;
MItSelectionList iter( selection );
float2Array vertex;

for ( ; !iter.isDone(); iter.next() )
{
iter.getDagPath( dagPath, component );

MItMeshEdge edgeIter( dagPath, component, &stat );
if( stat == MS::kSuccess )
{
txt += dagPath.fullPathName() + "\n";

edgeCount = edgeIter.count();

txt += MString("# Edges: ") + edgeCount + "\n";

for( ; !edgeIter.isDone(); edgeIter.next() )
{
edgeIndex = edgeIter.index();

v0Index = edgeIter.index(0);
v1Index = edgeIter.index(1);

//these are the variables I want to store in an like this array[][]

v0 = edgeIter.point( 0, MSpace::kWorld );
v1 = edgeIter.point( 1, MSpace::kWorld );
vertex[count*2].push_back(v0.x);
vertex[count*2].push_back(v0.y);
vertex[count*2].push_back(v0.z);
vertex[count*2+1].push_back(v1.x);
vertex[count*2+1].push_back(v1.y);
vertex[count*2+1].push_back(v1.z);

//end variables for array

txt = txt + "Edge " + edgeIndex + ": " +
v0Index + " (" + v0.x + ", " + v0.y + ", " + v0.z + ") " +
v1Index + " (" + v1.x + ", " + v1.y + ", " + v1.z + ", " + count + ")\n";

count = count+1;
}
}
}

MGlobal::displayInfo( txt );

return MS::kSuccess;
}

thank you so much for the help.

fbitonti
11-04-2006, 01:56 PM
I figured it out thanks a bunch for the help.

CGTalk Moderation
11-04-2006, 01:56 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.