Skip to content

Commit

Permalink
feat(extra): Add a component to set the view
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey authored and Chris Mackey committed Mar 7, 2024
1 parent ab94d18 commit ccff8e5
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 28 deletions.
Binary file modified ladybug_grasshopper/icon/LB Legend 2D Parameters.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ladybug_grasshopper/icon/LB Set View.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ladybug_grasshopper/icon/LB View From Sun.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 20 additions & 20 deletions ladybug_grasshopper/json/LB_Legend_2D_Parameters.json
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
{
"description": "Customize the properties of a screen-oreinted 2D legend displaying with the\n\"LB Preview VisualizationSet\" component.\n-",
"version": "1.7.2",
"nickname": "Legend2D",
"outputs": [
[
{
"type": null,
"access": "None",
"name": "leg_par2d",
"description": "A legend parameter object that can be plugged into any of the\nLadybug components with a legend.",
"access": "None",
"type": null,
"default": null
}
]
],
"code": "\n\ntry:\n from ladybug.legend import Legend2DParameters\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug:\\n\\t{}'.format(e))\ntry:\n from ladybug_{{cad}}.{{plugin}} import turn_off_old_tag\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\nturn_off_old_tag(ghenv.Component)\n\n\ndef parse_dim_text(dim_text):\n \"\"\"Parse text representing a dimension into an input for legend parameters.\"\"\"\n try:\n px_txt = int(dim_text)\n return '{}px'.format(px_txt)\n except ValueError:\n return dim_text\n\n\n# parse all of the inputs\norigin_x_ = parse_dim_text(origin_x_) if origin_x_ is not None else None\norigin_y_ = parse_dim_text(origin_y_) if origin_y_ is not None else None\nseg_height_ = parse_dim_text(seg_height_) if seg_height_ is not None else None\nseg_width_ = parse_dim_text(seg_width_) if seg_width_ is not None else None\ntext_height_ = parse_dim_text(text_height_) if text_height_ is not None else None\n\n# make the 2D legend parameters\nleg_par2d = Legend2DParameters(origin_x_, origin_y_, seg_height_, seg_width_, text_height_)\n",
"name": "LB Legend 2D Parameters",
"version": "1.7.1",
"category": "Ladybug",
"subcategory": "4 :: Extra",
"nickname": "Legend2D",
"inputs": [
{
"type": "string",
"access": "item",
"name": "origin_x_",
"description": "An integer in pixels to note the X coordinate of the base point from\nwhere the 2D legend will be generated (assuming an origin in the\nupper-left corner of the screen with higher positive values of\nX moving to the right). Alternatively, this can be a text string\nending in a % sign to denote the percentage of the screen where\nthe X coordinate exists (eg. 5%). The default is set to make the\nlegend clearly visible in the upper-left corner of the\nscreen (10 pixels).",
"access": "item",
"type": "string",
"default": null
},
{
"type": "string",
"access": "item",
"name": "origin_y_",
"description": "An integer in pixels to note the Y coordinate of the base point from\nwhere the legend will be generated (assuming an origin in the\nupper-left corner of the screen with higher positive values of\nY moving downward). Alternatively, this can be a text string\nending in a % sign to denote the percentage of the screen where\nthe X coordinate exists (eg. 5%). The default is set to make the\nlegend clearly visible in the upper-left corner of the\nscreen (50 pixels).",
"access": "item",
"type": "string",
"default": null
},
{
"type": "string",
"access": "item",
"name": "seg_height_",
"description": "A integer in pixels to note the height for each of the legend segments.\nAlternatively, this can be a text string ending in a % sign to\ndenote the percentage of the screen (eg. 5%). The default is set\nto make most legends readable on standard resolution\nscreens (25px for horizontal and 36px for vertical).",
"access": "item",
"type": "string",
"default": null
},
{
"type": "string",
"access": "item",
"name": "seg_width_",
"description": "An integer in pixels to set the width of each of the legend segments.\nAlternatively, this can be a text string ending in a % sign to\ndenote the percentage of the screen (eg. 5%). The default is set\nto make most legends readable on standard resolution\nscreens (36px for horizontal and 25px for vertical).",
"access": "item",
"type": "string",
"default": null
},
{
"type": "string",
"access": "item",
"name": "text_height_",
"description": "An integer in pixels to set the height for the legend text.\nAlternatively, this can be a text string ending in a % sign to\ndenote the percentage of the screen (eg. 2%).",
"access": "item",
"type": "string",
"default": null
}
]
],
"subcategory": "4 :: Extra",
"code": "\n\ntry:\n from ladybug.legend import Legend2DParameters\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug:\\n\\t{}'.format(e))\ntry:\n from ladybug_{{cad}}.{{plugin}} import turn_off_old_tag\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\nturn_off_old_tag(ghenv.Component)\n\n\ndef parse_dim_text(dim_text):\n \"\"\"Parse text representing a dimension into an input for legend parameters.\"\"\"\n try:\n px_txt = int(dim_text)\n return '{}px'.format(px_txt)\n except ValueError:\n return dim_text\n\n\n# parse all of the inputs\norigin_x_ = parse_dim_text(origin_x_) if origin_x_ is not None else None\norigin_y_ = parse_dim_text(origin_y_) if origin_y_ is not None else None\nseg_height_ = parse_dim_text(seg_height_) if seg_height_ is not None else None\nseg_width_ = parse_dim_text(seg_width_) if seg_width_ is not None else None\ntext_height_ = parse_dim_text(text_height_) if text_height_ is not None else None\n\n# make the 2D legend parameters\nleg_par2d = Legend2DParameters(origin_x_, origin_y_, seg_height_, seg_width_, text_height_)\n",
"category": "Ladybug",
"name": "LB Legend 2D Parameters",
"description": "Customize the properties of a screen-oreinted 2D legend displaying with the\n\"LB Preview VisualizationSet\" component.\n-"
}
63 changes: 63 additions & 0 deletions ladybug_grasshopper/json/LB_Set_View.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"version": "1.7.0",
"nickname": "SetView",
"outputs": [
[]
],
"inputs": [
{
"access": "item",
"name": "_direction",
"description": "A sun vector from which the the Rhino view will be generated.\nUse the \"LB SunPath\" component to generate sun vectors.",
"type": "Vector3d",
"default": null
},
{
"access": "item",
"name": "_position_",
"description": "The target point of the camera for the Rhino view that will be\ngenerated. This point should be close to the Rhino geometry that\nyou are interested in viewing from the sun. If no point is provided,\nthe Rhino origin will be used (0, 0, 0).",
"type": "Point3d",
"default": null
},
{
"access": "item",
"name": "look_around_",
"description": "An optional interger for the width (in pixels) of the Rhino\nviewport that will be generated.",
"type": "Point3d",
"default": null
},
{
"access": "item",
"name": "width_",
"description": "An optional interger for the width (in pixels) of the Rhino\nviewport that will be generated.",
"type": "int",
"default": null
},
{
"access": "item",
"name": "height_",
"description": "An optional interger for the height (in pixels) of the Rhino\nviewport that will be generated.",
"type": "int",
"default": null
},
{
"access": "item",
"name": "lens_len_",
"description": "An optional interger for the height (in pixels) of the Rhino\nviewport that will be generated.",
"type": "double",
"default": null
},
{
"access": "item",
"name": "mode_",
"description": "An optional text input for the display mode of the Rhino viewport\nthat will be generated. For example: Wireframe, Shaded, Rendered, etc.",
"type": "string",
"default": null
}
],
"subcategory": "4 :: Extra",
"code": "\nimport math\n\ntry:\n from ladybug_geometry.geometry3d import Vector3D\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_geometry:\\n\\t{}'.format(e))\n\ntry:\n from ladybug_{{cad}}.togeometry import to_vector3d, to_point2d\n from ladybug_{{cad}}.fromgeometry import from_vector3d\n from ladybug_{{cad}}.viewport import open_viewport, viewport_by_name, \\\n set_view_direction, set_view_display_mode\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, component_guid, \\\n get_sticky_variable, set_sticky_variable\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\nif all_required_inputs(ghenv.Component):\n # get the name of the view and the previous width/height\n view_name = 'SetView_{}'.format(component_guid(ghenv.Component))\n print(view_name) # print so that the user has the name if needed\n vw = get_sticky_variable('set_view_width')\n vh = get_sticky_variable('set_view_height')\n\n # if there are look-around coordinates, rotate the direction\n if look_around_ is not None:\n uv_pt = to_point2d(look_around_)\n dir_vec = to_vector3d(_direction)\n v = (uv_pt.y - 0.5) * math.pi\n dir_vec = dir_vec.rotate(dir_vec.cross(Vector3D(0, 0, 1)), v)\n u = -(uv_pt.x - 0.5) * math.pi\n dir_vec = dir_vec.rotate_xy(u)\n _direction = from_vector3d(dir_vec)\n\n # get the viewport from which the direction will be set\n view_port = None\n if width_ == vw and height_ == vh: # no need to generate new view; get existing one\n try:\n view_port = viewport_by_name(view_name)\n except ValueError: # the viewport does not yet exist\n pass\n if view_port is None:\n view_port = open_viewport(view_name, width_, height_)\n set_sticky_variable('set_view_width', width_)\n set_sticky_variable('set_view_height', height_)\n\n # set the direction of the viewport camera\n set_view_direction(view_port, _direction, _position_, lens_len_)\n\n # set the display mode if requested\n if mode_:\n set_view_display_mode(view_port, mode_)\n",
"category": "Ladybug",
"name": "LB Set View",
"description": "Open a new viewport in Rhino that shows the parallel-projected view from the sun.\n_\nThis is useful for understanding what parts of Rhino geometry are shaded at a\nparticular hour of the day.\n-"
}
4 changes: 2 additions & 2 deletions ladybug_grasshopper/json/LB_View_From_Sun.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.7.1",
"version": "1.7.2",
"nickname": "ViewFromSun",
"outputs": [
[]
Expand Down Expand Up @@ -42,7 +42,7 @@
}
],
"subcategory": "3 :: Analyze Geometry",
"code": "\ntry:\n from ladybug_{{cad}}.viewport import viewport_by_name, open_viewport, \\\n set_iso_view_direction, set_view_display_mode\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, component_guid\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\nif all_required_inputs(ghenv.Component):\n view_name = 'ViewFromSun_{}'.format(component_guid(ghenv.Component))\n print(view_name) # print so that the user has the name if needed\n\n # get the viewport from which the direction will be set\n view_port = None\n if not width_ and not height_: # no need to generate new view; get existing one\n try:\n view_port = viewport_by_name(view_name)\n except ValueError: # the viewport does not yet exist\n pass\n if view_port is None:\n view_port = open_viewport(view_name, width_, height_)\n\n # set the direction of the viewport camera\n set_iso_view_direction(view_port, _vector, _center_pt_)\n\n # set the display mode if requested\n if mode_:\n set_view_display_mode(view_port, mode_)\n",
"code": "\ntry:\n from ladybug_{{cad}}.viewport import viewport_by_name, open_viewport, \\\n set_iso_view_direction, set_view_display_mode\n from ladybug_{{cad}}.{{plugin}} import all_required_inputs, component_guid, \\\n get_sticky_variable, set_sticky_variable\nexcept ImportError as e:\n raise ImportError('\\nFailed to import ladybug_{{cad}}:\\n\\t{}'.format(e))\n\n\nif all_required_inputs(ghenv.Component):\n # get the name of the view and the previous width/height\n view_name = 'ViewFromSun_{}'.format(component_guid(ghenv.Component))\n print(view_name) # print so that the user has the name if needed\n vw = get_sticky_variable('sun_view_width')\n vh = get_sticky_variable('sun_view_height')\n\n # get the viewport from which the direction will be set\n view_port = None\n if width_ == vw and height_ == vh: # no need to generate new view; get existing one\n try:\n view_port = viewport_by_name(view_name)\n except ValueError: # the viewport does not yet exist\n pass\n if view_port is None:\n view_port = open_viewport(view_name, width_, height_)\n set_sticky_variable('sun_view_width', width_)\n set_sticky_variable('sun_view_height', height_)\n\n # set the direction of the viewport camera\n set_iso_view_direction(view_port, _vector, _center_pt_)\n\n # set the display mode if requested\n if mode_:\n set_view_display_mode(view_port, mode_)\n",
"category": "Ladybug",
"name": "LB View From Sun",
"description": "Open a new viewport in Rhino that shows the parallel-projected view from the sun.\n_\nThis is useful for understanding what parts of Rhino geometry are shaded at a\nparticular hour of the day.\n-"
Expand Down
4 changes: 2 additions & 2 deletions ladybug_grasshopper/src/LB Legend 2D Parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@

