Python in the interaction tag [SOLVED]
-
On 15/01/2015 at 12:49, xxxxxxxx wrote:
Hi there, I'm scripting within the new R16 interaction tag & I have 1 question : How can I pass variables between the pre-defined functions ?
In a normal script, I would do this via the main() function, like this:
def func1() :
var1 = "whatever"
return var1def func2(var1) :
print("Do something with " + var1)
returndef main() :
var1 = func1()
func2(var1)But in the interaction tag, there is no main()
What I want to do is:
- Test some conditions in mouseDown() because this executes only once on initial click
- pass the results of the tests to mouseDrag()
- execute some actions only if the conditions were metWhat should my approach be ? Should I define a global variable & use that ?
Here is my very early skeleton code with just a few print statements, I want to pass the variable poly_sel to mouseDrag() :
#Tweak tag script
#Predefined global variables : tag, op, proxy, doc, thread, qualifierimport c4d
from c4d import guidef mouseDown() :
#Initialization/start tweak code goes here
print("Mouse Down")
poly_sel = tag[c4d.INTERACTIONTAG_POLYINFO_SELECTIONTAG]
if poly_sel:
name = poly_sel.GetName()
if name == "Jaw Area":
print("Polygon Selection Named : Jaw Area")
else:
print("No Selection Tag")def mouseDrag() :
#Main code loop for when the mouse button is down and tweak is occuring happens here
print("Dragging...")def mouseUp() :
#Code for what happens when the mouse button is released goes here
print("Mouse Up") -
On 15/01/2015 at 13:13, xxxxxxxx wrote:
OK, I got it working by using a global variable.
According to my testing, this works fine :#Tweak tag script
#Predefined global variables : tag, op, proxy, doc, thread, qualifierimport c4d
from c4d import guipoly_sel = None
def mouseDown() :
#Initialization/start tweak code goes here
print("Mouse Down")
global poly_sel
poly_sel = tag[c4d.INTERACTIONTAG_POLYINFO_SELECTIONTAG]
if poly_sel:
name = poly_sel.GetName()
if name == "Jaw Area":
print("Polygon Selection Named : Jaw Area")
else:
print("No Selection Tag")def mouseDrag() :
#Main code loop for when the mouse button is down and tweak is occuring happens here
global poly_sel
if poly_sel:
print("Dragging...")def mouseUp() :
#Code for what happens when the mouse button is released goes here
print("Mouse Up")But is it a good way to do it ?
Is it considered dangerous ?
Is it OK for performance ?
Is there a better way you would recommend ?Thanks for any thoughts from any coder who is reading this.
-
On 19/01/2015 at 05:48, xxxxxxxx wrote:
Hello,
Global variables can be used. Just be aware that the scrip will be reinitialized when it is changed so the value of any global variable is lost. Also calling different global events like Undo and Redo can reinitialize the script.
If you want to save certain properties permanently you could write them to some userdata of the host object.
best wishes,
Sebastian -
On 20/01/2015 at 05:39, xxxxxxxx wrote:
Thanks Sebastian.
In fact, I don't want to save the values permanently, just to pass them
to the pre-defined functions like mouseDown().
I'm actually setting them back to None at the end, in mouseUp(), to avoid storing them.But thanks for your feedback, I will bear that method in mind if I need to store values in a tag script in future.