if (do_name() != 'Home') : ?>
endif ?>
Posted by Stefan G on Dec 04, 2008
Suppose you have a module circletest you'd like to ximport:
def circle(x, y, w): return _ctx.oval(x, y, w, w) def setup(): _ctx.circle = _ctx._ns["circle"] = circleyou can then do:
ci = ximport("circletest") ci.setup() circle(10, 10, 140)This will run the circle code from the first go.
Posted by Josh Caswell on Dec 06, 2008
include("util/comment.php"); ?>
Thanks, Stefan! Following the NB source around, I now think I see what is happening. Hadn't really grasped the significance of _ns in Context until you pointed it out. I appreciate the clue!
Stacks for context attributes
Posted by Josh Caswell on Dec 04, 2008Hello,
I've got a couple of pieces of code here which I hope will be useful to someone. First, I wrote a class to make certain attributes of the Context class act like transforms already do. Each instance of this class has a stack (implemented as a list) and a user-chosen action, which points to a method of Context. The user can call pop() and push() on the instance, and values are shuffled around and set by calling action().
The three specific Context attributes I had in mind for this class were fill, stroke, and strokeWidth. I anticipate the stacks being useful for recursive drawing; with a global stack for them, you don't have to pass around the value of each from one level of recursion to the next.
The second piece of code is pretty trivial, and probably everyone already has already implemented their own version of it. I've defined an ellipse() function to mimic Processing's, taking the center of the shape as an argument, instead of the corner of the bounding box, which Cocoa wants. The bit that I really want to show off, which may be useful for more people, is adding the function to the _ctx object as a method, so you can call ellipse() at the top level of your end script, and this brings up a question that hopefully someone can answer for me.
I have these two pieces of code in a "conveniences" module in my Application Support folder. In my end script, I do an ximport() of the module, and then I want to do _ctx.ellipse = conveniences.ellipse, so I can use it at top level. conveniences doesn't have access to _ctx until after it's been imported, however, so I have this setup function also in the module:
And in the end script:
After the ximport(), conveniences.ellipse() will draw a shape correctly. The thing that I haven't been able to puzzle out is: the very first time I run a script with a top-level call to ellipse(), I get a NameError saying that ellipse is not defined. If I run the script again immediately, without changing a thing, it works perfectly.
If anyone's got an explanation of that, I'd be glad to know. Again, hope someone (maybe even Fred and Tom) finds this stuff useful.
--Josh Caswell