ghenv.Component.Name = 'LB Legend 2D Parameters'
ghenv.Component.NickName = 'Legend2D'
ghenv.Component.Message = '1.7.1'
ghenv.Component.Message = '1.7.2'
ghenv.Component.Category = 'Ladybug'
ghenv.Component.SubCategory = '4 :: Extra'
ghenv.Component.AdditionalHelpFromDocStrings = '0'
ghenv.Component.AdditionalHelpFromDocStrings = '2'


try:
Expand Down
94 changes: 94 additions & 0 deletions ladybug_grasshopper/src/LB Set View.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Ladybug: A Plugin for Environmental Analysis (GPL)
# This file is part of Ladybug.
#
# Copyright (c) 2023, Ladybug Tools.
# You should have received a copy of the GNU Affero General Public License
# along with Ladybug; If not, see <http://www.gnu.org/licenses/>.
#
# @license AGPL-3.0-or-later <https://spdx.org/licenses/AGPL-3.0-or-later>

"""
Open a new viewport in Rhino that shows the parallel-projected view from the sun.
_
This is useful for understanding what parts of Rhino geometry are shaded at a
particular hour of the day.
-
Args:
_vector: A sun vector from which the the Rhino view will be generated.
Use the "LB SunPath" component to generate sun vectors.
_center_pt_: The target point of the camera for the Rhino view that will be
generated. This point should be close to the Rhino geometry that
you are interested in viewing from the sun. If no point is provided,
the Rhino origin will be used (0, 0, 0).
width_: An optional interger for the width (in pixels) of the Rhino
viewport that will be generated.
height_: An optional interger for the height (in pixels) of the Rhino
viewport that will be generated.
mode_: An optional text input for the display mode of the Rhino viewport
that will be generated. For example: Wireframe, Shaded, Rendered, etc.
Returns:
report: The name of the viewport that was opened.
"""

