MArgDatabase copy assignment doesn't work?

43 views
Skip to first unread message

Michael Boon

unread,
Mar 2, 2017, 6:13:44 PM3/2/17
to Python Programming for Autodesk Maya
Hey, I'm trying to process arguments in a C++ plugin. I'm a Python guy, not C++, so little things like constructors trip me up a lot.

If I do it like this:
MArgDatabase inputArgs(syntax(), argList);

it works fine.

But I want inputArgs to be a member variable, so it's already been constructed. I thought I would be able to copy it like this:
inputArgs = MArgDatabase(syntax(), args);

but that doesn't work. Can anyone explain why? I thought C++ classes automatically got copy assignment operators.

And what's the right way to populate an MArgDatabase that already exists? Or should I use a pointer (with the added complexity and memory management that I'm also not used to).

Thanks!

Michael Boon

unread,
Mar 2, 2017, 6:16:20 PM3/2/17
to Python Programming for Autodesk Maya
By "doesn't work" I mean it doesn't populate inputArgs with the supplied flags. syntax() gets called, and the plugin will reject bad flags, but any subsequent isFlagSet call I do returns false.

Justin Israel

unread,
Mar 2, 2017, 8:14:58 PM3/2/17
to Python Programming for Autodesk Maya
What kind of plugin is it and why do you want to store it in the constructor? Is there a reason to not use it when you actually need to process the args?


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/6aa917a3-89e7-4adc-aa4a-d8e62e8b0d0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Boon

unread,
Mar 2, 2017, 9:30:56 PM3/2/17
to Python Programming for Autodesk Maya
Sorry I have confused you there. I'm not storing it in the constructor, specifically. 

I am storing it though. The reason is this plugin has a lot of history and as a result has dozens of seldom-used flags. I'm storing the MArgDatabase to avoid maintaining pages of repeated code that copies flags from the MArgDatabase object into bools in some custom object.

I'm using a pointer (std::unique_ptr) now so I can call the constructor the usual way. Seems to work fine.

I'm still not sure what I have misinterpreted about default copy assignment operator, eg here. After reading that, I thought that it should be possible to copy one MArgDatabase to another.


On Friday, 3 March 2017 12:14:58 UTC+11, Justin Israel wrote:
What kind of plugin is it and why do you want to store it in the constructor? Is there a reason to not use it when you actually need to process the args?


On Fri, Mar 3, 2017 at 12:16 PM Michael Boon <boon...@gmail.com> wrote:
By "doesn't work" I mean it doesn't populate inputArgs with the supplied flags. syntax() gets called, and the plugin will reject bad flags, but any subsequent isFlagSet call I do returns false.


On Friday, 3 March 2017 10:13:44 UTC+11, Michael Boon wrote:
Hey, I'm trying to process arguments in a C++ plugin. I'm a Python guy, not C++, so little things like constructors trip me up a lot.

If I do it like this:
MArgDatabase inputArgs(syntax(), argList);

it works fine.

But I want inputArgs to be a member variable, so it's already been constructed. I thought I would be able to copy it like this:
inputArgs = MArgDatabase(syntax(), args);

but that doesn't work. Can anyone explain why? I thought C++ classes automatically got copy assignment operators.

And what's the right way to populate an MArgDatabase that already exists? Or should I use a pointer (with the added complexity and memory management that I'm also not used to).

Thanks!

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Mar 2, 2017, 9:45:53 PM3/2/17
to python_in...@googlegroups.com
On Fri, Mar 3, 2017 at 3:30 PM Michael Boon <boon...@gmail.com> wrote:
Sorry I have confused you there. I'm not storing it in the constructor, specifically. 

I am storing it though. The reason is this plugin has a lot of history and as a result has dozens of seldom-used flags. I'm storing the MArgDatabase to avoid maintaining pages of repeated code that copies flags from the MArgDatabase object into bools in some custom object.

But isn't the MArgDatabase meant to be constructed during the call of the command (assuming your plugin is a command) in order to handle the args that have been given at this time?
 

I'm using a pointer (std::unique_ptr) now so I can call the constructor the usual way. Seems to work fine.

