Woo woo, question number 4! Seems simple.... change a node's (shapenode primarily) size after the fact?
I've scoured the scene module trying to find an option other than deleting the node and replacing it with an updated one which seems excessive.
If I want to update a node's position (without animation) I can simply write:
self.node.position = x, y
But if I try using self.node.size in a similar way, nothing happens. The values update (I checked with a print command) but visually nothing changes.
I imagine this has to do with the involvement of a ui.Path object to create the Shapenode in the first place but I'm not sure what to do with that potential fact. What's the most straightforward way to update a Shapenode's dimensions?
There can be some pixelation when scaled up, so a redraw with a new ui.Path() might be better if you need to scale it up a lot.
Thank you both of you for responding! I can't stop asking questions but I've been feeling so bad that omz answers every time!
A couple things:
- The following did not have any effect:
Self.node.size = (width, height)
And I think I mentioned it but I've been trying to find a different variable or method to call without any luck.
- The following DID work:
self.node.path = ui.Path.rounded_rect(x, y, w, h, r)
However, would it be possible to eliminate all parameters except for width and height, and even eliminate the reference to the type of shape? Of course I can pass through the previous parameters and I can run a check to see if a given Shapenode object is a rect or rounded rect but all of that seems like a runaround solution if I could just access the path's dimensions directly.
This seems to work for me (code within an instance of
scenehence use of
my_path = ui.Path().rounded_rect(0, 0, 20, 20, 4) my_path.fill() my_path.close() self.my_node = ShapeNode(my_path, color = 'white', position = (100, 100), size = (20, 20)) self.add_child(self.my_node) self.my_node.size = (40, 40) # Changes size
In your method, which uses the path object, the x and y can just be zero I think, and you could set a variable equal to the path object like above to simplify the code. Not sure why the above is not working for you though, sorry! :)
@chriswilson , I should look at node. I have a sneaking suspicion that it would be also good for drawing into a ui.View not just scene. I am sure many things would not apply, but they could be like a no_op if used in a ui.View instead of a scene. I can barley do 2d graphics, that's why I haven't looked at it. But I have thought about it in the past
Another nice thing you can do in py3 is below. 2.7 does not allow you unpack like this when you have the named arg following. . Not a big deal. But it's nice you can do
w = 100 h = w r = (0, 0, w, h) s = ui.Path.rounded_rect(*r, 3)
Since I'm easily the inexperienced one here take what I say with a grain of salt but since I've been staring at the documentation 24/7, I think what you're asking for is explicitly mentioned as a useful possibility. In fact, here it is from the scene module documentation:
"Integration with the ui Module
The easiest way to run a scene is to use the run() function, which presents the scene in full-screen. Sometimes this is not what you want, especially if the scene you’re building is not actually a game. For more flexibility, you can also create a SceneView explicitly, set its scene attribute to the scene you want to present, and then add it to a view hierarchy that you created using the ui module.
You can also use this approach to add traditional UI elements (e.g. text fields) to your game. For this purpose, you don’t have to create a SceneView – you can simply use the view that was created automatically, using your Scene‘s view attribute (this will only work if a scene is already being presented of course).
The ui module can also be useful for rendering vector images (shapes, text etc.) that you want to use as textures. A Texture can be initialized from a ui.Image for this purpose, typically one that you create using an ui.ImageContext. The ShapeNode and LabelNode classes use this approach to render their content."
Ignore me if I'm just spouting useless stuff but your comment caught my eye!
Interestingly, your example (even with modifications to fit how I write), actually did the job perfectly. I even removed the parentheses from the tuplet (as I've been doing by habit for a little while already) and it still worked.
Even more interestingly, I tried it in my program one more time and it actually worked this time. Literally the same exact line: self.node.size = w, h. Weird stuff!
Also, it completely distorted it just like your predicted which means that despite all of this, it will probably be best to redo the path anyway like you said.
@WTFruit , thanks for the info. I do remember reading it sometime ago. But because I am still learning, it doesn't all sink in at the same time. I will try to get the AnalogClock.py from the Pythonista examples working in a ui.View. That would be great if I can manage it
Again, I'm still learning (hence all the questions I keep asking), but I've been basically focusing all my energy on doing stuff with scenes so please feel free to hit me up if you have any questions.
I'm learning too! :)
Thanks guys, I will also answer if i can
I haven't used the
scenemodule since it was updated from the render-loop version to nodes, so I can't help you with your main problem. But just a small thing about tuples and assignments. In Python, the lines
name = a, band
name = (a, b)do exactly the same. When assigining a tuple, it makes no difference if you use parens or not, because there's no other way that the operator precedence could work out. (Assignment is not an expression in Python, so
(name = a), bis invalid syntax and cannot happen by accident.)
In many other cases you need parens around tuples though, for example when calling a function, or when concatenating tuples with
+. For example
1, 2 + 3, 4will result in the tuple
(1, 5, 4), and
(1, 2) + (3, 4)will result in
(1, 2, 3, 4). Personally I almost always put parens around tuples just to be sure.
Assigning to the
sizeattribute of a
ShapeNodeshould work, with or without parentheses around the (x, y) value. I don't know why it didn't at first, I'd have to see the full code to know if this was due to some bug in the
scenemodule or something else.
As you've seen though, the shape gets distorted when you assign a different size. The reason is basically that a
ShapeNodeis a pretty simple subclass of
SpriteNode. The only difference is that a
texturefrom a vector path instead of an image file. But once the texture is generated, it behaves just like any other bitmap, i.e. you can't scale it without getting a blurry image or distortion.
I would assume user-error for now on my earlier attempts... Lord knows I make enough of them!
I know I'm cross-threading now but interestingly, I've had another error go away: when I asked about the modal_scene issue, and the last comment I made was that I got it to work but it was still throwing the same error into the console at the same time. Well, as I kept working that just randomly stopped eventually. I was adding things (more nodes and tweaking things) to the modal scene so based on your original comment that's likely related I suppose?
EDIT: on the subject of bugs, I'm assuming this is one: I have a codebase that works 100% fine at the moment, but when I try adding a new method to one of my classes it's throwing me an indentation error. I made it as simple as possible so there's nothing I could have screwed up... it's the most basic method I could create, it contains one print comment, I'm not calling it anywhere, I added the colon at the end of the definition... but it gives me the indentation error. Comment out those two lines of code though and it runs like a charm again.
You might be mixing tabs and spaces by accident. In the Pythonista settings, try enabling "Show Mixed Indentation", then the indent type that is not your default will be highlighted. In a single file, you should only use one type of indentation, and always the same number of spaces/tabs for one indentation level.
My indentation has been very consistent (only using tab) but I think I figured it out sort of. I neglected to mention that I actually got my project up and running in X-Code and I'm pretty sure that's what was causing trouble. I messed with the "intelligent indenting" preferences a little bit and the problem hasn't come back so far.
@WTFruit When you're editing Python code in Xcode, I would recommend never to use the regular "Paste" command – always use "Paste and Preserve Formatting". Xcode's automatic re-indentation fails spectacularly with Python code.
@omz Yeah I noticed that - never realised there was an option to paste with preserved formatting! Had to turn on 'show invisibles' and delete all the spaces that had appeared!
https://atom.io is a wonderful editor that I highly recommend. If you add the package
linter-flake8, Atom will catch indentation issues and a multitude of other errors in your Python code in realtime as you type! I find that it consistently saves me several run-and-crash cycles on Mac, Windoze, and Linux. See the video at https://atom.io/packages/linter-flake8