ghenv.Component.Name = 'LB Set View'
ghenv.Component.NickName = 'SetView'
ghenv.Component.Message = '1.7.0'
ghenv.Component.Category = 'Ladybug'
ghenv.Component.SubCategory = '4 :: Extra'
ghenv.Component.AdditionalHelpFromDocStrings = '2'

import math

try:
from ladybug_geometry.geometry3d import Vector3D
except ImportError as e:
raise ImportError('\nFailed to import ladybug_geometry:\n\t{}'.format(e))

try:
from ladybug_rhino.togeometry import to_vector3d, to_point2d
from ladybug_rhino.fromgeometry import from_vector3d
from ladybug_rhino.viewport import open_viewport, viewport_by_name, \
set_view_direction, set_view_display_mode
from ladybug_rhino.grasshopper import all_required_inputs, component_guid, \
get_sticky_variable, set_sticky_variable
except ImportError as e:
raise ImportError('\nFailed to import ladybug_rhino:\n\t{}'.format(e))


if all_required_inputs(ghenv.Component):
# get the name of the view and the previous width/height
view_name = 'SetView_{}'.format(component_guid(ghenv.Component))
print(view_name) # print so that the user has the name if needed
vw = get_sticky_variable('set_view_width')
vh = get_sticky_variable('set_view_height')