I'm still not sure what I have misinterpreted about default copy assignment operator, eg here. After reading that, I thought that it should be possible to copy one MArgDatabase to another.

Well I can't say for certain, but looking at the header for MArgParser.h, I can see that they have an opaque void *apiData protected member. And i don't see a custom copy constructor defined. So it is possible that this class isn't safe to copy and reuse since the default copy constructor would just copy the pointer and then both the original and the copy now point to the same data. Again I don't know what the source is doing but its a guess. This is backed up by the fact that in all the API examples, you see it used within doIt() methods on the stack, to locally handle the args.

 


On Friday, 3 March 2017 12:14:58 UTC+11, Justin Israel wrote:
What kind of plugin is it and why do you want to store it in the constructor? Is there a reason to not use it when you actually need to process the args?


On Fri, Mar 3, 2017 at 12:16 PM Michael Boon <boon...@gmail.com> wrote:
By "doesn't work" I mean it doesn't populate inputArgs with the supplied flags. syntax() gets called, and the plugin will reject bad flags, but any subsequent isFlagSet call I do returns false.


On Friday, 3 March 2017 10:13:44 UTC+11, Michael Boon wrote:
Hey, I'm trying to process arguments in a C++ plugin. I'm a Python guy, not C++, so little things like constructors trip me up a lot.

If I do it like this:
MArgDatabase inputArgs(syntax(), argList);

it works fine.

But I want inputArgs to be a member variable, so it's already been constructed. I thought I would be able to copy it like this:
inputArgs = MArgDatabase(syntax(), args);

but that doesn't work. Can anyone explain why? I thought C++ classes automatically got copy assignment operators.

And what's the right way to populate an MArgDatabase that already exists? Or should I use a pointer (with the added complexity and memory management that I'm also not used to).

Thanks!

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/4fa58fe1-8e1b-41d6-926b-ddfac27a9bfa%40googlegroups.com.

Michael Boon

unread,
Mar 2, 2017, 9:54:16 PM3/2/17
to Python Programming for Autodesk Maya


On Friday, 3 March 2017 13:45:53 UTC+11, Justin Israel wrote:

On Fri, Mar 3, 2017 at 3:30 PM Michael Boon <boon...@gmail.com> wrote:
Sorry I have confused you there. I'm not storing it in the constructor, specifically. 

I am storing it though. The reason is this plugin has a lot of history and as a result has dozens of seldom-used flags. I'm storing the MArgDatabase to avoid maintaining pages of repeated code that copies flags from the MArgDatabase object into bools in some custom object.

But isn't the MArgDatabase meant to be constructed during the call of the command (assuming your plugin is a command) in order to handle the args that have been given at this time?

Ah, yes, and that's what I'm doing. 
Yes it's a command; I forgot to say that earlier.
 

I'm using a pointer (std::unique_ptr) now so I can call the constructor the usual way. Seems to work fine.

I'm still not sure what I have misinterpreted about default copy assignment operator, eg here. After reading that, I thought that it should be possible to copy one MArgDatabase to another.

Well I can't say for certain, but looking at the header for MArgParser.h, I can see that they have an opaque void *apiData protected member. And i don't see a custom copy constructor defined. So it is possible that this class isn't safe to copy and reuse since the default copy constructor would just copy the pointer and then both the original and the copy now point to the same data. Again I don't know what the source is doing but its a guess. This is backed up by the fact that in all the API examples, you see it used within doIt() methods on the stack, to locally handle the args.

Ah, thanks, that's a useful insight for me. I will have to be careful there.

For some reason when I ask questions on here, I can't set them as questions (I can't add tags either, probably related). I'll mark this as "Completed" or "No action needed" or whatever I can.


 


On Friday, 3 March 2017 12:14:58 UTC+11, Justin Israel wrote:
What kind of plugin is it and why do you want to store it in the constructor? Is there a reason to not use it when you actually need to process the args?


On Fri, Mar 3, 2017 at 12:16 PM Michael Boon <boon...@gmail.com> wrote:
By "doesn't work" I mean it doesn't populate inputArgs with the supplied flags. syntax() gets called, and the plugin will reject bad flags, but any subsequent isFlagSet call I do returns false.


