How to list keys for a BaseContainer?
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 10/08/2011 at 16:43, xxxxxxxx wrote:
From what I've read so far in the documentation and on the forums, I understand that each BaseObject has a BaseContainer, which contains the attributes of that object. I thought the BaseContainer was a Python dictionary (or a subclass of it) but this doesn't seem to be the case, because it doesn't respond to dictionary methods like values() or keys().
How can I get a listing of the keys present in a given BaseContainer? I've seen some of them on the forums here (i.e. PRIM_CUBE_LEN, PRIM_CUBE_SUBX, etc.) but I can't find them anywhere in the documentation. Where should I be looking for this information?
To use a specific example, I'm trying to figure out which attributes of a MoText object I can control via Python. So I tried this:
myObject = c4d.BaseObject(1019268) doc.InsertObject(myObject) c4d.EventAdd() bc = myObject.GetDataInstance() print 'myObject keys:', bc.keys()
And got this error:
AttributeError: 'c4d.BaseContainer' object has no attribute 'keys'
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 11/08/2011 at 02:05, xxxxxxxx wrote:
Hi Jack,
You can iterate over a BaseContainer like this:
for index, value in bc: print "Index: %i, Value: %s", index, str(value)
And to find the IDs, take a look at the section "Finding the element ID" in "Symbols in CINEMA 4D" page (referenced in BaseList2D.__getitem__).
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 11/08/2011 at 23:09, xxxxxxxx wrote:
Thanks for your reply Yannick. That page in the docs is exactly what I was looking for!
I tried the code you suggested, but got the following error:
AttributeError: Parameter value not accessible (object unknown in Python)
Here is the entire script I was testing:
import c4d def main() : myObject = c4d.BaseObject(1019268) doc.InsertObject(myObject) c4d.EventAdd() bc = myObject.GetDataInstance() for index, value in bc: print "Index: %i, Value: %s", index, str(value) if __name__=='__main__': main()
Best,
Jack -
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 12/08/2011 at 00:08, xxxxxxxx wrote:
Originally posted by xxxxxxxx
You can iterate over a BaseContainer like this:
for index, value in bc: print "Index: %i, Value: %s", index, str(value)
I was able to get this loop to work with a cube primitive, but not the MoText object. With the cube, the string in the print statement wasn't formatted correctly until I changed that line to read:
print "Index: %i, Value: %s" % (index, str(value))
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 12/08/2011 at 03:04, xxxxxxxx wrote:
Lol this is a funny issue. The container of the MoText object contains a font-object which cannot be accessed with python, but within the loop the container want's to throw this font-object into the iteration. This gives this error. I don't think there is a way around, Sebastian just needs to fix this.
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 12/08/2011 at 07:52, xxxxxxxx wrote:
This code will return all the ID's for the MoText object:
import c4d from c4d import gui def main() : op = doc.GetActiveObject() bc = op.GetData() i=0 while i<51: print bc.GetIndexId(i) i=i+1 if __name__=='__main__': main()
-ScottA
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 12/08/2011 at 12:42, xxxxxxxx wrote:
is there a way to get the c4d enums for these ID numbers once they are retrieved?
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 12/08/2011 at 15:30, xxxxxxxx wrote:
If by enums you mean their text based names. I don't think they have been set up yet.
The only way I know to get them is to manually search through the .h files to find them. Getting the ID name of the option you want by dragging it into the console to get it's text based ID. Then searching for that name in the main Cinema4D folder.For example.
Searching for: MGTEXTOBJECT_EFFECTORLIST_WORD should result in finding the omograph_text.h file. Which contains both that ID's text based ID. And it's numerical one.-ScottA
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 12/08/2011 at 23:02, xxxxxxxx wrote:
Originally posted by xxxxxxxx
Lol this is a funny issue. The container of the MoText object contains a font-object which cannot be accessed with python, but within the loop the container want's to throw this font-object into the iteration. This gives this error. I don't think there is a way around, Sebastian just needs to fix this.
That's interesting, because one of my next questions was going to be "How do I set the font for the MoText object?" Is that currently not possible through Python?
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 13/08/2011 at 03:02, xxxxxxxx wrote:
Nope it isnt
See here.
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 13/08/2011 at 17:07, xxxxxxxx wrote:
Originally posted by xxxxxxxx
This code will return all the ID's for the MoText object:
Thanks Scott. I actually made a slight change to the while loop to print the ID and its type.
i=0 while i < 51: index = bc.GetIndexId(i) elementType = bc.GetType(index) print "Index: %i, Type: %i" % (index, elementType) i+=1
Where did you get the number 51? Is there a method that gives you the number of entries in a BaseContainer?
The types are apparently returned as int values. Is there any way to determine the type of a value from its ID? It seems that the simplest way to get this information is to drag the object or attribute to the Python console.
-
THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 13/08/2011 at 18:22, xxxxxxxx wrote:
I got the number 51 by printing ID's of the MoText object in increments of ten. Then once I got a return value of -1. I knew where the end of the container was.
The reason I did that was because I forgot how to iterate through a container at the time. And rather than look it up in my notes. I just wrote the loop that way as a crutch for my poor memory.Here's a different (more proper) way to write it using "len" as a way to automatically count the number of container entries:
import c4d from c4d import gui def main() : op = doc.GetActiveObject() bc = op.GetData() i=0 while i < len(bc) : index = bc.GetIndexId(i) elementType = bc.GetType(index) print "Index: %i, Type: %i" % (index, elementType) i+=1 if __name__=='__main__': main()
If Sebastian has set up the ID's for an object. Using value in a loop will work.
But in this case. He hasn't done that yet. So all we get in return are ID numbers.
You can drag each one into the console one at a time. But since the MoText object has lots of items in it. It's faster to get one of the names. Then search for the .h file it belongs to and get the other ID's names and numbers.When writing plugins with C++. We have to do that kind of searching for .h files a lot to include them in our code. So I've gotten used to doing that.
It's not something you'd typically have to do in Python that much.-ScottA