# if there are look-around coordinates, rotate the direction
if look_around_ is not None:
uv_pt = to_point2d(look_around_)
dir_vec = to_vector3d(_direction)
v = (uv_pt.y - 0.5) * math.pi
dir_vec = dir_vec.rotate(dir_vec.cross(Vector3D(0, 0, 1)), v)
u = -(uv_pt.x - 0.5) * math.pi
dir_vec = dir_vec.rotate_xy(u)
_direction = from_vector3d(dir_vec)

# get the viewport from which the direction will be set
view_port = None
if width_ == vw and height_ == vh: # no need to generate new view; get existing one
try:
view_port = viewport_by_name(view_name)
except ValueError: # the viewport does not yet exist
pass
if view_port is None:
view_port = open_viewport(view_name, width_, height_)
set_sticky_variable('set_view_width', width_)
set_sticky_variable('set_view_height', height_)

# set the direction of the viewport camera
set_view_direction(view_port, _direction, _position_, lens_len_)

# set the display mode if requested
if mode_:
set_view_display_mode(view_port, mode_)
14 changes: 10 additions & 4 deletions ladybug_grasshopper/src/LB View From Sun.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,43 @@
that will be generated. For example: Wireframe, Shaded, Rendered, etc.
Returns:
report: ...
report: The name of the viewport that was opened.
"""

ghenv.Component.Name = 'LB View From Sun'
ghenv.Component.NickName = 'ViewFromSun'
ghenv.Component.Message = '1.7.1'
ghenv.Component.Message = '1.7.2'
ghenv.Component.Category = 'Ladybug'
ghenv.Component.SubCategory = '3 :: Analyze Geometry'
ghenv.Component.AdditionalHelpFromDocStrings = '5'

try:
from ladybug_rhino.viewport import viewport_by_name, open_viewport, \
set_iso_view_direction, set_view_display_mode
from ladybug_rhino.grasshopper import all_required_inputs, component_guid
from ladybug_rhino.grasshopper import all_required_inputs, component_guid, \
get_sticky_variable, set_sticky_variable
except ImportError as e:
raise ImportError('\nFailed to import ladybug_rhino:\n\t{}'.format(e))


if all_required_inputs(ghenv.Component):
# get the name of the view and the previous width/height
view_name = 'ViewFromSun_{}'.format(component_guid(ghenv.Component))
print(view_name) # print so that the user has the name if needed
vw = get_sticky_variable('sun_view_width')
vh = get_sticky_variable('sun_view_height')

# get the viewport from which the direction will be set
view_port = None
if not width_ and not height_: # no need to generate new view; get existing one
if width_ == vw and height_ == vh: # no need to generate new view; get existing one
try:
view_port = viewport_by_name(view_name)
except ValueError: # the viewport does not yet exist
pass
if view_port is None:
view_port = open_viewport(view_name, width_, height_)
set_sticky_variable('sun_view_width', width_)
set_sticky_variable('sun_view_height', height_)

# set the direction of the viewport camera
set_iso_view_direction(view_port, _vector, _center_pt_)
Expand Down
Binary file modified ladybug_grasshopper/user_objects/LB Legend 2D Parameters.ghuser
Binary file not shown.
Binary file not shown.
Binary file modified ladybug_grasshopper/user_objects/LB View From Sun.ghuser
Binary file not shown.

0 comments on commit ccff8e5

Please sign in to comment.