Mmmm help for a basic thing (i think) !

(Gwenouille) #1

Hello !

I have the following question/Problem:

In a script: i use many similar functions acting on sizeX,LocX and RotX of the selected object.
Because thes functions also exist for Y and Z i thought i could make a generic function and call it with, let’s say RotZ as argument if i wanted it to act upon the object’s Z value
I dont mind having a lot of functions, but maybe it would be more elegant that way.

For example i could have a LocX reset function:
def ResetXLoc():
selObj=Blender.Object.GetSelected()
for obj in selObj:
obj.LocX=0.0
Blender.Redraw()

But because LocY and LocZ would be similar, why not make a generic reset function:
def Reset(Arg):
selObj=Blender.Object.GetSelected()
for obj in selObj:
obj.Arg=0.0
Blender.Redraw()

where Arg is either LocX LocY or LocZ depending on what i need…

Alas !
It doesn’t work so easily.
I thought i could try with functions i don’t fully master like exec() str() etc…
One idea was to pass the argument as a string (“LocX” for instance) and then make the " disappear with exec or so…

I wasn’t successful…:frowning:

Someone knows if it is at all possible ?

I am sure some master of Python around here will find a way !

See you !

0 Likes

(forTe) #2

My brain is a little dead today (well for the past couple of weeks), but lets see what we could do:


def resetX():
reset x code goes here

def resetY():
reset y code goes here

def resetZ():
reset z code goes here

def resetAll():
     resetX()
     resetY()
     resetZ()

This code will allow you to easily set one axis or reset them all

or you could do something like you suggested and use integers and elif as a switch statement:


def reset(arg):
     if arg == 0:
           reset X code
     elif arg == 1:
... so forth
     else:
           reset all code

There are more creative/efficient ways I’m sure ( I can think of little spurts of code of some of them), I’m just too brain dead to think them all the way through right now…

0 Likes

(tedi) #3

import Blender

args = 'LocX', 'LocY', 'RotZ' # one or more target values as strings

objs = Blender.Object.GetSelected()
def resetter(args, objs):
    for ob in objs:
        for arg in args: exec('ob.%s=0.0' % arg)

resetter(args, objs)
Blender.Redraw()

add your own error checking code.

(above could be shrunk into a semi-oneliner, but this way it is obvious what it does and how it should be called in your prog)

squeeze w/o lambada:


import Blender

args = 'LocX', 'LocY', 'RotX'

def rst(ob,arg): exec('ob.%s=0.0'%arg)
[rst(ob,arg) for arg in args for ob in Blender.Object.GetSelected()]
Blender.Redraw()

0 Likes

(j) #4

gwenouille: What does the whole code do, or should do?

0 Likes

(Gwenouille) #5

@ j: i am learning python and was trying to reLocate objects, doGUI etc…

@forTe:1st proposal: yep, that is exactly what i did. But knowing that the code is so similar between functions, and that i am eager to learn, i was thinking of a generic function.
2nd proposal: well, you still write all the code but inside one function… It is not exactly what i meant…
Thanks anyway !

@Tedi: it does look like what i was thinking of . I ll have a go soon !
I tried with exec() but couldn’t work out exactly how to do…

Thanks for your time guys !
I ll keep you posted

0 Likes

(Gwenouille) #6

@ Tedi: Thanks man: that does the trick. I wasn’t able to find out how to use the exec statement. I still find it a bit magical but i am learning !
It feels great to have people like you around !

0 Likes

(tedi) #7

well … shit happens.

http://docs.python.org/ref/exec.html

0 Likes