Post #2908: : post_02910
π·οΈ Tags
macro python-scripting advanced solved
βοΈ Related PME Features
- Macro Editor
- Python Scripting
- Command Field
π¬ Content
anminmakura:
I understand that the if syntax is written like this:
if [condition] [execution] [else] [execution 2]
One line if-else statement should look like this:
[execution] if [condition] [else] [execution 2]
if statement:
[execution] if [condition] [else] None
anminmakura:
For example, the syntax for the QuickOrigin Add-On that I often use is written like this:
How can I write the following execution processes in order in the command field?
if bpy.context.object.mode == "EDIT":
saved_location = bpy.context.scene.cursor.location.copy()
bpy.ops.view3d.snap_cursor_to_selected()
bpy.ops.object.mode_set(mode="OBJECT")
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
bpy.context.scene.cursor.location = saved_location
bpy.ops.object.mode_set(mode="EDIT")
if bpy.context.object.mode == "OBJECT":
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
This code is too long for one-liner code (hard to read) but do-able. Note that you can use only 1024 chars in PME one-liners. Here is the code:
{locals().update(saved_location=bpy.context.scene.cursor.location.copy()), bpy.ops.view3d.snap_cursor_to_selected(), bpy.ops.object.mode_set(mode="OBJECT"), bpy.ops.object.origin_set(type='ORIGIN_CURSOR'), setattr(bpy.context.scene.cursor, "location", saved_location), bpy.ops.object.mode_set(mode="EDIT")} if bpy.context.object.mode == "EDIT" else None; bpy.ops.object.origin_set(type='ORIGIN_CURSOR') if bpy.context.object.mode == "OBJECT" else None
Still itβs better to use external scripts for multi-line code.
β€οΈ 5 likes