PDA

View Full Version : Getting the numerical field values with python?


Scott Ayers
10-25-2010, 05:22 PM
When I use this code import c4d
from c4d import gui
def main():

obj = doc.GetActiveObject()
print obj#ID_BASEOBJECT_REL_POSITION:VECTOR_X>

if __name__=='__main__':
main()

The result is this: <c4d.BaseObject object at 0x0F1F40C0>

How do I get the actual (non hexidecimal) numerical value from the field like how it works with Coffee?

-ScottA

rustEdge
10-25-2010, 07:28 PM
it might be easier if you treat python a bit more like c++, at least in terms of syntax.

using the methods that are native to the C++ SDK seem to work though. (GetRelPos(), GetReal(), etc.)


i think this code would work for you.

import c4d
from c4d import gui

def main():
obj = doc.GetActiveObject()
xpos = obj.GetRelPos().x
print xpos

if __name__=='__main__':
main()


cheers!

-gene

Scott Ayers
10-25-2010, 08:28 PM
Yeah. That's ok as a fall back option.
But I just picked the positionX parameter as an example. There might be times when I would prefer to to use the parameters instead.

After more searching. I found the formatting it needs to work: import c4d
from c4d import gui

def main():
obj = doc.GetActiveObject()
segments = obj[c4d.PRIM_CUBE_SUBX]#<--This works
print segments #print only the X segment value

posX = obj[c4d.ID_BASEOBJECT_REL_POSITION:VECTOR_X]#<--This Fails!?
print posX #print only the X position value

if __name__=='__main__':
main()

But the strange thing is that some items like the segments parameters work fine. And other things like the position values don't.
If I want to target just the X position value for an object.
I'm forced to write it like this: import c4d
from c4d import gui

def main():
obj = doc.GetActiveObject()
posX = obj[c4d.ID_BASEOBJECT_REL_POSITION]#Gets all three vector values
print posX.x #print only the X position value

if __name__=='__main__':
main()

I don't understand why I can't just use: obj[c4d.ID_BASEOBJECT_REL_POSITION:VECTOR_X]
Instead of breaking it down by assigning it to it's own variable?

-ScottA

rustEdge
10-25-2010, 08:49 PM
Well, Python was initially designed with ease of use in mind, so most of the traditional coding conventions have been changed or removed entirely. Indentation instead of parentheses, # and """ for comments instead of // and /**/, etc. I'm guessing it's just those little details that's keeping Python from cooperating.

Anyways, I just experimented with typing in random stuff, and this seems to work. I just appended .x to the variable retrieval on your code.


import c4d
from c4d import gui

def main():
obj = doc.GetActiveObject()
posX = (obj[c4d.ID_BASEOBJECT_REL_POSITION]).x #Gets all three vector values
print posX #print only the X position value

if __name__=='__main__':
main()


It's been a while since I've worked on Python. It is pretty tricky going back and forth between that and things like C++ and Javascript.

But yeah, in terms of prototyping, nothing beats Python. It's as easy as Basic with much less chance of accidental crashes.

-gene

Per-Anders
10-25-2010, 08:53 PM
Because : means something else in Python.

You need to use this to get just the one sub parameter:

posX = obj[c4d.ID_BASEOBJECT_REL_POSITION, c4d.VECTOR_X]

Scott Ayers
10-25-2010, 09:26 PM
Thanks Anders.

I'll add this to my python notes.
Seems like thanking you is becoming a full time job. :)

-ScottA

CGTalk Moderation
10-25-2010, 09:26 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.