On Friday, 3 March 2017 10:13:44 UTC+11, Michael Boon wrote:
Hey, I'm trying to process arguments in a C++ plugin. I'm a Python guy, not C++, so little things like constructors trip me up a lot.

If I do it like this:
MArgDatabase inputArgs(syntax(), argList);

it works fine.

But I want inputArgs to be a member variable, so it's already been constructed. I thought I would be able to copy it like this:
inputArgs = MArgDatabase(syntax(), args);

but that doesn't work. Can anyone explain why? I thought C++ classes automatically got copy assignment operators.

And what's the right way to populate an MArgDatabase that already exists? Or should I use a pointer (with the added complexity and memory management that I'm also not used to).

Thanks!

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Justin Israel

unread,
Mar 2, 2017, 10:06:07 PM3/2/17
to python_in...@googlegroups.com
On Fri, Mar 3, 2017 at 3:54 PM Michael Boon <boon...@gmail.com> wrote:


On Friday, 3 March 2017 13:45:53 UTC+11, Justin Israel wrote:

On Fri, Mar 3, 2017 at 3:30 PM Michael Boon <boon...@gmail.com> wrote:
Sorry I have confused you there. I'm not storing it in the constructor, specifically. 

I am storing it though. The reason is this plugin has a lot of history and as a result has dozens of seldom-used flags. I'm storing the MArgDatabase to avoid maintaining pages of repeated code that copies flags from the MArgDatabase object into bools in some custom object.

But isn't the MArgDatabase meant to be constructed during the call of the command (assuming your plugin is a command) in order to handle the args that have been given at this time?

Ah, yes, and that's what I'm doing. 
Yes it's a command; I forgot to say that earlier.
 

I'm using a pointer (std::unique_ptr) now so I can call the constructor the usual way. Seems to work fine.

I'm still not sure what I have misinterpreted about default copy assignment operator, eg here. After reading that, I thought that it should be possible to copy one MArgDatabase to another.

Well I can't say for certain, but looking at the header for MArgParser.h, I can see that they have an opaque void *apiData protected member. And i don't see a custom copy constructor defined. So it is possible that this class isn't safe to copy and reuse since the default copy constructor would just copy the pointer and then both the original and the copy now point to the same data. Again I don't know what the source is doing but its a guess. This is backed up by the fact that in all the API examples, you see it used within doIt() methods on the stack, to locally handle the args.

Ah, thanks, that's a useful insight for me. I will have to be careful there.

For some reason when I ask questions on here, I can't set them as questions (I can't add tags either, probably related). I'll mark this as "Completed" or "No action needed" or whatever I can.

No worries. To be honest, even as a moderator, I haven't used the tagging system because I mostly interface with this list via email. I only use the web interface when I need to boot some spammers. The web interface for google groups is horrible.
 


 


On Friday, 3 March 2017 12:14:58 UTC+11, Justin Israel wrote:
What kind of plugin is it and why do you want to store it in the constructor? Is there a reason to not use it when you actually need to process the args?


On Fri, Mar 3, 2017 at 12:16 PM Michael Boon <boon...@gmail.com> wrote:
By "doesn't work" I mean it doesn't populate inputArgs with the supplied flags. syntax() gets called, and the plugin will reject bad flags, but any subsequent isFlagSet call I do returns false.


On Friday, 3 March 2017 10:13:44 UTC+11, Michael Boon wrote:
Hey, I'm trying to process arguments in a C++ plugin. I'm a Python guy, not C++, so little things like constructors trip me up a lot.

If I do it like this:
MArgDatabase inputArgs(syntax(), argList);

it works fine.

But I want inputArgs to be a member variable, so it's already been constructed. I thought I would be able to copy it like this:
inputArgs = MArgDatabase(syntax(), args);

but that doesn't work. Can anyone explain why? I thought C++ classes automatically got copy assignment operators.

And what's the right way to populate an MArgDatabase that already exists? Or should I use a pointer (with the added complexity and memory management that I'm also not used to).

Thanks!

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/61f901ac-f733-47a6-b72e-38db79e443f0%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages